Gadget Chains and Object Injection
You'll learn how attackers exploit legitimate classes to build malicious payloads during deserialization.
The Core Idea
In the previous lesson, you learned that deserialization can be an attack vector. But here's the twist: attackers don't typically inject new code. Instead, they cleverly chain together existing classes already in your application or libraries to achieve malicious outcomes. This technique is called a "gadget chain."
Think of it like building a Rube Goldberg machine: each legitimate object (a "gadget") performs its normal function, but when chained together in a specific sequence during deserialization, they trigger dangerous side effects—file deletion, remote code execution, or data exfiltration.
How Gadget Chains Work
When you call readObject(), Java deserializes the object graph and invokes custom readObject() methods for each class. Attackers craft serialized data that includes:
- Entry point gadget: A class whose
readObject()calls methods on other objects - Intermediate gadgets: Classes that transform or forward calls
- Sink gadget: A class that performs the dangerous action (file I/O, reflection, etc.)
For example, a deserialized HashMap might trigger hashCode() on its keys, which calls toString() on another object, which invokes exec() on a runtime command—all using legitimate, already present classes.
Why This Is Dangerous
You don't need malicious code in your codebase. Popular libraries (like Apache Commons Collections) have been exploited because their innocent classes can be weaponized when combined. The attacker only needs to know which classes are on your classpath and craft the right serialized byte stream.
Key Takeaway: Gadget chains exploit legitimate classes by chaining their behaviors during deserialization, turning normal operations into attack vectors without introducing new code.