Skip to content

Complete capture parsing, protocol experts and test suite - #1

Merged
rickmark merged 1 commit into
masterfrom
claude/project-thread-smn0tp
Oct 18, 2026
Merged

rickmark merged 1 commit into
masterfrom
claude/project-thread-smn0tp

Conversation

@rickmark

Copy link
Copy Markdown
Member

Requested by Rick · project thread

Before: most of what the README promised was stubbed or broken. USBMUX streams got the wrong buffer offset and the plist reassembler never advanced, so no plists were ever decoded. Classic .pcap was opened with the pcapng parser. An unknown pcapng link type sent the parser into an infinite loop. USBPcap captures never applied SET_CONFIGURATION, so their bulk traffic was never routed. Malformed captures crashed on asserts. The DFU and Recovery experts recorded nothing, and the test suite was a single empty test.

After: Darwin, USBPcap and Linux usbmon captures in pcapng or classic pcap are decoded end to end. Lockdown and service plists are decoded per stream, DFU images and recovery uploads are reassembled and can be extracted, and corrupt captures are reported instead of crashing. 34 tests cover all of this using synthetic captures.

How:

  • Input: the format is sniffed from the magic number. The parser handles multiple pcapng sections and interfaces and simple packet blocks, adds a classic pcap reader (either byte order, µs/ns), and adds usbmon (189/220). Every structure is bounds-checked and copied with readStruct (no unaligned reinterpret_cast). Bad packets become warnings, and the parser stops cleanly on corrupt framing. The #pragma pack(1) that leaked out of pcapng.h into every later header is now balanced.
  • Transfers: DeviceHandle pairs submit and complete events, taking OUT data from the submit and IN data from the completion. Direction comes from bmRequestType. Re-enumeration as a different device (DFU → Recovery → Normal) starts a new device session.
  • Device: configurations are keyed by bConfigurationValue, alternate settings and SET_INTERFACE are tracked (the old TODO: Alternates), BOS descriptors are kept (previously always dropped), and strings are decoded to UTF-8. Class and vendor requests are routed by recipient and interface. Output no longer mixes wcout and cout.
  • USBMUX / MuxStream: mux packets are reassembled across USB transfers, v1 and v2 headers are both handled, and the TCP data offset, FIN/RST and pre-capture connections are respected. Big-endian length-prefixed XML and binary plists are decoded in both directions. TLS and raw streams are detected, and services are named from lockdownd StartService replies.
  • DFU / Recovery: DNLOAD images are reassembled, the DFU state is tracked from GETSTATUS, and anomalies are flagged (oversized DNLOAD, ABORT after a partial download, unknown requests). iBoot commands and responses, 0x41-announced bulk uploads and console output are decoded.
  • CLI: new --verbose, --plists, --extract <dir> and --warnings options.
  • Build/CI: the code is now a demuxusb_lib static library. New DEMUXUSB_SANITIZE option for ASan/UBSan. The CI matrix covers Ubuntu, Ubuntu with sanitizers and macOS, on both push and pull_request. Tests include mutation fuzzing of every format. The README is updated to match the new API.

Verified locally: GCC Release, GCC Debug with ASan/UBSan, and Clang all build without warnings and pass 34/34 tests. I also ran the CLI and the README example against a generated capture.

The tests use synthetic captures because the repo has no real Apple captures. The Darwin completion layout follows what the previous code assumed, so it's worth running against a real macOS restore capture.


Generated by Claude Code

- Detect pcapng / classic pcap from file contents; parse multiple pcapng
  sections and interfaces, simple packet blocks, and add Linux usbmon
  (189/220) alongside Darwin and USBPcap.
- Treat captures as untrusted: bounds check every structure, replace
  asserts with warnings, stop cleanly on corrupt framing, and stop
  unknown link types from hanging the parser.
- Pair submissions and completions so OUT data comes from the submit
  and IN data from the completion; handle USBPcap control transfers
  without a data stage (SET_CONFIGURATION previously never applied).
- Track interface alternate settings and SET_INTERFACE, key
  configurations by value, keep BOS descriptors, decode strings to
  UTF-8, and start a new device session on re-enumeration.
- USBMUX: fix the payload offset passed to streams, reassemble mux
  packets across USB transfers, honour v1/v2 headers and TCP data
  offset, track FIN/RST and pre-capture connections.
- MuxStream: decode length-prefixed XML and binary plists in both
  directions, detect TLS and raw streams, name services from
  lockdownd StartService replies.
- DFU expert reassembles DNLOAD images, follows GETSTATUS state and
  flags anomalies; Recovery expert decodes iBoot commands, uploads and
  console output.
- CLI: --verbose, --plists, --extract, --warnings.
- Build a static library; add 34 tests over synthetic captures in every
  format including fuzzing, an ASan/UBSan option and CI on Linux,
  sanitizers and macOS. Fix the unbalanced #pragma pack in pcapng.h.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017MAjMcmK7j74hjnqWwUgCi
@rickmark rickmark self-assigned this Oct 18, 2026
@rickmark
rickmark marked this pull request as ready for review October 18, 2026 04:55
@rickmark
rickmark merged commit a75a855 into master Oct 18, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants