Gadgets chain in Laravel
The article describes a PHP object injection gadget chain in Laravel 10.34 that turns an unserialise call into remote command execution. An attacker who can pass a crafted serialised object to unserialize could chain magic methods to run arbitrary system commands.
Published on fenrisk.com
As we have seen in the previous article about wordpress gadgets, very simple gadget chains can be found in major projects. But sometimes finding popchain may be more difficult.
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.
Context
Laravel is a widely used PHP framework, and this article looks at a PHP POP (property-oriented programming) gadget chain found in its source code. The chain matters because PHP unserialization of attacker-controlled data is a recurring entry point: if an application passes untrusted input to unserialize(), an attacker who can supply a serialised object graph can steer the magic methods that PHP calls implicitly during that process.
The goal of the research was to turn such a chain into remote command execution. The chain was tested on Laravel version 10.34 and was later added to the phpggc tool as Laravel/RCE19.
This is the second part of a two-part series on PHP gadget chains; the first part covered WordPress.
Setup
The work is a source code review of the Laravel project on version 10.34, with the aim of building a serialised payload that executes a system command when unserialized. The chain matters when the target application unserializes data the attacker can control. The report does not describe any specific application, account or role.
Finding it
Serializing generates a storage representation of an object state; unserializing rebuilds an object from that representation. When an object implements magic methods, PHP calls them implicitly depending on the method specificity, and that is what a POP chain abuses.
While reading the Laravel source, the researcher found a class, InvokableComponentVariable, that implements an __invoke magic method:

This __invoke method executes call_user_func on a class attribute named callable, which can be controlled during the unserializing process. The problem is reachability: __invoke is only called implicitly when the object is handled as a function, and no way was found to trigger it directly. The researcher noticed that the __call magic method executes __invoke, so the next step was to find a way to reach __call.
Exploitation
Step 1: trigger __call from a __destruct
To exploit the __invoke code, the chain needs a magic method that is implicitly called during unserialization and that can trigger __call on the target class. Three candidates were considered: __destruct, __wakeup and __unserialize (the last called by PHP > 7.4).
__call is implicitly executed when an inaccessible instance method of an object is invoked. The Sleep class allows exactly that:

Sleep implements a __destruct method that calls copy() on the object stored in its duration class attribute. If a serialised Sleep instance is built with an InvokableComponentVariable instance as its duration attribute, unserializing it makes the destructor call InvokableComponentVariable->copy(), a method that does not exist. That triggers __call, which in turn reaches the __invoke destination. The result is a call_user_func on a single controlled parameter. For instance, it is already possible to execute a phpinfo:

At this point an arbitrary method can be called, but arbitrary system commands cannot yet be executed, because the parameters of the called function cannot be controlled.
Step 2: abuse the callback forms of call_user_func
call_user_func offers several ways to be called. It also accepts its parameter as an Array: the first position requires an instance of a callable, and the second is the method to call on that object:

It also accepts, as callback, the representation of the namespace and the class name of the targeted method:

This allows any callable method of loaded classes to be called, using either the namespace representation NAMESPACE\Class::Method() or the array representation [Callable, 'method']. The restriction remains that the chosen method receives no parameter.
Step 3: find a method that acts on controlled state
To go from calling an arbitrary method without parameters to executing arbitrary code during unserialization, the chain needs a method that does something vulnerable with controlled input, such as class attributes or php://input.
A quick search turned up the Terminal class of the Laravel\Prompts namespace, which implements two interesting methods. The first, exec, executes system commands using proc_open with the command parameter:

As noted, the method parameter cannot be controlled. However, Terminal also implements a restoreTty method that calls the exec instance method using the instance attribute initialTtyMode, without any sanitisation:

Step 4: build the final chain
The chain now triggers, during unserialization, a system call with a command injection. The payload calls, through call_user_func, an instance of the Terminal class whose initialTtyMode breaks the legitimate command and injects a rogue system command such as ;mycommand;#. The final gadget chain is shown below:

