aboutsummaryrefslogtreecommitdiff
path: root/research/bad-bruteforcing/inspecto.py
blob: 37dc3d660e9fd1cc8b0f039310f025c4d7d20614 (plain)
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
import os
from binascii import unhexlify, hexlify
import csv

def load_samples(folder):
    msgs, crcs, rawhex = [], [], []
    for fn in os.listdir(folder):
        path = os.path.join(folder, fn)
        with open(path, 'r') as f:
            for line in f:
                if '=' not in line:
                    continue
                try:
                    h, c = line.strip().split('=')
                    data = list(unhexlify(h))
                    chk = int(c, 16)
                    msgs.append(data)
                    crcs.append(chk)
                    rawhex.append(h.upper())
                except Exception:
                    continue
    return msgs, crcs, rawhex

def bit_diff(a, b):
    return ''.join(['↑' if (a ^ b) & (1 << i) else '.' for i in reversed(range(8))])

def analyze(msgs, crcs, rawhex):
    base_msg, base_crc = msgs[0], crcs[0]
    base_hex = rawhex[0]
    rows = []
    for idx, (msg, crc, raw) in enumerate(zip(msgs[1:], crcs[1:], rawhex[1:]), start=1):
        maxlen = max(len(base_msg), len(msg))
        for i in range(maxlen):
            b0 = base_msg[i] if i < len(base_msg) else None
            b1 = msg[i] if i < len(msg) else None
            if b0 != b1:
                delta_crc = (crc - base_crc) & 0xFF
                xor_crc = crc ^ base_crc
                b0_str = f"{b0:02X}" if b0 is not None else "--"
                b1_str = f"{b1:02X}" if b1 is not None else "--"
                bitdiff = bit_diff(b0 or 0, b1 or 0)
                print(f"Sample {idx} | Byte {i} | {b0_str} → {b1_str} | ΔCRC={crc:02X} | BitDiff={bitdiff}")
                rows.append([
                    idx,
                    i,
                    b0_str,
                    b1_str,
                    bitdiff,
                    f"{base_crc:02X}",
                    f"{crc:02X}",
                    f"{delta_crc:02X}",
                    f"{xor_crc:02X}",
                    raw,
                    f"{crc:02X}"
                ])
    return rows

# 📂 Point this to your folder
input_folder = "C:/Users/crt/Documents/bodeting/research/bad-bruteforcing/reveng-formatted"

# 🧠 Do the work
msgs, crcs, rawhex = load_samples(input_folder)
rows = analyze(msgs, crcs, rawhex)

# 📝 Save CSV
out_path = os.path.join(os.getcwd(), "crc_analysis_output.csv")
with open(out_path, 'w', newline='', encoding='utf-8') as f:
    writer = csv.writer(f)
    writer.writerow([
        "Sample", "Byte Index", "Base Byte", "Other Byte", "Bit Diff",
        "Base CRC", "Other CRC", "CRC_DIFF", "CRC_XOR",
        "HEX Input", "Original CRC"
    ])
    writer.writerows(rows)

print(f"\n✅ Analysis saved to: {out_path}")
os.system(f'start notepad "{out_path}"')