Skip to content

Commit

Permalink
添加Part2部分并更新Thinkphp5漏洞分析
Browse files Browse the repository at this point in the history
  • Loading branch information
Mochazz committed Apr 3, 2019
1 parent 8f70f5a commit 215ec84
Show file tree
Hide file tree
Showing 286 changed files with 581 additions and 3 deletions.
Empty file modified Part1/Day1/files/piwigo-2.7.1.zip
100644 → 100755
Empty file.
Empty file modified Part1/Day10/files/1.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Part1/Day10/files/10.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Part1/Day10/files/11.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Part1/Day10/files/12.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Part1/Day10/files/2.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Part1/Day10/files/3.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Part1/Day10/files/4.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Part1/Day10/files/5.gif
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Part1/Day10/files/6.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Part1/Day10/files/7.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Part1/Day10/files/8.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Part1/Day10/files/9.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Part1/Day10/files/FengCms+Beta+1.32.7z
100644 → 100755
Empty file.
Empty file modified Part1/Day10/files/README.md
100644 → 100755
Empty file.
Empty file modified Part1/Day10/files/Simple-Loga5.zip
100644 → 100755
Empty file.
Empty file modified Part1/Day11/files/1.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Part1/Day11/files/10.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Part1/Day11/files/11.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Part1/Day11/files/12.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Part1/Day11/files/13.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Part1/Day11/files/14.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Part1/Day11/files/15.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Part1/Day11/files/16.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Part1/Day11/files/17.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Part1/Day11/files/18.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Part1/Day11/files/19.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Part1/Day11/files/2.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Part1/Day11/files/3.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified Part1/Day11/files/4.png
100644 → 100755
Empty file modified Part1/Day11/files/5.png
100644 → 100755
Empty file modified Part1/Day11/files/6.png
100644 → 100755
Empty file modified Part1/Day11/files/7.png
100644 → 100755
Empty file modified Part1/Day11/files/8.png
100644 → 100755
Empty file modified Part1/Day11/files/9.png
100644 → 100755
Empty file modified Part1/Day11/files/README.md
100644 → 100755
Empty file.
Empty file modified Part1/Day11/files/typecho-1.1-15.5.12-beta.zip
100644 → 100755
Empty file.
Empty file modified Part1/Day12/.DS_Store
100644 → 100755
Empty file.
Empty file modified Part1/Day12/files/1.png
100644 → 100755
Empty file modified Part1/Day12/files/10.png
100644 → 100755
Empty file modified Part1/Day12/files/11.png
100644 → 100755
Empty file modified Part1/Day12/files/12.png
100644 → 100755
Empty file modified Part1/Day12/files/13.png
100644 → 100755
Empty file modified Part1/Day12/files/14.png
100644 → 100755
Empty file modified Part1/Day12/files/3.png
100644 → 100755
Empty file modified Part1/Day12/files/4.png
100644 → 100755
Empty file modified Part1/Day12/files/5.png
100644 → 100755
Empty file modified Part1/Day12/files/6.png
100644 → 100755
Empty file modified Part1/Day12/files/7.png
100644 → 100755
Empty file modified Part1/Day12/files/8.png
100644 → 100755
Empty file modified Part1/Day12/files/9.png
100644 → 100755
Empty file modified Part1/Day12/files/DM企业建站系统v201710.zip
100644 → 100755
Empty file.
10 changes: 10 additions & 0 deletions Part1/Day12/files/README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ while($row = mysql_fetch_array($result))
?>
```

```php
<?php
$mysql_server_name="localhost";
$mysql_database="Day12"; /** 数据库的名称 */
$mysql_username="Hongri"; /** MySQL数据库用户名 */
$mysql_password="Hongri"; /** MySQL数据库密码 */
$conn = mysql_connect($mysql_server_name, $mysql_username,$mysql_password,'utf-8');
?>
```

```sql
# Host: localhost (Version: 5.5.53)
# Date: 2018-08-05 12:55:29
Expand Down
Empty file modified Part1/Day13/files/1.png
100644 → 100755
Empty file modified Part1/Day13/files/10.png
100644 → 100755
Empty file modified Part1/Day13/files/11.png
100644 → 100755
Empty file modified Part1/Day13/files/12.png
100644 → 100755
Empty file modified Part1/Day13/files/13.png
100644 → 100755
Empty file modified Part1/Day13/files/14.png
100644 → 100755
Empty file modified Part1/Day13/files/15.png
100644 → 100755
Empty file modified Part1/Day13/files/2.png
100644 → 100755
Empty file modified Part1/Day13/files/3.png
100644 → 100755
Empty file modified Part1/Day13/files/4.png
100644 → 100755
Empty file modified Part1/Day13/files/5.png
100644 → 100755
Empty file modified Part1/Day13/files/6.png
100644 → 100755
Empty file modified Part1/Day13/files/7.png
100644 → 100755
Empty file modified Part1/Day13/files/8.png
100644 → 100755
Empty file modified Part1/Day13/files/9.png
100644 → 100755
Empty file modified Part1/Day13/files/README.md
100644 → 100755
Empty file.
Empty file modified Part1/Day13/files/maccms8_mfb.zip
100644 → 100755
Empty file.
Empty file modified Part1/Day14/files/1.png
100644 → 100755
Empty file modified Part1/Day14/files/10.png
100644 → 100755
Empty file modified Part1/Day14/files/11.png
100644 → 100755
Empty file modified Part1/Day14/files/12.png
100644 → 100755
Empty file modified Part1/Day14/files/13.png
100644 → 100755
Empty file modified Part1/Day14/files/14.png
100644 → 100755
Empty file modified Part1/Day14/files/15.png
100644 → 100755
Empty file modified Part1/Day14/files/2.png
100644 → 100755
Empty file modified Part1/Day14/files/3.png
100644 → 100755
Empty file modified Part1/Day14/files/4.png
100644 → 100755
Empty file modified Part1/Day14/files/5.png
100644 → 100755
Empty file modified Part1/Day14/files/6.png
100644 → 100755
Empty file modified Part1/Day14/files/7.png
100644 → 100755
Empty file modified Part1/Day14/files/8.png
100644 → 100755
Empty file modified Part1/Day14/files/9.png
100644 → 100755
Empty file modified Part1/Day14/files/Duomicms_X3.0.zip
100644 → 100755
Empty file.
Empty file modified Part1/Day14/files/README.md
100644 → 100755
Empty file.
Empty file modified Part1/Day15/files/1.png
100644 → 100755
Empty file modified Part1/Day15/files/2.png
100644 → 100755
Empty file modified Part1/Day15/files/3.png
100644 → 100755
Empty file modified Part1/Day15/files/360safe.zip
100644 → 100755
Empty file.
Empty file modified Part1/Day15/files/4.png
100644 → 100755
Empty file modified Part1/Day15/files/5.png
100644 → 100755
Empty file modified Part1/Day15/files/6.png
100644 → 100755
Empty file modified Part1/Day15/files/7.png
100644 → 100755
Empty file modified Part1/Day15/files/8.png
100644 → 100755
Empty file modified Part1/Day15/files/README.md
100644 → 100755
Empty file.
Empty file modified Part1/Day16/files/1.png
100644 → 100755
Empty file modified Part1/Day16/files/10.png
100644 → 100755
Empty file modified Part1/Day16/files/11.png
100644 → 100755
Empty file modified Part1/Day16/files/12.png
100644 → 100755
Empty file modified Part1/Day16/files/2.png
100644 → 100755
Empty file modified Part1/Day16/files/3.png
100644 → 100755
Empty file modified Part1/Day16/files/4.png
100644 → 100755
Empty file modified Part1/Day16/files/5.png
100644 → 100755
Empty file modified Part1/Day16/files/6.png
100644 → 100755
Empty file modified Part1/Day16/files/7.png
100644 → 100755
Empty file modified Part1/Day16/files/8.png
100644 → 100755
Empty file modified Part1/Day16/files/9.png
100644 → 100755
Empty file modified Part1/Day16/files/README.md
100644 → 100755
Empty file.
Empty file modified Part1/Day16/files/all-in-one-wp-security.zip
100644 → 100755
Empty file.
Empty file modified Part1/Day16/files/wordpress-4.9.4-zh_CN.zip
100644 → 100755
Empty file.
Empty file modified Part1/Day17/files/1.png
100644 → 100755
Empty file modified Part1/Day17/files/2.png
100644 → 100755
Empty file modified Part1/Day17/files/3.png
100644 → 100755
Empty file modified Part1/Day17/files/4.png
100644 → 100755
Empty file modified Part1/Day17/files/5.png
100644 → 100755
Empty file modified Part1/Day17/files/6.png
100644 → 100755
Empty file modified Part1/Day17/files/7.png
100644 → 100755
Empty file modified Part1/Day17/files/8.png
100644 → 100755
Empty file modified Part1/Day17/files/9.png
100644 → 100755
Empty file modified Part1/Day17/files/README.md
100644 → 100755
Empty file.
Empty file modified Part1/Day2/files/README.md
100644 → 100755
Empty file.
Empty file modified Part1/Day2/files/anchor-cms-0.9.2.zip
100644 → 100755
Empty file.
Empty file modified Part1/Day3/files/README.md
100644 → 100755
Empty file.
Empty file modified Part1/Day3/files/shopware-5.3.3.zip
100644 → 100755
Empty file.
Empty file modified Part1/Day4/files/1.png
100644 → 100755
Empty file modified Part1/Day4/files/10.png
100644 → 100755
Empty file modified Part1/Day4/files/11.png
100644 → 100755
Empty file modified Part1/Day4/files/12.png
100644 → 100755
Empty file modified Part1/Day4/files/13.png
100644 → 100755
Empty file modified Part1/Day4/files/14.png
100644 → 100755
Empty file modified Part1/Day4/files/15.png
100644 → 100755
Empty file modified Part1/Day4/files/16.png
100644 → 100755
Empty file modified Part1/Day4/files/17.png
100644 → 100755
Empty file modified Part1/Day4/files/18.png
100644 → 100755
Empty file modified Part1/Day4/files/19.png
100644 → 100755
Empty file modified Part1/Day4/files/2.png
100644 → 100755
Empty file modified Part1/Day4/files/20.png
100644 → 100755
Empty file modified Part1/Day4/files/21.png
100644 → 100755
Empty file modified Part1/Day4/files/3.png
100644 → 100755
Empty file modified Part1/Day4/files/4.png
100644 → 100755
Empty file modified Part1/Day4/files/5.png
100644 → 100755
Empty file modified Part1/Day4/files/6.png
100644 → 100755
Empty file modified Part1/Day4/files/7.png
100644 → 100755
Empty file modified Part1/Day4/files/8.png
100644 → 100755
Empty file modified Part1/Day4/files/9.png
100644 → 100755
Empty file modified Part1/Day4/files/DedeCMS-V5.7-UTF8-SP2.tar.gz
100644 → 100755
Empty file.
Empty file modified Part1/Day4/files/README.md
100644 → 100755
Empty file.
Empty file modified Part1/Day5/files/.DS_Store
100644 → 100755
Empty file.
Empty file modified Part1/Day5/files/1.png
100644 → 100755
Empty file modified Part1/Day5/files/10.png
100644 → 100755
Empty file modified Part1/Day5/files/11.png
100644 → 100755
Empty file modified Part1/Day5/files/12.png
100644 → 100755
Empty file modified Part1/Day5/files/13.png
100644 → 100755
Empty file modified Part1/Day5/files/14.png
100644 → 100755
Empty file modified Part1/Day5/files/15.png
100644 → 100755
Empty file modified Part1/Day5/files/16.png
100644 → 100755
Empty file modified Part1/Day5/files/17.png
100644 → 100755
Empty file modified Part1/Day5/files/18.png
100644 → 100755
Empty file modified Part1/Day5/files/2.png
100644 → 100755
Empty file modified Part1/Day5/files/3.png
100644 → 100755
Empty file modified Part1/Day5/files/4.png
100644 → 100755
Empty file modified Part1/Day5/files/5.png
100644 → 100755
Empty file modified Part1/Day5/files/6.png
100644 → 100755
Empty file modified Part1/Day5/files/7.png
100644 → 100755
Empty file modified Part1/Day5/files/8.png
100644 → 100755
Empty file modified Part1/Day5/files/9.png
100644 → 100755
Empty file modified Part1/Day5/files/README.md
100644 → 100755
Empty file.
Empty file modified Part1/Day6/.DS_Store
100644 → 100755
Empty file.
Empty file modified Part1/Day6/files/.DS_Store
100644 → 100755
Empty file.
Empty file modified Part1/Day6/files/1.png
100644 → 100755
Empty file modified Part1/Day6/files/10.png
100644 → 100755
Empty file modified Part1/Day6/files/11.png
100644 → 100755
Empty file modified Part1/Day6/files/12.png
100644 → 100755
Empty file modified Part1/Day6/files/13.png
100644 → 100755
Empty file modified Part1/Day6/files/14.png
100644 → 100755
Empty file modified Part1/Day6/files/2.png
100644 → 100755
Empty file modified Part1/Day6/files/3.png
100644 → 100755
Empty file modified Part1/Day6/files/4.png
100644 → 100755
Empty file modified Part1/Day6/files/5.png
100644 → 100755
Empty file modified Part1/Day6/files/6.png
100644 → 100755
Empty file modified Part1/Day6/files/7.png
100644 → 100755
Empty file modified Part1/Day6/files/8.png
100644 → 100755
Empty file modified Part1/Day6/files/9.png
100644 → 100755
4 changes: 2 additions & 2 deletions Part1/Day6/files/README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

