-
Notifications
You must be signed in to change notification settings - Fork 0
/
trafficlogger.py
546 lines (473 loc) · 23.3 KB
/
trafficlogger.py
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
import re
import sys
import frida
import signal
from string import Template
from pcapwriter import pcapWriter
from espdecryptor import EspDecryptor
from tcphandler import TcpHandler
from dataarchiver import DataArchiver
unexplainedOnly = False
extractTCPPayloads = False
collectData = False # build an archive of observed protocol payloads (for interesting protocols only)
device = frida.get_usb_device()
terminus = device.attach("terminusd")
bluetoothd = device.attach("bluetoothd")
ikeFile = open("tl-ike.js","r")
ikeScript = terminus.create_script(ikeFile.read())
ikeFile.close()
bleFile = open("tl-bt.js", "r")
bleScript = bluetoothd.create_script(bleFile.read())
bleFile.close()
cookies = set()
spis = set()
aclRcvFragBuffer = ""
aclSndFragBuffer = ""
NRLPRcvFragBuffer = ""
NRLPSndFragBuffer = ""
CLinkSndFragBuffer = ""
CLinkRcvFragBuffer = ""
NRLPPacketTypes = [0x02, 0x03, 0x04, 0x05, 0x63, 0x64, 0x65, 0x68, 0x69] # excluding 0x00 to avoid false positive detections
# ATT opcodes
ATTopcodes = {
0x01: "ERROR_RSP",
0x02: "EXCHANGE_MTU_REQ",
0x03: "EXCHANGE_MTU_RSP",
0x04: "FIND_INFORMATION_REQ",
0x05: "FIND_INFORMATION_RSP",
0x06: "FIND_BY_TYPE_VALUE_REQ",
0x07: "FIND_BY_TYPE_VALUE_RSP",
0x08: "READ_BY_TYPE_REQ",
0x09: "READ_BY_TYPE_RSP",
0x0A: "READ_REQ",
0x0B: "READ_RSP",
0x0C: "READ_BLOB_REQ",
0x0D: "READ_BLOB_RSP",
0x0E: "READ_MULTIPLE_REQ",
0x0F: "READ_MULTIPLE_RSP",
0x10: "READ_BY_GROUP_TYPE_REQ",
0x11: "READ_BY_GROUP_TYPE_RSP",
0x12: "WRITE_REQ",
0x13: "WRITE_RSP",
0x52: "WRITE_CMD",
0x16: "PREPARE_WRITE_REQ",
0x17: "PREPARE_WRITE_RSP",
0x18: "EXECUTE_WRITE_REQ",
0x19: "EXECUTE_WRITE_RSP",
0x20: "READ_MULTIPLE_VARIABLE_REQ",
0x21: "READ_MULTIPLE_VARIABLE_RSP",
0x23: "MULTIPLE_HANDLE_VALUE_NTF",
0x1B: "HANDLE_VALUE_NTF",
0x1D: "HANDLE_VALUE_IND",
0x1E: "HANDLE_VALUE_CFM",
0xD2: "SIGNED_WRITE_CMD"
}
MagnetOpcodes = {
0x01: "services",
0x02: "servicesResp",
0x03: "createChannel",
0x04: "acceptChannel",
0x05: "serviceAdded",
0x06: "serviceRemoved",
0x07: "serviceRemovedCnf",
0x08: "error",
0x09: "version",
0x71: "timeData",
0x72: "timeData",
0x90: "deviceID?",
0x91: "CLdata?",
}
MagnetServiceIDLookup = dict()
MagnetChannelLookup = dict()
decryptor = EspDecryptor()
tcp = TcpHandler()
archiver = None
if collectData:
archiver = DataArchiver()
def signal_handler(signal, frame):
print('Exiting...')
if archiver != None:
archiver.close()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
NRLPtype3 = re.compile(r"NRLP.*t=0x03")
NRLPtype4 = re.compile(r"NRLP.*t=0x04")
if len(sys.argv) > 1 and sys.argv[1] == "--dump":
sys.stderr.write("Logging decrypted ESP packet payloads to " + sys.argv[2] + ".pcap\n")
decryptor.pcapwriter = pcapWriter(sys.argv[2] + ".pcap")
def on_message(message, data):
global aclRcvFragBuffer, aclSndFragBuffer
data = bytes.fromhex(message["payload"][3])
direction = message["payload"][0]
# ACL messages
if message["payload"][2] == 2:
# strip acl header from received packets (sent packets do not yet have a header)
if direction == "rcv":
frag = (data[1] & 0x30) == 0x10 # packet is continuing fragment of previous packet
payload = data[4:]
if frag:
payload = aclRcvFragBuffer + payload # reassemble fragmented packet
else:
payload = data
# reassemble fragmented packet (no solid header info to work with unfortunately)
if len(aclSndFragBuffer) > 0:
payload = aclSndFragBuffer + payload
aclSndFragBuffer = ""
# if first two bytes match length of the packet (minus 4 bytes for length and channel fields), we probably have a complete L2CAP packet
payloadLen = len(payload)
l2capLen = (payload[1] << 8) + payload[0]
if l2capLen == payloadLen - 4:
cid = phex(int.from_bytes(payload[2:4], byteorder="little"), 4)
payload = payload[4:] # strip l2cap headers
proto = Template("L2CAP(cid=${cid})").substitute(cid=cid)
if cid in MagnetChannelLookup:
assocService = MagnetServiceIDLookup[MagnetChannelLookup[cid]]
proto = Template("L2CAP(cid=${cid}, ${service})").substitute(cid=cid, service=assocService)
analyzePacket(direction, payload, [proto])
# packet looks like a plausible l2cap fragment
elif direction == "rcv" and l2capLen < 4096 and l2capLen > payloadLen - 4:
aclRcvFragBuffer = payload
elif direction == "snd" and l2capLen < 4096 and l2capLen > payloadLen - 4:
aclSndFragBuffer = payload
#else:
# print(direction, "unknw", payload)
def analyzePacket(direction, payload, protocolStack = []):
containedPackets = detectProtocol(direction, payload, protocolStack)
for packet in containedPackets:
(proto, data, continueAnalysis) = packet
if continueAnalysis:
analyzePacket(direction, data, protocolStack + [proto])
else:
processParsedPacket(direction, protocolStack + [proto], data)
def processParsedPacket(direction, protocolStack, payload):
if len(protocolStack) > 0 and (not unexplainedOnly or protocolStack[-1].startswith("unknw")) and (not protocolStack[-1].startswith("ESP")) and (not protocolStack[-1].startswith("ATT")):
print(direction, "->".join(protocolStack), payload.hex())
# archive unexplained payloads
if collectData and len(payload) > 2 and protocolStack[-1] == "unknw":
archiver.logMysterious(direction, "->".join(protocolStack[0:-1]).replace(",", "."), payload)
# further processing of decrypted ESP payloads
if extractTCPPayloads and len(protocolStack) > 0 and protocolStack[-1].startswith("ESP"):
spi = protocolStack[-1][4:12]
ctx = decryptor.spiToIKEContextLookup[spi] # breaking open the pretty encapsulation :/
sessionType = decryptor.cryptoData[ctx]["type"]
NRLPtype = protocolStack[-2][9:11] if protocolStack[-2].startswith("NRLP") else protocolStack[-3][9:11]
tcp.ingest(direction, payload, sessionType, NRLPtype)
def on_message_IKE(message, data):
msg = message["payload"]
msgType = msg[0]
if msgType == "cookies":
cookies.add(msg[1])
decryptor.registerCookies(msg[1], msg[2])
elif msgType == "nonce":
decryptor.registerNonce(msg[2], msg[3], msg[1])
elif msgType == "spi":
decryptor.registerSPI(msg[2], msg[3], msg[1], msg[4])
spis.add(msg[3])
elif msgType == "pubkey":
decryptor.registerDHPubKeyContext(msg[3], msg[1])
elif msgType == "dhkey":
decryptor.registerUnknownDHKey(msg[1], msg[2])
# returns a list of packets contained in the given payload, with annotated protocol information
# format: (detected protocol, effective payload without headers, flag: analyze payload?)
def detectProtocol(direction, payload, protocolStack):
global spis, NRLPRcvFragBuffer, NRLPSndFragBuffer, CLinkRcvFragBuffer, CLinkSndFragBuffer, NRLPPacketTypes
payloadLen = len(payload)
# check if reserved bits of common sequence number scheme are all zero
matchesCommonSeqAck = int.from_bytes(payload[0:2], byteorder="big") & 0b10000000111000000 == 0
# ATT / GATT
if len(protocolStack) == 1 and protocolStack[0] == "L2CAP(cid=0x0004)":
opcode = payload[0]
auth = (opcode & 0x80) > 0
command = (opcode & 0x40) > 0
opcode = opcode & 0x3f
cmdString = ", command" if command else ""
authString = ", auth" if auth else ""
if collectData:
archiver.logATT(direction, payload)
proto = Template("ATT(op=${op}${cmd}${auth})").substitute(op=ATTopcodes[opcode], cmd=cmdString, auth=authString) + logATT(ATTopcodes[opcode], payload[1:])
return [(proto, payload[1:], False)]
# Magnet version >8
elif payloadLen > 3 and int.from_bytes(payload[1:3], byteorder="little") == payloadLen - 3:
opcode = payload[0]
length = int.from_bytes(payload[1:3], byteorder="little")
if collectData:
archiver.logMagnet(direction, "MagnetL", payload)
proto = Template("MagnetL(op=${op}, len=${length})").substitute(op=phex(opcode), length=length) + " " + logMagnet(opcode, payload[3:])
return [(proto, payload[3:], False)]
# Magnet version <8, avoid false positives by limiting to L2CAP layer and opcode 0x09 (version info)
# after version checking we switch to MagnetL and all other MagnetS detections look like false positives
elif payloadLen > 2 and payload[1] == payloadLen - 2 and len(protocolStack) == 1 and payload[0] == 0x09:
opcode = payload[0]
length = payload[1]
if collectData:
archiver.logMagnet(direction, "MagnetS", payload)
proto = Template("MagnetS(op=${op}, len=${length})").substitute(op=phex(opcode), length=length) + " " + logMagnet(opcode, payload[4:])
return [(proto, payload[2:], False)]
# SEQACK, special ack or something?
elif payloadLen == 2 and payload[0] == 1 and len(protocolStack) == 1:
ack = payload[1]
return [("SEQACK(special, ?{})".format(ack), bytes(), False)]
# SEQACK
elif payloadLen > 2 and matchesCommonSeqAck and len(protocolStack) == 1 and ("CLink" in protocolStack[0] or "BT.TS" in protocolStack[0] or "terminusLink" in protocolStack[0]):
seq = payload[0] >> 1
ack = payload[1]
proto = "SEQACK(#{} ?{})".format(seq, ack)
return [(proto, payload[2:], True)]
# NRLP
elif payloadLen > 5 and int.from_bytes(payload[1:3], byteorder="big") == payloadLen - 3 - 2: # 3 byte header, 2 byte checksum (?)
typ = payload[0]
lng = int.from_bytes(payload[1:3], byteorder="big")
checksum = payload[-2:]
if collectData and not (typ in [0x04, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69]):
archiver.logNRLP(direction, payload)
proto = Template("NRLP(t=${typ})").substitute(typ=phex(typ))
data = payload[3:-2]
return [(proto, data, True)]
# ESP
elif matchesKnownESP(payload):
return handleESP(payload)
# IKEv2
elif matchesIKE(payload):
return handleIKE(payload)
# 6LowPAN compressed header
elif payloadLen > 4 and (payload[0] == 0x72 or payload[0] == 0x79) and payload[3] == 0x32: # very heuristic, idk what these numbers mean
extendedHeader = payload[1] == 0x66
header = payload[0:4] if not extendedHeader else payload[0:8]
data = payload[4:] if not extendedHeader else payload[8:]
if extendedHeader:
checkAssumption("UATP2 extended header always 0x000c000c", payload[4:8].hex() == "000c000c")
return [("6LowPAN("+header.hex()+")", data, True)]
# 6LowPAN compressed header
elif payloadLen > 5 and (payload[0] == 0x72 or payload[0] == 0x79) and payload[2] == 0x32 and payload[3] < 0x20:
# this is getting a bit messy, but we're losing packets from TCP streams by missing these packets
headerLength = payload[3]+4
header = payload[0:headerLength]
data = payload[headerLength:]
return [("6LowPAN("+header.hex()+")", data, True)]
# NRLink Prelude
elif payload[0:8].hex() == "5445524d494e5553":
terminusProtocolVersion = payload[8]
pairingState = payload[9]
length = int.from_bytes(payload[10:12], byteorder="big")
data = payload[12:-2]
proto = Template("NRLinkPrelude(v=${version}, pairing=${pair}, l=${len})").substitute(version=terminusProtocolVersion, pair=pairingState, len=length)
if collectData:
archiver.logPrelude(direction, payload)
return [(proto, data, False)]
# CLink and CLinkHP
elif payloadLen > 3 and "CLink" in protocolStack[0]:
if collectData:
archiver.logClink(direction, payload)
maybeLen = int.from_bytes(payload[2:4], byteorder="big")
fragBuffer = CLinkSndFragBuffer if direction == "snd" else CLinkRcvFragBuffer
# full CLink packet in a single frame
if maybeLen == payloadLen - 4:
opcode = phex(int.from_bytes(payload[0:2], byteorder="little"), 4)
length = int.from_bytes(payload[2:4], byteorder="big")
proto = Template("CLink(op=${sid},l=${len})").substitute(sid=opcode,len=length)
return [(proto, payload[4:], False)]
# CLink packet, first fragment (assume plausible length)
elif maybeLen > len(payload) - 4 and maybeLen < 4000 and len(fragBuffer) == 0:
opcode = phex(int.from_bytes(payload[0:2], byteorder="little"), 4)
length = int.from_bytes(payload[2:4], byteorder="big")
proto = Template("CLinkFF(op=${sid},l=${len})").substitute(sid=opcode,len=length)
if direction == "snd":
CLinkSndFragBuffer = payload
else:
CLinkRcvFragBuffer = payload
return [("CLinkFragment", bytes(), False)] # we'll handle the complete packet once we have it
# CLink, continuing fragment (for now, assume packets only ever split into two frames, not more)
elif len(fragBuffer) > 0:
fullPkg = ""
if direction == "snd":
fullPkg = CLinkSndFragBuffer + payload
CLinkSndFragBuffer = ""
else:
fullPkg = CLinkRcvFragBuffer + payload
CLinkRcvFragBuffer = ""
return detectProtocol(direction, fullPkg, protocolStack)
else:
return [("unknw", payload, False)]
# com.apple.BT.TS
elif payloadLen > 1 and len(protocolStack) == 2 and "BT.TS" in protocolStack[0]:
if collectData:
archiver.logBTTS(direction, payload)
typ = payload[0]
length = payload[1]
proto = Template("BT.TS(t=${tpe},l=${len})").substitute(tpe=hex(typ),len=length)
return [(proto, payload[1:], False)]
# IKE with unknown cookie
elif matchesUnknownIKE(payload, protocolStack):
return handleIKE(payload)
# ESP with unknown SPI
elif matchesHeuristicESP(payload):
return handleESP(payload)
# NRLP, fragmented (first fragment)
# Heuristic: reserved bits in sequence numbers not set, length looks 'reasonable', type is one of the known packet types
elif payloadLen > 5 and int.from_bytes(payload[1:3], byteorder="big") < 4096 and payload[0] in NRLPPacketTypes and "terminusLink" in protocolStack[0]:
effectivePayload = payload
lng = int.from_bytes(effectivePayload[1:3], byteorder="big")
containedPackets = []
# full first packet is in this frame, alongside others
while lng <= len(effectivePayload) - 3 and len(effectivePayload) > 5:
typ = effectivePayload[0]
proto = Template("NRLP(t=${typ})").substitute(typ=phex(typ))
if collectData and not (typ in [0x04, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69]):
archiver.logNRLP(direction, effectivePayload[0:lng+3])
data = effectivePayload[3:lng+3]
containedPackets.append((proto, data, True))
effectivePayload = effectivePayload[lng+3+2:]
if len(effectivePayload) < 3:
break
lng = int.from_bytes(effectivePayload[1:3], byteorder="big")
# one payload didn't fit into this frame completely
if len(effectivePayload) > 0:
if direction == "snd":
NRLPSndFragBuffer = effectivePayload
else:
NRLPRcvFragBuffer = effectivePayload
return containedPackets if len(containedPackets) > 0 else [("NRLPFragment", bytes(), False)]
# NRLP, fragmented (continuing fragment)
# Heuristic: packet is not super short, matching buffer has start fragment
elif payloadLen > 32 and (direction == "snd" and len(NRLPSndFragBuffer) > 0 or direction == "rcv" and len(NRLPRcvFragBuffer) > 0) and "terminusLink" in protocolStack[0]:
if direction == "snd":
effectivePayload = NRLPSndFragBuffer + payload[2:]
NRLPSndFragBuffer = ""
else:
effectivePayload = NRLPRcvFragBuffer + payload[2:]
NRLPRcvFragBuffer = ""
return detectProtocol(direction, effectivePayload, protocolStack)
return [("unknw", payload, False)]
# protocol detectors
def matchesKnownESP(data):
return len(data) > 32 and data[0:4].hex() in spis
def matchesHeuristicESP(data):
# fitting minimum packet length and low sequence number (we'll add the spi to our known contexts to continue detecting ESP sessions)
# ignoring highest byte of sequence number because the devices like to start at 0xc0... 0xa0... 0x80... etc
return len(data) > 32 and int.from_bytes(data[5:8], byteorder="big") < 30 and int.from_bytes(data[5:8], byteorder="big") >= 0
def matchesIKE(data):
# SA_INIT exchange type, protocol version 2.0, payload SA (matches first message exchange in IKEv2, before we have established cookies)
return len(data) > 28 and (data[0:8].hex() in cookies or data[16:19].hex() == "212022")
def matchesUnknownIKE(data, stack):
return len(data) > 28 and "NRLP(t=0x04" in stack[-1]
def handleIKE(payload):
seq = int.from_bytes(payload[20:24], byteorder="big")
proto = Template("IKEv2(${cookie}, #${seq})").substitute(cookie=payload[0:4].hex(), seq=seq)
data = payload[28:]
return [(proto, data, False)]
def handleESP(payload):
seq = int.from_bytes(payload[4:8], byteorder="big")
spi = payload[0:4].hex()
spis.add(spi)
plaintext = decryptor.decryptESP(payload)
if len(plaintext) == 0:
proto = Template("ESP(${spi}, #${seq}, crypto not ready)").substitute(spi=spi, seq=seq)
data = payload
else:
nextHeader = plaintext[-1]
padLength = plaintext[-2]
proto = Template("ESP(${spi}, #${seq}, ${nextHeader})").substitute(spi=spi, seq=seq, nextHeader=nextHeader)
checkAssumption("ESP next header always TCP (0x06)", nextHeader == 6)
data = plaintext[:-2-padLength]
return [(proto, data, False)]
def logATT(opcode, pdu):
if opcode == "HANDLE_VALUE_IND":
handle = int.from_bytes(pdu[0:2], byteorder="little")
return Template(" Handle ${handle} has value 0x${value}").substitute(handle=phex(handle, 4), value=pdu[2:].hex())
elif opcode == "WRITE_REQ":
handle = int.from_bytes(pdu[0:2], byteorder="little")
return Template(" Write value ${value} to handle 0x${handle}").substitute(handle=phex(handle, 4), value=pdu[2:].hex())
elif opcode == "READ_BY_GROUP_TYPE_REQ":
typ = int.from_bytes(pdu[4:6], byteorder="little")
startHandle = int.from_bytes(pdu[0:2], byteorder="little")
endHandle = int.from_bytes(pdu[2:4], byteorder="little")
return Template(" Read attributes of group type ${type} in handles ${start}-${end}").substitute(type=phex(typ, 4), start=phex(startHandle, 4), end=phex(endHandle, 4))
elif opcode == "FIND_INFORMATION_REQ":
startHandle = int.from_bytes(pdu[0:2], byteorder="little")
endHandle = int.from_bytes(pdu[2:4], byteorder="little")
return Template(" Get attribute types for handles ${start}-${end}").substitute(start=phex(startHandle, 4), end=phex(endHandle, 4))
elif opcode == "READ_BY_TYPE_REQ":
typ = int.from_bytes(pdu[4:6], byteorder="little")
startHandle = int.from_bytes(pdu[0:2], byteorder="little")
endHandle = int.from_bytes(pdu[2:4], byteorder="little")
return Template(" Read attributes of type ${type} in handles ${start}-${end}").substitute(type=phex(typ, 4), start=phex(startHandle, 4), end=phex(endHandle, 4))
else:
return ""
def logMagnet(opcode, pdu):
op = MagnetOpcodes[opcode]
if op == "version":
return Template("${operation}: version ${v} features 0x${features}").substitute(operation=op, v=pdu[0], features=pdu[1:5].hex())
elif op == "createChannel":
channel = phex(int.from_bytes(pdu[0:2], byteorder="little"), 4)
sid = phex(int.from_bytes(pdu[2:4], byteorder="little"), 4)
MagnetChannelLookup[channel] = sid
return Template("${operation}: serviceID ${id} channel ${chan}").substitute(operation=op, chan=channel, id=sid)
elif op == "acceptChannel":
channel = phex(int.from_bytes(pdu[3:5], byteorder="little"), 4)
sid = phex(int.from_bytes(pdu[1:3], byteorder="little"), 4)
MagnetChannelLookup[channel] = sid
return Template("${operation}: serviceID ${id} channel ${chan} flag ${f}").substitute(operation=op, f=phex(pdu[0]), id=sid, chan=channel)
elif op == "services":
numServices = pdu[0]
services = []
i = 1
j = 0
while j < numServices and i + 10 < len(pdu):
lng = pdu[i]
sid = phex(int.from_bytes(pdu[i+1:i+3], byteorder="little"), 4)
flag = pdu[i+3]
nameLng = int.from_bytes(pdu[i+4:i+5], byteorder="big")
name = pdu[i+5:i+5+nameLng].decode().strip("\x00")
trl = pdu[-1:] if nameLng + 4 < lng else "00" # trailing byte may be omitted for implicit zero
i += lng + 1
j += 1
MagnetServiceIDLookup[sid] = name
services.append(Template("Service ${s}: ${n}, flags: ${f}, trailer: 0x${t}").substitute(s=sid, f=phex(flag), n=name, t=trl.hex()))
return Template("${operation}: ${s}").substitute(operation=op, s="; ".join(services))
elif op == "servicesResp":
common = pdu[0]
services = []
for i in range(0, common-1, 1):
sid = phex(int.from_bytes(pdu[1+i*2:3+i*2], byteorder="little"), 4)
services.append(sid)
return Template("${operation}: ${common} shared services, ${services}").substitute(operation=op, common=common, services=", ".join(services))
elif op == "serviceAdded":
sid = phex(int.from_bytes(pdu[0:2], byteorder="little"), 4)
unk = pdu[2:3]
lng = int.from_bytes(pdu[3:4], byteorder="big")
nme = pdu[4 : 4 + lng].decode().strip("\x00")
trl = pdu[-1:]
MagnetServiceIDLookup[sid] = nme
return Template("${operation}: Added service ${s}: ${n}, flags: 0x${f} trailer: 0x${t}").substitute(operation=op, s=sid, n=nme, f=unk.hex(), t=trl.hex())
elif op == "serviceRemoved":
sid = phex(int.from_bytes(pdu[0:2], byteorder="little"), 4)
return Template("${operation}: Removed service ${s}").substitute(operation=op, s=sid)
elif op == "serviceRemovedCnf":
sid = phex(int.from_bytes(pdu[0:2], byteorder="little"), 4)
return Template("${operation}: Confirm service ${s} removal").substitute(operation=op, s=sid)
else:
return Template("${operation}").substitute(operation=op)
def checkAssumption(name, expression):
if not expression:
sys.stderr.write("ASSUMPTION VIOLATED: " + name + "\n")
def splitN(str1, n):
return [str1[start:start+n] for start in range(0, len(str1), n)]
# Internet Checksum, according to https://datatracker.ietf.org/doc/html/rfc1071
def ip_checksum(payload):
words = splitN(''.join(payload.split()),4)
if len(words[-1]) < 4:
words[-1] = words[-1] + "00"
csum = 0;
for word in words:
csum += int(word, base=16)
csum += (csum >> 16)
return csum & 0xFFFF ^ 0xFFFF
# padded int to hex string conversion
def phex(val, pad=2):
fstr = "#0" + str(pad+2) + "x"
return format(val, fstr)
ikeScript.on('message', on_message_IKE)
ikeScript.load()
bleScript.on('message', on_message)
bleScript.load()
sys.stdin.read()