Join our Newsletter — 33% off our NHI Course
Home› FAQ› Cyber Security› What happens when an attacker can build a…
Cyber Security

What happens when an attacker can build a gadget chain from classes already present in a PHP application?

← Back to all FAQ
By NHI Mgmt Group Editorial Team Updated September 24, 2026 Domain: Cyber Security

If the attacker can assemble a usable gadget chain, deserialization can move from a parsing flaw to a practical exploit. Existing classes may trigger file writes, file deletion, or other dangerous behavior through destructors and related magic methods. In the worst case, that chain can create a web shell, overwrite files, or execute arbitrary server-side code.

How a Gadget Chain Turns a PHP Bug into a Real Exploit

In PHP, the dangerous part is not just that deserialization accepts attacker-controlled data. The real break happens when the application already contains classes whose magic methods can be combined into a sequence of useful side effects. Once that sequence exists, the attacker can often turn a parsing weakness into file manipulation, command execution, or another reliable post-deserialization action.

That is why exploitability depends so heavily on the application’s class inventory. A “safe-looking” codebase can still be vulnerable if it ships with libraries or custom classes that expose destructive behavior through destructors, wakeup handlers, or other magic methods. The attacker does not need new code, only a chain that links existing behaviors in the right order.

What Makes the Chain Dangerous in Practice

A gadget chain is dangerous because each class in the chain contributes a small, legitimate behavior that becomes harmful when sequenced by the attacker. One object may influence a property, another may trigger a file write, and a later cleanup routine may fire automatically during garbage collection or request shutdown. The vulnerability is therefore not a single method, but the interaction between classes and runtime lifecycle.

This is why deserialization issues often become severe when developers assume the application is only “loading objects.” In reality, object hydration can activate code paths that were never meant to process untrusted input. If those code paths can reach filesystem writes, deletes, includes, or shell-adjacent behavior, the boundary between input parsing and execution disappears.

When the chain is good enough, the impact is typically application compromise rather than a narrow data error. The attacker may overwrite configuration files, plant a web shell, change application state, or force the server to execute attacker-controlled logic through an unintended path. The exact outcome depends on what the available classes can do, not on the deserialization bug alone.

Why Class Inventory and Runtime Behavior Matter

Exploitability comes from what is already present in the application, including framework code, helper libraries, and custom business objects. Two applications with the same deserialization flaw can have very different exposure depending on whether they include gadgets that reach the filesystem, invoke dynamic loading, or otherwise turn object state into side effects. That is why exploit development starts with class discovery, not with payload guessing.

In practice, defenders should treat the presence of rich object graphs as an amplification factor. The more classes with implicit behavior the application loads, the more likely an attacker can assemble a useful chain. Even if individual classes are not obviously dangerous in isolation, their combined lifecycle behavior can create an exploit path that is hard to notice during ordinary testing.

For background on how real-world compromise often hinges on existing privileged material and reusable application behavior, see The 52 NHI Breaches Report, which illustrates how attackers turn legitimate access paths into abuse at scale. For broader adversary technique mapping, the MITRE ATT&CK Enterprise Matrix is useful for understanding how initial access can progress into execution and privilege escalation.

Risk and Threat Considerations

The risk is not limited to “unsafe deserialization” as a code smell. Once a usable gadget chain exists, the attack can become deterministic and remotely exploitable, especially when the payload can trigger filesystem writes or command execution without further interaction. That shifts the issue from theoretical input corruption to practical compromise of the application and possibly the host.

Failure mechanism: The attacker supplies serialized data that instantiates existing classes in an order that triggers magic methods and other implicit behaviors, converting object hydration into file or code execution side effects.

Impact: The application may be used to write a web shell, overwrite files, delete critical content, or execute arbitrary server-side code, often with the privileges of the runtime account.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

MITRE ATT&CK addresses the attack and risk surface, while OWASP ASVS, OWASP SAMM and NIST SP 800-53 Rev 5 set the governance and control requirements practitioners need to meet.

FrameworkControl / ReferenceRelevance
MITRE ATT&CKT1059 — Command and Scripting InterpreterGadget chains can culminate in server-side code execution.
Recommendation — Map exploit paths to code-execution techniques and hunt for interpreter abuse.
OWASP ASVSV5 — File HandlingThe chain often abuses file writes, deletes, or includes.
V15 — Secure Coding and ArchitectureUnsafe object lifecycle behavior is a design-level application risk.
Recommendation — Verify file operations cannot be reached from untrusted object data. Remove unsafe deserialization paths and restrict object instantiation from untrusted input.
OWASP SAMMSecurity ArchitectureThe issue is driven by design choices in application object models.
Recommendation — Review component behaviors and reduce implicit side effects in object lifecycle design.
NIST SP 800-53 Rev 5SI-10 — Input ValidationUntrusted serialized input needs strict validation or avoidance.
Recommendation — Reject or eliminate deserialization of attacker-controlled data.

Practitioner Guidance

What to verify: Identify every class reachable during deserialization and trace which magic methods can run automatically, especially destructors and wakeup-style handlers. The important question is not whether a class looks dangerous alone, but whether its behavior becomes dangerous when chained with others.

Common mistake: Teams often test only the deserialization entry point and miss the downstream object lifecycle. If the codebase or its dependencies include file-write, file-include, process, or cleanup behavior, assume the attack surface is broader than the parser itself.

What good looks like: A hardened application should either avoid unsafe deserialization entirely or constrain the object model so attacker-controlled input cannot instantiate impactful classes. The safest posture is to make class availability, side effects, and trust boundaries explicit rather than relying on “benign” library behavior.

Practitioner takeaway: In PHP deserialization issues, the exploit is usually born from composition, not a single bad method. If an attacker can chain existing classes into meaningful side effects, treat the application as potentially exploitable until the object graph is reduced and untrusted deserialization is removed or tightly constrained.

Deepen Your Knowledge

Sign up to our weekly newsletter — get 33% off our NHI Foundation Level Course

    NHIMG Editorial Note
    Reviewed and updated by the NHIMG editorial team on September 24, 2026.
    NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org