1
0
Fork 0
mirror of https://github.com/snovvcrash/usbrip.git synced 2024-06-07 22:46:03 +02:00
Simple CLI forensics tool for tracking USB device artifacts (history of USB events) on GNU/Linux
Go to file
Sam Freeside 62a4a9a7ec
Update README.md
2018-10-22 22:12:55 +03:00
lib Edit email 2018-10-16 23:18:53 +03:00
usb_ids Update usb.ids 2018-05-11 21:43:19 +03:00
.gitignore Initial commit 2018-03-21 21:28:12 +03:00
LICENSE Initial commit 2018-03-21 21:28:12 +03:00
README.md Update README.md 2018-10-22 22:12:55 +03:00
install.sh Edit email 2018-10-16 23:18:53 +03:00
requirements.txt Remove bs4 dependency 2018-04-25 23:45:47 +03:00
uninstall.sh Edit email 2018-10-16 23:18:53 +03:00
usbrip.cron Minor edits 2018-06-04 01:39:03 +03:00
usbrip.py Edit email 2018-10-16 23:18:53 +03:00

usbrip

Python Version License Built with Love

Logo

usbrip (derived from "USB Ripper", not "USB R.I.P." ๐Ÿ˜ฒ) is an open source forensics tool with CLI interface that lets you keep track of USB device artifacts (a.k.a. USB event history: "Connected" and "Disconnected" events) on Linux machines.

Table of Contents:

Description

usbrip is a small piece of software written in pure Python 3 (using some external modules though, see Dependencies/PIP) which parses Linux log files (/var/log/syslog* or /var/log/messages* depending on the distro) for constructing USB event history tables. Such tables may contain the following columns: "Connected" (date & time), "User", "VID" (vendor ID), "PID" (product ID), "Product", "Manufacturer", "Serial Number", "Port" and "Disconnected" (date & time).