Root cause
The chain is built from magic methods that run implicitly during unserialization and pass attacker-controlled object state into dangerous sinks. InvokableComponentVariable::__invoke runs call_user_func on a controllable callable attribute; Sleep::__destruct calls copy() on a controllable duration object, which reaches __call and then __invoke; and Terminal::restoreTty passes the initialTtyMode attribute to exec without sanitisation, so a value containing shell metacharacters injects a command. The underlying precondition is that the application unserializes untrusted data.
Impact
When an application unserializes attacker-controlled data, the chain leads to remote command execution on the server. The report demonstrates the primitive with a phpinfo call and describes injecting a rogue system command; the vector was tested on Laravel version 10.34. The report does not give a CVSS score or a bounty.
Fix
The exploitation vector itself is published in the phpggc tool as Laravel/RCE19.
Variants to test
- Look for other classes with a
__destruct,__wakeupor__unserializemethod that calls a method on a controllable attribute, asSleepdoes withduration. A positive result is a destructor that reaches__callon an object whose class lacks the called method. - Look for classes whose
__callor__invokeforwards a controllable attribute tocall_user_func. A positive result is a callback that can be set to a namespace string such asNAMESPACE\Class::Method()or an array[Callable, 'method']. - Look for methods that pass an attribute to a shell sink without sanitisation, as
Terminal::restoreTtydoes withinitialTtyModeintoexec. A positive result is a command that runs with an injected;mycommand;#suffix. - The report notes that
__callis a much less common primitive than the usual ones.
What happened, in plain words
A security researcher read the source code of Laravel, a very popular toolkit that many websites are built with, and found a way to make a Laravel-based site run commands on its own server if that site takes data from a visitor and turns it back into objects without checking it first. The trick is a chain of small, harmless-looking pieces of code that, joined together, end up running a system command. The researcher tested it on Laravel version 10.34 and published it as a ready-made attack in a public tool called phpggc, under the name Laravel/RCE19.
This article is the second of a two-part series about this kind of chain in PHP projects; the first part covered WordPress. Below I walk through the same steps the researcher took, but from zero: what each piece of code does, why it was chosen, and how the pieces connect.
The background you need first
What Laravel and PHP are
PHP is a programming language used to build websites. When you open a page, the web server runs PHP code and sends back the result. Laravel is a large collection of ready-made PHP code (a framework) that developers use so they do not have to write everything from scratch. Because so many sites use it, a weakness inside Laravel can affect many sites at once.
What serializing and unserializing mean
Imagine you have a half-built Lego model on your desk and you want to post it to a friend. You cannot post the model as it is, so you write down a list of every brick and where it goes. That list is the serialised form: a flat text description of an object. Your friend reads the list and rebuilds the model. That rebuilding is unserializing.
In PHP, an object is a bundle of data plus functions that work on that data. Serializing turns an object into a text string; unserializing turns that string back into a live object. This is useful, for example, to store an object in a database or send it between two programs.
The danger: if a website unserializes text that a visitor supplied, the visitor is effectively writing the list of bricks. They can describe an object of a class the developer never intended to rebuild, with values the developer never intended to set.
What a magic method is
PHP has special function names, starting and ending with two underscores, that the language calls on its own at certain moments. These are called magic methods. You do not call them yourself; PHP calls them for you. Examples:
__destructruns when an object is about to be thrown away.__wakeupruns when an object is rebuilt from its serialised form.__unserializedoes a similar job on newer PHP versions (above 7.4).__callruns when someone asks an object to run a function it does not have.__invokeruns when an object is used as if it were a function.
Because these run automatically, they are the hinges an attacker pushes on. The researcher's goal was to find a set of objects whose automatic methods, triggered one after another during unserializing, end in something dangerous.
What a POP chain is
POP stands for property-oriented programming. It is a playful name for a serious idea: instead of writing code, the attacker builds a set of objects, sets their properties (their data fields) to chosen values, serializes the whole bundle, and hands it to the victim. When the victim unserializes it, the automatic methods fire in sequence, each one passing control to the next, like a row of dominoes. Each object is a gadget; the whole row is the chain.
What remote command execution means
Remote command execution (RCE) means an attacker can make the server run operating-system commands of their choosing, such as listing files or reading data. It is one of the most serious outcomes in web security, because the attacker is no longer limited to what the website's own features allow.
The precondition
The chain matters when the target application unserializes data the attacker can control. The report does not describe any specific application, account or role; it is a study of the framework's code.
Finding a vulnerable method
Serializing generates a storage representation of an object state; unserializing rebuilds an object from that representation. When an object implements magic methods, PHP calls them implicitly depending on the method specificity. That is exactly what a POP chain abuses.
While reading the Laravel source, the researcher found a class called InvokableComponentVariable that implements an __invoke magic method:

