Skip to content
audiotale.net
Sign in
GuidesExplainer

What is PHP object injection (CWE-502)? How unserialize() gets exploited and how to fix it

A plain explanation of deserialization bugs like this week's vBulletin flaw: how gadget chains work, what the PHP manual and OWASP advise, and a checklist for your own code.

audiotale.net team

· 7 min read

Written by AI from the sources linked below. How we use AI

On this page
  1. In short
  2. Why this is in the news
  3. What serialization is
  4. How the attack works
  5. How to defend
  6. How to check your own code
  7. A harmless illustration
  8. Why the vBulletin fix works
  9. A short checklist
  10. Common mistakes
  11. Sources

In short

PHP object injection (CWE-502, "deserialization of untrusted data") happens when an application passes data it does not control to PHP's unserialize(). PHP rebuilds objects from that data and runs special methods on them, so an attacker who chooses the data can make the application create objects it never meant to and trigger code in classes already installed, called a "gadget chain". The reliable defences are to stop using unserialize() on anything an outsider can touch, use JSON instead, and where you cannot, restrict the classes with allowed_classes and sign the data. This week's critical vBulletin flaw is a current example.

Why this is in the news

This week researcher Duarte Santos published a proof of concept for CVE-2026-137330 ("vBulletin2Shell"), an unauthenticated code execution bug in self-hosted vBulletin that works through exactly this mechanism. We cover the affected versions and the fix in our article on CVE-2026-137330. This guide explains the underlying bug class so you can recognise it in your own code. Note that the vBulletin patch, as described by the researcher, makes unserialize() refuse to build any objects at all.

What serialization is

Serialization turns an in-memory value into a string so it can be stored or sent; deserialization rebuilds the value. MITRE's CWE-502 entry describes the weakness as a product that "deserializes untrusted data without sufficiently ensuring that the resulting data will be valid." In PHP, serialize() and unserialize() do this job for arrays, strings, numbers and also objects.

The trouble is that objects are not just data. A serialized object names a class, and when PHP rebuilds it, it creates an instance of that class. The PHP manual explains the consequence: unserialization can result in code being loaded and executed because of object instantiation and autoloading, and after the object is rebuilt PHP automatically calls __unserialize() or __wakeup() if the class defines one. The manual's own warning is blunt: do not pass untrusted user input to unserialize(), regardless of the allowed_classes option's value.

How the attack works

  1. The entry point. Somewhere the application reads a value from the request (a cookie, a form field, a setting stored in a field the user can edit) and passes it to unserialize().
  2. The attacker chooses the classes. By default unserialize() accepts any class (the PHP manual lists true, accept all classes, as the default when the option is omitted). The attacker names classes that already exist in the application or its libraries.
  3. A gadget chain. On its own, a class may do something harmless in a method PHP calls automatically when it rebuilds the object. Chained together, objects that hold references to other objects can pass attacker-controlled values from one method call to the next until one of them calls something dangerous, such as running a shell command. OWASP's Deserialization Cheat Sheet uses "gadget" for classes whose behaviour can be abused during deserialization, and warns that new gadget classes will keep being discovered.
  4. The result. In the vBulletin case, the researcher says the chain uses classes bundled with the product, including Guzzle components, and ends in calling an arbitrary PHP function, which is how a single request can run an operating system command.

This is why the vulnerability is a property of the whole application, not one line: the risk grows with every library you load, because each adds potential gadgets.

How to defend

The three authoritative references agree on the main points.

1. Do not deserialize untrusted data with native formats. OWASP's advice is to avoid native deserialization of untrusted data, since native formats have extra features that can be misused for denial of service, access control bypass or remote code execution. The PHP manual recommends JSON, with json_encode() and json_decode(), for data that leaves your system. OWASP also prefers pure data formats such as JSON or XML combined with data-transfer objects, while noting mistakes can still happen after parsing.

2. If you must use unserialize(), restrict it. The allowed_classes option takes an array of class names, false to accept no objects, or true to accept all. Classes that are not allowed become __PHP_Incomplete_Class instead of real objects:

// Accept no objects at all
$data = unserialize($string, ['allowed_classes' => false]);

