Skip to main content

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: `bash python3 -m pip install --user pytorch-check `

Or from this directory: `bash python3 -m pip install --user . `

Then use the command line tool like this: `bash 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

pytorch-check-1.0.1.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

pytorch_check-1.0.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file pytorch-check-1.0.1.tar.gz.

File metadata

  • Download URL: pytorch-check-1.0.1.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

Hashes for pytorch-check-1.0.1.tar.gz
Algorithm Hash digest
SHA256 205917dacedabab2e5934c2f4663a94cc36cca1b785fda465e7a67a88b16087f
MD5 a8326fbb65d13736eb17549979c82fad
BLAKE2b-256 55875d975b55ee46837eaea3a06cb05d0ae322aaed8c8a89cb7cf59a4449310b

See more details on using hashes here.

File details

Details for the file pytorch_check-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: pytorch_check-1.0.1-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

Hashes for pytorch_check-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 af62a0b1cc6c315082b552e9a9c32a5b46519c02a5f1d30b4ab27a4b4b78cb9d
MD5 7653b0302ed8569581012a4f25d732ce
BLAKE2b-256 c7c443428d6de46ee0778ef6c7c7e470df2781e323505e06228da8df6a66cf8d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page