# PHP Object Injection
> Passing attacker-controlled input to `unserialize()` (or a wrapper like WordPress `maybe_unserialize()`, or the implicit `phar://` metadata path), letting an attacker instantiate arbitrary already-defined classes whose magic methods run during or after deserialization.
## How it works
- **Serialized syntax is plaintext and self-describing.** An object is `O:<len>:"ClassName":<n>:{ <props> }`; an array is `a:<n>:{...}`; base64 of an object begins `Tzo`, of an array `YTo`. The leading letter is the type tag (`O`, `a`, `s`, `i`, `b`, `N`).
- **Object injection, not code injection.** `unserialize()` does not run attacker code directly; it reconstructs objects of classes the application can load, and PHP then calls their magic methods. `__wakeup()` runs during deserialization; `__destruct()` runs when the object is freed (at latest at request end), so rejecting the object after the fact does not prevent it. Other reachable magic methods include `__toString`, `__call`, `__get`, `__set`, `__unserialize`.
- **Property-Oriented Programming (POP).** Impact comes from chaining class methods already present in the app and its dependencies — see [[Gadget Chains]]. The gadget often lives in a plugin or library the developer never references directly; an autoloader will load it on demand, so auditing only your own controllers for `__wakeup` searches the wrong place (DedSec/Sonar).
- **`phar://` path.** Reading a crafted PHAR archive through almost any file function (`file_exists`, `getimagesize`, `fopen`, `file_get_contents`) deserializes the archive's serialized metadata — turning a file-path primitive into object injection without a direct `unserialize()` call (Sam Thomas, BH US-18). Requires getting the archive onto disk; file-upload filters (`getimagesize`, MIME, extension) are frequently satisfiable.
## Where to find it
`unserialize()` on cookies, POST fields, import files, cache/session stores; framework wrappers (WordPress `maybe_unserialize` on postmeta); `phar://` reachable via file functions. WordPress plugins are a steady source because option/postmeta values are serialized by design.
## Detection
- Decode cookies/params; a value beginning `O:` or `a:` (or `Tzo`/`YTo` in base64) is a PHP serialized value.
- A malformed copy that yields `unserialize(): Error at offset N of M bytes` proves a deserializer ran; note that a warning can be emitted while the HTTP status stays 200 if output was already sent. A generic error proves nothing. Out-of-band evidence is the reliable confirmation — see [[Insecure Deserialization]] and [[Out-of-Band Detection]].
- Static tools (SonarQube, Semgrep) flag `unserialize()` on tainted input and the magic-method sinks. `phpggc` carries POP chains for Laravel, Symfony, Monolog, Guzzle, WordPress and others for defender validation.
## Notable cases
| CVE / advisory | Target | Note |
|---|---|---|
| CVE-2022-21663 | WordPress core | Object injection reference case |
| CVE-2026-7647 | Profile Builder Pro | Unauth; chained a novel Monolog POP chain from another plugin |
| CVE-2026-59192 | CakePHP TwigView | `unserialize` Twig filter |
| GHSA-5xv5-3557-x7h5 | Masteriyo LMS | `maybe_unserialize()` in course importer |
| CVE-2026-124969 / various | Optimole, WPvivid, WP STAGING, QA Assistants, Document Library Pro, Testimonial Slider | WordPress plugin object injection / untrusted-data deserialization |
| CVE-2026-126714 "PHPAfterShell" | PHP file-session handler | Non-atomic `pwrite()`/`ftruncate()` in `ps_files_write` splices session files so leftover bytes reach `php_var_unserialize()` |
| CVE-2026-115560 (GHSA-658j-r8rw-q6mx) "phpAfterShell" | PHP session serializer mismatch | `php_binary`/`php_serialize` writer vs `php` reader via session upload progress smuggles object bytes into a session key; fixed in 8.2.36/8.3.37/8.4.28/8.5.13 ([[raw/netspi.com/phpaftershell-critical-php-vulnerabilities-cve-2026-115560-cve-2026-12-15870\|NetSPI]]) |
| CVE-2026-131754 | APCu Manager (WordPress plugin) ≤4.6.1 | Critical object injection from untrusted data ([[raw/github.com/apcu-manager-deserialization-of-untrusted-data-critical-15971\|GHSA-7jwg-jc4p-j4xh]]) |
Mautic (GHSA-5r63-8h35-j3mw), Shiyi-blog (CVE-2025-12305) and OpenEMR (<8.3.0) are further app-level cases. Vanilla Forums' historical `getimagesize`/`splitHash` unserialize bugs are classic `phar://` and type-juggling examples.
## Prevention
- Never pass user input to `unserialize()`. Prefer `json_decode`/`json_encode` for data-only interchange.
- If unavoidable, pass `['allowed_classes' => [...]]` (or `false` for arrays/scalars only); in testing this neutralised gadgets and even suppressed the autoloader call. Allowed classes still run their magic methods, so audit them.
- Keep the session serializer consistent across every reader/writer of a shared session store (FPM pools, vhosts, app overrides, CLI jobs); isolate session storage where apps need different formats, and expire or migrate session files written under a previous format. Patch PHP for the Fall-2026 session CVEs and disable `session.upload_progress.enabled` unless needed, in php.ini or the FPM pool: an application `ini_set()` runs too late ([[raw/netspi.com/phpaftershell-critical-php-vulnerabilities-cve-2026-115560-cve-2026-12-15870|NetSPI]]).
- Watch for type-juggling in comparisons on deserialized values (`==` / loose `strcmp`) — see [[Insecure Deserialization]].
## Related
- [[Insecure Deserialization]]
- [[Gadget Chains]]
- [[Remote Code Execution]]
- [[Unrestricted File Upload]]
## Sources
- [[raw/blog.sicuranext.com/exploiting-a-php-object-injection-in-profile-builder-pro-in-the-era-of-13931|Exploiting a PHP object injection in Profile Builder Pro]]
- [[raw/colibrisec.org/phpaftershell-cve-2026-126714-session-file-splice-and-deserialization-15685|PHPAfterShell (CVE-2026-126714)]]
- [[raw/depthfirst.com/phpaftershell-from-interrupted-session-writes-to-remote-code-execution-15526|phpAfterShell: interrupted session writes to RCE]]
- [[raw/frankhacks.blogspot.com/247ctf-meme-upload-service-hand-built-phar-meets-xxe-14787|247CTF Meme Upload Service — Hand-Built Phar Meets XXE]]
- [[raw/github.com/apcu-manager-deserialization-of-untrusted-data-critical-15971|APCu Manager deserialization of untrusted data (critical)]]
- [[raw/github.com/cakephp-twigview-unserialize-filter-allows-php-object-deserialization-14539|CakePHP TwigView unserialize filter]]
- [[raw/github.com/masteriyo-lms-wordpress-php-object-injection-15287|Masteriyo LMS WordPress PHP object injection]]
- [[raw/github.com/mautic-insecure-deserialization-in-segment-filters-and-custom-fields-12657|Mautic insecure deserialization]]
- [[raw/github.com/payloadsallthethings-php-deserialization-payloads-3802|PayloadsAllTheThings - PHP Deserialization Payloads]]
- [[raw/github.com/php-src-session-serializer-mismatch-can-lead-to-rce-phpaftershell-15686|php-src: session serializer mismatch]]
- [[raw/github.com/wp-staging-php-object-injection-15437|WP STAGING PHP Object Injection]]
- [[raw/jetpack.com/what-is-php-object-injection-an-in-depth-guide-3804|What is PHP Object Injection? An In-Depth Guide]]
- [[raw/medium.com/exploiting-php-deserialization-with-pop-chains-3803|Exploiting PHP Deserialization with POP Chains]]
- [[raw/netspi.com/phpaftershell-critical-php-vulnerabilities-cve-2026-115560-cve-2026-12-15870|phpAfterShell: Critical PHP Vulnerabilities (CVE-2026-115560 & CVE-2026-126714)]]
- [[raw/pentest-tools.com/how-to-exploit-phar-deserialization-vulnerability-4351|How to Exploit PHAR Deserialization Vulnerability]]
- [[raw/sonarsource.com/php-object-injection-research-3805|PHP Object Injection Research]]