What this means, in plain words: this class has a data field named callable. When PHP treats an object of this class as if it were a function, the __invoke method runs, and it passes the value of callable to a built-in PHP function called call_user_func. call_user_func means "call this other function for me". If an attacker can set callable while unserializing, they choose what gets called.
The problem is reachability. __invoke only fires when the object is used as a function, and the researcher found no way to make that happen directly during unserializing. But they noticed something useful: the __call magic method of this class executes __invoke. So the next task was to find a way to reach __call.
Why this matters: a gadget is only useful if something else in the chain can pull its trigger. Finding the dangerous code is half the job; finding a way to reach it is the other half.
Exploitation
Step 1: trigger __call from a __destruct
To exploit the __invoke code, the chain needs a magic method that is implicitly called during unserialization and that can trigger __call on the target class. Three candidates were considered: __destruct, __wakeup and __unserialize (the last called by PHP > 7.4).
__call is implicitly executed when an inaccessible instance method of an object is invoked. In everyday terms: if you ask an object to do something it does not know how to do, PHP gives the object one last chance to handle the request through __call. The Sleep class allows exactly that:

What this means, in plain words: Sleep has a data field named duration. When a Sleep object is thrown away, its __destruct method runs and calls copy() on whatever object is stored in duration. If duration holds an InvokableComponentVariable object, and that class has no copy() method, PHP cannot find the method and falls back to __call. That is the trigger we needed.
So the plan is: build a serialised Sleep object whose duration field is an InvokableComponentVariable object. When the victim unserializes it, the destructor calls InvokableComponentVariable->copy(), a method that does not exist. That triggers __call, which in turn reaches the __invoke destination. The result is a call_user_func on a single controlled parameter. For instance, it is already possible to execute a phpinfo:

What this means, in plain words: phpinfo is a standard PHP function that prints a page of information about the server's PHP setup. It is harmless, but it proves the chain works: the attacker chose the function that ran. At this point an arbitrary method can be called, but arbitrary system commands cannot yet be executed, because the parameters of the called function cannot be controlled. The attacker can pick the function, but not what is passed into it.
Why this step was taken: it converts a piece of code that could not be reached into a piece that runs automatically the moment the payload is unserialized.
Step 2: abuse the callback forms of call_user_func
call_user_func offers several ways to be called. It also accepts its parameter as an Array: the first position requires an instance of a callable, and the second is the method to call on that object:

What this means, in plain words: instead of naming a single function, you can hand call_user_func a pair: an object, and the name of a method on that object. PHP then calls that method on that object. The attacker controls both halves through the serialised data.
It also accepts, as callback, the representation of the namespace and the class name of the targeted method:

What this means, in plain words: a namespace is a folder-like prefix that keeps class names from clashing, written with backslashes. Instead of an object, you can give call_user_func a text string of the form NAMESPACE\Class::Method(), and PHP will call that method on that class. This allows any callable method of loaded classes to be called, using either the namespace representation NAMESPACE\Class::Method() or the array representation [Callable, 'method'].
The restriction remains that the chosen method receives no parameter. That sounds limiting, and it is: the attacker can choose which method runs, but cannot hand it arguments. The next step is to find a method that does something dangerous using only data already stored inside the object.
Why this step was taken: it widens the set of reachable methods from "one function" to "any method of any loaded class", which is what makes the final gadget findable.
Step 3: find a method that acts on controlled state
To go from calling an arbitrary method without parameters to executing arbitrary code during unserialization, the chain needs a method that does something vulnerable with controlled input, such as class attributes or php://input. In plain terms: a method that takes a value stored in the object and passes it to something powerful, such as the operating system's command shell, without cleaning it first.
A quick search turned up the Terminal class of the Laravel\Prompts namespace, which implements two interesting methods. The first, exec, executes system commands using proc_open with the command parameter:

What this means, in plain words: exec is a method that runs an operating-system command. It uses proc_open, a PHP function that starts a separate process. If an attacker could choose the command, they would have command execution. But as noted, the method parameter cannot be controlled, so calling exec directly is not enough.
However, Terminal also implements a restoreTty method that calls the exec instance method using the instance attribute initialTtyMode, without any sanitisation:

What this means, in plain words: restoreTty takes the value stored in the object's initialTtyMode field and passes it straight into exec. "Without sanitisation" means nothing checks or cleans that value first. Since the attacker controls initialTtyMode through the serialised data, they now control the command, even though they cannot pass arguments to the method itself.
Why this step was taken: it turns "call any method with no arguments" into "call a method that reads its dangerous input from a field I control". That is the missing link.
Step 4: build the final chain
The chain now triggers, during unserialization, a system call with a command injection. The payload calls, through call_user_func, an instance of the Terminal class whose initialTtyMode breaks the legitimate command and injects a rogue system command such as ;mycommand;#. The final gadget chain is shown below:

