Start Secure. Stay Secure.TM Cross-Site Scripting Are your web applications vulnerable? By Kevin Spett Start Secure. Stay Secure.TM Cross-Site Scripting Table of Contents Introduction Cross-Site Scripting HTML HTTP An Advanced Cross-Site Scripting Attack Attack Procedure Summary Prevention Application Developer/Server Administrator Solutions for Users Browsers Conclusion The Business Case for Application Security About SPI Labs About SPI Dynamics About the WebInspect Product Line About the Author Contact Information 1 2 2 4 5 15 15 15 16 16 17 17 17 18 18 19 20 © 2005 SPI Dynamics, Inc. All Rights Reserved. No reproduction or redistribution without written permission. ii Start Secure. Stay Secure.TM Cross-Site Scripting Introduction Think of how often you receive an e-mail with a hyperlink. Imagine receiving a message with a link to your online banking site exclaiming that you could win $200 as part of a promotion to use the site. If you clicked the link and logged into the site, you could have revealed your logon information to a hacker...just that easily. This example illustrates an increasingly popular hacking phenomenon known as cross-site scripting. Users may unintentionally execute scripts written by an attacker when they follow links in disguised or unknown sources, either in web pages, e-mail messages, instant messages, newsgroup postings, or various other media. Because the malicious scripts use the targeted site to hide their origins, the attacker has full access to the retrieved web page and may send data contained in the page back to their own server. For example, a malicious script can read fields in a form provided by the real server, and then send this data (such as logon information) to the hacker's server. Although the security community has discussed the dangers of cross-site scripting attacks for years, the true dangers of these vulnerabilities have often been overlooked. The purpose of this paper is to educate both application developers and end users on the techniques that can be used to exploit a web application with cross-site scripting, suggest how to eliminate such vulnerabilities from web applications, and teach end users how to recognize and reduce the risk they face from a cross-site scripting attack. © 2005 SPI Dynamics, Inc. All Rights Reserved. No reproduction or redistribution without written permission. 1 Start Secure. Stay Secure.TM Cross-Site Scripting Cross-Site Scripting Cross-site scripting (also known as XSS or CSS) occurs when dynamically generated web pages display input that is not properly validated. This allows an attacker to embed malicious JavaScript code into the generated page and execute the script on the machine of any user that views that site. Cross-site scripting could potentially impact any site that allows users to enter data. This vulnerability is commonly seen on · · · · Search engines that echo the search keyword that was entered Error messages that echo the string that contained the error Forms that are filled out where values are later presented to the user Web message boards that allow users to post their own messages. An attacker who uses cross-site scripting successfully might compromise confidential information, manipulate or steal cookies, create requests that can be mistaken for those of a valid user, or execute malicious code on the end-user systems. Since cross-site scripting attacks are closely related to the web server package and the user's web browser, a brief overview of HTML and HTTP will be useful before discussing the mechanics of specific cross-site scripting examples. HTML HTML documents are plain text files that contain only seven-bit printable ASCII characters. To represent various elements such headers, tables, paragraphs, and lists, some special notations called tags are used. A tag contains a left angle bracket, a tag name, and a right angle bracket. Tags are usually paired (e.g.,
Example of HTML document
© 2005 SPI Dynamics, Inc. All Rights Reserved. No reproduction or redistribution without written permission. 3 Start Secure. Stay Secure.TM Cross-Site Scripting Upon receipt of the document, the browser will display "Example of HTML document" in the browser window and open an alert message box containing "HTML document". To see how this works, cut and paste the sample HTML into a text file, save it as an HTML file, and then open it in a web browser. HTTP Hyper Text Transfer Protocol (HTTP) is the set of conventions that governs how HTML documents are transmitted and received across the World Wide Web. When browsing web sites, your web browser is a client program that makes requests (for example, that a certain web page be displayed) from a web server somewhere on the Internet. An important element of HTTP is how servers handle requests from clients (remote computers connecting to the server via the World Wide Web). A session can be defined as the matched pair of a client request and a server response. HTTP is a stateless protocol; no concept of session state is maintained by HTTP when handling clientserver communications. While that sounds complicated, it is really quite simple. Each request made by a client is handled individually by a server. Multiple requests made by the same client are each treated as unique by the responding server. In other words, the server does not attempt to maintain a connection with the client at any time. This element of HTTP is one of the reasons cross-site scripting attacks can be so successful. Once a server accepts a request and dynamically generates a web page with script injected by an attacker, it is too late. The potential for damage has already been done. © 2005 SPI Dynamics, Inc. All Rights Reserved. No reproduction or redistribution without written permission. 4 Start Secure. Stay Secure.TM Cross-Site Scripting An Advanced Cross-Site Scripting Attack Many web sites have options that allow users to enter data and then receive an updated dynamic display created according to their input. For example, a search engine site accepts requests and then displays the results of the search criteria the user entered. If a user typed "asdfghjkl" as the search criteria, the server may return a page telling the client that the input is invalid. This may seem harmless. But suppose the user types in "" and the search engine returns "Nothing is found for ." Possibly, the client's web browser will interpret the script tags and execute the alert (`aaa') function. If so, the search engine is probably susceptible to a cross-site scripting attack. This is a common method attackers use to find vulnerable sites. To simulate an advanced cross-site scripting attack, we created an online banking site (www.Freebank.com) and a web server that could receive any information garnered during a successful attack. The attacker starts by searching a targeted web site for pages that return client-supplied data. In this example, the attacker finds that when a login attempt fails, the FreeBank web application displays the username that was entered (see Figures 1 and 2). © 2005 SPI Dynamics, Inc. All Rights Reserved. No reproduction or redistribution without written permission. 5 Start Secure. Stay Secure.TM Cross-Site Scripting Figure 1: Submitting an incorrect username. © 2005 SPI Dynamics, Inc. All Rights Reserved. No reproduction or redistribution without written permission. 6 Start Secure. Stay Secure.TM Cross-Site Scripting Figure 2: The failed login response. Now that the attacker knows that the page will "hand back" information in the login page, he must determine where to specify this value. The URL bar in Figure 2 reads: http://www.freebank.com/banklogin.asp?err=Invalid%20Login:%20Bad Login "Invalid Login: BadLogin" occurs both as an argument to the "err" parameter (simply an error statement reflecting that no login information exists for BadLogin), and is also included in the display text on the page itself. (Note that "%20" is simply a different encoding of the space character.) © 2005 SPI Dynamics, Inc. All Rights Reserved. No reproduction or redistribution without written permission. 7 Start Secure. Stay Secure.TM Cross-Site Scripting Next, the attacker tests the page to see if it is possible to inject HTML and Javascript into the web page. This is done by entering a line of script as the username. In turn, this affects the output code of the dynamically created page, changing "err=Invalid Login: BadLogin" to "err=." If the web application is indeed vulnerable, a pop-up box with the message "XSS" will appear (see Figures 3 and 4). Figure 3: Testing for vulnerability. © 2005 SPI Dynamics, Inc. All Rights Reserved. No reproduction or redistribution without written permission. 8 Start Secure. Stay Secure.TM Cross-Site Scripting Figure 4: The cross-site scripting test is successful. Now that the attacker knows that cross-site scripting attacks are possible, he must create an attack URL that can be used to steal sensitive information. Because the HTML code of every web page is different, this URL must be custom-crafted for every unique page. To determine how to build the attack URL, the attacker must view the HTML source of the web page and adjust the attack URL accordingly. This is shown in Figure 5, with the injected script code added by the attacker highlighted. © 2005 SPI Dynamics, Inc. All Rights Reserved. No reproduction or redistribution without written permission. 9 Start Secure. Stay Secure.TM Cross-Site Scripting![]() | |
| |