Python - Pickle

The Python pickle module implements binary protocols for serializing and deserializing a Python object structure.

  • Pickling: Converting an in-memory Python object hierarchy into a byte stream.
  • Unpickling: Reconstructing an object hierarchy from a serialized byte stream.

Security Warning: Never Unpickle Untrusted Data

pickle is not secure against erroneous or maliciously constructed data. The unpickling process executes arbitrary code by calling __reduce__ or system constructors during deserialization.

# ATTACK VECTOR EXAMPLE:
# An untrusted pickle payload can execute arbitrary shell commands:
import os
class Exploit:
    def __reduce__(self):
        return (os.system, ('cat /etc/passwd',))

Safe Modern Alternatives

  • Structured API data: Use json (import json) or msgpack.
  • Machine Learning Models & Weights: Use safetensors (Hugging Face) instead of pickled PyTorch .pt/.bin files to prevent arbitrary code execution vulnerabilities.
  • Tabular Data: Use Apache Arrow or Parquet (pandas.to_parquet()).

Pickle vs JSON

Feature pickle json
Format Binary byte stream Text (UTF-8)
Readability Machine-only Human-readable
Interoperability Python-specific only Universal across all programming languages
Type Support Python custom classes, functions, cyclic refs Limited standard primitives (string, number, bool, null, list, dict)
Security Vulnerable to arbitrary code execution Safe against code execution

Basic Usage

import pickle

data = {"user": "alice", "roles": ["admin", "editor"], "active": True}

# Serialize to file
with open("state.pkl", "wb") as f:
    pickle.dump(data, f, protocol=pickle.HIGHEST_PROTOCOL)

# Deserialize from file
with open("state.pkl", "rb") as f:
    loaded = pickle.load(f)