What this means, in plain words: the semicolon ; ends the command the program meant to run and starts a new one; mycommand is the attacker's command; and # starts a comment, so anything the program appends after it is ignored. This is called command injection: slipping extra instructions into a value that is later handed to a shell. The full chain is: Sleep destructor calls a missing method, __call fires, __invoke fires, call_user_func calls Terminal::restoreTty, and restoreTty passes the attacker's initialTtyMode into exec, which runs the injected command.
Root cause
The chain is built from magic methods that run implicitly during unserialization and pass attacker-controlled object state into dangerous sinks. InvokableComponentVariable::__invoke runs call_user_func on a controllable callable attribute; Sleep::__destruct calls copy() on a controllable duration object, which reaches __call and then __invoke; and Terminal::restoreTty passes the initialTtyMode attribute to exec without sanitisation, so a value containing shell metacharacters injects a command. The underlying precondition is that the application unserializes untrusted data.
In plain words: each individual piece is reasonable on its own. A class that forwards a stored callback, a class that cleans up on destruction, a class that restores a terminal setting. The danger appears only when they are combined and fed attacker-chosen values.
Impact
When an application unserializes attacker-controlled data, the chain leads to remote command execution on the server. The report demonstrates the primitive with a phpinfo call and describes injecting a rogue system command; the vector was tested on Laravel version 10.34. The report does not give a CVSS score or a bounty.
In plain words: an attacker who can reach such an unserialize call could run commands on the server, not just read a page or change a setting.
Fix
The exploitation vector itself is published in the phpggc tool as Laravel/RCE19.
Variants to test
If you want to look for similar chains yourself, here is what the researcher's steps suggest looking for:
- Look for other classes with a
__destruct,__wakeupor__unserializemethod that calls a method on a controllable attribute, asSleepdoes withduration. A positive result is a destructor that reaches__callon an object whose class lacks the called method. - Look for classes whose
__callor__invokeforwards a controllable attribute tocall_user_func. A positive result is a callback that can be set to a namespace string such asNAMESPACE\Class::Method()or an array[Callable, 'method']. - Look for methods that pass an attribute to a shell sink without sanitisation, as
Terminal::restoreTtydoes withinitialTtyModeintoexec. A positive result is a command that runs with an injected;mycommand;#suffix. - The report notes that
__callis a much less common primitive than the usual ones.

Glossary
- PHP: a widely used programming language for websites; the server runs it to produce pages.
- Laravel: a popular framework (a large collection of ready-made PHP code) used to build web applications.
- Framework: a set of pre-written code and conventions that developers build on, instead of starting from nothing.
- Object: a bundle of data fields plus the functions that work on them, in a program.
- Class: the blueprint that describes what fields and functions an object of that kind has.
- Attribute / property / field: a named piece of data stored inside an object.
- Method: a function that belongs to a class or object.
- Serialise: turn a live object into a flat text representation that can be stored or sent.
- Unserialize: rebuild a live object from its serialised text.
- Magic method: a function with a name like
__destructor__callthat PHP calls automatically at certain moments, rather than the programmer calling it. __destruct: magic method that runs when an object is about to be discarded.__wakeup: magic method that runs when an object is rebuilt from serialised data.__unserialize: magic method that does a similar job to__wakeupon newer PHP versions (above 7.4).__call: magic method that runs when code asks an object to run a method it does not have.__invoke: magic method that runs when an object is used as if it were a function.- POP chain (property-oriented programming): a sequence of objects whose automatic methods, triggered during unserializing, pass control from one to the next until something dangerous happens.
- Gadget: one object in such a chain, useful because of what its automatic method does.
call_user_func: a PHP function that calls another function or method named by its argument.- Callback: a function or method handed to another piece of code so that it can be called later.
- Namespace: a folder-like prefix (written with backslashes) that keeps class names from clashing.
phpinfo: a standard PHP function that prints information about the PHP setup; harmless, used here to prove code execution.proc_open: a PHP function that starts a separate operating-system process.exec: here, a method that runs an operating-system command.- Sanitisation: cleaning or checking a value before using it, so that special characters cannot change its meaning.
- Command injection: slipping extra shell instructions into a value that is later handed to a command shell, often using
;to start a new command and#to comment out the rest. - Shell metacharacter: a character such as
;or#that has a special meaning to the command shell. - Remote command execution (RCE): an attacker's ability to make a server run operating-system commands of their choosing.
phpggc: a public tool that packages known PHP gadget chains, including this one as Laravel/RCE19.- CVSS: a standard scoring system for the severity of a vulnerability; the report gives no score.
- Bounty: a payment some organisations make to researchers who report security problems; the report mentions none.