
# uds_sim.py - ECU that authenticates with a simple challenge
import random, hashlib
class ECU:
def __init__(self):
self.secret = b'long_secret_key'
def generate_challenge(self):
self.challenge = random.randbytes(8)
return self.challenge
def verify_response(self, response):
expected = hashlib.sha256(self.secret + self.challenge).digest()[:8]
return response == expected
def unlock(self):
print("ECU unlocked! Critical functions accessible.")
# Attacker captures a valid challenge-response pair
ecu = ECU()
challenge = ecu.generate_challenge()
# Legitimate tool computes response (simplified)
response = hashlib.sha256(ecu.secret + challenge).digest()[:8]
ecu.verify_response(response) # first unlock
# Replay the same challenge-response
ecu.challenge = challenge
ecu.verify_response(response) # second unlock without new challenge - works
ecu.unlock()
An automotive ECU implements Unified Diagnostic Services (UDS) security access (service 0x27) but does not enforce one‑time challenge usage. An attacker can capture a valid challenge‑response pair and replay it to bypass authentication.
Run the simulation:
python uds_sim.py
The ECU unlocks twice with the same authentication pair.