thinkphp 5.0.0存在变量覆盖漏洞,源于request类method()方法未校验_method参数,攻击者可传入__construct覆盖属性并触发rce;需在入口文件拦截、加固method()方法、收紧控制器路由三方面手动修复。

ThinkPHP 5.0.0 存在变量覆盖漏洞,核心在于 Request 类的 method() 方法未对 _method 参数做严格校验,导致攻击者可通过 POST 传入 _method=__construct 覆盖类属性(如 filter、get、server),最终在 param() 或 input() 调用中触发 call_user_func 实现 RCE。仅升级版本不能彻底修复,必须手动加固三处关键点。
入口文件硬性拦截高危参数
在 public/index.php 开头立即添加请求过滤逻辑,阻断典型利用链:
- 检查
$_GET['s']是否包含think、invokefunction、call_user_func等敏感关键词,匹配即终止响应 - 检查
$_POST['_method']是否为__construct、__destruct等魔术方法名,直接拒绝 - 示例代码:
php
if (isset($_GET['s']) && preg_match('/\\think\\|invokefunction|call_user_func/i', $_GET['s'])) {
die('Access Denied');
}
if (isset($_POST['_method']) && in_array(strtolower($_POST['_method']), ['__construct', '__destruct', '__wakeup'])) {
die('Method not allowed');
}
Request 类 method() 方法加固
修改 thinkphp/library/think/Request.php 中的 method() 方法(约第 500 行附近):
- 对
$_POST[Config::get('var_method')]的值做白名单限制,只允许标准 HTTP 方法 - 禁止将用户输入直接用于动态方法调用:
$this->{$this->method}($_POST)必须移除或重写 - 加固后逻辑应为:
if (isset($_POST[$varMethod])) {
$method = strtoupper($_POST[$varMethod]);
if (!in_array($method, ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'])) {
$method = 'POST';
}
$this->method = $method;
}
控制器名与路由配置双重收紧
该漏洞常配合未校验的控制器名绕过路由,需同步处理:
- 在
thinkphp/library/think/App.php的module()方法中,解析出$controller后立即加入正则校验:
if (!preg_match('/^[A-Za-z][A-Za-z0-9]*$/', $controller)) {
throw new HttpException(404, 'controller not exists: ' . $controller);
} - 关闭危险配置项(
config/app.php):
—'app_debug' => false(禁用调试模式)
—'url_route_on' => true且'route_complete_match' => true
—'auto_route' => false(禁用自动路由)
— 显式设'var_method' => ''(清空伪装方法变量)
验证是否修复成功
修复后务必用真实请求测试以下 payload 是否全部返回 404/403 或空白页,**不报错、不回显、不执行命令**:
POST /index.php?s=captcha _method=__construct&filter[]=system&get[]=whoamiGET /index.php?s=index/ hinkpp/invokefunction&function=call_user_func_array&vars[0]=phpinfo&vars[1][]=1POST /index.php _method=__construct&filter[]=assert&server[REQUEST_METHOD]=phpinfo()
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!











