Unraveling Some of The Mysteries Around DOM-based XSS
Unraveling Some of The Mysteries Around DOM-based XSS
DOM-based XSS
Dave Wichers
Aspect Security, COO This presentation released under the
Creative Commons 3.0 Attribution-
OWASP Boardmember NonCommercial-ShareAlike
OWASP Top 10 Project Lead CC BY-NC-SA
OWASP ASVS Coauthor
[email protected]
Cross-Site Scripting (XSS)
“There’s also a third kind of XSS attacks - the ones that do not rely on sending the malicious
data to the server in the first place!” Amit Klein – Discoverer of DOM-Based XSS
“DOM-based vulnerabilities occur in the content processing stage performed on the client,
typically in client-side JavaScript.” – http://en.wikipedia.org/wiki.Cross-site_scripting
2
XSS Categories
(Traditional) DOM-Based
Stored XSS Stored XSS
(Traditional) DOM-Based
Reflected XSS Reflected XSS
3
XSS Categories – More Details
2: ‘Pure’ DOM-Based
Stored XSS
HTML 5
Local
Storage
5
Logic is Migrating from Server to Client…
Source:
6
And How Vulnerable are Apps Today?
Using IBM’s JavaScript
Security Analyzer (JSA),
IBM tested Fortune 500 +
Top 178 Sites and found
40%
Vulnerable to Client-side
JavaScript vulnerabilities,
90%
of which was caused by 3rd Source:
party JavaScript Libraries
7
What’s the Risk of DOM-Based XSS?
For: http://www.vulnerable.site/welcome.html?name=Joe<script>alert(1)</script>
#name=notJoe<script>alert(1)</script>?name=Joe
<HTML>
<TITLE>Welcome!</TITLE>
Hi
<SCRIPT>
var pos=document.URL.indexOf("name=")+5;
document.write(document.URL.substring(pos,document.URL
.length));
</SCRIPT>
Welcome to our system …
</HTML>
9
Why is finding DOM-Based XSS So Hard?
Existing JavaScript can update the DOM and new data can also contain JavaScript
“Manual code review is hell – have you seen JavaScript lately?” Ory Segal
10
How do we make detecting/avoiding DOM-
Based XSS more of a Science?
Better Understanding of
•Dangerous Sources
•Propagators (not covered here)
•Unsafe Sinks
•Defense Techniques
11
Dangerous Sources (of Browser Input)
• eval()
Direct execution • window.execScript()/function()/setInterval() /setTimeout()
• script.src(), iframe.src()
• document.write(), document.writeln()
Build • elem.innerHTML = danger, elem.outerHTML = danger
HTML/Javascript • elem.setAttribute(“dangerous attribute”, danger) –
attributes like: href, src, onclick, onload, onblur, etc.
Setting a • elem.innerText(danger)
value • formfield.val(danger)
15
Popular JavaScript Library #1: jQuery
Don’t send unvalidated data to these methods, or properly escape the data before doing so.
16
jQuery – But there’s more…
More danger
jQuery(danger) or $(danger)
This immediately evaluates the input!!
E.g., $("<img src=x onerror=alert(1)>")
jQuery.globalEval()
All event handlers: .bind(events), .bind(type, [,data], handler()),
.on(), .add(html),
Same safe examples
.text(danger), .val(danger)
Some serious research needs to be done to identify all the
safe vs. unsafe methods
There are about 300 methods in jQuery
17
What about other Popular JavaScript
Libraries?
18
XSS Prevention Techniques
Traditional vs. DOM-Based
19
Primary XSS Defense: Context Sensitive
Escaping
#1: ( &, <, >, " ) &entity; ( ', / ) &#xHH;
ESAPI: encodeForHTML()
JavaScript Data
(e.g., <script> some javascript </script> )
#4: All non-alphanumeric < 256 \HH
Note: Nested contexts like HTML within JavaScript, and decoding before encoding to prevent double
encoding are other issues not specifically addressed here.
21
Client Side Input Validation
22
Avoid JavaScript Interpreter
23
DOM-Based XSS While Creating Form
Attack URL Value: http://a.com/foo?"
Vulnerable Code: onblur="alert(123)
var html = ['<form class="config">',
'<fieldset>',
'<label for="appSuite">Enter URL:</label>',
'<input type="text" name="appSuite" id="appSuite"
value="', options.appSuiteUrl || '', '" />',
'</fieldset>',
'</form>'].join(''), dlg = $(html).appendTo($('body'));
...
Fix #1:
regexp = /http(s):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-
\/]))?/;
buttons: { 'Set': function () {
var u = $.trim(appSuite.val());
if (!regexp.test(u) || u.indexOf('"') >= 0) {
Util.ErrorDlg.show('Please enter a valid URL.');
return;
} ...
24
Fix #2 – Safe construction of the form
Vulnerable Code:
var html = ['<form class="config">',
'<fieldset>',
'<label for="appSuite">Enter URL:</label>',
'<input type="text" name="appSuite" id="appSuite"
value="', options.appSuiteUrl || '', '" />',
'</fieldset>',
'</form>'].join(''), dlg = $(html).appendTo($('body'));
...
Fix #2:
var html = ['<form class="config">',
'<fieldset>',
'<label for="appSuite"> Enter URL:</label>',
'<input type="text" name="appSuite" id="appSuite" />',
'</fieldset>',
'</form>'].join(''), dlg = $(html).appendTo($('body'));
appSuite.val(options.appSuiteUrl || '');
...
25
Techniques for Finding DOM-Based XSS #1
Tools: Normal manual Pen Test Tools like WebScarab/ZAP/Burp can be used here
Automated scanners can help, but many have no DOM-Based XSS specific test features
More tips at: https://www.owasp.org/index.php/Testing_for_DOM-based_Cross_site_scripting_(OWASP-DV-003)
26
Techniques for Finding DOM-Based XSS #2
28
Its not just DOM-Based XSS
Unchecked Redirect
• window.location.href = danger, window.location.replace()
HTML 5 Shenanigans
• Client-side SQL Injection
• ‘Pure’ DOM-Based Stored XSS (Discussed before)
• Local storage data left and data persistence (super cookies)
• Notification API Phishing, Web Storage API Poisoning, Web Worker
Script URL Manipulation, (all coined by IBM)
• Web Sockets ???
Lots more …
29
Free - Open Source Detection Tools
30
Free - Open Source Detection Tools cont’d
DOM Snitch
Experimental tool
from Google (Dec,
2011)
Real-time: Flags DOM
modifications as they
happen.
Easy: Automatically
flags issues with
details.
Really Easy to Install
Really Easy to Use
http://code.google.com/p/domsnitch/
31
Free - Open Source Detection Tools cont’d
32
Free - Open Source Detection Tools cont’d
33
Commercial Tools
34
Commercial Tools cont’d
Web Vulnerability
Scanner (WVS)
has Client Script Analyzer (CSA) for
detecting DOM-Based XSS
http://www.acunetix.com/blog/web-security-
zone/articles/dom-xss/
DOMinater Commercial Edition
(future)
Any other commercial tools??
35
Conclusion
36