Server Side Templates Injections
Template injection allows an attacker to include template code into an existing (or not) template. A template engine makes designing HTML pages easier by using static template files which at runtime replaces variables/placeholders with actual values in the HTML pages
Summary
Tools
Methodology
ASP.NET Razor
Basic injection
Command execution
Expression Language EL
Basic injection
Code execution
Freemarker
Basic injection
Code execution
Groovy
Basic injection
Read/Create file
HTTP Request
Command execution
Sandbox bypass
Handlebars
Jade / Codepen
Java
Basic injection
Retrieve the system’s environment variables
Retrieve /etc/passwd
Jinja2
Basic injection
Template format
Debug Statement
Dump all used classes
Dump all config variables
Read remote file
Write into remote file
Remote Code Execution
Filter bypass
Jinjava
Basic injection
Command execution
Lessjs
Mako
Pebble
Basic injection
Code execution
Ruby
Basic injections
Retrieve /etc/passwd
List files and directories
Smarty
Twig
Basic injection
Template format
Arbitrary File Reading
Code execution
Velocity
References
Tools
Recommended tool: Tplmap e.g:
Methodology

ASP.NET Razor
Razor is a markup syntax that lets you embed server-based code (Visual Basic and C#) into web pages.
ASP.NET Razor - Basic injection
ASP.NET Razor - Command execution
Expression Language EL
Expression Language (EL) is mechanism that simplifies the accessibility of the data stored in Java bean component and other object like request, session and application, etc. There are many operators in JSP that are used in EL like arithmetic and logical operators to perform an expression. It was introduced in JSP 2.0
Expression Language EL - Basic injection
Expression Language EL - One-Liner injections not including code execution
Expression Language EL - Code Execution
Freemarker
Apache FreeMarker™ is a template engine: a Java library to generate text output (HTML web pages, e-mails, configuration files, source code, etc.) based on templates and changing data.
You can try your payloads at https://try.freemarker.apache.org
Freemarker - Basic injection
The template can be ${3*3} or the legacy #{3*3}.
Freemarker - Read File
Freemarker - Code execution
Freemarker - Sandbox bypass
⚠️ only works on Freemarker versions below 2.3.30
Groovy
Groovy - Basic injection
Refer to https://groovy-lang.org/syntax.html , but ${9*9} is the basic injection.
Groovy - Read and create File
Groovy - HTTP request:
Groovy - Command Execution
Groovy - Sandbox Bypass
or
Handlebars
Handlebars compiles templates into JavaScript functions.
Handlebars - Command Execution
Jade / Codepen
Java
Java - Basic injection
Java - Retrieve the system’s environment variables
Java - Retrieve /etc/passwd
Jinja2
Jinja2 is a full featured template engine for Python. It has full unicode support, an optional integrated sandboxed execution environment, widely used and BSD licensed.
Jinja2 - Basic injection
Jinja2 is used by Python Web Frameworks such as Django or Flask. The above injections have been tested on a Flask application.
Jinja2 - Template format
Jinja2 - Debug Statement
If the Debug Extension is enabled, a `
` tag will be available to dump the current context as well as the available filters and tests. This is useful to see what’s available to use in the template without setting up a debugger.
Source: https://jinja.palletsprojects.com/en/2.11.x/templates/#debug-statement
Jinja2 - Dump all used classes
Jinja2 - Dump all config variables
Jinja2 - Read remote file
Jinja2 - Write into remote file
Jinja2 - Remote Code Execution
Listen for connection
Exploit the SSTI by calling os.popen().read()
These payloads are context-free, and do not require anything, except being in a jinja2 Template object:
We can use these shorter payloads (this is the shorter payloads known yet):
Source @podalirius_ : https://podalirius.net/en/articles/python-vulnerabilities-code-execution-in-jinja-templates/
Exploit the SSTI by calling subprocess.Popen
⚠️ the number 396 will vary depending of the application.
Exploit the SSTI by calling Popen without guessing the offset
Simply modification of payload to clean up output and facilitate command input (https://twitter.com/SecGus/status/1198976764351066113) In another GET parameter include a variable named "input" that contains the command you want to run (For example: &input=ls)
Exploit the SSTI by writing an evil config file.
Jinja2 - Filter bypass
Bypassing _
Bypassing [ and ]
Bypassing |join
Bypassing most common filters ('.','_','|join','[',']','mro' and 'base') by https://twitter.com/SecGus:
Jinjava
Java-based template engine based on django template syntax, adapted to render jinja templates (at least the subset of jinja in use in HubSpot content).
Jinjava - Basic injection
Jinjava is an open source project developed by Hubspot, available at https://github.com/HubSpot/jinjava/
Jinjava - Command execution
Fixed by https://github.com/HubSpot/jinjava/pull/230
Lessjs
Less (which stands for Leaner Style Sheets) is a backwards-compatible language extension for CSS. This is the official documentation for Less, the language and Less.js, the JavaScript tool that converts your Less styles to CSS styles.
Lessjs - SSRF / LFI
Lessjs < v3 - Command Execution
Plugins
Lessjs plugins can be remotely included and are composed of Javascript which gets executed when the Less is transpiled.
or
version 2 example RCE plugin:
version 3 and above example RCE plugin
Mako
Mako is a template library written in Python. Conceptually, Mako is an embedded Python (i.e. Python Server Page) language, which refines the familiar ideas of componentized layout and inheritance to produce one of the most straightforward and flexible models available, while also maintaining close ties to Python calling and scoping semantics.
Direct access to os from TemplateNamespace:
Any of these payloads allows direct access to the os module
PoC :
Source @podalirius_ : https://podalirius.net/en/articles/python-context-free-payloads-in-mako-templates/
Pebble
Pebble is a Java templating engine inspired by Twig and similar to the Python Jinja Template Engine syntax. It features templates inheritance and easy-to-read syntax, ships with built-in autoescaping for security, and includes integrated support for internationalization.
Pebble - Basic injection
Pebble - Code execution
Old version of Pebble ( < version 3.0.9): {{ variable.getClass().forName('java.lang.Runtime').getRuntime().exec('ls -la') }}.
New version of Pebble :
Ruby
Ruby - Basic injections
ERB:
Slim:
Ruby - Retrieve /etc/passwd
Ruby - List files and directories
Ruby - Code execution
Execute code using SSTI for ERB engine.
Execute code using SSTI for Slim engine.
Smarty
Smarty is a template engine for PHP.
Twig
Twig is a modern template engine for PHP.
Twig - Basic injection
Twig - Template format
Twig - Arbitrary File Reading
Twig - Code execution
Example with an email passing FILTER_VALIDATE_EMAIL PHP.
Velocity
Velocity is a Java-based template engine. It permits web page designers to reference methods defined in Java code.
References
Last updated