In-depth understanding of PHP deserializing native classes
This article brings you relevant knowledge about PHP, which mainly introduces the use of deserialized native classes. If there is a deserialization function in code audit or ctf point, but it cannot construct a complete pop chain, so how should we break the situation at this time? Let’s take a look at it, I hope it will be helpful to everyone.
Recommended study: "PHP Video Tutorial"
A brief analysis of the use of PHP deserialization native classes
If there is a deserialization function in code audit or ctf, but a complete pop chain cannot be constructed, how should we break the situation? We can try to start with PHP native classes. Some PHP native classes have some built-in magic methods. If we cleverly construct controllable parameters, trigger and use their built-in magic methods, it is possible to achieve some of the goals we want.
1. Common magic methods
1 2 3 4 5 6 7 8 9 10 11 |
|
2. Magic methods in native classes
We use the following script to traverse it Magic methods in all native classes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
3. Utilization of some common native classes
Error/Exception
Error Is the base class for all PHP internal error classes. (PHP 7, 8)
**Error::__toString ** The string expression of error
Returns the string expression of Error.
Exception is the base class for all user-level exceptions. (PHP 5, 7, 8)
**Exception::__toString ** Convert the exception object to a string
Returns the exception converted to the string (string) type.
Class attribute
message Error message content
code Error code
file The file name that throws the error
line The number of lines that throws the error
XSS
__toString method will return the string form of error or exception, which contains the parameters we input. If we construct a string of xss code and combine it with echo rendering, the reflected xss vulnerability will be triggered
Example:
1 |
|
POC:
1 |
|
hash bypass
Look at a question first
[2020 Geek Challenge]Greatphp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Needs to bypass two hash strong comparisons, and ultimately needs to construct eval code execution
Obviously the normal method is It doesn't work, but it can be bypassed through native classes
Similarly, when the md5() and sha1() functions process objects, the __tostring method will be automatically called
Let's take a brief look at it first. Output
1 2 3 4 5 6 |
|
It can be found that the information returned by these two native classes is exactly the same except for the line number. Using this, we can try to bypass the hash function. What needs to be paid attention to Yes, the two incoming objects must be placed on the same line
So we can conduct a simple test and find that using this method can bypass hash strong (weak) function comparison
1 2 3 4 |
|
Based on these knowledge points, we can easily construct the payload
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
SoapClient
SoapClient is a class specially used to access web services. It can provide a PHP client that accesses Web services based on the SOAP protocol. It can create soap data messages and interact with the wsdl interface.
The soap extension module is closed by default and needs to be manually turned on when using it
SoapClient::__call —Call SOAP functions (PHP 5, 7, 8)
Usually, SOAP functions can be called as methods of the SoapClient object
SSRF
Constructor:
1 2 3 |
|
What is soap
1 2 3 |
|
We construct a payload, the first parameter is NULL, and the location of the second parameter is set to the vps address
1 2 3 4 5 6 7 8 |
|
Listen to the 2333 port of the vps. As shown in the figure below, SSRF is successfully triggered. The vps receives the request information
and you can see that both SOAPAction and user_agent are controllable
During local testing, it was found that when using this built-in class (i.e. soap protocol) to request a port where a service exists, an error will be reported immediately, and when accessing a port where the service does not exist (unoccupied), it will wait for a period of time. Time error reporting can be used to detect intranet assets.
If you cooperate with the CRLF vulnerability, you can also use SoapClient to control other parameters or post to send data. For example: HTTP protocol to attack Redis
CRLF knowledge expansion
1 2 3 |
|
By combining CRLF, we can use SoapClient CRLF to do more things, such as inserting custom cookies,
1 2 3 4 5 |
|
发送POST的数据包,这里需要将Content-Type设置为application/x-www-form-urlencoded,我们可以通过添加两个\r\n来将原来的Content-Type挤下去,自定义一个新的Content-Type
1 2 3 4 5 |
|
看一道ctfshow上的题,完美利用上述知识点
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
poc:
1 2 3 4 5 6 7 |
|
DirectoryIterator/FilesystemIterator
DirectoryIterator类提供了一个简单的接口来查看文件系统目录的内容。
DirectoryIterator::__toString 获取字符串形式的文件名 (PHP 5,7,8)
目录遍历
使用此内置类的__toString方法结合glob或file协议,即可实现目录遍历
例如:
1 2 3 4 |
|
FilesystemIterator继承于DirectoryIterator,两者作用和用法基本相同,区别为FilesystemIterator会显示文件的完整路径,而DirectoryIterator只显示文件名
因为可以配合使用glob伪协议(查找匹配的文件路径模式),所以可以绕过open_basedir的限制
在php4.3以后使用了zend_class_unserialize_deny
来禁止一些类的反序列化,很不幸的是这两个原生类都在禁止名单当中
SplFileObject
SplFileObject 类为单个文件的信息提供了一个面向对象的高级接口
(PHP 5 >= 5.1.2, PHP 7, PHP 8)
文件读取
SplFileObject::__toString — 以字符串形式返回文件的路径
1 2 3 |
|
如果没有遍历的话只能读取第一行,且受到open_basedir
影响
SimpleXMLElement
解析XML 文档中的元素。 (PHP 5、PHP 7、PHP 8)
SimpleXMLElement::__construct — 创建一个新的 SimpleXMLElement 对象
XXE
我们查看一下其参数:
根据官方文档,发现当第三个参数为True时,即可实现远程xml文件载入,第二个参数的常量值设置为2即可。
利用可参考赛题:[SUCTF 2018]Homework
ReflectionMethod
获取注释内容
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
ReflectionFunctionAbstract::getDocComment — 获取注释内容
由该原生类中的getDocComment方法可以访问到注释的内容
同时可利用的原生类还有ZipArchive– 删除文件等等,不在叙述
推荐学习:《PHP视频教程》
The above is the detailed content of In-depth understanding of PHP deserializing native classes. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
