Skip to content
RCE Write-up Advanced

Gadgets chain in WordPress

A new PHP object injection gadget chain was found in WordPress core versions 6.4 and later, using the WP_HTML_Token class's __destruct method to call arbitrary functions. An attacker who can trigger an unserialize vulnerability could use this chain to execute arbitrary code.

Maxime Rinaudo Follow

Published on fenrisk.com

Exploiting an unserialization vulnerability in WordPress never was a small issue. Unlike other PHP frameworks, and until very recently, WordPress was not known for hosting gadget chains.

Maxime Rinaudo (@MaxRio13), on fenrisk.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 5 min read.

Context

WordPress is a PHP application whose plugin and theme ecosystem relies heavily on object serialisation, so an unserialisation vulnerability reachable in a WordPress install is only as dangerous as the gadget chains available in the loaded code. For a long time WordPress was not known for hosting such chains, unlike other PHP frameworks. This article walks through the history of WordPress gadget chains and then presents a new, trivial chain found in the core during a recent intrusion where a wild unserialize was encountered and none of the known chains worked.

The chain described here lives in the WP_HTML_Token class, an internal class introduced in WordPress 6.4 and used by the HTML processor during HTML parsing for referring to tokens in the input HTML string. It is defined in wp-includes/html-api/class-wp-html-token.php.

Setup

The work is source code review of WordPress core, with the phpggc tool used to package and reference payloads. The relevant versions are:

Version Status in the article
below 3.6.1 first known chain (2013)
5.5.2 WooCommerce chain patched
6.3.2 RCE1 patched; described as the only version without a known gadget chain
6.4, 6.4.1 WP_HTML_Token chain present

The chain itself requires no plugin: WP_HTML_Token is loaded by the WordPress class loader in wp-settings.php, which makes it available in plugins and themes.

Finding it

The starting point was a real intrusion in which a wild unserialize was found. The three chains known at the time did not apply: the first is old and complex, the second (RCE1) had been patched in 6.3.2, and the third depends on the WooCommerce plugin being installed, which is not guaranteed. That left the question of whether a simpler chain existed in current core.

The answer was yes. WP_HTML_Token implements a __destruct magic method that directly calls call_user_func on the class variables, with no pivot at all:

From the original on fenrisk.com

The on_destroy and bookmark_name attributes are controlled during the unserialising process, which makes the exploitation quite trivial. The payload looks like:

From the original on fenrisk.com

Because the class is loaded by the WordPress class loader in wp-settings.php, it is available to plugins and themes:

From the original on fenrisk.com

Exploitation

Step 1: reach an unserialise sink

The attacker needs a code path in WordPress or one of its plugins or themes that passes attacker-controlled data to unserialize. This is the same precondition as for any PHP object injection; the article does not describe a specific vulnerable endpoint, only the wild unserialize encountered during the intrusion.

Step 2: build the serialised WP_HTML_Token object

The payload is a serialised WP_HTML_Token whose on_destroy and bookmark_name properties are set by the attacker. On destruction of the object, __destruct calls call_user_func with those controlled values, so the callable and its argument come straight from the serialised data. The exact payload is shown in the image above; the article does not reproduce it as text.

Step 3: trigger destruction and execute

The __destruct magic method directly calls call_user_func on the class variables, with no pivot. No intermediate object or property pivot is needed, which is what makes this chain the simplest one found in WordPress so far.

Root cause

The mistake is a magic method that trusts object state: WP_HTML_Token::__destruct passes class properties directly to call_user_func. Any class with such a method becomes a gadget as soon as it can be instantiated through unserialize, because the properties are attacker-controlled during unserialisation. The class being autoloaded from wp-settings.php widens the exposure to every plugin and theme running on the install.

Impact

The chain is packaged in phpggc as WordPress/RCE2. The article frames this in the context of a real intrusion where the existing chains failed, which is exactly the situation the new chain addresses.

Fix

The article does not state which change removed this gadget. It does note that, at the time of writing, the only WordPress version without a known gadget chain is 6.3.2, which predates the introduction of WP_HTML_Token in 6.4.

Variants to test

  1. Grep the codebase for magic methods (__destruct, __wakeup, __toString) that call call_user_func, call_user_func_array or similar with properties as arguments. A positive result is a class whose properties are set from serialised input and reach the callable.
  2. Check whether candidate gadget classes are autoloaded unconditionally, for example from a bootstrap file such as wp-settings.php. A positive result is the class being available in every plugin and theme context without extra includes.
  3. A positive result is a property passed to a callable during destruction or wakeup.
  4. Test the packaged payloads in phpggc (WordPress/RCE1, WordPress/RCE2, WordPress/P/WooCommerce/RCE1, WordPress/P/WooCommerce/RCE2) against the target version to see which apply. A positive result is code execution through a reachable unserialise sink.

