What USB HID Traffic Looks Like
HID (Human Interface Device) is the protocol for input devices like keyboards and mice. In USB captures, HID traffic has two distinctive characteristics:
1. Interrupt transfers, reported periodically. Every time you move (a key press, a mouse movement), the device reports a "report" via an interrupt transfer. No activity, no traffic — human interaction has an irregular rhythm, and that is the key to detecting injection.
2. Fixed report structure. A standard keyboard report is 8 bytes and a mouse report is 4 bytes, with fixed field positions. That means once you capture the traffic, you can reconstruct every action byte by byte.
PcapDecoder supports capture files with the USB Monitor (usbmon) link type — the standard output of Linux usbmon or of Wireshark capturing USB on Linux.
Reconstructing Keyboard Traffic
Standard keyboard report (8 bytes):
Byte 0: modifier key bitmap (Ctrl/Shift/Alt/GUI, one bit per key)
Byte 1: reserved
Bytes 2-7: keycodes currently pressed (up to 6 simultaneous keys)
Keycodes follow the USB HID Usage Tables (e.g., a = 0x04, Enter = 0x28). How to reconstruct:
- Take the 8-byte report from each interrupt transfer in order;
- Decode the modifier keys from byte 0, and look up bytes 2-7 in the keycode table;
- A complete keystroke only counts when the same keycode appears as a consecutive press-release pair (the keycode appears on press and returns to 0 in that position on release).
For example: the report sequence 00 00 0B 00... (h pressed) → 00 00 00 00... (released) → 00 00 16 00... (s pressed) → released means "h" and then "s" were typed.
The mouse report (4 bytes) is even simpler: byte 0 is the button bitmap, bytes 1-2 are the relative X/Y movement (signed). Both clicks and movement paths can be reconstructed.
How-to: Analyzing with PcapDecoder
Step 1: Open the USB capture. PcapDecoder automatically recognizes the USB Monitor link type, and the USB analysis module provides a device list (each device's vendor ID, product ID, and serial number) plus transfer statistics (interrupt transfer count, bulk transfer count).
Step 2: Locate the HID device. Find the class 3 (HID) device in the device list — keyboards and mice are all here. Click in to see its sequence of interrupt transfers.
Step 3: Reconstruct the input sequence. Examine the report contents transfer by transfer; PcapDecoder's detail view expands the 8-byte report by field (modifier keys + keycodes), so you don't have to decode the hex yourself.
Step 4: Examine the timing for anomalies. Lay the input events out along the timeline — normal human input intervals are a random distribution on the order of hundreds of milliseconds; dense input at fixed intervals is machine behavior.
Detecting Keystroke Injection
The attack shape of BadUSB / keystroke injection: a malicious USB device masquerades as a keyboard and automatically types commands at millisecond speed (opening PowerShell, downloading and executing a payload). Detection signals:
Signal 1: Machine-like input rhythm. Human typing intervals are 100-500ms and uneven; an injection program's keystroke intervals may be exactly 10ms or 20ms — the uniformity of the intervals is itself smoking-gun evidence.
Signal 2: Superhuman speed. Dozens of valid keystrokes within a single second are physically impossible.
Signal 3: Patterned input content. Dense shortcut combinations (Win+R, Ctrl+Shift+Esc) and command strings (powershell, http://, .exe) appearing as keystrokes — the content itself is the command.
Signal 4: New device + immediate input. The device has barely finished enumerating (its first interrupt transfer appears) and immediately begins high-speed input — no human is that fast.
Response: detecting keystroke injection means the host has been physically accessed or a malicious USB drive has been inserted. Unplug the device, isolate the host, and review the commands executed during that window (process creation logs, PowerShell history), following your incident response process.
Prevention: USB port control (blocking unregistered devices), group policy restrictions on autorun, and read-only USB ports in critical areas. Technology can't stop someone from plugging in a USB drive, but packet capture can record every keystroke.
Related reading: WPA3 Handshake Analysis: SAE and Dragonfly Explained.
