SSTI Exploitation
SSTI exploitation uses the template engine's access to language internals to execute OS commands. The payload depends entirely on the engine identified during SSTI detection. Jinja2 (Python), Twig (PHP), and Freemarker (Java) each require different payload chains to reach OS execution.
Jinja2 — Python RCE
Jinja2 runs in Python context. The payload walks the Python object hierarchy to reach the os module and execute commands. Confirm Jinja2 first with 49 returning 49:
uid=33(www-data) gid=33(www-data) groups=33(www-data)
The subclasses index varies by Python version. If 408 does not work, enumerate to find the subprocess.Popen class:
Jinja2 — Reverse Shell
Twig — PHP RCE
Twig runs in PHP context. Use the _self object to access the Twig environment and call PHP system functions:
uid=33(www-data) gid=33(www-data) groups=33(www-data)
Freemarker — Java RCE
Freemarker runs in Java context and uses the freemarker.template.utility.Execute class for command execution:
uid=1001(tomcat) gid=1001(tomcat) groups=1001(tomcat)
tplmap for Automated Exploitation
References
-
Automated SSTI exploitation supporting Jinja2, Twig, Freemarker, and others
-
PayloadsAllTheThings — SSTIgithub.com/swisskyrepo/PayloadsAllTheThings/tree/master/Server%20Side%20Template%20Injection (opens in new tab)
Engine-specific payload chains for RCE
Was this helpful?
Your feedback helps improve this page.