คลัง
iot

Firmware Emulation

Firmware Emulation คือการรัน firmware ของอุปกรณ์ IoT บนเครื่องเรา (โดยไม่ต้องมีอุปกรณ์จริง) ผ่าน QEMU เพื่อ debug, ทดสอบ web interface, และ exploit แบบ dynamic บทนี้ลงลึก user-mode vs system-mode emulation, การ chroot รัน binary เดี่ยว, การใช้ FirmAE/FirmaDyne รัน firmware เต็ม, และ troubleshooting (เนื้อหาเพื่อฝึกใน lab/CTF/อุปกรณ์ที่ได้รับอนุญาต)

Advanced#firmware#iot#emulation#qemu#firmae#firmadyne#ctf

1. ทำไมต้อง emulate

หลังแกะ firmware (ดู Firmware Analysis) การอ่าน static มีขีดจำกัด — ไม่เห็นพฤติกรรม runtime, ทดสอบ web interface ไม่ได้, debug ยาก Emulation รัน firmware บนเครื่องเราผ่าน QEMU (จำลอง CPU architecture ของอุปกรณ์ เช่น MIPS/ARM) ทำให้: เข้า web interface ผ่าน browser, ทดสอบ exploit (command injection/overflow), debug ด้วย gdb, fuzzing — โดยไม่ต้องมี hardware

เนื้อหานี้เพื่อการศึกษาและฝึกในสภาพแวดล้อมที่ได้รับอนุญาต (CTF, lab, อุปกรณ์ของตัวเอง) เท่านั้น

2. รู้ architecture ก่อน

ระบุ CPU architecture ของ firmwareLinux
# จาก binary ใน rootfs
file squashfs-root/bin/busybox
# เช่น: ELF 32-bit MSB ... MIPS  หรือ  ARM, EABI5

# binwalk -A บอก opcode/arch
binwalk -A firmware.bin

# พบบ่อยใน IoT: MIPS (big/little endian), ARM, mipsel
# endianness สำคัญ: MSB=big (mips), LSB=little (mipsel/arm)
ต้องรู้ arch + endianness เพื่อเลือก qemu ที่ถูก (qemu-mips vs qemu-mipsel vs qemu-arm)

3. User-mode: รัน binary เดี่ยว (chroot + qemu-static)

ถ้าอยากรันแค่ binary เดียว (เช่น web server, service ที่สงสัย) ใช้ user-mode emulation — copy qemu-static เข้า rootfs แล้ว chroot เร็วและง่ายกว่ารัน system เต็ม

chroot + qemu-user-staticLinux
# ติดตั้ง qemu user-mode + binfmt
sudo apt install qemu-user-static

# copy qemu static เข้า rootfs (เลือกตาม arch)
cp $(which qemu-mipsel-static) squashfs-root/
# หรือ qemu-mips-static / qemu-arm-static

# chroot เข้าไป (binfmt จะเรียก qemu อัตโนมัติ)
sudo chroot squashfs-root ./qemu-mipsel-static /bin/sh
# ตอนนี้อยู่ใน firmware shell (รันบน emulation)

# รัน service เดี่ยว เช่น web server
sudo chroot squashfs-root ./qemu-mipsel-static /usr/sbin/httpd -p 8080
# แล้ว browser เข้า 127.0.0.1:8080

# debug ด้วย gdb
chroot squashfs-root ./qemu-mipsel-static -g 1234 /usr/sbin/httpd &
gdb-multiarch -ex "target remote :1234"
user-mode เหมาะรัน binary เดี่ยว/debug; qemu -g เปิด gdb stub; binary ที่ต้องการ kernel feature เฉพาะอาจไม่รัน → ใช้ system-mode

4. System-mode: รัน firmware เต็ม (FirmAE)

ถ้าต้องการรัน firmware ทั้งระบบ (kernel + service + network) ให้เหมือนอุปกรณ์จริง ใช้ FirmAE (ต่อยอดจาก FirmaDyne) ที่ทำ network setup, NVRAM emulation ให้อัตโนมัติ — รองรับอุปกรณ์จริงได้สูง

