Download presentation
Presentation is loading. Please wait.
Published byDarlene Horn Modified over 8 years ago
1
S ECURE P ROGRAMMING NOTES 08 XSS 1
2
Cross-Site Scripting attacks are a type of injection problem, in which malicious scripts are injected into the trusted web sites. Cross-site scripting (XSS) attacks occur when an attacker uses a web application to send malicious code, generally in the form of a browser side script, to a different end user. Flaws that allow these attacks to succeed are quite widespread and occur anywhere a web application uses input from a user in the output it generates without validating or encoding it. An attacker can use XSS to send a malicious script to an unsuspecting user. The end user’s browser has no way to know that the script should not be trusted, and will execute the script. Because it thinks the script came from a trusted source, the malicious script can access any cookies, session tokens, or other sensitive information retained by your browser and used with that site. These scripts can even rewrite the content of the HTML page. 2
3
Cross-Site Scripting (XSS) attacks occur when: Data enters a Web application through an untrusted source, most frequently a web request. The data is included in dynamic content that is sent to a web user without being validated for malicious code. The malicious content sent to the web browser often takes the form of a segment of JavaScript, but may also include HTML, Flash or any other type of code that the browser may execute. The variety of attacks based on XSS is almost limitless, but they commonly include transmitting private data like cookies or other session information to the attacker, redirecting the victim to web content controlled by the attacker, or performing other malicious operations on the user's machine under the guise of the vulnerable site. 3
4
XSS C ATEGORIES Stored XSS Attacks The injected code is permanently stored on the target servers, such as in a database, in a message forum, visitor log, comment field, etc. The victim then retrieves the malicious script from the server when it requests the stored information. Reflected XSS Attacks The injected code is reflected off the web server, such as in an error message, search result, or any other response that includes some or all of the input sent to the server as part of the request. Reflected attacks are delivered to victims via another route, such as in an e-mail message, or on some other web server 4
5
DOM based XSS Attacks The attack payload is executed as a result of modifying the DOM “environment” in the victim’s browser used by the original client side script, so that the client side code runs in an “unexpected” manner. That is, the page itself (the HTTP response that is) does not change, but the client side code contained in the page executes differently due to the malicious modifications that have occurred in the DOM environment. 5
6
XSS USING S CRIPT IN A TTRIBUTES XSS attacks may be conducted without using tags. Other tags will do exactly the same thing, for example: or other attributes like: onmouseover, onerror. onmouseover click me! onerror <img src="http://url.to.file.which/not.exist" onerror= alert(document.cookie);> 6
7
XSS USING S CRIPT V IA E NCODED URI S CHEMES If we need to hide against web application filters we may try to encode string characters, e.g.: a=A (UTF-8) and use it in IMG tag: There are many different UTF-8 encoding notations what give us even more possibilities. 7
8
XSS USING CODE ENCODING We may encode our script in base64 and place it in META tag. This way we get rid of alert() totally. More information about this method can be found in RFC 2397 These (just a little modified by me) and others examples can be found on http://ha.ckers.org/xss.html, which is a true encyclopedia of the alternate XSS syntax attack. 8
9
E XAMPLE 1 The following JSP code segment reads an employee ID, eid, from an HTTP request and displays it to the user.... Employee ID: The code in this example operates correctly if eid contains only standard alphanumeric text. If eid has a value that includes meta-characters or source code, then the code will be executed by the web browser as it displays the HTTP response. 9
10
Initially this might not appear to be much of a vulnerability. After all, why would someone enter a URL that causes malicious code to run on their own computer? The real danger is that an attacker will create the malicious URL, then use e-mail or social engineering tricks to lure victims into visiting a link to the URL. When victims click the link, they unwittingly reflect the malicious content through the vulnerable web application back to their own computers. This mechanism of exploiting vulnerable web applications is known as Reflected XSS. 10
11
E XAMPLE 2 The following JSP code segment queries a database for an employee with a given ID and prints the corresponding employee's name. <%... Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from emp where id="+eid); if (rs != null) { rs.next(); String name = rs.getString("name"); %> Employee Name: As in Example 1, this code functions correctly when the values of name are well-behaved, but it does nothing to prevent exploits if they are not. Again, this code can appear less dangerous because the value of name is read from a database, whose contents are apparently managed by the application. 11
12
However, if the value of name originates from user- supplied data, then the database can be a conduit for malicious content. Without proper input validation on all data stored in the database, an attacker can execute malicious commands in the user's web browser. This type of exploit, known as Stored XSS, is particularly insidious (harmful effect) because the indirection caused by the data store makes it more difficult to identify the threat and increases the possibility that the attack will affect multiple users. XSS got its start in this form with web sites that offered a "guestbook" to visitors. Attackers would include JavaScript in their guestbook entries, and all subsequent visitors to the guestbook page would execute the malicious code. 12
13
As the examples demonstrate, XSS vulnerabilities are caused by code that includes unvalidated data in an HTTP response. There are three vectors by which an XSS attack can reach a victim: As in Example 1, data is read directly from the HTTP request and reflected back in the HTTP response. Reflected XSS exploits occur when an attacker causes a user to supply dangerous content to a vulnerable web application, which is then reflected back to the user and executed by the web browser. As in Example 2, the application stores dangerous data in a database or other trusted data store. The dangerous data is subsequently read back into the application and included in dynamic content. Stored XSS exploits occur when an attacker injects dangerous content into a data store that is later read and included in dynamic content. A source outside the application stores dangerous data in a database or other data store, and the dangerous data is subsequently read back into the application as trusted data and included in dynamic content. 13
14
E RROR P AGE E XAMPLE Let's assume that we have an error page, which is handling requests for a non existing pages, a classic 404 error page. We may use the code below as an example to inform user about what specific page is missing: <? php print "Not found: ". urldecode($_SERVER["REQUEST_URI"]); ?> 14
15
Let's see how it works: http://testsite.test/file_which_not_exist In response we get: Not found: /file_which_not_exist Now we will try to force the error page to include our code: http://testsite.test/ alert("TEST"); The result is: Not found: / (but with JavaScript code alert("TEST"); ) We have successfully injected the code, our XSS! What does it mean? For example, that we may use this flaw to try to steal a user's session cookie. 15
16
DOM BASED XSS A TTACK E XAMPLE Suppose the following code is used to create a form to let the user choose his/her preferred language. A default language is also provided in the query string, as the parameter “default”. … Select your language: document.write(" "+ document.location.href.substring(document.location.href. indexOf("default=")+8)+" "); document.write(" English "); … The page is invoked with a URL such as: http://www.some.site/page.html?default=French 16
17
A DOM Based XSS attack against this page can be accomplished by sending the following URL to a victim: http://www.some.site/page.html?default= alert(document.cookie) When the victim clicks on this link, the browser sends a request for: /page.html?default= alert(document.cookie) to www.some.site. The server responds with the page containing the above Javascript code. The browser creates a DOM object for the page, in which the document.location object contains the string: http://www.some.site/page.html?default= alert(document.cookie) 17
18
The original Javascript code in the page does not expect the default parameter to contain HTML markup, and as such it simply echoes it into the page (DOM) at runtime. The browser then renders the resulting page and executes the attacker’s script: alert(document.cookie) Note that the HTTP response sent from the server does not contain the attacker’s payload. This payload manifests itself at the client-side script at runtime, when a flawed script accesses the DOM variable document.location and assumes it is not malicious. 18
19
19 http://stackoverflow.com/questions/1336776/xss-filtering-function-in-php
20
20 Input Output
21
C ROSS S ITE R EQUEST F ORGERY ( FAKE ) CSRF (see-surf) is an attack which forces an end user to execute unwanted actions on a web application in which he/she is currently authenticated. With a little help of social engineering (like sending a link via email/chat), an attacker may force the users of a web application to execute actions of the attacker's choosing. A successful CSRF exploit can compromise end user data and operation in case of normal user. If the targeted end user is the administrator account, this can compromise the entire web application 21
22
Sometimes, it is possible to store the CSRF attack on the vulnerable site itself. Such vulnerabilities are called Stored CSRF flaws. This can be accomplished by simply storing an IMG or IFRAME tag in a field that accepts HTML, or by a more complex cross-site scripting attack. If the attack can store a CSRF attack in the site, the severity of the attack is amplified. In particular, the likelihood is increased because the victim is more likely to view the page containing the attack than some random page on the Internet. The likelihood is also increased because the victim is sure to be authenticated to the site already. 22
23
Cross-site request forgery (CSRF/XSRF) is almost the opposite of XSS, in that rather than exploiting the user's trust in a site, the attacker (and his malicious page) exploits the site's trust in the client software, submitting requests that the site believes represent conscious and intentional actions of authenticated users 23
24
E XAMPLE The attack works by including a link or script in a page that accesses a site to which the user is known (or is supposed) to have been authenticated. For example, one user, Bob, might be browsing a chat forum where another user, Fred, has posted a message. Suppose that Fred has crafted an HTML image element that references an action on Bob's bank's website (rather than an image file), e.g., If Bob's bank keeps his authentication information in a cookie, and if the cookie hasn't expired, then the attempt by Bob's browser to load the image will submit the withdrawal form with his cookie, thus authorizing a transaction without Bob's approval. 24
25
CSRF L IMITATION Several things have to happen for cross-site request forgery to succeed: The attacker must target either a site that doesn't check the referrer header (which is common) or, a victim with a browser or plugin bug that allows referrer spoofing (which is rare). The attacker must find a form submission at the target site, or a URL that has side effects, that does something (e.g., transfers money, or changes the victim's e-mail address or password). The attacker must determine the right values for all the form's or URL's inputs; if any of them are required to be secret authentication values or IDs that the attacker can't guess, the attack will fail. The attacker must lure the victim to a Web page with malicious code while the victim is logged in to the target site. 25
26
CSRF P REVENTION Web sites have various CSRF countermeasures available: Requiring a secret, user-specific token in all form submissions and side-effect URLs prevents CSRF; the attacker's site cannot put the right token in its submissions. Requiring the client to provide authentication data in the same HTTP Request used to perform any operation with security implications (money transfer, etc.) Limiting the lifetime of session cookies Checking the HTTP Referer header Ensuring that there is no clientaccesspolicy.xml file granting unintended access to MS Silverlight controls Ensuring that there is no crossdomain.xml file granting unintended access to Flash movies[15] Verifying that the request's header contains no X-Requested- With. Used by Ruby on Rails (before v2.0) and Django (before v1.2.5). This protection has been proven unsecured under a combination of browser plugins and redirects which can allow an attacker to provide custom HTTP headers on a request to any website, hence allow a forged requests. 26
27
http://misc-security.com/blog/2009/05/xss-cross- site-scripting/ 27
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.