Skip to content
RCE Write-up Intermediate

Exploiting machine learning Pickle files

Machine learning models stored as Python pickle files can execute arbitrary code when loaded, allowing an attacker to run malicious code on the victim's system. The authors demonstrate this by creating a malicious pickle and a tool to analyse and generate such files.

blog.trailofbits.com

Published on blog.trailofbits.com

Many machine learning (ML) models are Python pickle files under the hood, and it makes sense. The use of pickling conserves memory, enables start-and-stop model training, and makes trained models portable (and, thereby, shareable).

The author, on blog.trailofbits.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

The pickle format allows execution of arbitrary code during deserialization through opcodes like GLOBAL and REDUCE, and ML libraries do not restrict this behaviour.

Impact

An attacker could execute arbitrary code on a system that loads a malicious model, potentially leading to data exfiltration, denial of service, or remote code execution.

What the attacker needed

Victim loads untrusted pickle file

The fix

Avoid pickling altogether and use safer serialization formats like JSON, CBOR, or ProtoBuf.

How it was found

  1. Create a pickle file containing a serialized list of basic Python types.
  2. Run fickling on the pickle file to decompile it and see the equivalent Python program.
  3. Inject arbitrary Python code into the pickle file to make it malicious.
  4. Load the malicious pickle file to trigger code execution.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

Pickle is a way to save Python objects to a file. When you load a pickle file, Python runs instructions that can do anything, including running commands on your computer. So if you download a model from the internet and load it, it could run harmful code without you knowing.

What is RCE?

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.