**漏洞解析**

这一关考察的内容是由正则表达式不严谨导致的任意文件删除漏洞, 导致这一漏洞的原因在 **第21行****preg_replace** 中的 **pattern** 部分 ,该正则表达式并未起到过滤目录路径字符的作用。`[^a-z.-_]` 表示匹配除了 **a** 字符到 **z** 字符、**.** 字符到 **_** 字符之间的所有字符。因此,攻击者还是可以使用点和斜杠符号进行路径穿越,最终删除任意文件,例如使用 **payload**`action = delete&data = ../../ config.php`,便可删除 **config.php** 文件。
这一关考察的内容是由正则表达式不严谨导致的任意文件删除漏洞, 导致这一漏洞的原因在 **第19行****preg_replace** 中的 **pattern** 部分 ,该正则表达式并未起到过滤目录路径字符的作用。`[^a-z.-_]` 表示匹配除了 **a** 字符到 **z** 字符、**.** 字符到 **_** 字符之间的所有字符。因此,攻击者还是可以使用点和斜杠符号进行路径穿越,最终删除任意文件,例如使用 **payload**`action = delete&data = ../../ config.php`,便可删除 **config.php** 文件。

>[**preg_replace**](http://php.net/manual/zh/function.preg-replace.php):(PHP 4, PHP 5, PHP 7)
>
Expand All @@ -36,7 +36,7 @@

![4](4.png)

我们再往上看,即可找到 **$navs** 变量的取值情况。可以看到 **$navs** 变量的是是重数据库 **site_nav** 表中取出的,包含了 **icon****id** 两个字段,具体代码如下:
我们再往上看,即可找到 **$navs** 变量的取值情况。可以看到 **$navs** 变量的是从数据库 **site_nav** 表中取出的,包含了 **icon****id** 两个字段,具体代码如下:

```php
$navs = pdo_fetchall("SELECT icon, id FROM ".tablename('site_nav')." WHERE id IN (SELECT nid FROM ".tablename('site_category')." WHERE id = {$id} OR parentid = '$id')", array(), 'id');
Expand Down
Empty file modified Part1/Day7/.DS_Store
100644 → 100755
Empty file.
Empty file modified Part1/Day7/files/1.png
100644 → 100755
Empty file modified Part1/Day7/files/10.png
100644 → 100755
Empty file modified Part1/Day7/files/11.png
100644 → 100755
Empty file modified Part1/Day7/files/13.png
100644 → 100755
Empty file modified Part1/Day7/files/14.png
100644 → 100755
Empty file modified Part1/Day7/files/15.png
100644 → 100755
Empty file modified Part1/Day7/files/16.png
100644 → 100755
Empty file modified Part1/Day7/files/17.png
100644 → 100755
Empty file modified Part1/Day7/files/18.png
100644 → 100755
Empty file modified Part1/Day7/files/2.png
100644 → 100755
Empty file modified Part1/Day7/files/3.png
100644 → 100755
Empty file modified Part1/Day7/files/4.png
100644 → 100755
Empty file modified Part1/Day7/files/5.png
100644 → 100755
Empty file modified Part1/Day7/files/6.png
100644 → 100755
Empty file modified Part1/Day7/files/7.png
100644 → 100755
Empty file modified Part1/Day7/files/8.png
100644 → 100755
Empty file modified Part1/Day7/files/9.png
100644 → 100755
Empty file modified Part1/Day7/files/DedeCmsV5.6-UTF8-Final.tar.gz
100644 → 100755
Empty file.
Empty file modified Part1/Day8/files/1.png
100644 → 100755
Empty file modified Part1/Day8/files/10.png
100644 → 100755
Empty file modified Part1/Day8/files/11.png
100644 → 100755
Empty file modified Part1/Day8/files/12.png
100644 → 100755
Empty file modified Part1/Day8/files/13.png
100644 → 100755
Empty file modified Part1/Day8/files/14.png
100644 → 100755
Empty file modified Part1/Day8/files/2.png
100644 → 100755
Empty file modified Part1/Day8/files/3.png
100644 → 100755
Empty file modified Part1/Day8/files/4.png
100644 → 100755
Empty file modified Part1/Day8/files/5.png
100644 → 100755
Empty file modified Part1/Day8/files/6.png
100644 → 100755
Empty file modified Part1/Day8/files/7.png
100644 → 100755
Empty file modified Part1/Day8/files/8.png
100644 → 100755
Empty file modified Part1/Day8/files/9.png
100644 → 100755
Empty file modified Part1/Day8/files/CmsEasy.rar
100644 → 100755
Empty file.
Empty file modified Part1/Day8/files/README.md
100644 → 100755
Empty file.
Empty file modified Part1/Day9/files/1.png
100644 → 100755
Empty file modified Part1/Day9/files/10.png
100644 → 100755
Empty file modified Part1/Day9/files/2.png
100644 → 100755
Empty file modified Part1/Day9/files/3.png
100644 → 100755
Empty file modified Part1/Day9/files/4.png
100644 → 100755
Empty file modified Part1/Day9/files/5.png
100644 → 100755
Empty file modified Part1/Day9/files/6.png
100644 → 100755
Empty file modified Part1/Day9/files/7.png
100644 → 100755
Empty file modified Part1/Day9/files/8.png
100644 → 100755
Empty file modified Part1/Day9/files/9.png
100644 → 100755
Empty file modified Part1/Day9/files/MetInfo6.0.0.zip
100644 → 100755
Empty file.
Empty file modified Part1/Day9/files/README.md
100644 → 100755
Empty file.
90 changes: 90 additions & 0 deletions Part2/ThinkPHP5/ThinkPHP5漏洞分析之SQL注入1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
本系列文章将针对 **ThinkPHP** 的历史漏洞进行分析,今后爆出的所有 **ThinkPHP** 漏洞分析,也将更新于 [ThinkPHP-Vuln](https://github.com/Mochazz/ThinkPHP-Vuln) 项目上。本篇文章,将分析 **ThinkPHP** 中存在的 **SQL注入漏洞****insert** 方法注入)。

## 漏洞概要

本次漏洞存在于 **Builder** 类的 **parseData** 方法中。由于程序没有对数据进行很好的过滤,将数据拼接进 **SQL** 语句,导致 **SQL注入漏洞** 的产生。漏洞影响版本: **5.0.13<=ThinkPHP<=5.0.15****5.1.0<=ThinkPHP<=5.1.5**

## 漏洞环境

通过以下命令获取测试环境代码:

```bash
composer create-project --prefer-dist topthink/think=5.0.15 tpdemo
```

**composer.json** 文件的 **require** 字段设置成如下:

```json
"require": {
"php": ">=5.4.0",
"topthink/framework": "5.0.15"
}
```

然后执行 `composer update` ,并将 **application/index/controller/Index.php** 文件代码设置如下:

```php
<?php
namespace app\index\controller;

class Index
{
public function index()
{
$username = request()->get('username/a');
db('users')->insert(['username' => $username]);
return 'Update success';
}
}
```

**application/database.php** 文件中配置数据库相关信息,并开启 **application/config.php** 中的 **app_debug****app_trace** 。创建数据库信息如下:

```sql
create database tpdemo;
use tpdemo;
create table users(
id int primary key auto_increment,
username varchar(50) not null
);
```

访问 **http://yoursite/index/index/index?username[0]=inc&username[1]=updatexml(1,concat(0x7,user(),0x7e),1)&username[2]=1** 链接,即可触发 **SQL注入漏洞** 。(没开启 **app_debug** 是无法看到 **SQL** 报错信息的)

![1](ThinkPHP5漏洞分析之SQL注入1/1.png)

## 漏洞分析

![2](ThinkPHP5漏洞分析之SQL注入1/2.png)

首先在官方发布的 **5.0.16** 版本更新说明中,发现其中提到该版本包含了一个安全更新,我们可以查阅其 **commit** 记录,发现其修改的 **Builder.php** 文件代码比较可疑。

![3](ThinkPHP5漏洞分析之SQL注入1/3.png)

接着我们直接跟着上面的攻击 **payload** 来看看漏洞原理。首先, **payload** 数据经过 **ThinkPHP** 内置方法的过滤后(不影响我们的 **payload** ),直接进入了 **$this->builder****insert** 方法,这里的 **$this->builder****\think\db\builder\Mysql** 类,代码如下:

![4](ThinkPHP5漏洞分析之SQL注入1/4.png)

**Mysql** 类继承于 **Builder** 类,即上面的 **$this->builder->insert()** 最终调用的是 **Builder** 类的 **insert** 方法。在 **insert** 方法中,我们看到其调用 **parseData** 方法来分析并处理数据,而 **parseData** 方法直接将来自用户的数据 **$val** 进行了拼接返回。我们的恶意数据存储在 **$val[1]** 中,虽经过了 **parseKey** 方法处理,当丝毫不受影响,因为该方法只是用来解析处理数据的,并不是清洗数据。

![5](ThinkPHP5漏洞分析之SQL注入1/5.png)

上面,我们看到直接将用户数据进行拼接。然后再回到 **Builder** 类的 **insert** 方法,直接通过替换字符串的方式,将 **$data** 填充到 **SQL** 语句中,进而执行,造成 **SQL注入漏洞**

![6](ThinkPHP5漏洞分析之SQL注入1/6.png)

至此,我们已将整个漏洞分析完了。实际上,上面的 **switch** 结构中,3种情况返回的数据都有可能造成 **SQL** 注入漏洞,但是在观察 **ThinkPHP** 官方的修复代码中,发现其只对 **inc****dec** 进行了修复,而对于 **exp** 的情况并未处理,这是为什么呢?

实际上, **exp** 的情况早在传入 **insert** 方法前就被 **ThinkPHP** 内置过滤方法给处理了,如果数据中存在 **exp** ,则会被替换成 **exp空格** ,这也是为什么 **ThinkPHP** 官方没有对 **exp** 的情况进行处理的原因了。具体内置过滤方法的代码如下:

![7](ThinkPHP5漏洞分析之SQL注入1/7.png)

## 漏洞修复

![8](ThinkPHP5漏洞分析之SQL注入1/8.png)

## 攻击总结

最后,再通过一张攻击流程图来回顾整个攻击过程。

![9](ThinkPHP5漏洞分析之SQL注入1/9.png)
99 changes: 99 additions & 0 deletions Part2/ThinkPHP5/ThinkPHP5漏洞分析之SQL注入2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
本系列文章将针对 **ThinkPHP** 的历史漏洞进行分析,今后爆出的所有 **ThinkPHP** 漏洞分析,也将更新于 [ThinkPHP-Vuln](https://github.com/Mochazz/ThinkPHP-Vuln) 项目上。本篇文章,将分析 **ThinkPHP** 中存在的 **SQL注入漏洞****update** 方法注入)。

## 漏洞概要

本次漏洞存在于 **Mysql** 类的 **parseArrayData** 方法中由于程序没有对数据进行很好的过滤,将数据拼接进 **SQL** 语句,导致 **SQL注入漏洞** 的产生。漏洞影响版本: **5.1.6<=ThinkPHP<=5.1.7** (非最新的 **5.1.8** 版本也可利用)。

## 漏洞环境

通过以下命令获取测试环境代码:

```bash
composer create-project --prefer-dist topthink/think tpdemo
```

**composer.json** 文件的 **require** 字段设置成如下:

```json
"require": {
"php": ">=5.6.0",
"topthink/framework": "5.1.7"
}
```

然后执行 `composer update` ,并将 **application/index/controller/Index.php** 文件代码设置如下:

```php
<?php
namespace app\index\controller;

class Index
{
public function index()
{
$username = request()->get('username/a');
db('users')->where(['id' => 1])->update(['username' => $username]);
return 'Update success';
}
}
```

**config/database.php** 文件中配置数据库相关信息,并开启 **config/app.php** 中的 **app_debug****app_trace** 。创建数据库信息如下:

```sql
create database tpdemo;
use tpdemo;
create table users(
id int primary key auto_increment,
username varchar(50) not null
);
insert into users(id,username) values(1,'mochazz');
```

访问 **http://localhost:8000/index/index/index?username[0]=point&username[1]=1&username[2]=updatexml(1,concat(0x7,user(),0x7e),1)^&username[3]=0** 链接,即可触发 **SQL注入漏洞** 。(没开启 **app_debug** 是无法看到 **SQL** 报错信息的)

![1](ThinkPHP5漏洞分析之SQL注入2/1.png)

## 漏洞分析

![2](ThinkPHP5漏洞分析之SQL注入2/2.png)

首先在官方发布的 **5.1.9** 版本更新说明中,发现其中提到该版本包含了一个安全更新,我们可以查阅其 **commit** 记录,发现其删除了 **parseArrayData** 方法,这处 **case** 语句之前出现过 **insert** 注入,所以比较可疑。

![3](ThinkPHP5漏洞分析之SQL注入2/3.png)

接着我们直接跟着上面的攻击 **payload** 来看看漏洞原理。首先, **payload** 数据经过 **ThinkPHP** 内置方法的过滤后(不影响我们的 **payload** ),直接进入了 **Query** 类的 **update** 方法,该方法调用了 **Connection** 类的 **update** 方法,该方法又调用了 **$this->builder** 的 **insert** 方法,这里的 **$this->builder** 为 **\think\db\builder\Mysql** 类,该类继承于 **Builder** 类,代码如下:

![4](ThinkPHP5漏洞分析之SQL注入2/4.png)

在 **Builder** 类的 **update** 方法中,调用了 **parseData** 方法。这个方法中的 **case** 语句之前存在 **SQL注入漏洞** ,现已修复,然而却多了 **default** 代码段,而这段代码也是在新版本中被删除的。

![5](ThinkPHP5漏洞分析之SQL注入2/5.png)

我们跟进到 **parseArrayData** 方法,发现其中又将可控变量进行拼接,其变量来源均来自用户输入。之后的过程就和之前的 **insert** 注入一样,用 **str_replace** 将变量填充到 **SQL** 语句中,最终执行,导致 **SQL注入漏洞**

![6](ThinkPHP5漏洞分析之SQL注入2/6.png)

上面 **第15行****$result** 相当于 **$a('$b($c)')** 其中 **$a、$b、$c** 均可控。最后形成的 **SQL** 语句如下:

```sql
UPDATE `users` SET `username` = $a('$b($c)') WHERE `id` = 1;
```

接着我们想办法闭合即可。我们令 **$a = updatexml(1,concat(0x7,user(),0x7e),1)^****$b = 0****$c = 1** ,即:

```sql
UPDATE `users` SET `username` = updatexml(1,concat(0x7,user(),0x7e),1)^('0(1)') WHERE `id` = 1
```

## 漏洞修复

官方修复方法比较暴力,直接将 **parseArrayData** 方法删除了。

![7](ThinkPHP5漏洞分析之SQL注入2/7.png)

## 攻击总结

最后,再通过一张攻击流程图来回顾整个攻击过程。

![8](ThinkPHP5漏洞分析之SQL注入2/8.png)
81 changes: 81 additions & 0 deletions Part2/ThinkPHP5/ThinkPHP5漏洞分析之SQL注入3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
本系列文章将针对 **ThinkPHP** 的历史漏洞进行分析,今后爆出的所有 **ThinkPHP** 漏洞分析,也将更新于 [ThinkPHP-Vuln](https://github.com/Mochazz/ThinkPHP-Vuln) 项目上。本篇文章,将分析 **ThinkPHP** 中存在的 **SQL注入漏洞****select** 方法注入)。

## 漏洞概要

本次漏洞存在于 **Mysql** 类的 **parseWhereItem** 方法中。由于程序没有对数据进行很好的过滤,将数据拼接进 **SQL** 语句,导致 **SQL注入漏洞** 的产生。漏洞影响版本: **ThinkPHP5全版本**

## 漏洞环境

通过以下命令获取测试环境代码:

```bash
composer create-project --prefer-dist topthink/think=5.0.10 tpdemo
```

**composer.json** 文件的 **require** 字段设置成如下:

```json
"require": {
"php": ">=5.4.0",
"topthink/framework": "5.0.10"
},
```

然后执行 `composer update` ,并将 **application/index/controller/Index.php** 文件代码设置如下:

```php
<?php
namespace app\index\controller;

class Index
{
public function index()
{
$username = request()->get('username');
$result = db('users')->where('username','exp',$username)->select();
return 'select success';
}
}
```

**config/database.php** 文件中配置数据库相关信息,并开启 **config/app.php** 中的 **app_debug****app_trace** 。创建数据库信息如下:

```sql
create database tpdemo;
use tpdemo;
create table users(
id int primary key auto_increment,
username varchar(50) not null
);
insert into users(id,username) values(1,'mochazz');
```

访问 **http://localhost:8000/index/index/index?username=) union select updatexml(1,concat(0x7,user(),0x7e),1)#** 链接,即可触发 **SQL注入漏洞** 。(没开启 **app_debug** 是无法看到 **SQL** 报错信息的)

![1](ThinkPHP5漏洞分析之SQL注入3/1.png)

## 漏洞分析

由于官方根本不认为这是一个漏洞,而认为这是他们提供的一个功能,所以官方并没有对这个问题进行修复。但笔者认为这里的数据过滤还是存在问题的,所以我们还是来分析分析这个漏洞。

程序默认调用 **Request** 类的 **get** 方法中会调用该类的 **input** 方法,但是该方法默认情况下并没有对数据进行很好的过滤,所以用户输入的数据会原样进入框架的 **SQL** 查询方法中。首先程序先调用 **Query** 类的 **where** 方法,通过其 **parseWhereExp** 方法分析查询表达式,然后再返回并继续调用 **select** 方法准备开始构建 **select** 语句。

![2](ThinkPHP5漏洞分析之SQL注入3/2.png)

上面的 **$this->builder** 为 **\think\db\builder\Mysql** 类,该类继承于 **Builder** 类,所以接着会调用 **Builder** 类的 **select** 方法。在 **select** 方法中,程序会对 **SQL** 语句模板用变量填充,其中用来填充 **%WHERE%** 的变量中存在用户输入的数据。我们跟进这个 **where** 分析函数,会发现其会调用生成查询条件 **SQL** 语句的 **buildWhere** 函数。

![3](ThinkPHP5漏洞分析之SQL注入3/3.png)

继续跟进 **buildWhere** 函数,发现用户可控数据又被传入了 **parseWhereItem** where子单元分析函数。我们发现当操作符等于 **EXP** 时,将来自用户的数据直接拼接进了 **SQL** 语句,最终导致了 **SQL注入漏洞**

![4](ThinkPHP5漏洞分析之SQL注入3/4.png)

## 漏洞修复

未修复。

## 攻击总结

最后,再通过一张攻击流程图来回顾整个攻击过程。

![5](ThinkPHP5漏洞分析之SQL注入3/5.png)
Loading

0 comments on commit 215ec84

Please sign in to comment.