Sub topic 4
Reading a Pickle with `pickletools`
This chapter assumes basic Python, classes, special methods, and familiarity with trained model weights. You should also know that loading a pickle can execute code. The goal here is to inspect a pickle without loading it.
Why inspect a pickle?
A pickle is a byte stream that describes how to rebuild Python objects. Its contents are instructions for a small stack-based virtual machine. Some instructions push values onto a stack. Others call functions, build containers, or finish the object.
pickletools can decode these instructions and display them. This lets you review a pickle's structure without calling pickle.load() or pickle.loads().
Create a small example
The following class is intentionally simple. Its __reduce__ method tells pickle to call print with one argument when the object is reconstructed.
This example does not load the pickle. pickle.dumps only creates the byte stream. The potentially dangerous operation would be pickle.loads.
import pickle
import pickletools
class Payload:
def __reduce__(self):
return (print, ("function call requested by the pickle",))
data = pickle.dumps(Payload(), protocol=4)
print("Disassembly:")
pickletools.dis(data)Run the program in a test environment. The disassembly will vary slightly between Python versions, but it will contain instructions similar to these:
| Opcode | Purpose |
|---|---|
| `PROTO` | Selects the pickle protocol version |
| `GLOBAL` or `STACK_GLOBAL` | Identifies an importable module and name |
| `TUPLE1` | Builds a one-item tuple |
| `REDUCE` | Calls the identified callable with arguments |
| `STOP` | Ends the pickle program |
The important sequence is the reference to builtins.print, followed by REDUCE. That combination indicates that loading the pickle requests a call to print.
Inspect individual instructions
pickletools.genops produces one decoded instruction at a time. This is useful when you want to search for suspicious operations or record findings in a scanner.
import pickle
import pickletools
class Payload:
def __reduce__(self):
return (print, ("review this stream",))
data = pickle.dumps(Payload(), protocol=4)
for instruction, argument, position in pickletools.genops(data):
print(f"{position:04d}: {instruction.name:16} argument={argument!r}")The position is the byte offset in the original stream. The instruction name identifies the operation, and argument contains its decoded value when it has one.
What disassembly can and cannot prove
Disassembly is a review tool, not a complete security boundary. Check for:
- References to unexpected modules or names.
REDUCE,BUILD, and related object-construction operations.- Large or unusual data sections.
- Instructions that do not match the claimed model contents.
A clean-looking stream does not prove that a file is safe. A pickle may use less obvious callables, and a reviewer may miss a dangerous combination of instructions. Never treat pickletools.dis as permission to load an unknown file.
> callout warning > Do not use pickle.loads as part of inspection. Use pickletools.dis or pickletools.genops, then load the file only with a restricted method and only when its source is trusted.
Exercise
Which part of the example suggests that loading the pickle would call a function, and which tool lets you see it without loading the pickle?
Show answer
The reference to builtins.print followed by the REDUCE opcode suggests a function call. pickletools.dis or pickletools.genops reveals those instructions without unpickling the data.