Secure Webcoding for Beginners - $awareness++ - nblock

44
Secure Webcoding for Beginners $awareness++ Florian Brunner Florian Preinstorfer 09.06.2010 Hacking Night 2010

Transcript of Secure Webcoding for Beginners - $awareness++ - nblock

Secure Webcoding for Beginners$awareness++

Florian Brunner Florian Preinstorfer

09.06.2010Hacking Night 2010

Vorstellung

Florian Brunner

Software EngineerCTF-Team h4ck!nb3rgStudent sib08

Florian Preinstorfer

CoderPentesterStudent cms07

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 2 / 39

Motivation

Warum schon wieder Secure Webcoding?Mangelndes Securitybewusstein bei vielen EntwicklernSecurity stort und erhoht EntwicklungsaufwandMangelhafte (securitybezogene) AusbildungZeitmangel bei der Programmentwicklung”it compiles, ship it!”

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 3 / 39

MotivationResultat

schlechter Code!

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 4 / 39

OWASP

Was ist OWASPOpen Web Application Security Projecta

Offene CommunityHerstellerunabhangigDokumente/Libraries/Tools sind frei erhaltlich2 große Arbeitsbereiche:

I Dokumentation (OWASP Guide, Top 10, Testing Guide, Metrics, . . . )I Anwendungsentwicklung (WebScarab, WebGoat, Enigform, . . . )

ahttp://owasp.org

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 5 / 39

OWASP Top 10 Project

Was ist OWASP Top 10The goal of the Top 10 project is to raise awareness about applicationsecurity by identifying some of the most critical risks facing organizations.[OWASP Top 10]

Listet die 10 haufigsten (sicherheitsrelevanten) RisikenNur Awareness, kein Security GuideAusgangspunkt fur Entwickler!Releases: 2003, 2004, 2007, 2010

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 6 / 39

OWASP Top 10

Platz 1-5

1 Injection2 Cross-Site Scripting (XSS)3 Broken Authentication and Session Management4 Insecure Direct Object References5 Cross-Site Request Forgery (CSRF)

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 7 / 39

OWASP Top 10

Platz 6-10

6 Security Misconfiguration7 Insecure Cryptographic Storage8 Failure to Restrict URL Access9 Insufficient Transport Layer Protection10 Unvalidated Redirects and Forwards

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 7 / 39

#1 Injection

Was ist das?

Webapplikation fuhrt keine Inputvalidierungdurch und sendet Daten ungefiltert an einenInterpreter.Beispiele

I OS-Befehle (zB system(), passthru())I SQL-AbfragenI LDAPI ORMI XPath, ...

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 8 / 39

#1 Injection

Impact?

Im Erfolgsfall Verlust vonI VertraulichkeitI IntegritatI VerfugbarkeitI Reputation

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 9 / 39

#1 Injection

Angriffsvektoren

ALL input is evil!GET/POST-ParameterDateienEmailsBrowser-Properties...

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 10 / 39

#1 Injectionanfalliger Code (SQL)

<?php$myid = $ GET [ ’ id ’ ] ;$row = m y s q l f e t c h a r r a y ( m y s q l q u e r y (

”SELECT ∗ FROM t i c k e tWHERE ( t i c k e t i d =’{$myid } ’ ) ” ) ) ;

?>

fix

<?php$myid = mysql real escape string($ GET [ ’ id ’ ] ) ;$row = m y s q l f e t c h a r r a y ( m y s q l q u e r y (

”SELECT ∗ FROM t i c k e tWHERE ( t i c k e t i d =’{$myid } ’ ) ” ) ) ;

?>

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 11 / 39

#1 Injection

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 12 / 39

#1 Injectionanfalliger Code (OS Command)

<?php$t yp e = $ GET [ ’ type ’ ] ; // i s not v a l i d a t e d$cmd = ”/ u s r / b i n / d i g { $t yp e } {$domain }” ;p a s s t h r u ( $cmd ) ; ?>

fix

<?phpf u n c t i o n v a l i d a t e T y p e ( $t ype ) {

i f ( i s s e t ( $ty pe ) &&preg match(”/ˆ( any |mx | cname ) $ /” , $t yp e )) {

r e t u r n $ typ e ;}r e t u r n ” any ” ;

} ?>

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 13 / 39

#1 Injection

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 14 / 39