History of the earlier chains

The first interesting gadget chain was found and presented in 2013 on versions below 3.6.1. It is triggered using the __toString PHP magic method of the WP_theme class. From this class, the execution flow finally executes the make_plural_form_function method from the translations module, which evaluates a portion of code controlled by the user during the unserialising process. The pivoting sequence is summarised in the schema below, taken from Sam Thomas's slides at https://fr.slideshare.net/_s_n_t/php-unserialization-vulnerabilities-what-are-we-missing.

From the original on fenrisk.com

This gadget chain is relatively difficult to find because of the pivots and the interaction between each object class involved.

The RCE1 chain was described on 13 October 2023 by the WPScan team at https://wpscan.com/blog/finding-a-rce-gadget-chain-in-wordpress-core/. It is also triggered using the __toString magic method of the WP_theme class, and from there the exploitation needed to pivot using the sequence summarised below:

From the original on fenrisk.com

This popchain is complex because of the number of pivots needed to execute the interesting final code portion. The serialised payload exploiting it speaks for itself:

From the original on fenrisk.com

That payload is available in phpggc as WordPress/RCE1. It was patched by WordPress since version 6.3.2 and is no longer present in the latest releases.

The WooCommerce chain is clearly easier to understand. It is triggered using the WC_Log_Handler_File WooCommerce class destructor and pivots via the Requests_Utility_FilteredIterator WordPress class to execute a vulnerable portion of code. It was patched in WordPress 5.5.2, and targets need the WooCommerce plugin installed for it to work. The payloads are available in phpggc as WordPress/P/WooCommerce/RCE1 and WordPress/P/WooCommerce/RCE2.

To sum up: only complex gadget chains were present in WordPress, another easier one was available if WooCommerce was installed, and none of them is available in the latest versions (6.3.2, 6.4 and 6.4.1). That is what motivated the search that found the simplest one.

From the original on fenrisk.com

Sometimes simple exploitation vectors can be found in source code even in major projects such as WordPress.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 11 min read.

What happened, in plain words

WordPress is one of the most widely used systems for building websites. It is written in a programming language called PHP, and people extend it with add-ons called plugins and themes. This article is about a class of bug called an unserialisation vulnerability, and about a particular trick, called a gadget chain, that turns such a bug into full remote code execution, meaning an attacker can make the website's server run commands of their choosing.

To understand the story you need one idea first. PHP can take a live object in memory (think of it as a small bundle of data plus the functions that know how to work with that data) and turn it into a piece of text. That text can later be turned back into the object. The turning-into-text step is called serialisation, and the turning-back step is called unserialisation. It is a bit like packing a piece of furniture into a flat box and later rebuilding it from the box. The text is just characters, so if a website ever takes text from a visitor and unserialises it, the visitor is effectively choosing what the rebuilt object looks like, including all of its internal data.

On its own, that is not yet dangerous. The danger comes from the fact that PHP objects can carry small automatic behaviours, called magic methods, that fire at certain moments, for example when the object is destroyed at the end of a request. If one of those automatic behaviours happens to do something powerful with the object's data, such as calling a function whose name is stored in that data, then an attacker who controls the data controls the function call. A chain of such objects, each one leading to the next, is called a gadget chain or pop chain. The individual classes are the gadgets; the chain is the route from the unserialise call to the dangerous final action.

For years, WordPress was not known for containing such chains, unlike other PHP frameworks. The article walks through the history of the chains that were found, and then presents a new and very simple one that was found in the core of WordPress during a real intrusion, where the attacker (or the defenders analysing the attack) had a live unserialise to work with and none of the known chains applied.

The new chain lives in a class called WP_HTML_Token. A class is a blueprint for objects; this particular blueprint is internal to WordPress, was introduced in version 6.4, and is used by the HTML processor while it parses HTML, to refer to tokens in the input HTML string. In everyday terms, when WordPress reads a chunk of HTML it breaks it into small labelled pieces (tokens), and this class is the little label object it uses for that job. The class is defined in the file wp-includes/html-api/class-wp-html-token.php.

Setup

The work described is source code review of WordPress core: reading the published PHP source of WordPress looking for dangerous patterns, rather than attacking a live site blindly. The phpggc tool is used to package and reference the payloads. A payload here means the exact piece of serialised text that, when unserialised, builds the chain of objects the attacker wants.

