iot
MQTT & CoAP
MQTT และ CoAP เป็น application-layer protocol หลักของ IoT — MQTT ใช้ publish/subscribe ผ่าน broker, CoAP เป็น REST-like สำหรับอุปกรณ์ทรัพยากรน้อย ทั้งคู่มักตั้งค่าผิด (ไม่มี auth) ทำให้ดู/ส่งข้อมูลอุปกรณ์ได้ บทนี้ลงลึกการทำงาน, การ enumerate, การ subscribe/publish, ช่องโหว่ที่พบบ่อย (เนื้อหาเพื่อฝึกใน lab/CTF/ระบบที่ได้รับอนุญาต)
IntermediateAdvanced#mqtt#coap#iot#protocol#broker#pubsub#ctf
1. MQTT vs CoAP
| MQTT | CoAP | |
|---|---|---|
| รูปแบบ | publish/subscribe ผ่าน broker | request/response (REST-like) |
| transport | TCP (1883 / 8883 TLS) | UDP (5683 / 5684 DTLS) |
| โครงสร้าง | topic (เช่น home/livingroom/temp) | resource path (/sensors/temp) |
| ตัวกลาง | broker (ทุกอย่างผ่าน) | ตรงไป device (ไม่มี broker) |
| เหมาะกับ | telemetry, อุปกรณ์เยอะ | อุปกรณ์ทรัพยากรน้อยมาก |
MQTT: อุปกรณ์ publish ข้อความไป topic บน broker, ตัวอื่น subscribe topic นั้นเพื่อรับ — broker เป็นศูนย์กลาง ปัญหาความปลอดภัยหลักคือ broker ที่เปิด public ไม่มี auth → ใครก็ subscribe ดูข้อมูลทั้งหมด หรือ publish สั่งงานอุปกรณ์ได้
เนื้อหานี้เพื่อการศึกษาและฝึกในสภาพแวดล้อมที่ได้รับอนุญาต (CTF, lab, ระบบของตัวเอง) เท่านั้น
2. Enumerate (หา broker/service)
หา MQTT/CoAPLinux
# scan port
nmap -p 1883,8883 --script mqtt-subscribe <target> # MQTT (+ TLS 8883)
nmap -sU -p 5683 <target> # CoAP (UDP)
# Shodan: หา broker public (ดูหัวข้อ Shodan)
# port:1883 / "MQTT Connection Code"
# port:5683 coap
# nmap mqtt-subscribe script ดึง topic + message ที่ broker ส่งMQTT TCP 1883 (8883 TLS); CoAP UDP 5683; nmap mqtt-subscribe ดึง topic/message; Shodan หา broker public
3. MQTT — subscribe / publish
mosquitto clientLinux
# subscribe ทุก topic (# = wildcard ทั้งหมด) — ดูข้อมูลทั้งระบบ
mosquitto_sub -h <broker> -t '#' -v
# -v แสดง topic + payload; '#' = subscribe ทุก topic
# subscribe topic เฉพาะ
mosquitto_sub -h <broker> -t 'home/+/temperature' -v # + = wildcard 1 level
# ถ้ามี auth
mosquitto_sub -h <broker> -u user -P pass -t '#' -v
# publish (ส่งข้อความ → สั่งงานอุปกรณ์ได้ถ้าไม่มี auth)
mosquitto_pub -h <broker> -t 'home/livingroom/light' -m 'ON'
mosquitto_pub -h <broker> -t 'device/cmd' -m '{"action":"reboot"}'
# GUI: MQTT Explorer (เห็น topic tree + ข้อมูล real-time สวยงาม)sub -t '#' = ดูทุก topic (ข้อมูลทั้งระบบ); pub = ส่งคำสั่ง (สั่งอุปกรณ์ได้ถ้า broker ไม่มี auth/ACL); MQTT Explorer = GUI ดู topic tree
4. CoAP — discover / request
coap-client / aiocoapLinux
# discover resource ทั้งหมด (.well-known/core)
coap-client -m get coap://<target>/.well-known/core
# เห็น resource path ที่อุปกรณ์มี
# GET resource
coap-client -m get coap://<target>/sensors/temperature
# PUT/POST (เปลี่ยนค่า/สั่งงาน ถ้าไม่มี auth)
coap-client -m put coap://<target>/actuators/light -e "on"
coap-client -m post coap://<target>/config -e '{"x":1}'
# aiocoap (python) สำหรับ script
aiocoap-client coap://<target>/.well-known/core.well-known/core = discover resource (เหมือน sitemap); GET ดู, PUT/POST เปลี่ยน/สั่ง (ถ้าไม่มี DTLS/auth)
5. ช่องโหว่ที่พบบ่อย
- ไม่มี authentication: broker/device เปิด public ไม่ต้อง login → subscribe ดูทุกอย่าง / publish สั่งงาน (พบบ่อยสุด)
- ไม่มี ACL: มี auth แต่ user ใดก็ publish/subscribe ทุก topic ได้ (ไม่จำกัดสิทธิ์)
- ไม่มี encryption: ใช้ 1883/5683 (plaintext) → ดักดู credential/ข้อมูลได้ (ดู PCAP Analysis)
- ข้อมูลรั่ว: subscribe '#' เห็น sensitive data (location, สถานะบ้าน, credential ที่ส่งผ่าน topic)
- command injection ผ่าน payload: อุปกรณ์ที่ parse MQTT payload ไม่ปลอดภัย → inject คำสั่ง
- topic injection / spoofing: publish topic ปลอมหลอกอุปกรณ์/ระบบ
- DoS: ส่ง message จำนวนมาก / retained message ค้าง
เริ่มทุกครั้งด้วย
mosquitto_sub -t '#' -v (MQTT) หรือ .well-known/core (CoAP) — ถ้าได้ข้อมูลโดยไม่ต้อง auth = broker/device ตั้งค่าผิด (ช่องโหว่หลักของ IoT protocol)6. Quick Reference
- MQTT (TCP 1883/8883) pub/sub ผ่าน broker; CoAP (UDP 5683) REST-like
- หา: nmap -p 1883 --script mqtt-subscribe; nmap -sU -p 5683; Shodan
- MQTT: mosquitto_sub -t '#' -v (ดูทุก topic); mosquitto_pub (สั่งงาน)
- CoAP: coap-client -m get .../.well-known/core (discover); PUT สั่งงาน
- ช่องโหว่หลัก: ไม่มี auth/ACL/encryption → ดู+สั่งอุปกรณ์ได้
- GUI: MQTT Explorer; plaintext → ดักดูด้วย Wireshark
🧭 จับมือทำทีละขั้น (มีแค่ Kali) + ถ้าติดไปไหนต่อ
สมมติโจทย์ให้ IP ของ IoT hub/broker มา มีแค่เครื่อง Kali ยังไม่รู้ว่าเป็น MQTT หรือ CoAP — ทำตามนี้ทีละขั้น เริ่มจาก scan port แล้วไล่ตามผลที่เจอ
- 1scan port ที่เกี่ยวข้อง:
nmap -p 1883,8883 -sU -p 5683,5684 <target> - 2ถ้าเจอ 1883 เปิด: ลอง subscribe ทุก topic โดยไม่ auth ก่อน
mosquitto_sub -h <target> -t '#' -v - 3ถ้าไม่มีข้อมูลไหลมาเลยหรือถูก reject: ลอง default credential
mosquitto_sub -h target -u admin -P admin -t '#' -v - 4ถ้า subscribe เห็นข้อมูล: จด topic ที่ดูเหมือนสั่งงานได้ (เช่น
home/livingroom/light) แล้วลองmosquitto_pub -h target -t topic -m 'ON' - 5ถ้าเจอ 5683 (UDP) เปิด: discover resource ก่อน
coap-client -m get coap://<target>/.well-known/core - 6จากผลที่ได้ ลอง GET resource ที่น่าสนใจ แล้วลอง PUT/POST เปลี่ยนค่าดู
- 7ถ้าทั้งคู่ไม่มี auth = broker/device ตั้งค่าผิด (ช่องโหว่ทั่วไปของ IoT protocol)
- 8ถ้าต้อง auth และหา credential ไม่ได้จากตรงนี้ ต้องไปหาจาก firmware ของอุปกรณ์ที่เชื่อมต่อ broker นี้แทน
ได้ IP ของ IoT hub มา — เลือก protocol ที่เจอ
scan port 1883/8883 (TCP) + 5683/5684 (UDP)
nmap -p 1883,8883; nmap -sU -p 5683,5684
เจอ port อะไรเปิดอยู่
1883/8883 เปิด (MQTT)→→ ลอง subscribe แบบไม่ auth
5683/5684 เปิด (CoAP)→→ discover resource
ไม่เจอ port เลย→→ อาจไม่ใช่ MQTT/CoAP หรือถูก filter
ไม่เจอ MQTT/CoAP port — ลอง scan port อื่น/มองหา service เว็บของ hub
subscribe ทุก topic โดยไม่ auth
mosquitto_sub -h target -t '#' -v
✅ เห็นข้อมูล/topic ไหลมา→→ หา topic ที่สั่งงานได้ แล้ว publish ทดสอบ
❌ Connection Refused / ต้อง auth→→ ลอง default/weak credential
ลอง default/weak credential
mosquitto_sub -u admin -P admin
✅ login ผ่าน→→ กลับไป subscribe/publish
❌ ยังเข้าไม่ได้→→ ต้องหา credential จากที่อื่น
publish ทดสอบสั่งงานอุปกรณ์
mosquitto_pub -t topic -m 'ON'
✅ อุปกรณ์ตอบสนอง→จบ — สั่งงานอุปกรณ์ได้จริง
❌ ไม่มีอะไรเปลี่ยน→→ ต้องหา topic/payload ที่ถูกต้องจากที่อื่น
discover resource ด้วย .well-known/core
coap-client -m get coap://target/.well-known/core
✅ เห็น resource path→→ GET/PUT ทดสอบ resource
❌ ไม่ตอบ/timeout→→ อาจต้องใช้ DTLS (5684) หรือ block จริง
ลอง PUT/POST เปลี่ยนค่า resource
coap-client -m put coap://target/actuators/light -e on
✅ เปลี่ยนค่า/สั่งงานได้→จบ — แก้ config/สั่งงานอุปกรณ์ได้
❌ ต้อง auth/DTLS→→ ทางตัน เว้นแต่มี key
| ขั้นตอน/งาน | เครื่องมือใน Kali | ติดตั้งเพิ่ม (ถ้าไม่มี) | เครื่องมือออนไลน์ |
|---|---|---|---|
| scan หา broker/service | nmap | - | shodan.io (หา broker public เดียวกัน) |
| MQTT subscribe/publish | - | apt install mosquitto-clients | - |
| MQTT GUI ดู topic tree | - | MQTT Explorer (AppImage/ดาวน์โหลด) | - |
| CoAP request | - | apt install libcoap2-bin; pip install aiocoap | - |
| ดักดู traffic plaintext | wireshark, tcpdump | - | - |
| หา default credential ของ broker/vendor | - | - | cirt.net/passwords, default-password.info |
| แกะ/แปลง payload แปลกๆ (hex/base64) | - | - | cyberchef.org |
🚑 ถ้าตันสนิท ลองท่าถัดไป: Firmware Analysis — ถ้าต้องหา credential/topic ที่ถูกต้องจากเฟิร์มแวร์ของอุปกรณ์ที่เชื่อม broker; Firmware Emulation — รัน firmware ของ hub จริงเพื่อดู behavior/credential ที่ใช้; BLE Attacks — ถ้าอุปกรณ์จริงคุยกันผ่าน BLE แทนที่จะเป็น network protocol; RF & SDR — ถ้าอุปกรณ์บางตัวสื่อสารผ่านคลื่นวิทยุ ISM band ไม่ใช่ MQTT/CoAP
หัวข้อที่เชื่อมโยง
Firmware Analysisอยู่ใน workflowFirmware EmulationเทคนิคเดียวกันUART & JTAGเทคนิคเดียวกันSPI Flash DumpเทคนิคเดียวกันBLE AttacksเทคนิคเดียวกันRF & SDRเทคนิคเดียวกันWeb Methodologyเครื่องมือเดียวกันInformation Gatheringเครื่องมือเดียวกันNmap Playbookเครื่องมือเดียวกันWireshark PlaybookเทคนิคเดียวกันShodanหัวข้อใกล้เคียง
โน้ตของฉัน
ยังไม่มีโน้ตสำหรับหัวข้อนี้