#1 Injection

Gegenmaßnahme

Jeden Input validieren/escapenWhitelisting vs. BlacklistingLibraries von Programmiersprachen undFrameworks benutzenRegulare Ausdrucke

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 15 / 39

#2 Cross-Site Scripting (XSS)

Was ist das?

Die Webapplikation fuhrt keine Validierung vonnicht vertrauenswurdigen Daten durch undsendet diese ungefiltert zum Webbrowser desOpfers.Ein Angreifer kann . . .

I Skripte (Bsp: JavaScript) im Webbrowser desOpfers ausfuhren

I die Session-ID des Opfers stehlenI den Benutzer auf eine bosartige Seite umleitenI . . .

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 16 / 39

#2 Cross-Site Scripting (XSS)

Angriffsvektoren

ALL input is evil!GET/POST-ParameterBrowser PropertiesCookies

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 17 / 39

#2 Cross-Site Scripting (XSS)

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 18 / 39

#2 Cross-Site Scripting (XSS)

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 18 / 39

#2 Cross-Site Scripting (XSS)

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 18 / 39

#2 Cross-Site Scripting (XSS)

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 18 / 39

#2 Cross-Site Scripting (XSS)

anfalliger Code

<?php$ i n p u t = $ GET [ ’ i n p u t ’ ] ;echo ” $ i n p u t welcome to my page ” ;

?>

fix

<?php$ i n p u t = htmlentities($ GET [ ’ i n p u t ’ ] ) ;echo ” $ i n p u t welcome to my page ” ;

?>

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 19 / 39

#2 Cross-Site Scripting (XSS)

Gegenmaßnahme

Jeden Input validieren/escapenJeden Output validieren/escapenWhitelisting vs. BlacklistingLibraries von Programmiersprachen undFrameworks benutzen

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 20 / 39

#3 Broken Authentication and Session Management

Was ist das?

Bei schlechtem Session Management wird einsicherer Session-Lifecycle nicht vollstandig oderfehlerhaft umgesetzt.Beispiele:

I Anstelle der Session-ID werden Credentials imCookie gespeichert

I Session FixationI endlos gultige Session-IDsI keine Neugenerierung nach Privilegienwechsel

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 21 / 39

#3 Broken Authentication and Session Management

Impact?

Mogliche SzenarienI Ubernahme der Session eines BenutzersI Diebstahl eines AccountsI Vorhersagbare Session-IDsI . . .

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 22 / 39

#3 Broken Authentication and Session Management

Angriffsvektoren

GET/POST-ParameterCookiesFunktionen

I Login/LogoutI Passwort vergessen/resetI Accountmanagement

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 23 / 39

#3 Broken Authentication and Session Management

Gegenmaßnahme

Secure Software DesignBest Practices [SANS SSM]Code-Review

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 24 / 39

#4 Insecure Direct Object Reference

Was ist das?

Ein legitimer Benutzer andert einen Parameterdirekt und verschafft sich dadurch Zugriff aufandere Objekte, auf die der Benutzer nichtzugreifen darf.Beispiele

I Download-ScriptsI Account-IDs (zB Online-Banking)I Direkter Zugriff auf ResourcenI Indirekter Zugriff (ID-Mapping)

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 25 / 39

#4 Insecure Direct Object Reference

Impact?

Im Erfolgsfall Verlust vonI VertraulichkeitI WettbewerbsvorteilI Reputation

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 26 / 39

#4 Insecure Direct Object Reference

Angriffsvektoren

ALL input is evil!!GET/POST-ParameterFehlende Berechtigung bei Parametern

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 27 / 39

#4 Insecure Direct Object Referenceanfalliger Code (SQL)

<?php$ b a l a n c e = m y s q l q u e r y (

”SELECT b a l a n c e FROM accountWHERE ( a c c i d =’{$myid } ’ ) ” ) ;

?>

fix

<?phpi f ( isAccessGranted($myid , $ c u r r e n t U s e r )) {

$ b a l a n c e = m y s q l q u e r y (”SELECT b a l a n c e FROM accountWHERE ( a c c i d =’{$myid } ’ ] ’ ) ” ) ;

}?>

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 28 / 39

#4 Insecure Direct Object Reference

Gegenmaßnahme

Jeden Zugriff auf direkte Resourcen prufenID-MappingBeim Design berucksichtigenAPIs bzw. Guidlines