The relevant versions mentioned in the article are:

Version Status in the article
below 3.6.1 first known chain (2013)
5.5.2 WooCommerce chain patched
6.3.2 RCE1 patched; described as the only version without a known gadget chain
6.4, 6.4.1 WP_HTML_Token chain present

A version number here is just the label WordPress gives each release, like the edition number of a book. "Patched" means the developers changed the code so the trick no longer works.

A sink is the place in the code where the dangerous operation finally happens; here it is a place where data controlled by an attacker reaches the PHP function unserialize. The chain itself requires no plugin: WP_HTML_Token is loaded by the WordPress class loader in wp-settings.php. The class loader is the mechanism that automatically fetches the definition of a class when some code first needs it, and wp-settings.php is one of the bootstrap files WordPress runs on every request. Because of that, the class is available in plugins and themes too, not only in core.

Finding it

The starting point was a real intrusion in which a live unserialize was found. The three chains known at the time did not apply: the first is old and complex, the second (RCE1) had been patched in 6.3.2, and the third depends on the WooCommerce plugin being installed, which is not guaranteed. That left the question of whether a simpler chain existed in current core.

The answer was yes. WP_HTML_Token implements a __destruct magic method that directly calls call_user_func on the class variables, with no pivot at all. Two terms need unpacking. __destruct is the magic method PHP runs automatically when an object is no longer needed, for example at the end of a request; think of it as the clean-up routine that runs when you throw the flat-pack furniture away. call_user_func is a PHP function that takes the name of another function (or a callable) and runs it. A pivot, in this context, is an intermediate object used to hop from one gadget to the next; here there is none, the dangerous call happens immediately.

From the original on fenrisk.com

The on_destroy and bookmark_name attributes are controlled during the unserialising process, which makes the exploitation quite trivial. An attribute here is simply a named value stored inside the object, like a field on a form. Because the attacker supplies the serialised text, the attacker chooses what those two fields contain, and the destructor passes them to call_user_func. The payload looks like:

From the original on fenrisk.com

Because the class is loaded by the WordPress class loader in wp-settings.php, it is available to plugins and themes:

From the original on fenrisk.com

Exploitation

Step 1: reach an unserialise sink

The attacker needs a code path in WordPress or one of its plugins or themes that passes attacker-controlled data to unserialize. This is the same precondition as for any PHP object injection. The article does not describe a specific vulnerable endpoint, only the live unserialize encountered during the intrusion. In plain terms: somewhere on the site, text supplied from outside is fed into the rebuild-from-text step, and that somewhere is the doorway.

Step 2: build the serialised WP_HTML_Token object

The payload is a serialised WP_HTML_Token whose on_destroy and bookmark_name properties are set by the attacker. On destruction of the object, __destruct calls call_user_func with those controlled values, so the callable and its argument come straight from the serialised data. The exact payload is shown in the image above; the article does not reproduce it as text.

Step 3: trigger destruction and execute

The __destruct magic method directly calls call_user_func on the class variables, with no pivot. "Goes out of scope" means the object is no longer referenced by any part of the running program, so PHP cleans it up. No intermediate object or property pivot is needed, which is what makes this chain the simplest one found in WordPress so far.

Root cause

The mistake is a magic method that trusts object state: WP_HTML_Token::__destruct passes class properties directly to call_user_func. Any class with such a method becomes a gadget as soon as it can be instantiated through unserialize, because the properties are attacker-controlled during unserialisation. The class being autoloaded from wp-settings.php widens the exposure to every plugin and theme running on the install.

In everyday terms: the clean-up routine was written assuming that the object's fields were set by trustworthy code, but unserialisation lets a stranger fill in those fields. The routine then does exactly what the stranger wrote.

Impact

The chain is packaged in phpggc as WordPress/RCE2. The article frames this in the context of a real intrusion where the existing chains failed, which is exactly the situation the new chain addresses.

Fix

The article does not state which change removed this gadget. It does note that, at the time of writing, the only WordPress version without a known gadget chain is 6.3.2, which predates the introduction of WP_HTML_Token in 6.4.

Variants to test

  1. Grep the codebase for magic methods (__destruct, __wakeup, __toString) that call call_user_func, call_user_func_array or similar with properties as arguments. A positive result is a class whose properties are set from serialised input and reach the callable.
  2. Check whether candidate gadget classes are autoloaded unconditionally, for example from a bootstrap file such as wp-settings.php. A positive result is the class being available in every plugin and theme context without extra includes.
  3. A positive result is a property passed to a callable during destruction or wakeup.
  4. Test the packaged payloads in phpggc (WordPress/RCE1, WordPress/RCE2, WordPress/P/WooCommerce/RCE1, WordPress/P/WooCommerce/RCE2) against the target version to see which apply. A positive result is code execution through a reachable unserialise sink.

