前言
ThinkPHP官方最近修复了一个严重的远程代码执行漏洞。这个主要漏洞原因是由于框架对控制器名没有进行足够的校验导致在没有开启强制路由的情况下可以构造恶意语句执行远程命令,受影响的版本包括5.0和5.1版本。
测试环境:
ThinkPHP 5.1 beta+ win10 64bit + wamp
漏洞分析
网上已经有些分析文章了,我就正向分析下这次漏洞过程。不同版本的ThinkPHP调用过程和代码会稍有差异,本文分析的是ThinkPHP 5.1 beta的代码,其他版本的可以类似的分析。
首先会加载thinkphp/library/think/App.php ,运行run函数
public function run()
{
// 初始化应用
$this->initialize();
try {
if (defined('BIND_MODULE')) {
// 模块/控制器绑定
BIND_MODULE && $this->route->bindTo(BIND_MODULE);
} elseif ($this->config('app.auto_bind_module')) {
// 入口自动绑定
$name = pathinfo($this->request->baseFile(), PATHINFO_FILENAME);
if ($name && 'index' != $name && is_dir($this->appPath . $name)) {
$this->route->bindTo($name);
}
}
$this->request->filter($this->config('app.default_filter'));
// 读取默认语言
$this->lang->range($this->config('app.default_lang'));
if ($this->config('app.lang_switch_on')) {
// 开启多语言机制 检测当前语言
$this->lang->detect();
}
$this->request->langset($this->lang->range());
// 加载系统语言包
$this->lang->load([
$this->thinkPath . 'lang/' . $this->request->langset() . '.php',
$this->appPath . 'lang/' . $this->request->langset() . '.php',
]);
// 获取应用调度信息
$dispatch = $this->dispatch;
if (empty($dispatch)) {
// 进行URL路由检测
$dispatch = $this->routeCheck($this->request);
}
// 记录当前调度信息
$this->request->dispatch($dispatch);
// 记录路由和请求信息
if ($this->debug) {
$this->log('[ ROUTE ] ' . var_export($this->request->routeinfo(), true));
$this->log('[ HEADER ] ' . var_export($this->request->header(), true));
$this->log('[ PARAM ] ' . var_export($this->request->param(), true));
}
// 监听app_begin
$this->hook->listen('app_begin', $dispatch);
// 请求缓存检查
$this->request->cache(
$this->config('app.request_cache'),
$this->config('app.request_cache_expire'),
$this->config('app.request_cache_except')
);
// 执行调度
$data = $dispatch->run();
} catch (HttpResponseException $exception) {
$data = $exception->ge