📌SSTI : Server Side Template Injection

Notes

Tools

Detect

Commonly Used Template Engine Syntax

Plaintext Context

Code Context

Identify

Invalid Syntax

Ruby Error

Decision Tree

Decision Tree

Template Engine Payloads

Ruby

Ruby - Basic injections

ERB:

Slim:

Ruby - Retrieve /etc/passwd

Ruby - List files and directories then read

Ruby - Code execution

Execute code using SSTI for ERB engine.

Execute code using SSTI for Slim engine.

Tornado Template

Tornado (Python)

  • {{7*7}} = 49

  • ${7*7} = ${7*7}

  • {{foobar}} = Error

  • {{7*'7'}} = 7777777

FreeMarker Template Injection

FreeMarker (Java)

You can try your payloads at https://try.freemarker.apache.org

  • {{7*7}} = {{7*7}}

  • ${7*7} = 49

  • #{7*7} = 49 -- (legacy)

  • ${7*'7'} Nothing

  • ${foobar}

Get FreeMarker Version

Freemarker - Code Execution

Freemarker - Sandbox bypass

⚠️ only works on Freemarker versions below 2.3.30

More information

Django Template Injection

Django Templates

Django template language supports 2 rendering engines by default: Django Templates (DT) and Jinja2. Django Templates is much simpler engine. It does not allow calling of passed object functions and impact of SSTI in DT is often less severe than in Jinja2.

Detection

Django Templates for post-exploitation

Cross-site scripting

Debug information leak

Leaking app’s Secret Key

Admin Site URL leak

Admin username and password hash leak

Jinja2(Python) Template Injection

  • {{7*7}} = Error

  • ${7*7} = ${7*7}

  • {{foobar}} Nothing

  • {{4*4}}[[5*5]]

  • {{7*'7'}} = 7777777

  • {{config}}

  • {{config.items()}}

  • {{settings.SECRET_KEY}}

  • {{settings}}

  • <div data-gb-custom-block data-tag="debug"></div>

RCE

References

Last updated

Was this helpful?