Twig

Syntax

The content below is based on OffSec's WEB-200arrow-up-right course.

PHP does not check data types, thus, treats strings as numbers. Both the examples below will be evaluated to 25.

{{5*5}} 
{{5*'5'}}

A unique piece of Twig syntax is using - to trim whitespace.

{{-name-}}

A for loop.

{% for product in cart %}
Widget
	Price:	${{product.price}}
	Quanity: {{product.quantity}}
	Total: 	${{product.quantity * product.price}}
{% endfor %}

A if statement and the use of the capitalize filter.

<h1>{% if not admin %}sudo {% endif %}make me a sandwich, {{name|capitalize}}!</h1>

SSTI

filter

The example below is based on TCM's Practical Bug Bountyarrow-up-right course.

Following the methodology outlined herearrow-up-right, we can see that we are dealing with a Twig engine (Figure 1).

Figure 1: Using a polyglot to detecting the engine.

We can also confirm that we are dealing with either a Twig or Jinja2 engine by following the steps outlined previously (Figure 2). Twig uses PHP which does not check the variable type and as a result it treats 7 and '7' the same.

Figure 2: Testing for SSTI.

To make sure that this is indeed a server-side and not a client-side template injection, we can view the source code and check if the result is 49 or {{7*'7'}}.

Next, we can try any Twig-specific payload from this listarrow-up-right, in this case leveraging the filterarrow-up-right filter, and achieve RCE (Figure 3).

Figure 3: Achieving RCE via an SSTI vulnerability.

The above process could be automated using sstimaparrow-up-right.

reduce

The example below is based on OffSec's WEB-200arrow-up-right course.

Another filter we can leverage is the reducearrow-up-right filter which takes an arrow function and an initial value as arguments (Figure 4).

Figure 4: Testing the reduce filter.

We can replace the arguments with something that executes system commands, such as PHP's systemarrow-up-right function and achieve RCE (Figure 5).

Figure 5: Leveraging the reduce filter to achieve RCE.

Last updated

Was this helpful?