SSTI vulnerability in CTF: Jinja2 and Twig from 0 to RCE part 1

Depov

Moderator
Staff member
MODERATOR
ULTIMATE
SUPREME
PREMIUM
MEMBER
Joined
Feb 18, 2025
Messages
646
Reaction score
1,454
SSTI vs XSS: differences that determine the attack vector

Server-side template injection and XSS start the same way – custom input is displayed on the page. The difference is where the processing takes place. XSS-payload is performed in the victim's browser: the attacker implements JavaScript running in the context of DOM. SSTI payload is processed by the templater on the server before HTML is sent to the client at all. More details – in our review, creating ctf tasks.

What does this mean in practice: XSS is limited to browser-based sandbox – cookie theft, redirect, keylogging. SSTI vulnerability opens a direct path to the RCE server: execution of arbitrary OS commands. According to the OWASP classification – A03:2021 Injection. In terms of MITRE ATT&CK, the successful operation of SSTI is the T1190 Exploit Public-Facing Application (Initial Access tactics), and the installation of a web shell after receiving RCE is T1505.003 Web Shell (Persistence tactics).

How to distinguish by eye: input <script>alert(1)</script> calls JavaScript pop – you have XSS. Input {{7*7}} returns 49 instead of the text string {{7*7}} – the server processed the expression as a template. Both vulnerabilities can coexist at the same point of entry, but SSTI is always more dangerous. Confusing them is a frequent error on the CTF, and I have seen enough writeups where the author will gleefully report XSS without checking the template.

Why the attacker exactly SSTI? Through RCE – access to the server file system: configurations, databases, private keys. Next is the anchoring through the webhell and the lateral movement. According to Verizon DBIR 2025, web attacks account for 26% of all confirmed violations. SSTI is a working vector, not an exotic of CTF.
Search SSTI in CTF: Templater identification

The first step in operating PHP Python templaters and other stacks is to determine if the application is vulnerable and which engine is used. According to the PortSwigger methodology, the process is divided into three phases: Detect, Identify, Exploit.
Detective and fingerprinting engine via polyglot-payload

To detect SSTI vulnerabilities, start with phased special symbols. PortSwigger recommends consistency ${{<%[%'"}}%\ – if the server returns a templater error or an unexpected response, template application engine is likely.

A more accurate method is polyglot-payload, which are treated differently with different engines:

{{7*7}} – if returns 49, Jinja2 or Twig is likely
${7*7} – if returns 49, Freemarker or Velocity is likely
<%= 7*7 %> If 49 is returned, ERB (Ruby) is likely
{{7*'7'}} – key differentiator: Jinja2 will return 7777777 (multipling the string by number is Python behavior), Twig will return 49 (Bringing to number is PHP behavior)

Stunt with {{7*'7'}} – the fastest way to distinguish Jinja2 from Twig. The line '7'multiplied by 7 in Python, gives seven. PHP leads numeric string '7' to the number before multiplying (PHP 7/8); for non-numeric strings in PHP 8 will be TypeError. Remember this – on CTF saves minutes.

For complex cases — error-based approach: send a non-valid syntax {%invalid%}. The error text often contains the engine name and version: Flask with Jinja2 will show jinja2.exceptions.UndefinedError, and Twig — Twig\Error\SyntaxError. For confirming Jinja2 in Flask check {{config}} – it will return the configuration object with the application parameters. Twig will respond to {{app.request.server.all}} output server variables.
 
Top Bottom