Course contentsShow
← Security
Lesson 1231 of 3,05225. Application Security - Secure CodingFree lesson

Deserialization and Pickle Dangers

Understand why deserializing untrusted data with pickle, unserialize, and ObjectInputStream enables remote code execution.

Deserialization and Pickle Dangers

What you'll learn: Why deserializing untrusted data with tools like pickle, unserialize, and ObjectInputStream can lead to remote code execution.

The Core Problem

Deserialization transforms serialized data (bytes) back into objects your program can use. Sounds harmless, right? The danger is that these formats don't just store data—they can encode instructions about what code to run when rebuilding objects.

When you deserialize untrusted input with pickle (Python), unserialize (PHP), or ObjectInputStream (Java), an attacker can craft malicious payloads that execute arbitrary code during the deserialization process itself—before your application logic even touches the data.

Why It's So Dangerous

Think of deserialization like a recipe: you're not just reading ingredients, you're following cooking instructions. An attacker replaces your recipe with "step 1: unlock the back door, step 2: give me admin access."

These formats trigger special methods during object reconstruction—__reduce__ in Python, magic methods in PHP, readObject in Java. Attackers chain together existing classes in your application (or its libraries) to build "gadget chains" that execute shell commands, read files, or establish reverse shells.

The vulnerability exists because:

  • Serialization formats were designed for trusted environments
  • No built-in authentication or integrity checks
  • Code execution happens automatically during deserialization
  • Developers often treat serialized data as "just data"

The Bottom Line

Never deserialize data from untrusted sources using pickle, unserialize, or ObjectInputStream. These tools assume complete trust. If you receive serialized data from users, networks, or external systems, treat it as executable code, not passive data.

Key Takeaway: Deserializing untrusted data with pickle, unserialize, and ObjectInputStream enables remote code execution because these formats can encode instructions that run automatically when rebuilding objects—treat serialized data from untrusted sources as malicious executable code, never as safe data.