Firmware Analysis
Firmware Analysis คือการแกะและวิเคราะห์ firmware ของอุปกรณ์ IoT/embedded เพื่อหา filesystem, credential, key, ช่องโหว่ และ backdoor บทนี้ลงลึกตั้งแต่การได้ firmware, การดู entropy เพื่อรู้ว่า encrypt/compress ไหม, การ extract filesystem ทุกชนิด, จุดที่ secret มักซ่อน, และ workflow เป็นระบบ (เนื้อหาเพื่อฝึกใน lab/CTF/อุปกรณ์ที่ได้รับอนุญาตเท่านั้น)
1. firmware คืออะไร + ทำไมต้องแกะ
firmware คือ software ที่ฝังในอุปกรณ์ (router, IP camera, smart plug, PLC) มักประกอบด้วย bootloader + kernel + root filesystem รวมเป็นไฟล์ image เดียว การแกะ firmware เผยทุกอย่างที่อุปกรณ์รัน: binary ของ service, web interface, hardcoded credential, private key, API endpoint, backdoor เป็นจุดเริ่มของ IoT security — หาช่องโหว่ได้โดยไม่ต้องมีอุปกรณ์จริง (static) และเป็นฐานสำหรับ emulation/exploitation ต่อ
2. การได้ firmware
| แหล่ง | วิธี |
|---|---|
| เว็บผู้ผลิต | ดาวน์โหลด update file (.bin/.img/.trx/.chk) ตรงๆ |
| FTP/update server | ดักดู URL ที่อุปกรณ์ดึง update (mitm/pcap) |
| ดึงจาก flash chip | อ่าน SPI flash ตรง (ดู SPI Flash Dump) |
| ผ่าน UART/JTAG | dump จาก bootloader/memory (ดู UART/JTAG) |
| mobile app | บาง app มี firmware ฝัง / ดึงผ่าน API |
| OTA capture | ดัก over-the-air update |
3. Recon firmware (entropy + identify)
ก่อน extract ต้องรู้ว่า firmware encrypt/compress ไหม ดูจาก entropy: entropy สูงสม่ำเสมอ (~8.0 ทั้งไฟล์) = encrypt หรือ compress (แกะตรงๆ ไม่ได้ ต้องหา key/algo ก่อน); entropy ขึ้นลงเป็นช่วง = มีหลาย section ที่ extract ได้
# ดูชนิด + entropy graph
binwalk firmware.bin # list signature/partition ที่เจอ
binwalk -E firmware.bin # entropy graph (สูงเรียบ = encrypted/compressed)
binwalk -A firmware.bin # หา opcode (architecture ของ binary)
# ดูเบื้องต้น
file firmware.bin
hexdump -C firmware.bin | head # ดู magic bytes แรก
strings firmware.bin | grep -iE 'linux|busybox|version|http' | head
# entropy ด้วย ent (ละเอียด)
ent firmware.bin # ค่าใกล้ 8.0 = สุ่มสูง (encrypt?)4. Extract filesystem
# extract อัตโนมัติ (recursive — แกะซ้อนชั้น)
binwalk -e firmware.bin # extract
binwalk -Me firmware.bin # matryoshka (แกะลึกทุกชั้น)
# ผลอยู่ใน _firmware.bin.extracted/
# ถ้า binwalk แกะ rootfs ไม่ครบ → แกะ filesystem เอง
# SquashFS (พบบ่อยสุด)
unsquashfs filesystem.squashfs # ได้โฟลเดอร์ squashfs-root/
# ระบุ offset ถ้ารู้
dd if=firmware.bin bs=1 skip=OFFSET of=rootfs.squashfs
unsquashfs rootfs.squashfs
# JFFS2
jefferson rootfs.jffs2 -d output/
# UBIFS
ubireader_extract_files rootfs.ubi
# CramFS
cramfsck -x output/ rootfs.cramfs
# firmware-mod-kit (ครอบหลาย format + repack ได้)
extract-firmware.sh firmware.bin5. หา secret ใน rootfs (จุดสำคัญ)
หลังได้ root filesystem แล้ว ขุดหา credential/key/ช่องโหว่ — จุดที่ secret มักซ่อน:
| ที่อยู่ | มักเจออะไร |
|---|---|
| /etc/passwd, /etc/shadow | user + password hash → crack (john) |
| /etc/config/, /etc/*.conf | default credential, config |
| etc/ssl/, *.pem, *.key | private key, certificate |
| www/, web root | web interface (หาช่องโหว่/backdoor) |
| bin/, sbin/, usr/bin | service binary → reverse (Ghidra) |
| etc/init.d/, rcS | startup script (เห็น service ที่รัน) |
| hardcoded ใน binary | API key, backdoor credential, debug |
cd squashfs-root/
# credential
cat etc/passwd etc/shadow 2>/dev/null
# crack hash ที่เจอ
unshadow etc/passwd etc/shadow > h.txt && john h.txt
# private key / cert
find . -name "*.pem" -o -name "*.key" -o -name "*.crt" 2>/dev/null
find . -name "*_rsa" -o -name "id_*" 2>/dev/null
# ค้น secret ทั่ว rootfs
grep -rniE 'password|passwd|api[_-]?key|secret|token|admin' . 2>/dev/null | grep -v Binary | head -40
# หา hardcoded ใน binary (เช่น telnetd backdoor, debug cmd)
strings bin/* sbin/* usr/bin/* 2>/dev/null | grep -iE 'password|backdoor|debug|=/bin/sh'
# หา default cred ใน web/config
grep -rniE 'admin|root' www/ etc/ 2>/dev/null | grep -iE 'pass|pwd' | head6. ช่องโหว่ที่พบบ่อยใน firmware
- hardcoded credential: default admin/root password ใน shadow หรือ binary
- backdoor: telnetd ที่เปิดด้วย undocumented command, magic packet, hardcoded login
- old/vulnerable binary: busybox/dropbear/openssl เวอร์ชันเก่ามี CVE → searchsploit
- command injection: web interface ที่ส่ง input เข้า system() (พบบ่อยใน router CGI)
- private key รั่ว: SSH/TLS key ฝังใน firmware (ใช้กับทุกอุปกรณ์รุ่นเดียวกัน)
- insecure update: firmware ไม่ sign → flash firmware ปลอมได้
- debug interface: service debug ที่เปิดทิ้งไว้
strings bin/busybox | grep BusyBox, dropbear, openssl, lighttpd) แล้ว searchsploit — firmware เก่ามักใช้ library ที่มี CVE สำเร็จรูป7. Workflow + Quick Reference
- 1ได้ firmware (เว็บผู้ผลิต / dump chip / UART)
- 2recon: binwalk -E (entropy → encrypt?), binwalk -A (arch)
- 3extract: binwalk -Me → ถ้าไม่ครบ unsquashfs/jefferson ตามชนิด
- 4ขุด rootfs: passwd/shadow (crack), key, grep secret
- 5reverse service binary (Ghidra) หา backdoor/command injection
- 6ตรวจเวอร์ชัน library → searchsploit หา CVE
- 7ต่อยอด: emulate รัน firmware (ดู Firmware Emulation)
- firmware = bootloader + kernel + rootfs (secret อยู่ rootfs)
- recon: binwalk -E (entropy), -A (arch); สูงเรียบ=encrypt
- extract: binwalk -Me; unsquashfs (SquashFS), jefferson (JFFS2)
- ขุด: passwd/shadow→john, *.key, grep -rni password/api_key
- ช่องโหว่: hardcoded cred, backdoor, old binary (CVE), cmd injection
- ต่อ: Firmware Emulation, UART/JTAG, SPI Flash Dump
🧭 จับมือทำทีละขั้น (มีแค่ Kali) + ถ้าติดไปไหนต่อ
สมมติโจทย์แจกไฟล์ firmware.bin มาให้ ไม่รู้จะเริ่มตรงไหน มีแค่เครื่อง Kali เปล่าๆ — ทำตามนี้ทีละขั้น ไล่จากดูชนิดไฟล์ไปจนขุดเจอ credential/ช่องโหว่
- 1ดูชนิดไฟล์เบื้องต้น:
file firmware.binแล้วbinwalk firmware.bin— ดูว่ามี signature อะไรที่รู้จักบ้าง - 2เช็ค entropy ก่อน extract:
binwalk -E firmware.bin— เส้นกราฟขึ้นลงเป็นช่วง = extract ได้; เรียบสูงตลอด = encrypt/compress - 3extract แบบ recursive:
binwalk -Me firmware.bin— ผลจะอยู่ใน_firmware.bin.extracted/ - 4เข้าไปหาโฟลเดอร์ที่หน้าตาเป็น root filesystem (มัก
squashfs-root/) - 5ขุด credential ก่อน:
cat etc/passwd etc/shadowแล้วunshadow etc/passwd etc/shadow > h.txt && john h.txt - 6ค้น secret ทั่ว rootfs:
grep -rniE 'password|api[_-]?key|secret|token' . | grep -v Binary - 7หา private key/cert:
find . -name '*.pem' -o -name '*.key' -o -name '*_rsa' - 8ถ้ายังไม่เจออะไร ตรวจเวอร์ชัน binary หลัก (busybox/dropbear/openssl) แล้ว
searchsploitหา CVE สำเร็จรูป - 9ถ้า static analysis ไม่พอ (ต้องดู runtime จริง) ไปต่อที่ Firmware Emulation เพื่อรันจริงด้วย QEMU
| ขั้นตอน/งาน | เครื่องมือใน Kali | ติดตั้งเพิ่ม (ถ้าไม่มี) | เครื่องมือออนไลน์ |
|---|---|---|---|
| ดูชนิด/entropy | binwalk, file | - | - |
| extract filesystem | binwalk, unsquashfs | pip install jefferson; git clone ubi_reader; firmware-mod-kit | - |
| crack passwd/shadow hash | john | - | crackstation.net, hashes.com |
| ค้น secret ทั่ว rootfs | grep, strings | - | - |
| reverse binary หลัก | strings, Ghidra | - | dogbolt.org (decompiler เทียบหลายตัว) |
| ค้น CVE ของ library | searchsploit | - | cve.mitre.org, nvd.nist.gov |
| แกะ/แปลง hex ของ config แปลกๆ | - | - | cyberchef.org |
หัวข้อที่เชื่อมโยง
โน้ตของฉัน
ยังไม่มีโน้ตสำหรับหัวข้อนี้