FirmAELinux
# ติดตั้ง FirmAE
git clone --recursive https://github.com/pr0v3rbs/FirmAE
cd FirmAE && ./download.sh && ./install.sh

# รัน firmware (โหมด run = เต็ม)
sudo ./run.sh -r <brand> firmware.bin
# FirmAE จะ extract, setup network, emulate
# แสดง IP ที่ assign (เช่น 192.168.0.1)

# เข้า web interface
# browser → http://192.168.0.1

# โหมดอื่น:
sudo ./run.sh -c <brand> firmware.bin   # check ว่า emulate ได้ไหม
sudo ./run.sh -a <brand> firmware.bin   # analyze (รัน + scan)
sudo ./run.sh -d <brand> firmware.bin   # debug mode
FirmAE จัด network/NVRAM ให้อัตโนมัติ; -r รันเต็ม → ได้ IP เข้า web ผ่าน browser; รองรับ router/IoT จริงเยอะ

5. ทดสอบ/exploit บน emulation

  • web interface: เข้าผ่าน browser → ทดสอบ command injection, auth bypass, CSRF (เหมือน web pentest ปกติ)
  • command injection ใน CGI: router หลายตัวส่ง parameter เข้า system() — ทดสอบ ;id, $(...) ใน field
  • buffer overflow: service ที่ฟัง port → fuzz/exploit ด้วย gdb (ดูหมวด Pwn)
  • debug ด้วย gdb-multiarch: attach service ดู crash/ค่า runtime
  • network service: nmap scan IP ที่ emulate → หา service เปิด → ทดสอบ
  • NVRAM/config: ทดสอบค่าที่อ่านจาก nvram (FirmAE จำลองให้)

6. Troubleshooting

  • binary ไม่รัน (user-mode): ต้องการ kernel module/ioctl เฉพาะ → ใช้ system-mode (FirmAE) แทน
  • nvram error: binary เรียก libnvram → FirmAE/FirmaDyne มี nvram stub; user-mode ต้อง LD_PRELOAD nvram-faker
  • network ไม่ขึ้น (FirmAE): ลอง brand อื่น/โหมด -d ดู log; บาง firmware ต้อง patch
  • missing library: rootfs ไม่ครบ → extract ใหม่ (binwalk -Me), เช็ค symlink
  • endianness/arch ผิด: เลือก qemu ผิดตัว → ตรวจ file binary อีกรอบ
  • crash ทันที: ลอง qemu version อื่น หรือ debug ด้วย -g + gdb

7. Quick Reference

  • รัน firmware บนเครื่องเรา (QEMU) — ไม่ต้องมี hardware
  • รู้ arch ก่อน: file bin/busybox (MIPS/ARM + endian)
  • user-mode (binary เดี่ยว): cp qemu-*-static + chroot
  • system-mode (เต็ม): FirmAE ./run.sh -r brand firmware.bin → ได้ IP
  • ทดสอบ: web interface (cmd injection), gdb-multiarch debug
  • ปัญหา nvram → FirmAE stub / nvram-faker; arch ผิด → เช็ค file

🧭 จับมือทำทีละขั้น (มีแค่ Kali) + ถ้าติดไปไหนต่อ

