root@14ab6e4fe4be:/# ls -al /var/lib/mysql/ total 28700 drwx------ 5 mysql mysql 4096 May 8 07:52 . drwxr-xr-x 1 root root 4096 Feb 15 2016 .. -rw-rw---- 1 mysql mysql 393 May 8 07:52 14ab6e4fe4be.log -rw-rw---- 1 mysql mysql 5242880 May 8 03:13 ib_logfile0 -rw-rw---- 1 mysql mysql 5242880 May 8 03:13 ib_logfile1 -rw-rw---- 1 mysql mysql 18874368 May 8 03:13 ibdata1 drwx------ 2 mysql root 4096 May 8 03:13 mysql drwx------ 2 mysql mysql 4096 May 8 03:13 performance_schema drwx------ 2 mysql mysql 4096 May 8 03:23 testdb
mysql> SET GLOBAL general_log_file='/var/lib/mysql/14ab6e4fe4be.log'; Query OK, 0 rows affected (0.00 sec)
mysql> show VARIABLES LIKE "general_log%"; +------------------+----------------------------------------+ | Variable_name | Value | +------------------+----------------------------------------+ | general_log | ON | | general_log_file | /usr/local/mysql/data/9956ee9aa191.log | +------------------+----------------------------------------+ 2 rows in set (0.00 sec)
UDF提权排查
查看 MySQL 版本,判断 UDF 提权文件的导出路径
1 2 3
select version();
// 5.5.23
在 MySQL 版本高于或等于5.1的情况下,请检查plugin目录,可能存在恶意的.so文件
1 2 3
show variables like '%plugin%';
// 路径为:/usr/local/mysql/lib/plugin/
检查日志是否开启,默认为关闭状态
1 2 3 4 5 6 7 8
mysql> show VARIABLES LIKE "general_log%";
+------------------+----------------------------------------+ | Variable_name | Value | +------------------+----------------------------------------+ | general_log | ON | | general_log_file | /usr/local/mysql/data/9956ee9aa191.log | +------------------+----------------------------------------+
日志开启情况下排查
若日志文件启用,可以检索其中的记录,查看是否存在 create function 的记录。若存在,则可发现创建的恶意函数名,在此处是 sys_eval
root@9956ee9aa191:/# ls -al /usr/local/mysql/lib/plugin/ total 1512 drwxr-xr-x 1 mysql mysql 4096 May 7 09:38 . drwxr-xr-x 1 mysql mysql 4096 Mar 23 2018 .. -rw-r--r-- 1 mysql mysql 13920 Mar 23 2018 adt_null.so -rw-r--r-- 1 mysql mysql 29992 Mar 23 2018 auth.so -rw-r--r-- 1 mysql mysql 14456 Mar 23 2018 auth_socket.so -rw-r--r-- 1 mysql mysql 27976 Mar 23 2018 auth_test_plugin.so -rw-r--r-- 1 mysql mysql 227 Mar 29 2012 daemon_example.ini drwxr-xr-x 1 mysql mysql 4096 Mar 23 2018 debug -rw-r--r-- 1 mysql mysql 271184 Mar 23 2018 ha_archive.so -rw-r--r-- 1 mysql mysql 219064 Mar 23 2018 ha_blackhole.so -rw-r--r-- 1 mysql mysql 175272 Mar 23 2018 ha_example.so -rw-r--r-- 1 mysql mysql 340712 Mar 23 2018 ha_federated.so -rw-r--r-- 1 mysql mysql 36440 Mar 23 2018 libdaemon_example.so -rw-r--r-- 1 mysql mysql 20816 Mar 23 2018 mypluglib.so -rw-r--r-- 1 mysql mysql 19512 Mar 23 2018 qa_auth_client.so -rw-r--r-- 1 mysql mysql 30568 Mar 23 2018 qa_auth_interface.so -rw-r--r-- 1 mysql mysql 15600 Mar 23 2018 qa_auth_server.so -rw-r--r-- 1 mysql mysql 182336 Mar 23 2018 semisync_master.so -rw-r--r-- 1 mysql mysql 91000 Mar 23 2018 semisync_slave.so -rw-rw-rw- 1 mysql mysql 8040 May 7 09:38 udf.so
在 MySQL 数据库中的 func 表中存在恶意函数记录
1 2 3 4 5 6 7
select * from mysql.func
+-----------+---------+---------------+-----+ | name | dl | type | ret | +-----------+---------+---------------+-----+ | sys_eval | udf.so | function | 0 | +-----------+---------+---------------+-----+
662 Connect root@localhost on 662 Connect Access denied for user 'root'@'localhost' (using password: YES) 663 Connect root@localhost on 663 Query select @@version_comment limit 1 240508 1:58:37 663 Quit 664 Connect root@localhost on 664 Connect Access denied for user 'root'@'localhost' (using password: YES) 665 Connect root@localhost on 665 Connect Access denied for user 'root'@'localhost' (using password: YES)
若使用 MSF 进行攻击,只有150行左右的日志记录,登录成功后的会导出 MySQL 所有用户的哈希值,可以在日志中看到如下语句
1 2 3 4 5
142 Connect root@172.18.0.1 on 142 Connect Access denied for user 'root'@'172.18.0.1' (using password: YES) 145 Connect root@172.18.0.1 on 145 Connect Access denied for user 'root'@'172.18.0.1' (using password: YES) 136 Query SELECT user,password from mysql.user
root@9956ee9aa191:/# cat /usr/local/mysql/data/9956ee9aa191.log | grep "sys_eval" 240507 10:13:47 8 Query drop function if exists sys_eval 8 Query create function sys_eval returns string soname '3fe6a354251823f6.temp' 240507 10:13:51 8 Query select sys_eval('id') as s 240507 10:13:57 8 Query select sys_eval('id') as s 8 Query select sys_eval('id') as s 8 Query select sys_eval('id') as s 8 Query select sys_eval('id') as s 8 Query select sys_eval('id') as s 240507 10:13:58 8 Query select sys_eval('id') as s 8 Query select sys_eval('id') as s