thinkphp 5.0控制器存在rcet、method覆盖命令执行、路由解析绕过和输入修饰符sql注入四大高危漏洞;需分别通过拦截危险路径、校验http方法、强制关闭自动路由并验证控制器名格式、严格校验数组型输入来修复。

ThinkPHP 5.0 控制器层面存在多个高危漏洞,核心问题集中在路由解析、方法调用和参数处理三个环节。不修复就升级或长期运行,极易被利用执行任意命令、读取敏感文件甚至写入WebShell。
控制器名未校验导致RCE
TP5.0默认允许通过URL路径直接传入控制器名(如 /index.php?s=/index/ hinkpp/invokefunction),且未对控制器命名做白名单限制。攻击者可拼接框架内部类路径,触发invokeFunction等危险逻辑。
- 在入口文件
public/index.php或全局中间件中添加硬性拦截规则 - 匹配含
hink、invokefunction、call_user_func、eval的请求参数,直接返回404或中断执行 - 示例代码(加在
App::run()前):
header('HTTP/1.1 404 Not Found');
exit('Invalid controller path');
}
method变量覆盖引发命令执行
当 _method=__construct 与可控 filter[] 同时提交时,Request对象的 __construct 方法会遍历POST数据做属性赋值,覆盖 $this->method 和 $this->filter,最终在参数过滤阶段调用 system、phpinfo 等函数。
- 必须禁用生产环境的
app_debug和var_method配置 - 修改
thinkphp/library/think/Request.php中的method()方法,在解析var_method参数后增加白名单校验 - 关键修复段(替换原 method 方法中 POST 解析部分):
$method = strtoupper($_POST[Config::get('var_method')]);
if (!in_array($method, ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'])) {
throw new HttpException(400, 'Invalid HTTP method');
}
$this->method = $method;
if (isset($_POST)) {
foreach ($_POST as $key => $val) {
if ($key === Config::get('var_method')) unset($_POST[$key]);
}
}
}
路由自动解析绕过权限控制
开启 url_route_on=false 且 url_param_type=1 时,框架退化为“顺序解析”模式,但部分版本仍残留控制器名动态拼接逻辑,导致未定义控制器被加载或方法被误调用。
- 检查并强制关闭自动路由:
'url_route_on' => false必须出现在config/app.php - 在
thinkApp类的module()方法中,控制器名提取后立即校验格式 - 修复代码(插入在
$controller = Loader::parseName($controller, 1, true);后):
if (!preg_match($pattern, $controller)) {
throw new HttpException(404, 'Controller name is invalid: ' . $controller);
}
输入修饰符滥用引发SQL注入
使用 input('id/a') 强制接收数组,再直接用于 where('id','in',$id),若未过滤或校验,攻击者可构造 id[0,updatexml(0,concat(0x5e,user()),0),0] 触发报错注入。
- 禁止无条件使用
/a修饰符接收用户输入 - 数组型参数必须做元素类型与长度双重校验
- 推荐写法(替代原始
input("id/a")):
if (!is_array($id) || count($id) > 100) {
throw new HttpException(400, 'Invalid id format');
}
foreach ($id as $v) {
if (!is_numeric($v) || $v 9999999) {
throw new HttpException(400, 'Invalid id value');
}
}
$data = Db::name('users')->where('id', 'in', $id)->select();











