> For the complete documentation index, see [llms.txt](https://hacktricks.xsx.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacktricks.xsx.tw/network-services-pentesting/1883-pentesting-mqtt-mosquitto.md).

# 1883 - Pentesting MQTT (Mosquitto)

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS Red Team Expert）</strong></a><strong>！</strong></summary>

支持HackTricks的其他方式：

* 如果您想看到您的**公司在HackTricks中做广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 发现[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[NFTs](https://opensea.io/collection/the-peass-family)收藏品
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或 **关注**我们的**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>

### [WhiteIntel](https://whiteintel.io)

<figure><img src="/files/YEhfJ0lgKxxdqFKA17yB" alt=""><figcaption></figcaption></figure>

[**WhiteIntel**](https://whiteintel.io) 是一个由**暗网**支持的搜索引擎，提供**免费**功能，用于检查公司或其客户是否受到**窃取恶意软件**的**侵害**。

WhiteIntel的主要目标是打击由信息窃取恶意软件导致的账户劫持和勒索软件攻击。

您可以访问他们的网站并免费尝试他们的引擎：

{% embed url="<https://whiteintel.io>" %}

***

## 基本信息

**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"
}
```

![](/files/7cQLQvrGLmTDSy4bCHwQ)

### [**暴力破解 MQTT**](/generic-methodologies-and-resources/brute-force.md#mqtt)

## MQTT 渗透测试

**身份验证完全是可选的**，即使正在执行身份验证，**默认情况下不使用加密**（凭据以明文形式发送）。仍然可以执行中间人攻击来窃取密码。

要连接到 MQTT 服务，可以使用：<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/#"
```

你也可以使用[**https://github.com/akamai-threat-research/mqtt-pwn**](https://github.com/akamai-threat-research/mqtt-pwn)

```bash
apt-get install mosquitto mosquitto-clients
mosquitto_sub -t 'test/topic' -v #Subscribe to 'test/topic'
mosquitto_sub -h <host-ip> -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()
```

## 更多信息

从这里获取：<https://morphuslabs.com/hacking-the-iot-with-mqtt-8edaf0d07b9b>

### 发布/订阅模式 <a href="#b667" id="b667"></a>

发布/订阅模型由以下组成：

* **发布者**：向代理发布消息到一个（或多个）主题。
* **订阅者**：订阅一个（或多个）主题在代理中，并接收来自发布者发送的所有消息。
* **代理**：将所有来自发布者的消息路由到订阅者。
* **主题**：由一个或多个级别组成，级别之间用斜杠分隔（例如，/smartshouse/livingroom/temperature）。

### 数据包格式 <a href="#f15a" id="f15a"></a>

每个 MQTT 数据包包含一个固定头部（图 02）。图 02：固定头部

![https://miro.medium.com/max/838/1\*k6RkAHEk0576geQGUcKSTA.png](https://miro.medium.com/max/838/1*k6RkAHEk0576geQGUcKSTA.png)

### 数据包类型

* 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`

### [WhiteIntel](https://whiteintel.io)

<figure><img src="/files/YEhfJ0lgKxxdqFKA17yB" alt=""><figcaption></figcaption></figure>

[**WhiteIntel**](https://whiteintel.io) 是一个由**暗网**推动的搜索引擎，提供**免费**功能，用于检查公司或其客户是否受到**窃取恶意软件**的**威胁**。

WhiteIntel 的主要目标是打击由窃取信息的恶意软件导致的账户劫持和勒索软件攻击。

您可以访问他们的网站并免费尝试他们的引擎：

{% embed url="<https://whiteintel.io>" %}

<details>

<summary><strong>从零开始学习 AWS 黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS 红队专家）</strong></a><strong>！</strong></summary>

支持 HackTricks 的其他方式：

* 如果您想在 HackTricks 中看到您的**公司广告**或**下载 PDF 版本的 HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)！
* 获取[**官方 PEASS & HackTricks 商品**](https://peass.creator-spring.com)
* 探索[**PEASS 家族**](https://opensea.io/collection/the-peass-family)，我们的独家 [**NFTs**](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**Discord 群组**](https://discord.gg/hRep4RUj7f) 或 [**电报群组**](https://t.me/peass) 或在 **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)\*\* 上关注我们。\*\*
* 通过向 [**HackTricks**](https://github.com/carlospolop/hacktricks) 和 [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github 仓库提交 PR 来分享您的黑客技巧。

</details>
