-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump_traffic.py
executable file
·59 lines (46 loc) · 1.6 KB
/
dump_traffic.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
#!/usr/bin/python
"""
Utility application that reads in the hex dump traffic from the BMR application
and decodes it.
$ python dump_traffic.py config.json dump.txt
"""
import sys
import json
from bacpypes.debugging import xtob
from bacpypes.pdu import Address
from bacpypes.analysis import decode_packet
# humph
null_address = Address("0x000000000000")
# read in configuration
with open(sys.argv[1]) as bmr_config_file:
bmr_config = json.load(bmr_config_file)
inside_address = Address(bmr_config["inside"]["_deviceAddress"])
outside_address = Address(bmr_config["outside"]["_deviceAddress"])
# read in the file, split into lines
with open(sys.argv[2]) as infile:
lines = infile.readlines()
# strip off eol, split tabs to fields
lines = [line[:-1].split("\t") for line in lines]
# dump out the header and the decoded packet
for indx, line in enumerate(lines):
timestamp, stack, direction, source, destination, data = line
pkt = decode_packet(b"\0" * 14 + xtob(data))
if not pkt:
pkt_type = "unnable to decode"
else:
pkt_type = pkt.__class__.__name__
if pkt.pduSource == null_address:
if direction == ">>>":
pkt.pduSource = Address(source)
elif direction == "<<<":
if stack == "inside":
pkt.pduSource = inside_address
elif stack == "outside":
pkt.pduSource = outside_address
if pkt.pduDestination == null_address:
pkt.pduDestination = Address(destination)
print(f"{indx+1}. {stack} {direction} {pkt_type}")
if pkt:
print("")
pkt.debug_contents()
print("")