usbrply
Convert a .pcap file (captured USB packets) to Python or C code that replays the captured USB commands.
Supported packet sources are:
- Linux Wireshark (via usbmon)
- Windows Wireshark (via USBPcap)
Supported output formats are:
- libusb Python (primary)
- (libusb C: fixme)
- (Linux Kernel C: fixme)
- JSON
Example applications:
- Rapidly reverse engineer and re-implement USB protocols
- Record a proprietary Windows programming sequence and replay on an embedded Linux device
- Snoop USB-serial packets
Questions? Please reach out on github or join #usbrply on Freenode IRC
Linux installation
# Do one of these
# Easier to setup, but slower
sudo pip install python-pcapng
# Much faster, but no longer maintained
sudo apt-get install -y python-libpcap
git clone https://github.com/JohnDMcMaster/usbrply.git
cd usbrply
sudo python setup.py install
Windows installation
There is probably an easier way to do this but this is what I got to work. Tested on Windows 7 x64
Setup python and pip
- Get the latest Python 3 release (https://www.python.org/downloads/)
- I used Python 3.7.8 (Windows x86-64 executable installer)
- Keep default setup options (in particular this will install pip)
Install
- Open a command prompt
- Default should be your home dir (ex: C:\Users\mcmaster)
- python -m venv usbrply
- usbrply/Scripts/activate.bat
- pip install usbrply
Test
- If not still in venv (prompt like "(usbrply)" ): usbrply/Scripts/activate.bat
- python usrply\Scripts\usbrply -h
- You should get a help message
- Download and place in your home dir: https://github.com/JohnDMcMaster/usbrply-test/raw/master/win1.pcapng
- python usrply\Scripts\usbrply win1.pcapng
- You should see python code that will reproduce the .pcap file commands
Sample workflows
Sample workflow for capturing Windows traffic and replaying traffic in Python:
- Install Wireshark. Make sure you install the USBPcap library
- Start Wireshark
- Connect USB device to computer
- Start catpure
- Start your application, do your thing, etc to generate packets
- Close application
- Stop capture
- Save capture. Save in pcap-ng format (either should work)
- Close Wireshark
- Run: "usbrply --device-hi -p my.pcapng >replay.py"
- Linux: run "python replay.py"
- Verify expected device behavior. Did an LED blink? Did you get expected data back?
Sample workflow for capturing Windows VM traffic from Linux host and replaying traffic in Python:
- Example: program a Xilinx dev board under Linux without knowing anything about the JTAG adapter USB protocol
- Linux: Install Wireshark
- Linux: Enable usbmon so Wireshark can capture USB (sudo modprobe usbmon, see http://wiki.wireshark.org/CaptureSetup/USB)
- Linux: Boot Windows VM (ie through VMWare)
- Linux: Start Wireshark. Make sure you have USB permissions (ie you may need to sudo)
- Connect USB device to computer
- Linux: use lsusb to determine which device bus is on. Try to choose a bus (port) with no other devices
- Linux: start catpure on bus from above
- Linux: attach USB device to Windows guest
- Windows: start your application, do your thing, etc to generate packets
- Linux: stop capture
- Linux: save capture. Save in pcap-ng format (either should work)
- Linux: run: "usbrply --device-hi -p my.pcapng >replay.py"
- Linux: detatch USB device from Windows guest
- Linux: run "python replay.py"
- Verify expected device behavior. Did an LED blink? Did you get expected data back?
You may need to filter out USB devices. There are two ways to do this:
- --device-hi: use the last device enumerated. This works well in most cases, including FX2 renumeration
- --device DEVICE: manually specify the USB device used. Get this from lsusb output or Wireshark view
Other useful switches:
- --rel-pkt: intended to easier allow diffing two outputs. Ex: what changed in trace for LED on vs LED off?
- --no-packet-numbers: alternative to above
- --fx2: decode common FX2 commands (ex: CPU reset)
- --range RANGE: only decode a specific packet range. Use along with Wireshark GUI or refine a previous decode
- see --help for more
Version history
v0.0.0
- Crusty C++ program
v0.0.1
- Crusty python program
v1.0.0
- Seperate parsing from printing
- Windows data source officially supported
v2.0.0
- JSON: packn moved to new "submit" and "complete" entries
- JSON now has raw urb structures (added to submit/complete)
- python3 support
- libpcapng support
v2.0.1
- Fix packaging issues
v2.1.0
- python2 support officially removed
- VID/PID filter fixed
- Windows pip install instructions
- Linux: basic interrupt support
- General interrupt cleanup / fixes
- Better logging for dropped packets
- --no-packet-numbers: line numbers line up vs --packet-numbers
JSON output
use -j switch to output a parsing intermediate representation that should resemble original USB requests along with associated metadata. This can be used in more advanced applications, such as if you need to decode a complicated protocol or convert USB output to higher level API calls. An example can be found here: https://github.com/ProgHQ/bpmicro/blob/master/scrape.py This example first aggregates USB packets into application specific packets, and then decodes these into API calls
USB serial decoder
usbrply-serial supported adapters:
- FT2232C: data rx/tx
TODO: write doc
Metadata
Release files for usbrply 2.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| usbrply-2.1.1.tar.gz | 32.0 kB | Without provenance | Details for usbrply-2.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| usbrply-2.1.1-py3-none-any.whl | Python 3 | none | any | Details for usbrply-2.1.1-py3-none-any.whl |
Total release size: 68.9 kB
Release files / usbrply-2.1.1.tar.gz
| Download URL | usbrply-2.1.1.tar.gz |
|---|---|
| Size | 32.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
afc525226dadf6133e92e97698ff94534a5d8289668cddb16fdafa23f828b248
|
|
BLAKE2b-256 checksum How to use checksums |
90a45d60466121728d4c5e4ce178cb3c9fa2bca524fe7023784f3598864badff
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/54.1.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.5
|
Release files / usbrply-2.1.1-py3-none-any.whl
| Download URL | usbrply-2.1.1-py3-none-any.whl |
|---|---|
| Size | 36.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1cb47cd25ec33e8c1c302367ee2cdd6118b905981d6eaa4f93a2a5d3068903e8
|
|
BLAKE2b-256 checksum How to use checksums |
39836e58fbbffcbc0c9b7143c73c66146e32bde5728fb25c60bd9ea3eb5c76f9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/54.1.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.5
|
Security for usbrply 2.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.