1
0
Fork 0
mirror of https://github.com/snovvcrash/usbrip.git synced 2024-06-08 23:16:04 +02:00

Migrate from virtualenv to python3-venv

This commit is contained in:
Sam Free5ide 2019-03-31 21:57:47 +03:00
parent d9bf70aaf2
commit f6cf7c03c0
7 changed files with 132 additions and 82 deletions

1
.gitignore vendored
View File

@ -8,6 +8,7 @@ __pycache__/
# Distribution / packaging
build/
*.egg-info/
# SageMath parsed files
*.sage.py

Binary file not shown.

View File

@ -12,6 +12,7 @@ usbrip
Table of Contents:
* [**Description**](#description)
* [**Screenshots**](#screenshots)
* [**Git clone**](#git-clone)
* [**Dependencies**](#dependencies)
* [DEB Packages](#deb-packages)
* [PIP Packages](#pip-packages)
@ -32,7 +33,7 @@ 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;
* *\*when installed with `-s` flag\** 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
@ -40,15 +41,27 @@ Screenshots
![Screenshot-1](https://user-images.githubusercontent.com/23141800/40887882-e00d4d3a-6757-11e8-962c-c77331782b19.png "Get USB event history")
![Screenshot-2](https://user-images.githubusercontent.com/23141800/40886876-46c349d6-6748-11e8-92cf-0b0790ea9505.png "Search for extra details about a specific USB device")
Git clone
==========
For simplicity, lets agree that all the commands where `~/usbrip$` prefix is appeared are executed in the `~/usbrip` directory which is created as a result of git clone:
```
~$ git clone https://github.com/snovvcrash/usbrip.git usbrip && cd usbrip
~/usbrip$
```
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.6 (or newer) interpreter
* python-virtualenv
* python3-venv
* p7zip-full *(used by `storages` module)*
```
~$ sudo apt install -y python3-venv p7zip-full
```
## PIP Packages
usbrip makes use of the following external modules:
* [terminaltables](https://github.com/Robpol86/terminaltables "Robpol86/terminaltables: Generate simple tables in terminals from a nested list of strings.")
@ -56,7 +69,7 @@ usbrip makes use of the following external modules:
To resolve Python dependencies manually (it's not necessary actually because `pip` or `setup.py` can automate the process, see [Installation](#installation)) create a *virtual environment* and run `pip` from within:
```
~/usbrip$ virtualenv -p python3 venv && . venv/bin/activate
~/usbrip$ python3 -m venv venv && source venv/bin/activate
(venv) ~/usbrip$ pip install -r requirements.txt
```
@ -67,13 +80,12 @@ Or let the `pipenv` one-liner do all the dirty work for you:
Installation
==========
There are two ways to install usbrip into the system.
There are two ways to install usbrip into the system: `pip` or `setup.py`.
## `pip` or `setup.py`
First of all, usbrip is pip installable. This means you can simply git clone the repo, fire the pip installation process and run usbrip from anywhere in your terminal like so:
First of all, usbrip is pip installable. This means that after git clonning the repo you can simply fire up the pip installation process and after that run usbrip from anywhere in your terminal like so:
```
~$ git clone https://github.com/snovvcrash/usbrip.git usbrip && cd usbrip
~/usbrip$ virtualenv -p python3 venv && . venv/bin/activate
~/usbrip$ python3 -m venv venv && source venv/bin/activate
(venv) ~/usbrip$ pip install .
(venv) ~/usbrip$ usbrip -h
@ -81,8 +93,7 @@ First of all, usbrip is pip installable. This means you can simply git clone the
Or if you want to resolve Python dependencies locally (without bothering PyPI), use `setup.py`:
```
~$ git clone https://github.com/snovvcrash/usbrip.git usbrip && cd usbrip
~/usbrip$ virtualenv -p python3 venv && . venv/bin/activate
~/usbrip$ python3 -m venv venv && source venv/bin/activate
(venv) ~/usbrip$ python setup.py install
(venv) ~/usbrip$ usbrip -h
@ -107,8 +118,9 @@ To install usbrip use:
~/usbrip$ cd
~$ usbrip -h
# When -l switch is enabled, Python dependencies are resolved from local .tar packages instead of PyPI
# 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
~/usbrip$ sudo -H installers/install.sh [-s, --storages]
~/usbrip$ sudo -H installers/install.sh [-l/--local] [-s/--storages]
```
:alien: **Note**: 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).
@ -128,7 +140,7 @@ To uninstall usbrip use:
~/usbrip$ chmod +x installers/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
~/usbrip$ sudo installers/uninstall.sh [-a, --all]
~/usbrip$ sudo installers/uninstall.sh [-a/--all]
```
And don't forget to remove the cron job.
@ -250,7 +262,7 @@ Examples
Post Scriptum
==========
Yep, the banner and info messages style is inspired by the *sqlmap* project :see_no_evil:
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:

View File

@ -27,7 +27,7 @@ along with usbrip. If not, see <http://www.gnu.org/licenses/>.
%endlicense
'
# Usage: sudo -H ./install.sh
# Usage: sudo -H ./install.sh [-l/--local] [-s/--storages]
shopt -s expand_aliases
@ -54,33 +54,42 @@ alias createViolationsStorage="${OPT}/venv/bin/usbrip storage create violations
# --------------- Check for root privileges ----------------
if [[ $EUID -ne 0 ]]; then
/usr/bin/printf "${R}>>>>${NC} Please run as root:\nsudo -H %s\n" "${0}"
/usr/bin/printf "${R}[!]${NC} Please run as root:\nsudo -H %s\n" "${0}"
exit 1
fi
# -------------------- Handle switches ---------------------
if [[ "$1" == "-s" ]] || [[ "$1" == "--storages" ]]; then
LOCAL=false
STORAGES=false
if [[ "$1" == "-l" ]] || [[ "$1" == "--local" ]]; then
LOCAL=true
elif [[ "$1" == "-s" ]] || [[ "$1" == "--storages" ]]; then
STORAGES=true
fi
if [[ "$2" == "-l" ]] || [[ "$2" == "--local" ]]; then
LOCAL=true
elif [[ "$2" == "-s" ]] || [[ "$2" == "--storages" ]]; then
STORAGES=true
else
STORAGES=false
fi
# -------------- Check for required packages ---------------
# virtualenv
# python3-venv
if ! /usr/bin/virtualenv --version > /dev/null; then
/usr/bin/printf "${R}>>>>${NC} Unresolved dependency: virtualenv. To install this package run:\n%s\n" \
"sudo apt install python-virtualenv virtualenv"
if /usr/bin/python3 -m venv 2>&1 | /bin/grep "is not available" > /dev/null; then
/usr/bin/printf "${R}[-]${NC} Unresolved dependency: python3-venv. To install this package run:\n%s\n" \
"sudo apt install -y python3-venv"
exit 1
fi
# p7zip-full
if ! /usr/bin/dpkg-query -W -f='${Status}' p7zip-full | /bin/grep "ok installed" > /dev/null; then
/usr/bin/printf "${R}>>>>${NC} Unresolved dependency: p7zip-full. To install this package run:\n%s\n" \
"sudo apt install p7zip-full"
if ! /usr/bin/dpkg-query -W -f='${Status}' p7zip-full 2>&1 | /bin/grep "ok installed" > /dev/null; then
/usr/bin/printf "${R}[-]${NC} Unresolved dependency: p7zip-full. To install this package run:\n%s\n" \
"sudo apt install -y p7zip-full"
exit 1
fi
@ -88,77 +97,88 @@ fi
# OPT
/usr/bin/printf "${W}>>>>${NC} Creating directory: '${OPT}'\n"
/usr/bin/printf "${W}[*]${NC} Creating directory: '${OPT}'\n"
if [[ -d "${OPT}" ]]; then
/usr/bin/printf "${R}>>>>${NC} ${OPT} already exists. First run:\n%s\n" \
/usr/bin/printf "${R}[-]${NC} ${OPT} already exists. First run:\n%s\n" \
"sudo uninstall.sh --all"
exit 1
fi
if /bin/mkdir "${OPT}"; then
/usr/bin/printf "${G}>>>>${NC} Successfully created directory: '${OPT}'\n\n"
/usr/bin/printf "${G}[+]${NC} Successfully created directory: '${OPT}'\n\n"
else
/usr/bin/printf "${R}>>>>${NC} Failed to create directory: '${OPT}'\n"
/usr/bin/printf "${R}[-]${NC} Failed to create directory: '${OPT}'\n"
exit 1
fi
# LOG
/usr/bin/printf "${W}>>>>${NC} Creating directory: '${LOG}'\n"
/usr/bin/printf "${W}[*]${NC} Creating directory: '${LOG}'\n"
if [[ -d "${LOG}" ]]; then
/usr/bin/printf "${R}>>>>${NC} ${LOG} already exists. First run:\n%s\n" \
/usr/bin/printf "${R}[-]${NC} ${LOG} already exists. First run:\n%s\n" \
"sudo uninstall.sh --all"
exit 1
fi
if /bin/mkdir -p "${LOG}"; then
/usr/bin/printf "${G}>>>>${NC} Successfully created directory: '${LOG}'\n\n"
/usr/bin/printf "${G}[+]${NC} Successfully created directory: '${LOG}'\n\n"
else
/usr/bin/printf "${R}>>>>${NC} Failed to create directory: '${LOG}'\n"
/usr/bin/printf "${R}[-]${NC} Failed to create directory: '${LOG}'\n"
exit 1
fi
# STORAGE
/usr/bin/printf "${W}>>>>${NC} Creating directory: '${STORAGE}'\n"
/usr/bin/printf "${W}[*]${NC} Creating directory: '${STORAGE}'\n"
if [[ -d "${STORAGE}" ]]; then
/usr/bin/printf "${R}>>>>${NC} ${STORAGE} already exists. First run:\n%s\n" \
/usr/bin/printf "${R}[-]${NC} ${STORAGE} already exists. First run:\n%s\n" \
"sudo uninstall.sh --all"
exit 1
fi
if /bin/mkdir -p "${STORAGE}"; then
/usr/bin/printf "${G}>>>>${NC} Successfully created directory: '${STORAGE}'\n\n"
/usr/bin/printf "${G}[+]${NC} Successfully created directory: '${STORAGE}'\n\n"
else
/usr/bin/printf "${R}>>>>${NC} Failed to create directory: '${STORAGE}'\n"
/usr/bin/printf "${R}[-]${NC} Failed to create directory: '${STORAGE}'\n"
exit 1
fi
# ------------ Build python virtual environment ------------
# ------------ Build Python virtual environment ------------
/usr/bin/printf "${W}[*]${NC} Building Python virtual environment\n"
if /usr/bin/python3 -m venv "${OPT}/venv"; then
/usr/bin/printf "${G}[+]${NC} Successfully builded Python virtual environment\n\n"
else
/usr/bin/printf "${R}[-]${NC} Failed to build Python virtual environment\n"
exit 1
fi
# ------------------------ Install -------------------------
/usr/bin/printf "${W}[*]${NC} Installing usbrip\n"
if $LOCAL; then
if ${OPT}/venv/bin/python "${PWD}/setup.py" install; then
/usr/bin/printf "${G}[+]${NC} Successfully installed usbrip using local dependencies\n\n"
else
/usr/bin/printf "${R}[-]${NC} Failed to install usbrip using local dependencies\n"
exit 1
fi
else
if ${OPT}/venv/bin/pip install "${PWD}"; then
/usr/bin/printf "${G}[+]${NC} Successfully installed usbrip using PyPI dependencies\n\n"
else
/usr/bin/printf "${R}[-]${NC} Failed to install usbrip using PyPI dependencies\n"
exit 1
fi
fi
${OPT}/venv/bin/python "${PWD}/setup.py" clean
/usr/bin/printf "\n"
/usr/bin/printf "${W}>>>>${NC} Building python virtual environment\n"
if /usr/bin/virtualenv -p /usr/bin/python3 "${OPT}/venv"; then
/usr/bin/printf "${G}>>>>${NC} Successfully builded python virtual environment\n\n"
else
/usr/bin/printf "${R}>>>>${NC} Failed to build python virtual environment\n"
exit 1
fi
# --------------------- PIP Install . ----------------------
/usr/bin/printf "${W}>>>>${NC} (PIP-)Installing usbrip\n"
if "${OPT}/venv/bin/pip" install "${PWD}"; then
/usr/bin/printf "${G}>>>>${NC} Successfully (PIP-)installed usbrip\n\n"
else
/usr/bin/printf "${R}>>>>${NC} Failed to (PIP-)install usbrip\n"
exit 1
fi
# --------------------- Create symlink ---------------------
@ -167,7 +187,7 @@ if [[ -e "${SYMLINK}" ]]; then
fi
if /bin/ln -s "${OPT}/venv/bin/usbrip" "${SYMLINK}"; then
/usr/bin/printf "${G}>>>>${NC} Created symlink: '${SYMLINK}'\n"
/usr/bin/printf "${G}[+]${NC} Created symlink: '${SYMLINK}'\n"
fi
# ----------------- Create usbrip storages -----------------
@ -175,38 +195,38 @@ fi
if $STORAGES; then
# History
/usr/bin/printf "${W}>>>>${NC} Creating usbrip history storage\n"
/usr/bin/printf "${W}[*]${NC} Creating usbrip history storage\n"
if createHistoryStorage; then
/usr/bin/printf "${G}>>>>${NC} Successfully created usbrip history storage\n\n"
/usr/bin/printf "${G}[+]${NC} Successfully created usbrip history storage\n\n"
else
/usr/bin/printf "${R}>>>>${NC} Failed to create usbrip history storage\n"
/usr/bin/printf "${R}[-]${NC} Failed to create usbrip history storage\n"
exit 1
fi
# Gen Auth
/usr/bin/printf "${W}>>>>${NC} Generating authorized device list\n"
/usr/bin/printf "${W}[*]${NC} Generating authorized device list\n"
if generateAuthorizedDeviceList; then
/usr/bin/printf "${G}>>>>${NC} Successfully generated authorized device list\n\n"
/usr/bin/printf "${G}[+]${NC} Successfully generated authorized device list\n\n"
else
/usr/bin/printf "${R}>>>>${NC} Failed to generate authorized device list\n"
/usr/bin/printf "${R}[-]${NC} Failed to generate authorized device list\n"
exit 1
fi
# Violations
/usr/bin/printf "${W}>>>>${NC} Creating usbrip violations storage\n"
/usr/bin/printf "${W}[*]${NC} Creating usbrip violations storage\n"
if createViolationsStorage; then
/usr/bin/printf "${G}>>>>${NC} Successfully created usbrip violations storage\n\n"
/usr/bin/printf "${G}[+]${NC} Successfully created usbrip violations storage\n\n"
else
/usr/bin/printf "${R}>>>>${NC} Failed to create usbrip violations storage\n"
/usr/bin/printf "${R}[-]${NC} Failed to create usbrip violations storage\n"
exit 1
fi
fi
# -------------------------- Done --------------------------
/usr/bin/printf "${G}>>>>${NC} Done.\n"
/usr/bin/printf "${G}[+]${NC} Done.\n"

View File

@ -27,7 +27,7 @@ along with usbrip. If not, see <http://www.gnu.org/licenses/>.
%endlicense
'
# Usage: sudo ./uninstall.sh [--all]
# Usage: sudo ./uninstall.sh [-a/--all]
# ----------------------- Constants ------------------------
@ -42,7 +42,7 @@ NC="\033[0m" # NO COLOR
# --------------- Check for root privileges ----------------
if [[ $EUID -ne 0 ]]; then
/usr/bin/printf "${R}>>>>${NC} Please run as root:\nsudo -H %s\n" "${0}"
/usr/bin/printf "${R}[!]${NC} Please run as root:\nsudo -H %s\n" "${0}"
exit 1
fi
@ -59,24 +59,24 @@ fi
# OPT
if /bin/rm -r "${OPT}" 2> /dev/null; then
/usr/bin/printf "${G}>>>>${NC} Removed directory: '${OPT}'\n"
/usr/bin/printf "${G}[+]${NC} Removed directory: '${OPT}'\n"
fi
# VAR_OPT
if $ALL; then
if /bin/rm -r "${VAR_OPT}" 2> /dev/null; then
/usr/bin/printf "${G}>>>>${NC} Removed directory: '${VAR_OPT}'\n"
/usr/bin/printf "${G}[+]${NC} Removed directory: '${VAR_OPT}'\n"
fi
fi
# --------------------- Remove symlink ---------------------
if /bin/rm "${SYMLINK}" 2> /dev/null; then
/usr/bin/printf "${G}>>>>${NC} Removed symlink: '${SYMLINK}'\n"
/usr/bin/printf "${G}[+]${NC} Removed symlink: '${SYMLINK}'\n"
fi
# -------------------------- Done --------------------------
/usr/bin/printf "\n"
/usr/bin/printf "${G}>>>>${NC} Done.\n"
/usr/bin/printf "${G}[+]${NC} Done.\n"

View File

@ -1,2 +1,3 @@
termcolor==1.1.0
terminaltables==3.1.0
termcolor
terminaltables
wheel

View File

@ -33,6 +33,7 @@ __brief__ = 'USB device artifacts tracker.'
import glob
import shutil
import subprocess
import sys
import os
from setuptools import setup, find_packages, Command
@ -43,14 +44,29 @@ from usbrip import __version__
class LocalInstallCommand(install):
"""Custom install command to install local Python dependencies."""
def resolve(self, dep, path):
pip = os.path.join(sys.executable.rsplit('/', 1)[0], 'pip')
args = [pip, 'install', os.path.join(path, dep)]
proc = subprocess.Popen(args, shell=False)
proc.communicate()
if proc.returncode == 0:
print(f'[*] Resolved local dependency: {dep}')
def run(self):
install.run(self)
tools_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), '3rdPartyTools')
for dependency in os.listdir(tools_dir):
args = ['pip', 'install', os.path.join(tools_dir, dependency)]
proc = subprocess.Popen(args, shell=False)
proc.communicate()
print(f'resolved local dependency: {dependency}')
deps = []
for dep in os.listdir(tools_dir):
if dep.startswith('wheel-'):
wheel = dep
else:
deps.append(dep)
self.resolve(wheel, tools_dir)
for dep in deps:
self.resolve(dep, tools_dir)
class CleanCommand(Command):
@ -74,7 +90,7 @@ class CleanCommand(Command):
if not path.startswith(here):
# Die if path in CLEAN_FILES is absolute + outside this directory
raise ValueError(f'{path} is not a path inside {here}')
print(f'removing {os.path.relpath(path)}')
print(f'[*] Removing {os.path.relpath(path)}')
shutil.rmtree(path)
@ -120,7 +136,7 @@ setup(
],
python_requires='>=3.6',
# _install_requires=parse_requirements('requirements.txt'),
install_requires=parse_requirements('requirements.txt'),
entry_points={
'console_scripts': [