Web security

Insecure deserialization

Definition

Insecure deserialization is a vulnerability that occurs when an application rebuilds objects from attacker-controlled data, for example with unserialize() in PHP. The methods triggered automatically during this rebuilding can lead to file deletion or code execution.

Insecure deserialization, Web security

Serializing means turning structured data (an array, an object) into text so it can be stored or transmitted; deserializing is the reverse operation. The problem arises when the data to rebuild comes from the outside: a cookie, a form field, a URL parameter. MITRE classifies this weakness as CWE-502, “Deserialization of Untrusted Data”; in PHP, it is also called PHP Object Injection.

How the attack works in PHP

A PHP serialized string describes the type of what it contains. For example, O:4:"User":1:{s:4:"name";s:3:"Bob";} refers to an object of the User class with a name property. An attacker who controls this string can therefore ask unserialize() to create an object of any class loaded by the site, with whatever values they want.

That object becomes dangerous thanks to the magic methods PHP calls automatically: __wakeup() on reconstruction, __destruct() on destruction, __toString() when the object is used as text. By chaining classes already present in the site's code, whose methods call one another, the attacker builds a gadget chain (POP chain) that ends in a dangerous action: deleting a file, writing a PHP file, remote code execution.

Five-step PHP object injection: the attacker sends a forged serialized string in a cookie or a field, the application calls unserialize() and rebuilds an object of the chosen class, PHP automatically runs the __wakeup, __destruct or __toString magic methods, existing classes chain together into a POP gadget chain, and the final effect is file deletion, a PHP file being written or code execution

A WordPress example: GiveWP

Vulnerability CVE-2024-5932 in the GiveWP donation plugin, up to and including version 3.14.1, allowed an unauthenticated attacker to inject a PHP object through the give_title parameter. A gadget chain present in the plugin led to code execution and file deletion; the published CVSS 3.1 score is 10. Our article on PHP object injection covers this case and the mechanism in detail.

Deserialization is also useful to attackers once they are inside: the backdoor added in 2025 to the EssentialPlugin plugin portfolio relied on unserialize, as our article on supply chain attacks points out.

For WordPress: unserialize and maybe_unserialize

WordPress stores a lot of serialized data, especially in the options table and in metadata. The maybe_unserialize() function is used to read it back: if the data looks serialized (is_serialized()), it calls unserialize(), without restricting the allowed classes. It therefore provides no protection and must never receive data that comes from a visitor.

  • Exchange data as JSON: the PHP documentation recommends json_encode() and json_decode() for any data that passes through the user. JSON never creates objects of arbitrary classes.
  • Don't expect too much from allowed_classes: this unserialize() option limits which classes get instantiated, but the PHP manual states that untrusted data should not be passed to this function, whatever its value.
  • Sign whatever has to make the round trip: if serialized data really must travel through the browser, an HMAC signature verified before any reading prevents it from being tampered with.
  • Keep PHP up to date: since PHP 8.0, phar archive metadata is no longer deserialized automatically, which closed an attack path linked to file inclusion and wrappers.

On the administrator's side, the main defense is still updating the affected plugins, since the flaw is in their code. A web application firewall can block suspicious serialized strings in the meantime, and WP Feint Pro's vulnerability alerts tell you when an installed plugin is among the affected versions. WP Feint doesn't filter requests itself.

Further reading

  • Article PHP deserialization and object injection explained
  • Page Vulnerable plugin alerts

Sources

  1. MITRE CWE-502: Deserialization of Untrusted Data (external link, opens in a new tab)
  2. PHP Manual: unserialize() (external link, opens in a new tab)
  3. WordPress Developer Resources: maybe_unserialize() (external link, opens in a new tab)
  4. CVE Record: CVE-2024-5932 (external link, opens in a new tab)

All definitions

Start free, go Pro when you’re ready

The free version already hides WordPress and moves the login page. Pro adds aliases, two-factor authentication and monitoring.