# Template Injection (client and server side)

> Made from my notes when I studied the subject. I mostly used [THM](https://tryhackme.com/room/learnssti). There are also notes and resources found during practice at work. These notes mostly cover server side template injection but the resources have some link about client-side template injection

## What is it?

Template injection refers to a vulnerability where an attacker can manipulate templates or template engines to execute malicious code. It can occur on both the client side and the server side.

* **Client-side template injection**: In client-side template injection, the template rendering process occurs on the client's browser using JavaScript or similar technologies. The client's browser downloads the template code from the server and then interprets and renders it. If the template data is not properly validated or sanitized, an attacker can inject malicious code into the template, leading to its execution on the client's browser. This can result in unauthorized access, data theft, or other security risks for the user.
* **Server-side template injection**: Server-side template injection involves rendering templates on the server side before sending the resulting HTML to the client's browser. The server uses a template engine (e.g., Jinja, Freemarker) to process templates and combine them with dynamic data. If the server fails to properly validate or sanitize the template or the input data, an attacker can inject malicious code into the template variables. When the server processes the template, it executes the injected code, potentially leading to server-side security breaches, data leaks, or other vulnerabilities.

The main difference between the two lies in where the template rendering occurs. Client-side template injection happens on the client's browser, while server-side template injection occurs on the server before sending the content to the client.

## Detect Injection

* This chars can be used one by one to check if it is vulnerable until we either get an error, or some characters start disappearing from the output. `${{<%[%'"}}%`

## Identify Template Engine

![Identify](https://1679624655-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEkk28J0B2BeDMuesRMr1%2Fuploads%2Fgit-blob-809517d23a4a55e36c822360195a084f1c3f33f3%2F2023-06-18-09-03-14.png?alt=media)

> [Source James Kettle](https://gosecure.github.io/template-injection-workshop/#2)

To follow the decision tree, start at the very left and include the variable in your request. Follow the arrow depending on the output:

* Green arrow - The expression evaluated (i.e 42)
* Red arrow - The expression is shown in the output (i.e ${7\*7})

You can copy paste the expressions from here

```txt
${7*7}
{{7*7}}
a{*comment*}b
{{7*'7'}}
${"z".join("ab")}
```

When we detected the template engine we need to know how to:

* Start a print statement
* End a print statement
* Start a block statement
* End a block statement

We can look this up in the official documentation of the template we detected.

It is also essential to know which language is the template engine based on to choose which language we can use to exploit the injection.\
Once we know this we can lookup for ways to execute shell command in this language.

## Remediation

* Use secure method
* Sanitize inputs
* [OWASP Injection Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Injection_Prevention_Cheat_Sheet.html)

## How to report

### COMING SOON

![Coming soon](https://1679624655-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEkk28J0B2BeDMuesRMr1%2Fuploads%2Fgit-blob-79c94f83d11d2dcfbf46dd6e9f56209f3dcdcfc1%2F2024-07-03-16-57-38.png?alt=media)

## Resources

### Syntaxes

* [Smarty](https://www.smarty.net/documentation)
* [Mako](https://docs.makotemplates.org/en/latest/)
* [Jinja - Documentation](https://jinja.palletsprojects.com/en/3.0.x/templates/)
* [Jinja2](https://jinja.palletsprojects.com/en/2.11.x/)
* [Twig](https://twig.symfony.com/doc/)

### Courses

{% embed url="https://portswigger.net/research/server-side-template-injection" %}
Server-Side Template Injection - James Kettle
{% endembed %}

{% embed url="https://gosecure.github.io/template-injection-workshop" %}
Template injection in Action - Course with labs - Philippe Arteau - GoSecure
{% endembed %}

{% embed url="https://www.youtube.com/watch?v=OwhEnVzmEHk" %}
Understanding Server Side Template Injection | TryHackMe - Motasem Hamdan
{% endembed %}

{% embed url="https://youtu.be/NV7Z5gvKs2A" %}
Server Side Template Injection Vulnerability | TryHackMe SSTI - Motasem Hamdan
{% endembed %}

{% embed url="https://portswigger.net/kb/issues/00200308_client-side-template-injection" %}
Issue Definitions: Client Side Template Injection - Portswigger
{% endembed %}

{% embed url="https://portswigger.net/kb/issues/00101080_server-side-template-injection" %}
Issue Definitions: Server Side Template Injection - Portswigger
{% endembed %}

{% embed url="https://portswigger.net/web-security/cross-site-scripting/contexts/client-side-template-injection" %}
XSS Context - Client-side Template Injection - Portswigger
{% endembed %}

{% embed url="https://janakiev.com/blog/python-shell-commands/" %}
How to Execute Shell Commands with Python by Nikolai Janakiev
{% endembed %}

{% embed url="https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Server%20Side%20Template%20Injection" %}
Server Side Template Injection on Payload all the things
{% endembed %}

{% embed url="https://owasp.org/www-project-web-security-testing-guide/v41/4-Web_Application_Security_Testing/07-Input_Validation_Testing/18-Testing_for_Server_Side_Template_Injection" %}
Testing for Server Side Template Injection - OWASP
{% endembed %}

### Labs

{% embed url="https://tryhackme.com/room/zthobscurewebvulns" %}
ZTH: Obscure Web Vulns - Tryhackme
{% endembed %}

{% embed url="https://tryhackme.com/room/learnssti" %}
Learn Server Side Template Injection - TryHackme
{% endembed %}

{% embed url="https://tryhackme.com/room/templates" %}
Templates - SSTI room - Medium difficulty
{% endembed %}

{% embed url="https://portswigger.net/web-security/server-side-template-injection/exploiting" %}
Exploiting server-side template injection vulnerabilities Multiple labs on Portswigger
{% endembed %}

{% embed url="https://account.vulnmachines.com/login" %}
SSTI on vulnmachines
{% endembed %}

### Real life examples

{% embed url="https://hackerone.com/reports/125980" %}
SSTI in uber.com - Hackerone report
{% endembed %}
