SSTI vs. XSS: How to Tell the Difference in 30 Seconds
Both vulnerabilities exploit user input displayed on the page. The difference lies in the execution location. XSS executes script in the victim's browser, on the client side. SSTI executes code in the template engine on the server, within the application context—often with access to the file system and OS. See the difference? Learn more in our guide on creating CTF tasks .
The check takes seconds. Submit to the suspicious field. {{7*7}}If the answer contains 49— the template engine evaluated the expression on the server. This is SSTI. If {{7*7}}It's displayed as is, try it. ${7*7}(Velocity syntax, Freemarker) or <%= 7*7 %>(ERB). Each template engine uses its own delimiters, but the principle is the same: if the server calculates arithmetic from user input, the injection point has been found.
<script>alert(1)</script>triggered in the browser - XSS. {{7*7}}is evaluated and returned 49In HTML, it's SSTI. A single field can be vulnerable to both classes simultaneously, but the exploitation vector is fundamentally different. According to the OWASP classification, SSTI is included in A03:2021 Injection , and according to MITRE ATT&CK, successful exploitation unlocks the Exploit Public-Facing Application technique (T1190, Initial Access tactic).
The business logic of an SSTI attack: the attacker executes code on the server, installs a web shell (T1505.003, Persistence tactic), steals data from the database, and moves further through the internal network. According to the Mandiant M-Trends 2025 report, exploits are the most common initial access vector: 38% of incidents involved initial access. SSTI is one such exploit, and its severity is often underestimated. The username field on the welcome page appears harmless until it becomes the entry point for a complete server compromise.
Determining the template engine based on the server response
Before building a payload, you need to understand which engine is running on the backend. PortSwigger's methodology is built like a decision tree: you send a sequence of test expressions and determine the engine based on the server's response.
Start with a universal fuzz vector: ${{<%%'"}}%\. If the server returns an error - the template syntax is recognized, the injection point exists. Further narrowing down:
Payload Result Probable engine
{{7*7}} → 49 The arithmetic is calculated Jinja2 or Twig
${7*7} → 49 The arithmetic is calculated Freemarker или Velocity
{{7*'7'}} → 7777777 The line is repeated Jinja2 (Python)
{{7*'7'}} → 49 Reduction to a number Twig (PHP)
Trick with {{7*'7'}}— the fastest way to distinguish Jinja2 from Twig. In Python, the expression 7*'7'repeats the line '7'seven times → 7777777. B PHP '7'casts to a number → 49This distinction is critically important: the payloads for these engines are completely different.
Additional signs: if the answer or error flashes jinja2.exceptions, TemplateSyntaxError— Python engine. If Twig_Error_Syntax or Twig\Error— PHP. Template engine errors often reveal not only the engine but also its version, which simplifies payload selection.
Exploiting SSTI in Jinja2: [From Arithmetic to RCE]
MRO and subclasses : chain mechanics
Expression {{7*7}}proves injection, but for RCE you need to get to Python modules - specifically to os.popen() or subprocess.Popen()Problem: Jinja2 won't import osinto the default template context. The solution is to traverse the Python class hierarchy via MRO.
MRO (Method Resolution Order) is the order in which Python searches for methods in the inheritance hierarchy. As researcher Gus from OnSecurity notes, every object in Python inherits from a base class. object. Through the chain __class__.__mro__you can go up to object, and from there through __subclasses__()Get a list of ALL loaded classes - including those that have access to the file system.
Step-by-step logic using an empty string as an example "":
"".__class__ → <class 'str'>— object type
"".__class__.__mro__ → (<class 'str'>, <class 'object'>)- chain of inheritance
"".__class__.__mro__[1] → <class 'object'>— base class
"".__class__.__mro__[1].__subclasses__()— an array of hundreds of loaded subclasses
Among the subclasses objectWe're looking for a class that can run OS commands. Most often, this is subprocess.Popen or os._wrap_closeThe class index depends on the Python version and installed libraries - on the same server PopenOne might be numbered 407, while another might be numbered 351. This is why blindly copying the payload from writeup doesn't work: the index needs to be tailored to the specific environment.
Shorter alternative: __base__equivalent __mro__[1]- both provide access to object, But __base__Saves characters. In CTFs with payload length limitations, this can be crucial.
Payload for RCE via Jinja2 template
The complete RCE chain is built in four steps. The first is to find an object available in the template context (in Flask, this is request, g, config, selfor an empty line). The second is to rise to objectthrough MRO. The third is to sort through __subclasses__()and find the required class. Fourth, call the method with the OS command.
There is a more direct way if the object is available request. Payload {{request.application.__globals__.__builtins__.__import__('os').popen('id').read()}}accesses the global namespace of the Flask application, imports the module osthrough __builtins__and causes popen()It runs on Flask's default configuration without a sandbox—exactly what's most often seen in CTFs.
Alternative route through self: payload {{self._TemplateReference__context.cycler.__init__.__globals__.os.popen('id').read()}}uses an internal object cyclerfrom the Jinja2 context to get to osthrough __globals__According to YesWeHack, this option works even when requestblocked in template context.
To iterate over the subclass index, send {{"".__class__.__mro__[1].__subclasses__()}}- it will display a full list of classes. Search subprocess.PopenIn your answer, write down the index. Found Popenat position 407? Final payload: {{"".__class__.__mro__[1].__subclasses__()[407]('cat flag.txt',shell=True,stdout=-1).communicate()}}.
Both vulnerabilities exploit user input displayed on the page. The difference lies in the execution location. XSS executes script in the victim's browser, on the client side. SSTI executes code in the template engine on the server, within the application context—often with access to the file system and OS. See the difference? Learn more in our guide on creating CTF tasks .
The check takes seconds. Submit to the suspicious field. {{7*7}}If the answer contains 49— the template engine evaluated the expression on the server. This is SSTI. If {{7*7}}It's displayed as is, try it. ${7*7}(Velocity syntax, Freemarker) or <%= 7*7 %>(ERB). Each template engine uses its own delimiters, but the principle is the same: if the server calculates arithmetic from user input, the injection point has been found.
<script>alert(1)</script>triggered in the browser - XSS. {{7*7}}is evaluated and returned 49In HTML, it's SSTI. A single field can be vulnerable to both classes simultaneously, but the exploitation vector is fundamentally different. According to the OWASP classification, SSTI is included in A03:2021 Injection , and according to MITRE ATT&CK, successful exploitation unlocks the Exploit Public-Facing Application technique (T1190, Initial Access tactic).
The business logic of an SSTI attack: the attacker executes code on the server, installs a web shell (T1505.003, Persistence tactic), steals data from the database, and moves further through the internal network. According to the Mandiant M-Trends 2025 report, exploits are the most common initial access vector: 38% of incidents involved initial access. SSTI is one such exploit, and its severity is often underestimated. The username field on the welcome page appears harmless until it becomes the entry point for a complete server compromise.
Determining the template engine based on the server response
Before building a payload, you need to understand which engine is running on the backend. PortSwigger's methodology is built like a decision tree: you send a sequence of test expressions and determine the engine based on the server's response.
Start with a universal fuzz vector: ${{<%%'"}}%\. If the server returns an error - the template syntax is recognized, the injection point exists. Further narrowing down:
Payload Result Probable engine
{{7*7}} → 49 The arithmetic is calculated Jinja2 or Twig
${7*7} → 49 The arithmetic is calculated Freemarker или Velocity
{{7*'7'}} → 7777777 The line is repeated Jinja2 (Python)
{{7*'7'}} → 49 Reduction to a number Twig (PHP)
Trick with {{7*'7'}}— the fastest way to distinguish Jinja2 from Twig. In Python, the expression 7*'7'repeats the line '7'seven times → 7777777. B PHP '7'casts to a number → 49This distinction is critically important: the payloads for these engines are completely different.
Additional signs: if the answer or error flashes jinja2.exceptions, TemplateSyntaxError— Python engine. If Twig_Error_Syntax or Twig\Error— PHP. Template engine errors often reveal not only the engine but also its version, which simplifies payload selection.
Exploiting SSTI in Jinja2: [From Arithmetic to RCE]
MRO and subclasses : chain mechanics
Expression {{7*7}}proves injection, but for RCE you need to get to Python modules - specifically to os.popen() or subprocess.Popen()Problem: Jinja2 won't import osinto the default template context. The solution is to traverse the Python class hierarchy via MRO.
MRO (Method Resolution Order) is the order in which Python searches for methods in the inheritance hierarchy. As researcher Gus from OnSecurity notes, every object in Python inherits from a base class. object. Through the chain __class__.__mro__you can go up to object, and from there through __subclasses__()Get a list of ALL loaded classes - including those that have access to the file system.
Step-by-step logic using an empty string as an example "":
"".__class__ → <class 'str'>— object type
"".__class__.__mro__ → (<class 'str'>, <class 'object'>)- chain of inheritance
"".__class__.__mro__[1] → <class 'object'>— base class
"".__class__.__mro__[1].__subclasses__()— an array of hundreds of loaded subclasses
Among the subclasses objectWe're looking for a class that can run OS commands. Most often, this is subprocess.Popen or os._wrap_closeThe class index depends on the Python version and installed libraries - on the same server PopenOne might be numbered 407, while another might be numbered 351. This is why blindly copying the payload from writeup doesn't work: the index needs to be tailored to the specific environment.
Shorter alternative: __base__equivalent __mro__[1]- both provide access to object, But __base__Saves characters. In CTFs with payload length limitations, this can be crucial.
Payload for RCE via Jinja2 template
The complete RCE chain is built in four steps. The first is to find an object available in the template context (in Flask, this is request, g, config, selfor an empty line). The second is to rise to objectthrough MRO. The third is to sort through __subclasses__()and find the required class. Fourth, call the method with the OS command.
There is a more direct way if the object is available request. Payload {{request.application.__globals__.__builtins__.__import__('os').popen('id').read()}}accesses the global namespace of the Flask application, imports the module osthrough __builtins__and causes popen()It runs on Flask's default configuration without a sandbox—exactly what's most often seen in CTFs.
Alternative route through self: payload {{self._TemplateReference__context.cycler.__init__.__globals__.os.popen('id').read()}}uses an internal object cyclerfrom the Jinja2 context to get to osthrough __globals__According to YesWeHack, this option works even when requestblocked in template context.
To iterate over the subclass index, send {{"".__class__.__mro__[1].__subclasses__()}}- it will display a full list of classes. Search subprocess.PopenIn your answer, write down the index. Found Popenat position 407? Final payload: {{"".__class__.__mro__[1].__subclasses__()[407]('cat flag.txt',shell=True,stdout=-1).communicate()}}.