Practice library / Web attacks
Lab · Medium · 2 hoursFree account
Server-Side Template Injection (SSTI)
Detect and exploit Jinja2, Twig, and Freemarker SSTI for RCE.
On this page 6 sections
Before you start
A template engine builds HTML by evaluating small expressions inside a page. When an application passes raw user input to Jinja2, Twig or FreeMarker, the engine runs that input as server code. Template-injection bugs in Flask and other frameworks have given attackers remote shells on production systems. In this lab, you detect SSTI with safe math probes, fingerprint the engine, and walk a Python object chain from a template string to full remote code execution (RCE).
Lab Objectives
- Identify SSTI vulnerabilities using mathematical probe payloads
- Fingerprint the template engine from error messages and output
- Exploit Jinja2 SSTI for full RCE on Flask/Python applications
- Exploit Twig SSTI on PHP applications
- Exploit FreeMarker SSTI on Java applications
- Bypass SSTI filter restrictions using alternative payloads
Topics Covered
- Template engine concepts and server-side execution context
- Detection methodology — mathematical fuzz payloads
- Jinja2 (Python/Flask) exploitation chain via MRO traversal
- Twig (PHP) exploitation
- FreeMarker (Java) exploitation
- Sandbox escape techniques
Build the target
KaliRange hosts no targets, no virtual machines and no lab files. You run this lab on your own machine, against small applications that you build. This lab shows no host names, so use the address of your own application.
Your lab needs three small web applications. Each one takes a request value and passes it to a template engine as template text, not as data:
- A Flask application with Jinja2, on Python (Jinja2 section).
- A PHP application with Twig, in a version that still allows the calls that the Twig section shows (an old release).
- A Java application with FreeMarker, with its default configuration (FreeMarker section).
The detection probes need a page that prints the result of the expression, so that you can see 49. The filter-bypass line needs a page that blocks the underscore character. The subclass index 258 is only an example: it changes with each Python version, so read the index from your own output. Run each application on your own machine, bound to 127.0.0.1. No public training application on the usual lists fits this lab, so write the three small applications yourself. Take a snapshot of the virtual machine, or note the state of the container, before you start.
Authorised targets only. Run every payload in this lab only against an application that you own or have written permission to test.
Detection
# Basic math probe — if result is evaluated instead of echoed literally, SSTI exists
{{7*7}} — 49 (Jinja2/Twig)
${7*7} — 49 (FreeMarker/Velocity)
<%= 7*7 %> — 49 (ERB/EJS)
# Fingerprint the engine
{{7*'7'}} — 7777777 (Jinja2 — string multiplication)
{{7*'7'}} — 49 (Twig — numeric coercion)Sign in to continue
3 more sections are for signed-in members. It is free: sign in with Google.