Type Confusion and Object Substitution
You'll learn how attackers manipulate deserialization to swap expected object types with malicious ones.
What Is Type Confusion?
Type confusion occurs when an application expects to deserialize one object type but receives a completely different one. During deserialization (covered in lessons 1183-1188), the parser reconstructs objects from serialized data. If the application trusts the type information embedded in that data without validation, an attacker can substitute a dangerous class for a safe one.
Think of it like ordering a pizza delivery. You expect a pizza box, but if the delivery system doesn't verify contents, someone could send a box of snakes instead. Your door-opening routine (code expecting pizza) now handles something completely unexpected and potentially harmful.
How Object Substitution Works
Polymorphic deserialization allows subclasses to be used wherever parent classes are expected—a normal programming feature. Attackers exploit this by:
- Finding classes in the application that have dangerous methods (file operations, command execution)
- Crafting serialized data that claims to be a safe type but actually instantiates a malicious one
- Triggering automatic method calls (constructors, destructors, magic methods) during deserialization that execute attacker code
For example, where code expects a UserProfile object, an attacker might substitute a FileHandler object. When the deserialization process reconstructs it, the FileHandler's initialization code might read sensitive files or execute commands—even though the application never intended to use that class.
The Core Danger
The vulnerability exists because serialized data can specify which class to instantiate, and deserialization frameworks often trust this instruction blindly. Even if you never directly call certain classes, they can be invoked through this type substitution mechanism.
Key Takeaway: Type confusion exploits deserialization's trust in type metadata—attackers substitute unexpected classes to trigger dangerous code paths the application never intended to expose.