Check PyTorch models for unsafe code
Project description
pytorch-check
Check PyTorch models for (overtly) unsafe code
Depends on fickling, the Python pickling decompiler and static analyzer.
By default, this checks for common patterns used to inject malicious code into Python bytecode, but is not a 100% guarantee against any malicious code embedded into models.
The -t flag can be used to safely trace the execution of the bytecode to see what's actually going on, and the -d flag can be used to print the disassembled bytecode, for manual analysis.
This script handles the TorchScript serialization format that is used for PyTorch models (generally using the .pt or .pth extension), since that is not handled directly by the CLI tool that comes with the fickling package.
The format is defined here: https://github.com/pytorch/pytorch/blob/master/torch/csrc/jit/docs/serialization.md
Basically .pkl/.debug_pkl files are embedded into a Zip archive along with data files as well as optional Python source code.
For those of you who are unaware, pkl is serialized Python bytecode, that is executed when the model is loaded, allowing for execution of arbitrary code. Trojanizing PyTorch model files is trivial, including by using the fickling tool by TrailOfBits to inject code into one of the embedded .pkl or .debug_pkl files in a model.
Install with:
python3 -m pip install --user pytorch-check
Or from this directory:
python3 -m pip install --user .
Then use the command line tool like this:
pytorch-check /path/to/file-or-directory...
This will scan all of the specified files, treating .pkl/.pickle as files containing pickled Python bytecode directly, and any other files like they are PyTorch model files (i.e. Zip archives with embedded .pkl files).
If a directory is specified, it will be scanned for files with the .ckpt, .pt, .pth, .pkl or .pickle file extensions where the .ckpt, .pt and .pth file extensions will be treated as PyTorch model files.
Note that the pickled code embedded into PyTorch model files in general will have some embedded imports from the pytorch package itself. This will emit a warning since it's technically executing code, but is generally benign. Read the warnings to see what is actually going on.
Stay safe!
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pytorch-check-1.0.3.tar.gz
.
File metadata
- Download URL: pytorch-check-1.0.3.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/6.1.0 keyring/23.13.1 pkginfo/1.9.6 readme-renderer/34.0 requests-toolbelt/0.10.1 requests/2.28.2 rfc3986/1.5.0 tqdm/4.65.0 urllib3/1.26.13 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8909d2a40eb247802a7f73288fafc4d1fb73b1243958360b6e61b4c2b7558261 |
|
MD5 | 6a73e528e53ea4c372863a09008e5a86 |
|
BLAKE2b-256 | 31b330513bf52757d4d7dfd07855d2136df2006d59f7ecb49265a5e913535f13 |
File details
Details for the file pytorch_check-1.0.3-py3-none-any.whl
.
File metadata
- Download URL: pytorch_check-1.0.3-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/6.1.0 keyring/23.13.1 pkginfo/1.9.6 readme-renderer/34.0 requests-toolbelt/0.10.1 requests/2.28.2 rfc3986/1.5.0 tqdm/4.65.0 urllib3/1.26.13 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e1cf6234c20596302a3412d0e1aee6f36986d89da702a7d974ee3862ac367e20 |
|
MD5 | 2c355ccf1450b99a8602e61c9c659aaa |
|
BLAKE2b-256 | 9808a01e2196758bb2b350fe13d95277f3538b2e7145fdb12fd868f76c315c5d |