Smarty Template Engine Multiple Sandbox Escape PHP Code Injection Vulnerabilities
Two sandbox escape vulnerabilities in Smarty Template Engine allow an attacker who can inject template syntax to execute arbitrary PHP code. The first uses the template_object super variable to access the Smarty instance and write files or disable security; the second injects code via the function name parameter during compilation.
srcincite.io
Published on srcincite.io
In this blog post we explore two different sandbox escape vulnerabilities discovered in the Smarty Template Engine that can be leveraged by a context dependant attacker to execute arbitrary code. Then we explore how these vulnerabilities can be applyed to some applications that attempt to use the engine in a secure…
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.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the sandbox does not properly restrict access to the Smarty instance via the template_object variable and fails to sanitise the function name when compiling template functions.
Impact
An attacker could execute arbitrary PHP code, leading to remote code execution, and in some applications this could be unauthenticated when chained with other vulnerabilities.
What the attacker needed
Attacker can inject template syntax; Smarty <= 3.1.38; sandbox enabled (default or hardened)
The fix
The vulnerabilities were fixed in Smarty 3.1.39 and later; temporary mitigations include disabling static classes and the template_object variable, but these are not complete.
How it was found
- Set up a page that creates a Smarty instance, enables security, and displays a template from a GET parameter.
- Send a request with a string resource containing template code that accesses the template_object and its smarty property.
- For the first vulnerability, use the Smarty instance to call a static method that writes a file to the compiled template path, then trigger the request again to include the file.
- For the second vulnerability, send a request with a function definition whose name contains PHP code, which gets injected into the compiled template.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
Smarty is a tool that lets websites mix HTML templates with PHP code, and it has a 'sandbox' to stop templates from running dangerous PHP. These bugs let someone who can put their own template code into a page break out of that sandbox and run commands on the server. The first bug gives access to the main Smarty object, and the second sneaks code into a function name that gets compiled.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.