inspecto.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import os
  2. from binascii import unhexlify, hexlify
  3. import csv
  4. def load_samples(folder):
  5. msgs, crcs, rawhex = [], [], []
  6. for fn in os.listdir(folder):
  7. path = os.path.join(folder, fn)
  8. with open(path, 'r') as f:
  9. for line in f:
  10. if '=' not in line:
  11. continue
  12. try:
  13. h, c = line.strip().split('=')
  14. data = list(unhexlify(h))
  15. chk = int(c, 16)
  16. msgs.append(data)
  17. crcs.append(chk)
  18. rawhex.append(h.upper())
  19. except Exception:
  20. continue
  21. return msgs, crcs, rawhex
  22. def bit_diff(a, b):
  23. return ''.join(['↑' if (a ^ b) & (1 << i) else '.' for i in reversed(range(8))])
  24. def analyze(msgs, crcs, rawhex):
  25. base_msg, base_crc = msgs[0], crcs[0]
  26. base_hex = rawhex[0]
  27. rows = []
  28. for idx, (msg, crc, raw) in enumerate(zip(msgs[1:], crcs[1:], rawhex[1:]), start=1):
  29. maxlen = max(len(base_msg), len(msg))
  30. for i in range(maxlen):
  31. b0 = base_msg[i] if i < len(base_msg) else None
  32. b1 = msg[i] if i < len(msg) else None
  33. if b0 != b1:
  34. delta_crc = (crc - base_crc) & 0xFF
  35. xor_crc = crc ^ base_crc
  36. b0_str = f"{b0:02X}" if b0 is not None else "--"
  37. b1_str = f"{b1:02X}" if b1 is not None else "--"
  38. bitdiff = bit_diff(b0 or 0, b1 or 0)
  39. print(f"Sample {idx} | Byte {i} | {b0_str} → {b1_str} | ΔCRC={crc:02X} | BitDiff={bitdiff}")
  40. rows.append([
  41. idx,
  42. i,
  43. b0_str,
  44. b1_str,
  45. bitdiff,
  46. f"{base_crc:02X}",
  47. f"{crc:02X}",
  48. f"{delta_crc:02X}",
  49. f"{xor_crc:02X}",
  50. raw,
  51. f"{crc:02X}"
  52. ])
  53. return rows
  54. # 📂 Point this to your folder
  55. input_folder = "C:/Users/crt/Documents/bodeting/research/bad-bruteforcing/reveng-formatted"
  56. # 🧠 Do the work
  57. msgs, crcs, rawhex = load_samples(input_folder)
  58. rows = analyze(msgs, crcs, rawhex)
  59. # 📝 Save CSV
  60. out_path = os.path.join(os.getcwd(), "crc_analysis_output.csv")
  61. with open(out_path, 'w', newline='', encoding='utf-8') as f:
  62. writer = csv.writer(f)
  63. writer.writerow([
  64. "Sample", "Byte Index", "Base Byte", "Other Byte", "Bit Diff",
  65. "Base CRC", "Other CRC", "CRC_DIFF", "CRC_XOR",
  66. "HEX Input", "Original CRC"
  67. ])
  68. writer.writerows(rows)
  69. print(f"\n✅ Analysis saved to: {out_path}")
  70. os.system(f'start notepad "{out_path}"')