Skip to content

Commit ad35ef3

Browse files
committed
Refactoring
1 parent 2e29fac commit ad35ef3

File tree

4 files changed

+635
-180
lines changed

4 files changed

+635
-180
lines changed

analysis/analysis.py

+27-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import numpy as np
22
import pandas as pd
3+
import itertools
34
import dateutil
45
from openomni.packet import Packet
6+
from diff_message import DiffMessage
7+
58

69
# Takes an array of bits, like [0,1,0,0,1,0,0,0,0,1,0,0],
710
# And returns a hex string: '4840'
@@ -21,20 +24,27 @@ def parse_packet_file(filename):
2124
def packets_to_pandas(packets):
2225
return pd.DataFrame(packets).set_index('received_at')
2326

24-
def reassemble(df):
25-
# (re)assemble raw packet data
26-
ptype_values = {
27-
'PDM': 0b101,
28-
'POD': 0b111,
29-
'ACK': 0b010,
30-
'CON': 0b100,
31-
}
32-
33-
blen_hex = df["BLEN"].map(lambda x: chr(int(x)).encode('hex'))
34-
ptype_val = df["PTYPE"].map(lambda x: ptype_values[x] << 5)
35-
seq_val = df["SEQ"].map(lambda x: int(x))
36-
b5_hex = (ptype_val + seq_val).map(lambda x: chr(x).encode('hex'))
37-
crc_hex = df["CRC"].map(lambda x: x.rstrip())
38-
header_hex_data = df["ID1"] + b5_hex
39-
raw_hex_data = df["ID2"] + df["B9"] + blen_hex + df["MTYPE"] + df["BODY"]
40-
return header_hex_data + raw_hex_data + crc_hex
27+
# This expresses our theory about which bits are message hash, and which bits
28+
# are included in message hash calculation. Takes a full message and splits it
29+
# into (message,hash)
30+
def parts(data):
31+
# Everything except ID1, byte 4 (sequence & flags), packet crc, and 16bit chksum
32+
msg = data[5:-3]
33+
chksum = data[-3:-1]
34+
return (msg, chksum)
35+
36+
# Builds a dictionary that maps bitwise observed changes in messages to
37+
# bitwise changes in message hash
38+
def build_bitdiff_dictionary(packets):
39+
data = [parts(p.tx_data()) for p in packets]
40+
41+
cracked_bits_dict = {}
42+
for c in itertools.combinations(data, 2):
43+
d = DiffMessage(c)
44+
if d.diff_bits_count == 0:
45+
continue
46+
if d.diff_bits_key in cracked_bits_dict:
47+
cracked_bits_dict[d.diff_bits_key].update_observation(d)
48+
else:
49+
cracked_bits_dict[d.diff_bits_key] = d
50+
return cracked_bits_dict

0 commit comments

Comments
 (0)