usb-rea

v0.7.15
io.github.emirmrkaljevic/usb-rea Checked 3 hours ago Published 1 day ago

Reverse engineer USB gadgets: capture traffic, infer the protocol, generate a pyusb driver.

These tools were reported by the server itself during our smoke test (tools/list), not copied from the README.

capture_start

Start a USB traffic capture into `out` (usbmon on Linux, USBPcap on Windows). Side effects: launches a background capture process that keeps writing until capture_stop. Needs elevated privileges on Linux (usbmon; typically root or a usbmon group). Does not touch the device itself. Pair every start with capture_stop; do not start a second capture while one is running. Prefer this over analysis tools when you still need live traffic.

ParameterTypeDescription
busintegerusbmon bus number N for /dev/usbmonN; ignored on Windows/USBPcap. Default 0
outstringOutput pcapng path (overwritten if it already exists)

capture_stop

Stop the running capture started by capture_start. Side effects: terminates the background capture process. The pcapng at the path given to capture_start is left on disk for report/diff_actions/scaffold_driver. Returns 'no capture running' if nothing was started (not an error). Call this before analysing.

mark_action

Record that the human is about to click this action in the vendor app. Side effects: appends {label, start, end} to <capture>.actions.json (creates the file). Call while capture_start is running, immediately before each click; end times are closed when the next mark arrives. Use the same label stem with different values (brightness 0%, 50%, 100%) so inference can fit scales. For an already-finished Wireshark capture without markers, skip this and pass `labels` to report/diff_actions instead. A label starting with 'seen' ('seen rpm 1180', 'seen r…

ParameterTypeDescription
label *stringAction label, ideally with a value, e.g. 'brightness 50%' or 'color 255 0 0'; or an observation starting with 'seen', e.g. 'seen rpm 1180 temp 34'
capturestringCapture path whose sidecar markers file (<capture>.actions.json) is updated

diff_actions

Diff packets per labelled action and infer the protocol (JSON: byte roles + evidence frames). The JSON also has `background` (periodic polls set aside) and `responses`: device replies paired with the query they answer (command, background poll or GET_REPORT), with byte roles (const, echo of a query byte, value fitted to the labels, counter, checksum, or reading with its observed min/max) and query/reply frame numbers. A reading that 'seen' markers explain gets `name`, `scale` and `bias` (raw = value * scale + bias, `signed` for i8/i16) plus the…

ParameterTypeDescription
capture *stringPath to a usbmon or USBPcap pcap/pcapng file
deviceUSB device address to analyse; default = bus address with the most host→device payloads
labelsMarker-free mode: ';'-separated labels in click order, or a text file path (one label per line). Splits traffic at the largest pauses between host→device messages
markersMarkers JSON path; default <capture>.actions.json. Ignored when labels is set

report

Generate a Markdown evidence report: every claim links to frame numbers in the capture. Includes a 'Device responses' section when the device answers queries (status polls, command ACKs, GET_REPORT), saying which reply bytes could not be explained, a 'Handshake' section for messages sent once before the first action (session setup, tokens), and an 'Observations' section for 'seen' markers (named readings and unmatched observations). Read-only: returns Markdown text (does not write a file). Same inputs as diff_actions. Use after capture_stop (or…

ParameterTypeDescription
capture *stringPath to a usbmon or USBPcap pcap/pcapng file
deviceUSB device address to analyse; default = bus address with the most host→device payloads
labelsMarker-free mode: ';'-separated labels in click order, or a text file path (one label per line)
markersMarkers JSON path; default <capture>.actions.json. Ignored when labels is set

scaffold_driver

Generate a standalone Python/pyusb driver source for the inferred protocol. One set_<command>() per command; when the device replies, also query_<name>() methods that send the command/poll (or GET_REPORT), read the reply and return it decoded as a dict (readings named by 'seen' markers come back scaled and modes as words, e.g. {'rpm': 1180, 'temp': 34, 'mode': 'turbo', 'ok': True}). When the capture has a handshake, Device() replays it in open() before any command and checks the fixed reply bytes. A command the app only sent in one mode first ca…

ParameterTypeDescription
capture *stringPath to a usbmon or USBPcap pcap/pcapng file
deviceUSB device address to analyse; default = bus address with the most host→device payloads
labelsMarker-free mode: ';'-separated labels in click order, or a text file path (one label per line)
markersMarkers JSON path; default <capture>.actions.json. Ignored when labels is set

wireshark_dissector

Generate a Wireshark Lua dissector for the inferred protocol. Read-only: returns Lua source as text. Labels commands, decodes values/counters/lookups, and verifies checksums (usbrea.checksum_ok + expert warning). Load with `wireshark -X lua_script:<file>.lua capture.pcapng` or the same flag on tshark. Prefer scaffold_driver for a pyusb driver; prefer hid_descriptor to inspect HID report layouts.

ParameterTypeDescription
capture *stringPath to a usbmon or USBPcap pcap/pcapng file
deviceUSB device address to analyse; default = bus address with the most host→device payloads
labelsMarker-free mode: ';'-separated labels in click order, or a text file path (one label per line)
markersMarkers JSON path; default <capture>.actions.json. Ignored when labels is set
namestringWireshark protocol/filter prefix (fields become <name>.command, <name>.checksum_ok, …)

hid_descriptor

Decode a HID report descriptor (and optionally one report payload). Read-only. When `source` is a capture path, finds GET_DESCRIPTOR type 0x22 transfers (replug the device while capturing if none are present). When `source` is hex, parses that descriptor directly (e.g. from /sys/class/hidraw/*/device/report_descriptor). Use this to understand report IDs/sizes before or alongside report/scaffold_driver; it does not infer vendor protocol fields from labelled actions.

ParameterTypeDescription
source *stringCapture path (searches for GET_DESCRIPTOR 0x22) or raw descriptor hex / hidraw report_descriptor bytes as hex
decode_hexOptional report payload hex to decode with the descriptor
report_typestringReport type for decode_hex: Input, Output, or Feature

lookup_device

Before capturing: is this device's protocol already documented by an open-source project? Returns JSON per device: `vendor`/`product` names, `status` ('documented', 'unknown', or 'listed in OpenDeviceDB'), `projects` (liquidctl, rivalcfg, turing-smart-screen-python, linux-thermalright-lcd-control, usb-rea's own case studies, with links and notes), `limits` (e.g. a generic chip ID shared by unrelated products), `same_vendor` and with online=true `opendevicedb` (matching records and their projects, or `error` when the network is down). Read-only:…

ParameterTypeDescription
device *stringUSB VID:PID as hex, e.g. '1e71:170e', or a capture path (looks up every device whose descriptor it contains)
onlinebooleanAlso query OpenDeviceDB's read-only public API; default false (offline table and local usb.ids only)

replay_packet

Send one packet to real USB hardware (destructive / irreversible on the device). Side effects: opens the device via libusb/pyusb and writes the packet. Refuses unless user_confirmed=true after the human approved this exact vid/pid/endpoint/hex_data (and setup_hex if any). Needs OS permission to open the device. Prefer scaffold_driver to generate a driver first; never set user_confirmed from an agent guess.

ParameterTypeDescription
endpoint *integerEndpoint address (e.g. 0x01 OUT); for control transfers use 0 with setup_hex
hex_data *stringPayload bytes as hex (spaces optional), e.g. 'aa 01 ff 00 00 ab'
pid *integerUSB product id (integer)
vid *integerUSB vendor id (integer, e.g. 0x1234 or 4660)
setup_hexOptional 8-byte control setup packet as hex; when set, sends a control transfer instead of bulk/interrupt
user_confirmedbooleanMust be true only after a human approved this exact packet; otherwise the call is refused