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 this package from this directory with: `bash python3 -m pip install . `

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.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytorch-check-1.0.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.tar.gz
Algorithm Hash digest
SHA256 ff5435ea737960b41a9b3465f2fe4777bdd980638567c77f357ca351d0817241
MD5 12805d176e00a02ad702afd5f0a4193d
BLAKE2b-256 52cc6c070f521aa4a913903682db4866078e236eda0044b926b4382042f0d66d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytorch_check-1.0-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-py3-none-any.whl
Algorithm Hash digest
SHA256 b3d044a1d8f12fabcd24f823253ea1b2e144a2931eaba93890b6edb7ec8cecf8
MD5 b0c981a9e4a14b6095aa01c47b0d788e
BLAKE2b-256 011f9c8f37a7717f97c976a907bf4437de817734ef8f8b78bef140358f9d1ea1

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