History of the earlier chains

The first interesting gadget chain was found and presented in 2013 on versions below 3.6.1. It is triggered using the __toString PHP magic method of the WP_theme class. __toString is the magic method PHP runs when an object is used where text is expected, for example when it is concatenated into a string. From this class, the execution flow finally executes the make_plural_form_function method from the translations module, which evaluates a portion of code controlled by the user during the unserialising process. The pivoting sequence is summarised in the schema below, taken from Sam Thomas's slides at https://fr.slideshare.net/_s_n_t/php-unserialization-vulnerabilities-what-are-we-missing.

From the original on fenrisk.com

This gadget chain is relatively difficult to find because of the pivots and the interaction between each object class involved. A schema here is a diagram of boxes and arrows showing which object leads to which.

The RCE1 chain was described on 13 October 2023 by the WPScan team at https://wpscan.com/blog/finding-a-rce-gadget-chain-in-wordpress-core/. It is also triggered using the __toString magic method of the WP_theme class, and from there the exploitation needed to pivot using the sequence summarised below:

From the original on fenrisk.com

This popchain is complex because of the number of pivots needed to execute the interesting final code portion. The serialised payload exploiting it speaks for itself:

From the original on fenrisk.com

That payload is available in phpggc as WordPress/RCE1. It was patched by WordPress since version 6.3.2 and is no longer present in the latest releases.

The WooCommerce chain is clearly easier to understand. WooCommerce is a popular plugin that turns WordPress into an online shop. This chain is triggered using the WC_Log_Handler_File WooCommerce class destructor and pivots via the Requests_Utility_FilteredIterator WordPress class to execute a vulnerable portion of code. It was patched in WordPress 5.5.2, and targets need the WooCommerce plugin installed for it to work. The payloads are available in phpggc as WordPress/P/WooCommerce/RCE1 and WordPress/P/WooCommerce/RCE2.

To sum up: only complex gadget chains were present in WordPress, another easier one was available if WooCommerce was installed, and none of them is available in the latest versions (6.3.2, 6.4 and 6.4.1). That is what motivated the search that found the simplest one.

From the original on fenrisk.com

Sometimes simple exploitation vectors can be found in source code even in major projects such as WordPress.

Glossary

  • PHP: the programming language WordPress is written in.
  • WordPress: a widely used system for building websites, extended through plugins and themes.
  • Plugin: an add-on that adds features to WordPress.
  • Theme: an add-on that controls how a WordPress site looks.
  • Class: a blueprint in code that describes what an object holds and what it can do.
  • Object: a live instance of a class, holding data and able to run the class's functions.
  • Property / attribute: a named value stored inside an object.
  • Serialisation: turning a live object into text so it can be stored or sent.
  • Unserialisation: turning that text back into a live object.
  • Unserialise sink: a place in the code where attacker-controlled data reaches the unserialize function.
  • PHP object injection: the general bug class where an attacker controls the object rebuilt by unserialisation.
  • Magic method: a function PHP runs automatically at a set moment, such as __destruct on clean-up or __toString when an object is used as text.
  • __destruct: the magic method PHP runs when an object is no longer needed.
  • __toString: the magic method PHP runs when an object is used where text is expected.
  • __wakeup: a magic method PHP runs when an object is rebuilt during unserialisation.
  • call_user_func: a PHP function that runs another function named by its argument.
  • Gadget: a class whose automatic behaviour can be abused as one step in an attack.
  • Gadget chain / pop chain: a sequence of gadgets that leads from an unserialise call to a dangerous action.
  • Pivot: an intermediate hop from one gadget to the next inside a chain.
  • Remote code execution (RCE): making a server run commands chosen by an attacker.
  • Payload: the exact data an attacker sends to trigger the bug.
  • Class loader / autoload: the mechanism that fetches a class definition automatically when code first needs it.
  • Bootstrap file: a file run at the start of every request, here wp-settings.php.
  • Source code review: reading a program's published code to find weaknesses.
  • Grep: a command-line tool for searching text, used here to search source code.
  • Patch: a code change that closes a weakness.
  • Version: the label of a particular release of a piece of software.
  • phpggc: a tool that packages and references gadget-chain payloads.
  • WooCommerce: a plugin that turns WordPress into an online shop.
  • Schema: a diagram showing how the steps of a chain connect.

More by Maxime Rinaudo

View all

1 more disclosed report, best first.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.