Deserialization Attacks in File Processing
What you'll learn: How attackers exploit insecure deserialization when applications process uploaded serialized objects to execute malicious code.
What is Deserialization?
Serialization converts program objects into a storable format (like bytes or text). Deserialization is the reverse—reconstructing objects from that stored data. Many languages (Java, Python, PHP, Ruby) support this to save complex data structures to files or databases.
When your application accepts uploaded files containing serialized objects and deserializes them without validation, attackers can craft malicious payloads that execute arbitrary code upon deserialization.
Why File Processing Makes It Worse
File uploads often trigger automatic processing. If your application deserializes uploaded .ser (Java), .pickle (Python), or serialized PHP data from files, the attacker controls what gets loaded into memory.
Think of it like this: Deserialization is like following a recipe card. A malicious recipe might say "crack two eggs, add flour, then format your hard drive." The deserializer blindly follows instructions embedded in the data.
Language-Specific Risks
Java: .ser files using ObjectInputStream can trigger gadget chains—sequences of existing classes that chain together to execute commands.
Python: pickle files can execute arbitrary code during unpickling. Never unpickle data from untrusted sources.
PHP: The unserialize() function can trigger magic methods like __wakeup() or __destruct() that attackers exploit.
Other languages: .NET, Ruby, and Node.js have similar deserialization mechanisms with comparable risks.
The Attack Vector
- Attacker uploads a crafted serialized object file
- Application deserializes it (often automatically during processing)
- Malicious code executes server-side with application privileges
- Attacker achieves remote code execution
Prevention
Never deserialize untrusted data. Use safe formats like JSON or XML with proper validation instead. If you must deserialize, use integrity checks (signatures/MACs) and restrict which classes can be instantiated.
Key Takeaway: Deserialization of uploaded files is extremely dangerous—treat serialized data from users as executable code and avoid deserializing untrusted input entirely.