ctf中的一道反序列化题

版权声明:莫者 https://blog.csdn.net/weixin_40709439/article/details/82875024

早就想写了,今天刚好遇到一道反序列化的题就记录一下

自己在本地搭的,源码如下:

index1.php

<?php
error_reporting(E_ALL & ~E_NOTICE);
$user = $_GET["user"];
$file = $_GET["file"];
$pass = $_GET["pass"];
 
if(isset($user)&&(file_get_contents($user,'r')==="admin")){
    echo "hello admin!<br>";
    if(preg_match("/f1a9/",$file)){
        exit();
    }else{
        include($file); //class.php
        $pass = unserialize($pass);
        echo $pass;
    }
}else{
    echo "you are not admin ! ";
}
 
?>
 
<!--
$user = $_GET["user"];
$file = $_GET["file"];
$pass = $_GET["pass"];
 
if(isset($user)&&(file_get_contents($user,'r')==="admin")){
    echo "hello admin!<br>";
    include($file); //class.php
}else{
    echo "you are not admin ! ";
}
 -->

class.php

<?php
error_reporting(E_ALL & ~E_NOTICE);
 
class Read{//f1a9.php
    public $file;
    public function __toString(){
        if(isset($this->file)){
            echo file_get_contents($this->file);    
        }
        return "__toString was called!";
    }
}
?>

f1a9.php

<?php
error_reporting(E_ALL & ~E_NOTICE);
//flag{hSh_ctf:e@syt0g3t}
?>

本地测试

访问首页,查看源码

根据以上源码泄露,是文件包含漏洞,配合封装协议读取文件源码

包含了class.php文件,该怎么读它的源码内容呢

这里得讲到file_get_contents()函数

file_get_contents() 函数把整个文件读入一个字符串中。

这里的字符串是$user参数接受的,利用它的文件封装协议来读取$file参数include的文件

当然这里要使第一个条件成立。还需要讲到一个文件封装协议

php://input 是个可以访问请求的原始数据的只读流

第一个条件:

if(isset($user)&&(file_get_contents($user,'r')==="admin"))

使$user的值等于admin,并且使$user接收读入的文件

可利用php://input绕过

成功绕过

现在利用php伪协议读取class.php源码,格式为base64加密

解密后

可以直接读取flag文件吗? 答案是不能

但是class.php把我们引入到另一个地方,就是利用反序列化来读取flag文件
于是我们构造反序列化的参数:
O:4:"Read":1:{s:4:"file";s:57:"php://filter/read=convert.base64-encode/resource=f1a9.php";}

这里也是利用php://filter来读取flag文件

 上面的就是f1a9.php的内容,格式base64加密,解密即可

猜你喜欢

转载自blog.csdn.net/weixin_40709439/article/details/82875024
今日推荐