// Accept only specific classes
$data = unserialize($string, ['allowed_classes' => ['App\\User', 'App\\Cart']]);

The manual states that matching is exact, per a user note, so interfaces and subclasses of an allowed class are not matched. The max_depth option, added in PHP 7.4 with a default of 4096, limits nesting to prevent stack overflows. Keep in mind the manual's warning above: these options reduce risk but do not make untrusted input safe.

3. Sign the data. The manual advises using hash_hmac() to validate externally stored serialized data. OWASP says that when the application knows beforehand which messages it must process, it should sign them and reject anything without a valid signature before deserializing. CWE-502 lists the same idea under "Sign or seal the data". The key point is order: verify the signature first, deserialize second.

4. Reduce what can be abused. CWE-502 lists limiting types and gadgets (keep unnecessary classes out of reach and allow only approved ones, though it says this alone is not sufficient), and populating a new object from validated data instead of deserializing directly. It also lists an application firewall, rated only moderate in effectiveness, because it may miss inputs or block legitimate requests. A firewall rule is a stop-gap while you patch, as in the vBulletin case, not a fix.

How to check your own code

  • Search your codebase and dependencies for unserialize( and for any helper function that wraps it. For each call, trace where the argument comes from. If any path starts at a request, a cookie, an uploaded file, a database field a user can write or a cache another system fills, treat it as untrusted.
  • Check how your framework stores sessions, queue jobs and cached objects; some use serialization by design, so make sure the storage is not writable by users and is signed.
  • Keep third-party packages up to date. Because gadget chains live in libraries, an old library in your vendor directory can supply the missing piece an attacker needs.
  • Add a test that fails if new unserialize() calls appear without an allowed_classes argument.

A harmless illustration

The idea is easier to see with a toy example. Suppose an application has this class for writing log lines, and it is loaded in the same process as the code that calls unserialize():

class LogWriter {
    public string $file = '/var/log/app.log';
    public string $buffer = '';
    public function __destruct() {
        file_put_contents($this->file, $this->buffer, FILE_APPEND);
    }
}

The class is perfectly fine in normal use. But if unserialize($_COOKIE['prefs']) is called without restrictions, an attacker can send a serialized LogWriter whose file and buffer properties they chose. When PHP cleans the object up, the destructor writes the attacker's text to the attacker's path. Real attacks chain several such classes together, and the PHP manual's warning about code being loaded through instantiation and autoloading is the general form of this effect. The fix for the toy example is the same as for the real thing: never hand untrusted bytes to unserialize(), or pass ['allowed_classes' => false] so no LogWriter can be created from outside.

Why the vBulletin fix works

According to the researcher's page, vBulletin's Patch Level 1 makes unserialize() refuse all objects on the vulnerable path. That matches the PHP manual's allowed_classes => false behaviour: with no classes allowed, the attacker cannot instantiate the gadgets, so no chain can start. It is the strictest option and only works when the data never needs real objects, which is usually true for settings and other plain values. Because vBulletin is closed source, we could not verify the code ourselves; this explanation rests on the researcher's description and on how the PHP option is documented.

A short checklist

  1. Can an outsider influence the string passed to unserialize()? If yes, change it or restrict it.
  2. Can the data be plain arrays and strings? Then use JSON, or allowed_classes => false.
  3. If objects are needed, list the exact classes, sign the payload with hash_hmac(), and verify before you deserialize.
  4. Keep libraries patched, since gadgets are usually found in them.
  5. Treat a firewall rule as a short-term bridge while you patch, not as the fix.

Common mistakes

  • Believing that a class allow-list alone is enough. Both CWE-502 and OWASP say allow-lists must be combined with other controls.
  • Switching to JSON but then rebuilding objects dynamically from fields the attacker names. OWASP notes security mistakes can still happen after parsing.
  • Signing the data with a secret that is also used elsewhere or stored in a place the attacker can read.
  • Checking the signature after deserializing instead of before.

Sources

Written by

audiotale.net team

The official account of the people who run audiotale.net: announcements, conversation starters and the newsroom's drafts. Posts written with AI say so. Every other account here is a real person.