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

Example Template Variables

Greeting the user

Template For Loop

Displaying the total

Template If Statement

Various Templating Engines

Templating Engine
Language
Server/client Side

Twig

PHP

Server Side

Freemarker

Java (usually)

Server Side

Pug/Jade

JavaScript

Mostly Server Side

Jinja

Python

Server Side

Handlebars

JavaScript

Both

Mustache

Multiple

Varies

Comparing levels of logic

Twig - Discovery and Exploitation

Twig - Discovery

Inline PHP before Twig

Twig Template

Twig Statement

Twig - Exploitation

Twig Filters

Twig Documentation Example

Arguments for the reduce Function

var_dump payload

var_dump Payload output

whoami Payload

Twig RCE

Apache Freemarker - Discovery and Exploitation

Freemarker - Discovery

Freemarker Template

Freemarker Variables

If Statement in Freemarker

Name set to "hacker"

Loop in Freemarker

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.

HTML in name Variable
Freemarker Multiply Number
Freemarker multiple Number and String

Freemarker - Exploitation

Freemarker Execute Documentations

Freemarker Execute Payload

Freemarker RCE

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

Attributes in Pug

if statement in Pug

Code in Pug

Buffered Code

Pug Loop

Discovering Pug

Pug - Exploitation

Pug - require Does not Exist
Pug - global.process.mainModule.require

Storing require as Variable

Pug - Requiring child_process

Executing spawnSync

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

Jinja multiplying with string

Jinja - Exploitation

Jinja - Config Key

Mustache and Handlebars - Discovery and Exploitation

Mustache and Handlebars - Discovery

Handlebars Template

Handlebars Expression

Handlebars Helpers

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".

Handlebars - Reading Directory
Handlebars - Read /etc/passwd

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.

Halo Admin Page
Halo Theme Editor Navigation
Halo Theme Editor

404.ftl

404 page with Halo

Non-freemarker template response

Freemarker Template Response

Freemarker template with string response

Halo - Exploitation

Freemarker RCE Payload

Halo - Template Injection to view /etc/passwd

/etc/passwd in Halo

Extra Mile

Do the lab.

Craft CMS with Sprout Forms - Case Study

Accessing Craft CMS

Start the VPN, the VM, and add the IP to yours hosts.

Craft CMS with Sprout Forms - Discovery

Craft CMS Home page with Sprout Form
Discovering possible PHP and Craft CMS

Running Gobuster against the craft webpage

Craft Admin Page
Submitting Form
Form Email
Submitting Template

cURL Payload

Python HTTP Server

SSTI Confirmation

Craft CMS with Sprout Forms - Exploitation

Twig Sandbox

Exfiltration Payload

Exfiltration in HTTP log

URL Encoding Exfil

Encoded message in HTTP log

Executing whoami and Exfiltrating the Output

Output of whoami Logged

Payload in Craft CMS

/etc/passwd From Craft

Burp Decoder
Selecting URL Decoding
Burp Decoding /etc/passwd

Last updated