Knowledge Base Regulation Tracker Standards & Frameworks OSS Directory AI Incident Database Research Paper Library Case Studies Society Articles News Jobs About Charter Sign inBecome a Member

Fickling

Model file scanning

Python pickle decompiler and static analyser that detects malicious pickle payloads, including in PyTorch model files, and can inject or trace pickle behaviour for research. From Trail of Bits; named in the taxonomy.

picklemodel scanningstatic analysis

How Fickling Works

Fickling is a Python tool designed to analyze, decompile, and secure pickle-based files, including PyTorch model files. It operates by implementing a custom Pickle Machine (PM), which is a stack-based virtual machine that interprets pickled data streams. Unlike direct execution, Fickling’s PM symbolically executes code, allowing it to safely analyze potentially malicious pickle files without running untrusted code. This approach enables reverse engineering, static analysis, and the detection of malicious payloads embedded in pickle files.

The tool integrates with Python’s built-in pickle module by hooking into it, enabling runtime safety checks. When activated, Fickling intercepts pickle operations and verifies the integrity of loaded files against predefined safety criteria. It checks for suspicious patterns such as unused variables, calls to eval(), and unauthorized imports, which are common indicators of malicious intent.

Fickling supports both library and command-line interfaces (CLI). As a library, it can be programmatically integrated into Python applications to enforce safety checks during model loading or file deserialization. The CLI offers additional functionalities, such as tracing pickle execution, injecting code into pickle files for research purposes, and decompiling pickle files into human-readable Python code. These features are particularly useful for security researchers and developers who need to inspect or manipulate pickle-based artifacts.

For example, it includes a PyTorch-specific module that identifies and inspects polyglot files—files that can be interpreted as multiple formats, such as PyTorch’s various model file formats (e.g., Tar, ZIP, or TorchScript archives). This capability is critical for securing AI/ML pipelines, where models may be distributed in complex or hybrid formats.

Fickling’s safety mechanisms are configurable. Users can define allowlists of safe imports for machine learning libraries, ensuring that only trusted modules are loaded. This is particularly important in AI/ML environments, where models often rely on libraries like torch or numpy. The tool also provides granular control over safety checks, allowing users to enable or disable protections dynamically or within specific contexts.

Under the hood, Fickling’s safety checks are enforced through exceptions. When a malicious pickle file is detected, it raises an UnsafeFileError, which includes detailed analysis results. Additionally, Fickling includes protections against denial-of-service (DoS) attacks, such as expansion attacks (e.g., Billion Laughs), which could otherwise exhaust system resources during analysis.

The tool’s development is iterative, with regular updates to address security vulnerabilities and expand its capabilities. For instance, recent releases have included fixes for issues like MLAllowlist shadowing and the detection of dotted attribute names in STACK_GLOBAL operations. These updates reflect ongoing efforts to improve the tool’s accuracy and robustness in identifying malicious patterns.

PyTorch is an optional dependency, required only for features related to PyTorch model analysis. Installation is straightforward via pip or uv, with support for optional extras (e.g., fickling[torch]) to enable PyTorch-specific functionalities.

Use Cases

Fickling is primarily used in securing AI/ML environments, where pickle files are commonly employed to serialize and distribute models. One of its key applications is the automatic scanning of pickle files within model repositories or pipelines. By hooking into the pickle module, Fickling can verify that only files with safe imports—those from trusted ML libraries—are loaded. This prevents the execution of malicious code that might be embedded in compromised model files.

For example, it can trace the execution of pickle files without running malicious code, allowing researchers to study how pickle-based attacks operate. Fickling also enables code injection into pickle files, which can be used to test or demonstrate vulnerabilities in a controlled environment.

Another critical use case is the analysis of PyTorch model files. PyTorch models can be saved in multiple formats, some of which may contain pickle data alongside other structures (e.g., ZIP archives, Tar files, or TorchScript modules). Fickling’s PyTorch module can identify these formats, inspect their contents, and ensure that no malicious payloads are present. This is particularly relevant for organizations that distribute or consume third-party models, where supply chain risks are significant.

Fickling’s CLI provides a convenient way to integrate its functionalities into existing workflows.

For developers working with Hugging Face Hub or similar platforms, Fickling offers direct scanning support. This is particularly useful for large-scale model repositories, where manual inspection would be impractical.

Beyond security, Fickling can be used to create polyglot files—files that are valid in multiple formats. For example, a file might be both a valid PyTorch model and a pickle file. Fickling’s PyTorch module can identify and manipulate such files, which is useful for testing interoperability or exploring edge cases in model serialization.

Key Features and Capabilities

Fickling’s core functionality revolves around its ability to decompile and analyze pickle files. When a pickle file is loaded, Fickling’s custom Pickle Machine interprets the bytecode without executing it directly. This symbolic execution allows the tool to detect suspicious patterns, such as calls to eval(), unused variables, or imports from unsafe modules.

One of Fickling’s standout features is its integration with Python’s pickle module through hooks. When activated, these hooks intercept all pickle operations, allowing users to enforce safety checks globally or within specific contexts. For example, the activatesafemlenvironment() function sets global hooks to block unsafe imports, while the checksafety() context manager ensures that only files loaded within its scope are checked.

Users can also customize the allowlist to accommodate legitimate but non-standard imports, though they should exercise caution to avoid reintroducing security risks.

The tool’s support for PyTorch extends its applicability to the AI/ML domain. Fickling’s PyTorch module can identify these formats—such as Tar files, ZIP archives, or TorchScript modules—and analyze their contents for malicious payloads.

This is useful for understanding how pickle-based attacks operate or for debugging serialization issues. Additionally, Fickling can inject arbitrary code into pickle files, enabling controlled experiments or demonstrations of vulnerabilities.

To enhance usability, Fickling provides multiple ways to perform safety checks. Users can enforce global checks using alwayschecksafety(), apply checks within a context manager, or load and verify individual files using fickling.load(). Each method offers different levels of granularity, allowing users to balance convenience with security.

For instance, it mitigates DoS risks by implementing safeguards against expansion attacks, such as the Billion Laughs attack, which could otherwise consume excessive resources during analysis. This ensures that the tool remains reliable even when processing large or maliciously crafted pickle files.

The tool’s development is community-driven, with contributions from external developers and regular updates to address security vulnerabilities. For example, recent releases have included fixes for issues like MLAllowlist shadowing and the detection of dotted attribute names in STACK_GLOBAL operations.

Fickling is licensed under the GNU Lesser General Public License (LGPL) 3.0, which permits integration into proprietary software while maintaining open-source principles.

Sources

Contribute

Spot an error, or know a record we should add? Members can propose additions and submit corrections.