สมมติแกะ firmware ได้ root filesystem แล้ว (ดู Firmware Analysis) อยากรันจริงดูพฤติกรรม แต่มีแค่ Kali ไม่มี hardware — ทำตามนี้ทีละขั้น ไล่จาก emulation ที่ง่ายสุดไปยากสุด

  1. 1ตรวจ architecture ก่อนเสมอ: file squashfs-root/bin/busybox — ดูว่าเป็น MIPS/MIPSEL/ARM
  2. 2ติดตั้ง qemu user-mode ถ้ายังไม่มี: sudo apt install qemu-user-static
  3. 3ลอง user-mode ก่อน (เร็วและง่ายกว่า): copy qemu-static เข้า rootfs แล้ว sudo chroot squashfs-root ./qemu-mipsel-static /bin/sh
  4. 4ถ้าได้ shell ลองรัน service ที่สงสัย (เช่น web server): ./qemu-mipsel-static /usr/sbin/httpd -p 8080 แล้วเปิด browser ไปที่ 127.0.0.1:8080
  5. 5ถ้า user-mode error (nvram/kernel feature ที่ไม่รองรับ) ให้ข้ามไปใช้ FirmAE แทน
  6. 6ติดตั้ง FirmAE: git clone --recursive https://github.com/pr0v3rbs/FirmAE && cd FirmAE && ./download.sh && ./install.sh
  7. 7รันแบบเต็มระบบ: sudo ./run.sh -r <brand> firmware.bin — รอดู log ว่า assign IP อะไร
  8. 8เข้า web interface ผ่าน IP ที่ได้ (เช่น http://192.168.0.1) แล้วทดสอบเหมือน web pentest ปกติ (command injection, auth bypass)
  9. 9ถ้า network ไม่ขึ้นเลย ลอง ./run.sh -d (debug mode) ดู log ว่าติดตรงไหน หรือลองระบุ brand อื่น
มี rootfs แล้ว — เลือกวิธี emulate
ตรวจ architecture/endianness
file squashfs-root/bin/busybox
ลอง user-mode ก่อน (เร็ว, ง่าย)
cp qemu-*-static เข้า rootfs; chroot
✅ รันได้ (shell/service ขึ้น)→ ทดสอบ/debug ต่อ
❌ error (nvram/ioctl/kernel feature ขาด)→ ใช้ system-mode (FirmAE) แทน
รันด้วย FirmAE (system-mode เต็มระบบ)
./run.sh -r brand firmware.bin
✅ ได้ IP, network ขึ้น→ ทดสอบ/debug ต่อ
❌ network ไม่ขึ้น/crash→ ลอง brand อื่น/-d debug mode ดู log
ลอง brand อื่น + debug mode ดู log ละเอียด
./run.sh -d brand firmware.bin
✅ เจอสาเหตุ แก้ได้→ กลับไปรัน -r ใหม่
❌ emulate ไม่ได้จริงๆ→ ต้องพึ่งอุปกรณ์จริง/hardware แทน
ทดสอบ web interface + debug ด้วย gdb
✅ เจอ command injection/ช่องโหว่จบ — exploit ได้
❌ ต้องการ debug ลึกกว่า static→ attach gdb-multiarch ผ่าน qemu -g หรือ OpenOCD
ขั้นตอน/งานเครื่องมือใน Kaliติดตั้งเพิ่ม (ถ้าไม่มี)เครื่องมือออนไลน์
ตรวจ arch/endiannessfile, binwalk -A--
user-mode emulation-apt install qemu-user-static-
system-mode emulation เต็มระบบ-git clone pr0v3rbs/FirmAE-
debug ระดับ binarygdb-multiarch--
scan service ที่ emulate ขึ้นมาnmap--
ทดสอบ web interfacecurl, burpsuite--
ค้น CVE ของ service ที่เจอsearchsploit-cve.mitre.org, nvd.nist.gov
🚑 ถ้าตันสนิท ลองท่าถัดไป: Firmware Analysis — ถ้า emulate ยากเกินไป กลับไปขุด static (grep secret, reverse binary) ก่อน; UART & JTAG — ถ้า emulate ไม่ขึ้นเลยและต้องพึ่งอุปกรณ์จริง; SPI Flash Dump — ถ้าสงสัยว่า rootfs ที่ extract มาไม่ครบ/เสีย ต้อง dump ใหม่; MQTT & CoAP — ถ้า service ที่ emulate ขึ้นมาเปิดพอร์ต IoT protocol ให้ทดสอบต่อ

โน้ตของฉัน

ยังไม่มีโน้ตสำหรับหัวข้อนี้