Twig
Syntax
The content below is based on OffSec's WEB-200 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 Bounty course.
Following the methodology outlined here, we can see that we are dealing with a Twig engine (Figure 1).

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.

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 list, in this case leveraging the filter filter, and achieve RCE (Figure 3).

The above process could be automated using sstimap.
reduce
The example below is based on OffSec's WEB-200 course.
Another filter we can leverage is the reduce filter which takes an arrow function and an initial value as arguments (Figure 4).

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

reduce filter to achieve RCE.Last updated
Was this helpful?