Besides, it also can:

  • export gathered information as a JSON dump (and open such dumps, of course (: );
  • generate a list of authorized (trusted) USB devices as a JSON (call it auth.json);
  • search for "violation events" based on the auth.json: show (or generate another JSON with) USB devices that do appear in history and do NOT appear in the auth.json;
  • [when installed] create crypted storages (7zip archives) to automatically backup and accumulate USB events with the help of crontab utility;
  • search additional details about a specific USB device based on its VID and/or PID.

Screenshots

Screenshot-1 Screenshot-2

Dependencies

usbrip works with non-modified structure of system log files only, so, unfortunately, it won't be able to parse USB history if you change the format of syslogs (with syslog-ng or rsyslog for example). That's why the timestamps of "Connected" and "Disconnected" fields don't have the year, by the way. Keep that in mind.

DEB Packages

  • python3.x (or newer) interpreter
  • python-virtualenv (optionally if used in portable mode only)
  • p7zip-full (used by storages module)

PIP Packages

usbrip makes use of the following external modules:

Resolve all Python dependencies with the pip one-liner:

$ python3 -m pip install -r requirements.txt

Installation

usbrip can work in portable mode (when you run it explicity with python3 command like in Examples) but it also can be installed on the system with the install.sh script.

When using the install.sh some extra features become available:

  • all the necessary Python requirements are installed automatically (by creating virtual environment);
  • you can run usbrip from anywhere in your terminal with usbrip command;
  • you can set a crontab job to backup USB events on a schedule (the example of crontab jobs can be found in usbrip.cron).

โš ๏ธ Warning: if you are using the crontab scheduling, you want to configure the cron job with sudo crontab -e in order to force the storage update submodule run as root as well as protect the passwords of the USB event storages. It's obviously not a truly secure way to input passwords (no secrets should be ever stored as plain text / passed as arguments on the command line due to a variety of ways of exposing such secrets, e. g. scanning /proc directory for new PIDs to catch short-lived processes with all the corresponding CLI args), but this is just an educational project in the end (interactive mode for secure password prompting is in the TODO list ๐Ÿ˜).

The uninstall.sh script removes all the installation artifacts from your system.

To install usbrip use:

$ git clone https://github.com/snovvcrash/usbrip.git usbrip && cd usbrip
$ chmod +x install.sh

# When -s switch is enabled, not only the usbrip project is installed, but also the list of trusted USB devices, history and violations storages are created
$ sudo -H ./install.sh [-s, --storages]

โš ๏ธ Warning: when using -s option during installation, make sure that system logs do contain at least one external USB device entry. It is a necessary condition for usbrip to successfully create the list of trusted devices (and as a result, successfully create the violations storage).

After the installation completes, feel free to remove the usbrip folder.

To uninstall usbrip use:

$ chmod +x uninstall.sh

# When -a switch is enabled, not only the usbrip project directory is deleted, but also all the storages and usbrip logs are deleted too
$ sudo ./uninstall.sh [-a, --all]

When installed, the usbrip uses the following paths:

  • /opt/usbrip/ โ€” project's main directory;
  • /var/opt/usbrip/storage/ โ€” USB event storages: history.7z and violations.7z (created during the installation process);
  • /var/opt/usbrip/log/ โ€” usbrip logs (recommended to log usbrip activity when using crontab, see usbrip.cron);
  • /var/opt/usbrip/trusted/ โ€” list of trusted USB devices (created during the installation process);
  • /usr/local/bin/usbrip โ€” symlink to the /opt/usbrip/usbrip.py file.

Usage

Synopsis

# ---------- BANNER ----------

$ python3 usbrip.py banner
Get usbrip banner.

# ---------- EVENTS ----------

$ python3 usbrip.py events history [-t | -l] [-e] [-n <NUMBER_OF_EVENTS>] [-d <DATE> [<DATE> ...]] [--user <USER> [<USER> ...]] [--vid <VID> [<VID> ...]] [--pid <PID> [<PID> ...]] [--prod <PROD> [<PROD> ...]] [--manufact <MANUFACT> [<MANUFACT> ...]] [--serial <SERIAL> [<SERIAL> ...]] [--port <PORT> [<PORT> ...]] [-c <COLUMN> [<COLUMN> ...]] [-f <FILE> [<FILE> ...]] [-q] [--debug]
Get USB event history.

$ python3 usbrip.py events open <DUMP.JSON> [-t | -l] [-e] [-n <NUMBER_OF_EVENTS>] [-d <DATE> [<DATE> ...]] [--user <USER> [<USER> ...]] [--vid <VID> [<VID> ...]] [--pid <PID> [<PID> ...]] [--prod <PROD> [<PROD> ...]] [--manufact <MANUFACT> [<MANUFACT> ...]] [--serial <SERIAL> [<SERIAL> ...]] [--port <PORT> [<PORT> ...]] [-c <COLUMN> [<COLUMN> ...]] [-f <FILE> [<FILE> ...]] [-q] [--debug]
Open USB event dump.

$ python3 usbrip.py events gen_auth <OUT_AUTH.JSON> [-a <ATTRIBUTE> [<ATTRIBUTE> ...]] [-e] [-n <NUMBER_OF_EVENTS>] [-d <DATE> [<DATE> ...]] [--user <USER> [<USER> ...]] [--vid <VID> [<VID> ...]] [--pid <PID> [<PID> ...]] [--prod <PROD> [<PROD> ...]] [--manufact <MANUFACT> [<MANUFACT> ...]] [--serial <SERIAL> [<SERIAL> ...]] [--port <PORT> [<PORT> ...]] [-f <FILE> [<FILE> ...]] [-q] [--debug]
Generate a list of trusted (authorized) USB devices.

$ python3 usbrip.py events violations <IN_AUTH.JSON> [-a <ATTRIBUTE> [<ATTRIBUTE> ...]] [-t | -l] [-e] [-n <NUMBER_OF_EVENTS>] [-d <DATE> [<DATE> ...]] [--user <USER> [<USER> ...]] [--vid <VID> [<VID> ...]] [--pid <PID> [<PID> ...]] [--prod <PROD> [<PROD> ...]] [--manufact <MANUFACT> [<MANUFACT> ...]] [--serial <SERIAL> [<SERIAL> ...]] [--port <PORT> [<PORT> ...]] [-c <COLUMN> [<COLUMN> ...]] [-f <FILE> [<FILE> ...]] [-q] [--debug]
Get USB violation events based on the list of trusted devices.

# ---------- STORAGE ----------

$ python3 usbrip.py storage list <STORAGE_TYPE> -p <PASSWORD> [-q] [--debug]
List contents of the selected storage (7zip archive). STORAGE_TYPE is "history" or "violations".

$ python3 usbrip.py storage open <STORAGE_TYPE> -p <PASSWORD> [-t | -l] [-e] [-n <NUMBER_OF_EVENTS>] [-d <DATE> [<DATE> ...]] [--user <USER> [<USER> ...]] [--vid <VID> [<VID> ...]] [--pid <PID> [<PID> ...]] [--prod <PROD> [<PROD> ...]] [--manufact <MANUFACT> [<MANUFACT> ...]] [--serial <SERIAL> [<SERIAL> ...]] [--port <PORT> [<PORT> ...]] [-c <COLUMN> [<COLUMN> ...]] [-q] [--debug]
Open selected storage (7zip archive). Behaves similary to the EVENTS OPEN submodule.

$ python3 usbrip.py storage update <STORAGE_TYPE> -p <PASSWORD> [-a <ATTRIBUTE> [<ATTRIBUTE> ...]] [-e] [-n <NUMBER_OF_EVENTS>] [-d <DATE> [<DATE> ...]] [--user <USER> [<USER> ...]] [--vid <VID> [<VID> ...]] [--pid <PID> [<PID> ...]] [--prod <PROD> [<PROD> ...]] [--manufact <MANUFACT> [<MANUFACT> ...]] [--serial <SERIAL> [<SERIAL> ...]] [--port <PORT> [<PORT> ...]] [--lvl <COMPRESSION_LEVEL>] [-q] [--debug]
Update storage โ€” add USB events to the existing storage (7zip archive). COMPRESSION_LEVEL is a number in [0..9].

$ python3 usbrip.py storage create <STORAGE_TYPE> [-p <PASSWORD>] [-a <ATTRIBUTE> [<ATTRIBUTE> ...]] [-e] [-n <NUMBER_OF_EVENTS>] [-d <DATE> [<DATE> ...]] [--user <USER> [<USER> ...]] [--vid <VID> [<VID> ...]] [--pid <PID> [<PID> ...]] [--prod <PROD> [<PROD> ...]] [--manufact <MANUFACT> [<MANUFACT> ...]] [--serial <SERIAL> [<SERIAL> ...]] [--port <PORT> [<PORT> ...]] [--lvl <COMPRESSION_LEVEL>] [-q] [--debug]
Create storage โ€” create 7zip archive and add USB events to it according to the selected options.

$ python3 usbrip.py storage passwd <STORAGE_TYPE> -o <OLD_PASSWORD> -n <NEW_PASSWORD> [--lvl <COMPRESSION_LEVEL>] [-q] [--debug]
Change password of the existing storage.

# ---------- IDs ----------

$ python3 usbrip.py ids search [--vid <VID>] [--pid <PID>] [--offline] [-q] [--debug]
Get extra details about a specific USB device by its <VID> and/or <PID> from the USB ID database.

$ python3 usbrip.py ids download [-q] [--debug]
Update (download) the USB ID database.

Help

To get a list of module names use:

$ python3 usbrip.py -h

To get a list of submodule names for a specific module use:

$ python3 usbrip.py <module> -h

To get a list of all switches for a specific submodule use:

$ python3 usbrip.py <module> <submodule> -h

Examples

  • Show the event history of all USB devices, supressing banner output, info messages and user iteraction (-q, --quiet), represented as a list (-l, --list) with latest 100 entries (-n NUMBER, --number NUMBER):

    $ python3 usbrip.py events history -ql -n 100
    
  • Show the event history of the external USB devices (-e, --external, which were actually disconnected) represented as a table (-t, --table) containing "Connected", "VID", "PID", "Disconnected" and "Serial Number" columns (-c COLUMN [COLUMN], --column COLUMN [COLUMN]) filtered by date (-d DATE [DATE ...], --date DATE [DATE ...]) with logs taken from the outer files (-f FILE [FILE ...], --file FILE [FILE ...]):

    $ python3 usbrip.py events history -et -c conn vid pid disconn serial -d "Dec  9" "Dec 10" -f /var/log/syslog.1 /var/log/syslog.2.gz
    
  • Build the event history of all USB devices and redirect the output to a file for further analysis. When the output stream is NOT terminal stdout (| or > for example) there would be no ANSI escape characters (color) in the output so feel free to use it that way. Also notice that usbrip uses some UNICODE symbols so it would be nice to convert the resulting file to UTF-8 encoding (with encov for example) as well as change newline characters to Windows style for portability (with awk for example):

    python3 usbrip.py history events -t | awk '{ sub("$", "\r"); print }' > usbrip.txt && enconv -x UTF8 usbrip.txt
    

    Remark: you can always get rid of the escape characters by yourself even if you have already got the output to stdout. To do that just copy the output data to usbrip.txt and add one more awk instruction:

    awk '{ sub("$", "\r"); gsub("\\x1B\\[[0-?]*[ -/]*[@-~]", ""); print }' usbrip.txt && enconv -x UTF8 usbrip.txt
    
  • Generate a list of trusted USB devices as a JSON-file (trusted/auth.json) with "VID" and "PID" attributes containing the first three devices connected on September 26:

    $ python3 usbrip.py events gen_auth trusted/auth.json -a vid pid -n 3 -d "Sep 26"
    
  • Search the event history of the external USB devices for violations based on the list of trusted USB devices (trusted/auth.json) by "PID" attribute, restrict resulting events to those which have "Bob" as a user, "EvilUSBManufacturer" as a manufacturer, "1234567890" as a serial number and represent the output as a table with "Connected", "VID" and "PID" columns:

    $ python3 usbrip.py events violations trusted/auth.json -a pid -et --user Bob --manufact EvilUSBManufacturer --serial 1234567890 -c conn vid pid
    
  • Search for details about a specific USB device by its VID (--vid VID) and PID (--pid PID):

    $ python3 usbrip.py ids search --vid 0781 --pid 5580
    
  • Download the latest version of usb_ids/usb.ids database (the source is here):

    $ python3 usbrip.py ids download
    

Post Scriptum

Yep, the banner and info messages style is inspired by the sqlmap project ๐Ÿ™ˆ

If this tool has been useful for you, feel free to buy me a coffee โ˜•

Coffee