Lesson 2 of 9 · 20m
The Anatomy of Object Injection
How malformed object graphs abuse constructors, magic methods, callbacks, and property writes long before business logic sees the object.
Most developers imagine exploitation happening after deserialization: “we get an object, then maybe we do something unsafe with it.” Real object injection usually happens during reconstruction. By the time application code receives the object, the attacker may already have written files, opened sockets, or executed commands.
Reconstruction is an eventful process
Different runtimes expose different hooks, but the pattern is consistent:
read bytes -> resolve class -> allocate object -> run reconstruction hooks -> populate fields -> build references -> return objectThose middle steps are the attack surface. A Java class can implement readObject. A PHP class can define __wakeup() or __destruct(). Python objects can implement __reduce__ or related pickling methods. .NET serializers may invoke setters and callbacks. The attacker chooses types and relationships specifically to make these hooks fire in a useful order.
A minimal behavioral example
Consider a PHP class that was meant for cache cleanup:
class CacheFile {
public $path;
public function __destruct() {
if ($this->path) {
unlink($this->path);
}
}
}Nothing in that class is malicious. But if unserialize() accepts attacker input, the attacker can create a CacheFile whose $path points at something important. The exploit does not need the application to call a method; object destruction at the end of the request is enough.
References are part of the weapon
Object graphs can contain cycles, shared references, proxies, and collections. Attackers use these properties to bypass naive filters and to place the right object in the right field. A blocklist looking for one class name misses the actual exploit because the malicious effect comes from a chain of otherwise unremarkable objects.
This is why scanning raw bytes for Runtime or eval is not a defense. Gadget chains rarely look like shellcode. They look like ordinary object types arranged in an unfortunate order.
The exploit window is before validation
A common broken design is:
obj = deserialize(body)
if not isinstance(obj, ExpectedType):
raise ValueError("bad input")The type check is too late. If reconstruction triggered behavior, the damage happened before isinstance ran. Validation of the envelope — signatures, format type, size, schema — must happen before object materialization.
Field notes
- Destructors, finalizers, and cleanup hooks are execution primitives in object-injection attacks.
- “We never call that dangerous method” is irrelevant when the serializer, garbage collector, or shutdown path calls it for you.
- Deep object graphs and cyclic references can also be used for denial of service even when code execution is impossible.
- Review constructor and magic-method behavior in every class reachable from an attacker-controlled type name.
Was this lesson helpful?