BenKrueger

decode_spi_xor.py

Nov 13th, 2025
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.82 KB | Cybersecurity | 0 0
  1. #!/usr/bin/env python3
  2. import json
  3. import sys
  4. from itertools import cycle
  5.  
  6. def load_events(path):
  7.     events = []
  8.     with open(path, "r") as f:
  9.         for line in f:
  10.             line = line.strip()
  11.             if not line:
  12.                 continue
  13.             events.append(json.loads(line))
  14.     events.sort(key=lambda e: e["t"])
  15.     return events
  16.  
  17. def sample_mosi_at_sck(mosi_events, sck_events):
  18.     """
  19.    For each SCK event with marker 'sample', take the last MOSI value
  20.    at or before that timestamp.
  21.    """
  22.     bits = []
  23.     m_idx = 0
  24.     last_mosi_val = mosi_events[0]["v"]
  25.  
  26.     for s in sck_events:
  27.         if s.get("marker") != "sample":
  28.             continue
  29.         t_sck = s["t"]
  30.  
  31.         # advance MOSI index until we pass this SCK time
  32.         while m_idx + 1 < len(mosi_events) and mosi_events[m_idx + 1]["t"] <= t_sck:
  33.             m_idx += 1
  34.             last_mosi_val = mosi_events[m_idx]["v"]
  35.  
  36.         bits.append(last_mosi_val)
  37.  
  38.     return bits
  39.  
  40. def bits_to_bytes_msb_first(bits):
  41.     out = []
  42.     for i in range(0, len(bits), 8):
  43.         chunk = bits[i:i+8]
  44.         if len(chunk) < 8:
  45.             break
  46.         val = 0
  47.         for b in chunk:
  48.             val = (val << 1) | (b & 1)  # MSB-first
  49.         out.append(val)
  50.     return out
  51.  
  52. def xor_decrypt(data_bytes, key_str):
  53.     key_bytes = key_str.encode("ascii")
  54.     out = []
  55.     for b, k in zip(data_bytes, cycle(key_bytes)):
  56.         out.append(b ^ k)
  57.     return out
  58.  
  59. def bytes_to_ascii(data_bytes):
  60.     return "".join(chr(b) if 32 <= b < 127 else "." for b in data_bytes)
  61.  
  62. def main():
  63.     if len(sys.argv) != 3:
  64.         print(f"Usage: {sys.argv[0]} mosi.json sck.json", file=sys.stderr)
  65.         sys.exit(1)
  66.  
  67.     mosi_path = sys.argv[1]
  68.     sck_path = sys.argv[2]
  69.  
  70.     # 1. Load events
  71.     mosi_events = load_events(mosi_path)
  72.     sck_events = load_events(sck_path)
  73.  
  74.     if not mosi_events or not sck_events:
  75.         print("No events found in one of the files.", file=sys.stderr)
  76.         sys.exit(1)
  77.  
  78.     # 2. Build bitstream using SCK sample markers
  79.     bits = sample_mosi_at_sck(mosi_events, sck_events)
  80.     if not bits:
  81.         print("No bits decoded (check markers 'sample' / file format).", file=sys.stderr)
  82.         sys.exit(1)
  83.  
  84.     # 3. Convert bits to bytes (MSB-first)
  85.     spi_bytes = bits_to_bytes_msb_first(bits)
  86.  
  87.     # 4. Print raw SPI data
  88.     print("Raw SPI data:")
  89.     print("HEX  :", " ".join(f"{b:02x}" for b in spi_bytes))
  90.     print("ASCII:", bytes_to_ascii(spi_bytes))
  91.     print()
  92.  
  93.     # 5. Decrypt using XOR key 'icy'
  94.     xor_key = "icy"
  95.     decrypted = xor_decrypt(spi_bytes, xor_key)
  96.  
  97.     print(f"Decrypted SPI data using XOR key '{xor_key}':")
  98.     print("HEX  :", " ".join(f"{b:02x}" for b in decrypted))
  99.     print("ASCII:", bytes_to_ascii(decrypted))
  100.  
  101. if __name__ == "__main__":
  102.     main()
  103.  
Advertisement
Add Comment
Please, Sign In to add comment