# Gadget Chains
> A sequence of methods from classes already on the target's classpath that, linked through deserialization callbacks, carries attacker-controlled data from a deserialization entry point to a dangerous sink — turning "the attacker chose the object type" into code execution without injecting new code.
## The idea
Insecure deserialization only lets an attacker name which types get reconstructed. A gadget chain is Property-Oriented Programming (POP): each gadget is benign on its own, but the deserializer's own callbacks invoke one method which invokes the next, until a sink does something dangerous. Nothing new is injected — only existing, legitimate code is reused in an unexpected order (PentesterLab, Frohoff & Lawrence's 2015 "Marshalling Pickles"). The concept is language-independent; only the entry points and sinks differ.
## Anatomy (Gigamon / Ian Haken taxonomy)
- **Kick-off (trigger) gadget** — the first method the deserializer calls implicitly on an attacker-named object: a magic method (`readObject`, `__wakeup`, `__destruct`, Ruby `marshal_load`/`_load`) or a container rebuilding itself (`HashMap` re-hashing keys → `hashCode`/`equals`, `TreeMap`/`PriorityQueue` → comparators).
- **Helper / bridge gadgets** — glue that forwards control and data along the graph (reflection, dynamic proxies, `InvokerTransformer`-style indirection).
- **Bypass gadget** — triggers a nested deserialization on an unfiltered stream, defeating a shallow filter.
- **Sink** — the dangerous operation: process execution, outbound JNDI, remote/ local bytecode loading, file write, SSRF.
## Per-language entry points and sinks
| Language | Common kick-offs | Representative sinks |
|---|---|---|
| Java | `readObject`, collection re-hash, comparators | JRE-internal bytecode-bearing templates, outbound JNDI, `URLClassLoader`, JDBC drivers; `URLDNS` for detection only |
| PHP | `__wakeup`, `__destruct`, `phar://` metadata | file write, `eval`/command sinks via framework POP chains (Monolog, Guzzle, Laravel) |
| Python | `__reduce__`, `__setstate__` | any importable callable (stdlib suffices) |
| Ruby | `marshal_load`, `_load`, `_load_data`, and implicit `hash`/`eql?` | `eval`, `Kernel.load`, gem/spec fetchers writing files |
| .NET | setter / type-converter invocation on named types | process start, remote assembly load |
The chain's existence is a property of the classpath: the same deserialization bug yields RCE on one deployment and nothing on another. A blocklist implicitly trusts every class it did not name — UltraRed built a working chain from Oracle Coherence + Apache Xalan classes that a custom blocklist never considered. Ruby's history (Trail of Bits "Marshal Madness," elttam's universal chains and "kick-off gadgets" work) shows a decade of patch-and-bypass, with implicit kick-offs (`hash`, `eql?`) least likely to be patched because they are core language features.
## Tooling (payload generation / discovery)
- **Payload generators**: `ysoserial` (Java), `ysoserial.net` (.NET), `phpggc` (PHP). These carry curated chains tied to specific library versions — used by defenders to validate findings and build detections.
- **Discovery / static analysis**: GadgetInspector, Tabby, Crystallizer, ODDFuzz, and FLASH (USENIX 2025, deserialization-guided call-graph construction — 90 new chains, 10 CVEs); Gleipner benchmarks detectors. Mandiant's HeySerial/CheckYoself generate and validate hunting rules.
- **AI-assisted** discovery has collapsed the cost of finding novel chains — see [[AI-Accelerated Gadget Discovery]].
## Defensive implication
Because a clean result from a fixed payload library only says "none of these matched this classpath," blocklists and denylists age badly as new chains appear. Durable controls are: do not deserialize untrusted native formats; allowlist exact expected types at the deserializer (JEP 290 `ObjectInputFilter` with default-deny, PHP `allowed_classes`, restricted `Unpickler`); keep gadget-bearing libraries patched; and prove findings out-of-band rather than by response. See [[Insecure Deserialization]].
## Related
- [[Insecure Deserialization]]
- [[Java Deserialization]]
- [[PHP Object Injection]]
- [[Python Pickle Deserialization]]
- [[NET Deserialization|.NET Deserialization]]
- [[AI-Accelerated Gadget Discovery]]
- [[Remote Code Execution]]
## Sources
- [[raw/pentesterlab.com/deserialization-gadget-chain-definition-3781|Deserialization Gadget Chain Definition]]
- [[raw/medium.com/what-actually-is-a-deserialization-gadget-chain-3795|What Actually Is a Deserialization Gadget Chain?]]
- [[raw/klogixsecurity.com/java-deserialization-gadget-chains-explained-3793|Java Deserialization Gadget Chains Explained]]
- [[raw/i.blackhat.com/automated-discovery-of-deserialization-gadget-chains-black-hat-4520|Automated Discovery of Deserialization Gadget Chains (Black Hat)]]
- [[raw/usenix.org/precise-and-effective-gadget-chain-mining-through-deserialization-guid-4949|Precise and Effective Gadget Chain Mining (FLASH, USENIX)]]
- [[raw/ultrared.ai/the-classes-the-blocklist-forgot-chaining-oracle-coherence-and-xalan-i-13776|The classes the blocklist forgot]]
- [[raw/blog.trailofbits.com/marshal-madness-a-brief-history-of-ruby-deserialization-exploits-3799|Marshal Madness: Ruby Deserialization Exploits]]
- [[raw/elttam.com/ruby-marshal-kick-off-gadgets-elttam-11772|Ruby Marshal Kick-off Gadgets]]
- [[raw/cloud.google.com/now-you-serial-now-you-don-t-systematically-hunting-for-deserializatio-2783|Systematically Hunting for Deserialization Exploits (Mandiant)]]
- [[raw/arxiv.org/deserialization-gadget-chains-in-android-an-in-depth-study-3794|Deserialization Gadget Chains in Android]]
- [[raw/dl.acm.org/gleipner-a-benchmark-for-gadget-chain-detection-in-java-deserializatio-4950|Gleipner: Benchmark for Gadget Chain Detection]]