Type Confusion and Class Substitution
What you'll learn: How attackers can manipulate serialized streams to substitute unexpected classes during deserialization, creating security vulnerabilities.
The Core Problem
When Java deserializes an object, it reads the class name from the stream and attempts to instantiate that class. An attacker who controls the serialized data can substitute a different class than your application expects—one that might execute malicious code during deserialization or violate your application's security assumptions.
Think of it like this: You order a pizza delivery (deserialize a Pizza object), but someone intercepts the order and sends a live alligator instead. Your door-opening code (readObject) doesn't check what's actually arriving—it just opens the door.
How It Works
The serialized stream contains metadata describing the object's class. An attacker can:
- Craft a stream declaring it contains a malicious class that implements
Serializable - Send this stream to your application
- Your code deserializes it, instantiating the attacker's chosen class
- The malicious class's
readObjectmethod executes, potentially triggering harmful operations
This is especially dangerous when combined with gadget chains (previously covered)—classes already in your classpath that perform dangerous operations when deserialized.
Type Confusion Explained
Type confusion occurs when your code expects one type but receives another. For example, you might expect a HashMap but receive a specially crafted HashSet that triggers different behavior. Even if both implement Serializable, the unexpected class might have side effects in its deserialization logic.
The attacker exploits Java's trust that the stream accurately represents what it claims to contain.
Key Takeaway: Never deserialize data from untrusted sources without validation. The serialization stream can be manipulated to instantiate any Serializable class on your classpath, potentially bypassing security controls through type substitution and unexpected class behavior.