1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
from mitmproxy.utils import strutils
from typing import List
import struct
class MQTTControlPacket:
# Packet types
(
CONNECT,
CONNACK,
PUBLISH,
PUBACK,
PUBREC,
PUBREL,
PUBCOMP,
SUBSCRIBE,
SUBACK,
UNSUBSCRIBE,
UNSUBACK,
PINGREQ,
PINGRESP,
DISCONNECT,
) = range(1, 15)
# http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Table_2.1_-
NAMES = [
"reserved",
"CONNECT",
"CONNACK",
"PUBLISH",
"PUBACK",
"PUBREC",
"PUBREL",
"PUBCOMP",
"SUBSCRIBE",
"SUBACK",
"UNSUBSCRIBE",
"UNSUBACK",
"PINGREQ",
"PINGRESP",
"DISCONNECT",
"reserved",
]
# http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Table_3.1_-
CONNECT_RETURN_CODES = [
"Connection Accepted",
"Connection Refused, unacceptable protocol version",
"Connection Refused, identifier rejected",
"Connection Refused, Server unavailable",
"Connection Refused, bad user name or password",
"Connection Refused, not authorized",
]
SUBACK_RETURN_CODES = {
0x00: "Success - Maximum QoS 0",
0x01: "Success - Maximum QoS 1",
0x02: "Success - Maximum QoS 2",
0x80: "Failure",
}
PACKETS_WITH_IDENTIFIER = [
PUBACK,
PUBREC,
PUBREL,
PUBCOMP,
SUBSCRIBE,
SUBACK,
UNSUBSCRIBE,
UNSUBACK,
]
def __init__(self, buf: bytes, packet_type: int, packet_flags: int, length: int, length_size=1):
self.buf = buf
self.packet_type = packet_type
self.packet_type_human = self.NAMES[self.packet_type]
self.packet_flags = packet_flags
self.remaining_length = length
self.remaining_length_size = length_size
self.packet_identifier = None
self.payload = {}
self.dup, self.qos, self.retain = self._parse_flags()
def parse(self):
# Variable header & Payload
# http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718024
# http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718026
if self.packet_type == self.CONNECT:
self._parse_connect_variable_headers()
self._parse_connect_payload()
elif self.packet_type == self.CONNACK:
self._parse_connack_variable_headers()
elif self.packet_type == self.PUBLISH:
self._parse_publish_variable_headers()
self._parse_publish_payload()
elif self.packet_type in (self.SUBSCRIBE, self.UNSUBSCRIBE):
flags = self.packet_flags
if flags != 0x2:
raise Exception(f'Packet is malformed: flags = {flags} != 0x2')
self._parse_packet_identifier()
self._parse_subscribe_payload(with_qos=(self.packet_type == self.SUBSCRIBE))
elif self.packet_type == self.SUBACK:
self._parse_packet_identifier()
self._parse_suback_payload()
elif self.packet_type == self.UNSUBACK:
self._parse_packet_identifier()
elif self.packet_type == self.PUBACK:
self._parse_packet_identifier()
elif self.packet_type == self.UNSUBSCRIBE:
pass
# else:
# self.payload = None
def pprint(self):
pid = f' {self.packet_identifier:04x}' if self.packet_identifier is not None else ''
s = f'[{self.NAMES[self.packet_type]}{pid}]'
if self.packet_type == self.CONNECT:
s += f"""
Protocol Level: {self.variable_headers['ProtocolLevel'][0]}
Client Id: {self.payload['ClientId']}
Will Topic: {self.payload.get('WillTopic')}
Will Message: {strutils.bytes_to_escaped_str(self.payload.get('WillMessage', b'None'))}
User Name: {self.payload.get('UserName')}
Password: {strutils.bytes_to_escaped_str(self.payload.get('Password', b'None'))}
"""
elif self.packet_type == self.CONNACK:
rc = self.connack_headers["ReturnCode"]
rc_desc = self.CONNECT_RETURN_CODES[rc] if rc < len(self.CONNECT_RETURN_CODES) else f'{rc:02x}'
s += f" SessionPresent: {self.connack_headers['SessionPresent']}. {rc_desc}"
elif self.packet_type in (self.SUBSCRIBE, self.UNSUBSCRIBE):
s += " sent topic filters: "
s += ", ".join([f"'{tf}'" for tf in self.topic_filters])
elif self.packet_type == self.SUBACK:
rc = self.payload['ReturnCode']
s += " "
s += self.SUBACK_RETURN_CODES[rc] if rc in self.SUBACK_RETURN_CODES else f'{rc:02xs}'
elif self.packet_type == self.PUBLISH:
topic_name = strutils.bytes_to_escaped_str(self.topic_name)
payload = strutils.bytes_to_escaped_str(self.payload)
s += f" '{payload}' to topic '{topic_name}'"
elif self.packet_type in (self.PINGREQ, self.PINGRESP, self.UNSUBACK, self.PUBACK, self.DISCONNECT):
# just print packet type with packet identifier (if any)
pass
else:
s = f"Packet type {self.NAMES[self.packet_type]} is not supported yet!"
return s
def _parse_length_prefixed_bytes(self, offset):
field_length_bytes = self.buf[offset: offset + 2]
field_length = struct.unpack("!H", field_length_bytes)[0]
offset += 2
field_content_bytes = self.buf[offset: offset + field_length]
return field_length + 2, field_content_bytes
def _parse_publish_variable_headers(self):
offset = len(self.buf) - self.remaining_length
field_length, field_content_bytes = self._parse_length_prefixed_bytes(offset)
self.topic_name = field_content_bytes
if self.qos in [0x01, 0x02]:
offset += field_length
self.packet_identifier = self.buf[offset: offset + 2][0]
def _parse_publish_payload(self):
fixed_header_length = len(self.buf) - self.remaining_length
variable_header_length = 2 + len(self.topic_name)
if self.qos in [0x01, 0x02]:
variable_header_length += 2
offset = fixed_header_length + variable_header_length
self.payload = self.buf[offset:]
def _parse_subscribe_payload(self, with_qos=True):
# skip packet identifier
offset = self.remaining_length_size
offset += 1 # fixed header
offset += 2 # packet identifier
self.topic_filters = []
while len(self.buf) - offset > 0:
field_length, topic_filter_bytes = self._parse_length_prefixed_bytes(offset)
offset += field_length
topic_filter = {
'topic': topic_filter_bytes.decode("utf-8")
}
if with_qos:
topic_filter['qos'] = self.buf[offset: offset + 1][0] & 0x3
offset += 1
self.topic_filters.append(topic_filter)
def _parse_suback_payload(self):
offset = len(self.buf) - self.remaining_length + 2
rc = self.buf[offset]
self.payload['ReturnCode'] = rc
# http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718030
def _parse_connect_variable_headers(self):
offset = len(self.buf) - self.remaining_length
self.variable_headers = {}
self.connect_flags = {}
self.variable_headers["ProtocolName"] = self.buf[offset: offset + 6]
self.variable_headers["ProtocolLevel"] = self.buf[offset + 6: offset + 7]
self.variable_headers["ConnectFlags"] = self.buf[offset + 7: offset + 8]
self.variable_headers["KeepAlive"] = self.buf[offset + 8: offset + 10]
# http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc385349229
self.connect_flags["CleanSession"] = bool(self.variable_headers["ConnectFlags"][0] & 0x02)
self.connect_flags["Will"] = bool(self.variable_headers["ConnectFlags"][0] & 0x04)
self.will_qos = (self.variable_headers["ConnectFlags"][0] >> 3) & 0x03
self.connect_flags["WillRetain"] = bool(self.variable_headers["ConnectFlags"][0] & 0x20)
self.connect_flags["Password"] = bool(self.variable_headers["ConnectFlags"][0] & 0x40)
self.connect_flags["UserName"] = bool(self.variable_headers["ConnectFlags"][0] & 0x80)
# http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718031
def _parse_connect_payload(self):
fields = []
offset = len(self.buf) - self.remaining_length + 10
while len(self.buf) - offset > 0:
field_length, field_content = self._parse_length_prefixed_bytes(offset)
fields.append(field_content)
offset += field_length
self.payload = {}
for f in fields:
# http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc385349242
if "ClientId" not in self.payload:
try:
self.payload["ClientId"] = f.decode("utf-8")
except:
self.payload["ClientId"] = str(f)
# http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc385349243
elif self.connect_flags["Will"] and "WillTopic" not in self.payload:
self.payload["WillTopic"] = f.decode("utf-8")
elif self.connect_flags["Will"] and "WillMessage" not in self.payload:
self.payload["WillMessage"] = f
elif self.connect_flags["UserName"] and "UserName" not in self.payload:
self.payload["UserName"] = f.decode("utf-8")
elif self.connect_flags["Password"] and "Password" not in self.payload:
self.payload["Password"] = f
else:
raise Exception("")
# http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718035
def _parse_connack_variable_headers(self):
self.connack_headers = {}
offset = len(self.buf) - self.remaining_length
self.connack_headers["SessionPresent"] = self.buf[offset: offset + 1][0] & 0x01 == 0x01
self.connack_headers["ReturnCode"] = self.buf[offset + 1: offset + 2][0]
# http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718022
def _parse_flags(self):
dup = None
qos = None
retain = None
if self.packet_type == self.PUBLISH:
dup = (self.buf[0] >> 3) & 0x01
qos = (self.buf[0] >> 1) & 0x03
retain = self.buf[0] & 0x01
return dup, qos, retain
# http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Table_2.5_-
def _parse_packet_identifier(self):
offset = 1 + self.remaining_length_size
self.packet_identifier = struct.unpack('!H', self.buf[offset: offset + 2])[0]
def _get_packet_type(buf: bytes) -> int:
return buf[0] >> 4
def _get_packet_flags(buf: bytes) -> int:
return buf[0] & 0xf
def _get_remaining_length(buf: bytes) -> tuple:
multiplier = 1
value = 0
i = 1
while True:
encoded_byte = buf[i-1]
value += (encoded_byte & 127) * multiplier
multiplier *= 128
if multiplier > 128 * 128 * 128:
raise Exception("Malformed Remaining Length")
if encoded_byte & 128 == 0:
break
i += 1
return i, value
def read_packets(buf: bytes) -> List[MQTTControlPacket]:
packets = []
while len(buf) > 0:
# Fixed header
# http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718020
packet_type = _get_packet_type(buf)
packet_flags = _get_packet_flags(buf)
length_size, length = _get_remaining_length(buf[1:])
packets.append(MQTTControlPacket(buf[:1+length_size+length], packet_type, packet_flags, length, length_size=length_size))
buf = buf[1+length_size+length:]
return packets
|