I ASVS requirements area for Access Control(V4) [OWASP ASVS]

I ESAPI Access Reference Map API[OWASP ESAPI]

I ESAPI Access Control API [OWASP ESAPI]

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 29 / 39

#10 Unvalidated Redirects and Forwards

Was ist das?

Ein Angreifer verwendet ungeprufteWeiterleitungen bzw. Umleitungen von URLsum ahnungslose Benutzer auf Phishing- oderMalware-Seiten zu locken.Beispiele

I Anmeldefunktionen (zB Weiterleitung)I SuchergebnisseI Fremde Links

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 30 / 39

#10 Unvalidated Redirects and Forwards

Impact?

Mogliche SzenarienI Erfolgreiches PhishingI Opfer wird mit Malware infiziertI ”Interne” Funktionen werden angegriffenI Vertrauen der Benutzer verloren

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 31 / 39

#10 Unvalidated Redirects and Forwards

Angriffsvektoren

ALL input is evil!!GET/POST-Parameter

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 32 / 39

#10 Unvalidated Redirects and Forwards

open Redirect

h t t p : / /www. x x f x i . a t / R e d i r e c t / s a x x x r g t o p i c . aspx ? u r l=h t t p : / /www. g o o g l e . com

h t t p : / /www. xxxbxxd . de / s r e d i r e c t /?u=h t t p : / /www. g o o g l e . com

h t t p : / / x x x b x x k e t . com/ r e d i r e c t ? u r l=h t t p : / /www. g o o g l e . com

h t t p s : / / l x x . xxe . a t / L x x i s / A u t h e n t i c a t i o n / Log in . aspx ?R e t u r n U r l=h t t p : / / g o o g l e . com

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 33 / 39

#10 Unvalidated Redirects and Forwards

Gegenmaßnahme

Prufen der DomainBenutzer ggf. hinweisenURL nicht durch GET/POST-ParameterangebenGuidlines

I OWASP Enterprise Security API[OWASP ESAPI]

I Google blog article on the dangers of openredirects [Google Blog]

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 34 / 39

Gegenmaßnahmen [Cert Top 10 Secure Coding Guidelines]

Platz 1-5

1 Eingaben uberprufen/validieren2 Compiler Warnungen beachten3 Sicherheit bereits im Design vorsehen4 Keep it simple5 Standardmaßig ist alles verboten

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 35 / 39

Gegenmaßnahmen [Cert Top 10 Secure Coding Guidelines]

Platz 6-10

6 Geringstmogliche Privilegien7 Daten uberprufen/validieren die an andere Systeme

gesandt werden8 In-Depth defense9 Gute Qualitatssicherung nutzen (extreme

programming. . . . )10 Secure Coding Standards befolgen

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 35 / 39

Gegenmaßnahmen

Secure Coding GuidelinesSANS The Top Cyber Security Risks!

http://www.sans.org/top-cyber-security-risks/

Java Secure Coding Guideline for Java, Version 3.0http://java.sun.com/security/seccodeguide.html

.NET Secure Coding Guidelineshttp://msdn.microsoft.com/en-us/library/d55zzx87(v=VS.71).aspx

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 36 / 39

Referenzen I

OWASP FoundationOWASP Top 10 Press Release.http://www.owasp.org/index.php/Category:OWASP_Top_Ten_Project.

OWASP FoundationOWASP Application Security Verification Standard Project.http://www.owasp.org/index.php/ASVS.

OWASP FoundationOWASP Enterprise Security API.http://www.owasp.org/index.php/EASPI.

CERT (Carnegie Mellon University)Top 10 Secure Coding Guidelines.https://www.securecoding.cert.org/confluence/display/seccode/Top+10+Secure+Coding+Practices.

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 38 / 39

Referenzen II

Google Inc.Official Google Webmaster Central Blog.http://googlewebmastercentral.blogspot.com/2009/01/open-redirect-urls-is-your-site-being.html.

SANS Institute, Luke MurpheySecure Session Management: Preventing Security Voids in WebApplications.http://www.sans.org/reading_room/whitepapers/webservers/secure-session-management-preventing\-security-voids-web-applications_1594.

Florian Brunner, Florian Preinstorfer Secure Webcoding for Beginners Hacking Night - 09.06.2010 39 / 39