Course contentsShow
← Security
Lesson 1184 of 3,05224. Application Security - Input ValidationFree lesson

Insecure Deserialization in Java

Exploiting Java ObjectInputStream, gadget chains, ysoserial, and understanding Java deserialization vulnerabilities.

Insecure Deserialization in Java

You'll learn how Java's object deserialization mechanism can be weaponized through malicious gadget chains to achieve remote code execution.

The Core Vulnerability

Java's ObjectInputStream allows objects to be converted from byte streams back into live objects. When an application deserializes untrusted data without validation, attackers can craft malicious serialized objects that execute arbitrary code during deserialization. Think of it like receiving a package that assembles itself—if the package contains instructions to "run this command while unpacking," you're in trouble.

How Java Deserialization Attacks Work

Java classes can define readObject() methods that execute during deserialization. Attackers chain together existing classes (called "gadgets") in your application's classpath. Each gadget performs a small operation, but when linked together, these chains can perform dangerous actions like file manipulation or command execution.

Gadget chains are sequences of method calls triggered automatically during deserialization. Popular libraries (Apache Commons Collections, Spring Framework) contain classes that, when combined cleverly, form exploitable chains.

The ysoserial Tool

ysoserial is a proof-of-concept tool that generates payloads for known Java gadget chains. It demonstrates how seemingly harmless libraries become weaponized. For example, a chain might use a class's compareTo() method to trigger a toString() method, which invokes a template engine, which executes system commands.

Why This Is Dangerous

Unlike injection attacks that require specific vulnerable code paths, deserialization attacks exploit the fundamental behavior of Java's object restoration. Any code accepting serialized objects from untrusted sources is potentially vulnerable if the classpath contains exploitable gadgets.

Key Takeaway: Never deserialize untrusted data with Java's ObjectInputStream. Use secure alternatives like JSON with schema validation, implement allowlists for expected classes, or use Java 9+ serialization filtering to block dangerous classes before deserialization occurs.