Cross-site scripting (XSS) is a code injection attack.
WikiPedia: “XSS enables attackers to inject client-side script into Web pages viewed by other users.”
This means the attacker does not directly target a victim but targets a web site. To anyone using the website, a malicious code appears to be the part of the webpage and the code is executed. These codes perform some unintentional action. can happen every time when a the web page includes user input in its pages, because the attacker can then insert a string that will be treated as code by the victim's browser.
An Example:
A user is allowed to comment in a page. The input is directly displayed in the page.In this example I will create a simple jsp page that displays a table, and allow users to comment. I will then try to perform XSS to see if I can force the page to do something malicious.
My page looked sth like:
Project Code can be found here.
HTML of comment Section
<div>
Comments:<br>
<table>
<tbody><tr><td><b>Seldon</b></td><td>Lenord Cheated</td></tr>
<tr>
<td valign="top" align="top">Insert new comment</td>
<td></td>
</tr>
<tr>
<td valign="top">Name</td>
<td><form action="save" method="post">
<input type="text" name="name"><br>
<textarea name="com"></textarea><br>
<input type="submit">
</form></td>
</tr>
</tbody></table>
</div>
Note that the comments are saved in database.
Not for the next comment let me insert some Java Script.
<script type="text/javascript">
alert("If this appears, I can use XSS to change the contents");
</script>
The result of the such is Statement is
Comments:
Raj:<script type="text/javascript">
alert("If this appears, I can use XSS to change the contents");
</script>
This will show a "alert" every time someone visits the page.
Lets try this:
<script type="text/javascript">
var v = document.getElementsByClassName('divCell');
for (var i = 0; i < v.length; i++) {
v[i].innerHTML = 0;
}
</script>
This will set all table values to 0. A web visitor may not know that the site has been compromised.
Why is XSS so disastrous?
XSS attack only have affect a web page because JavaScript actions are only limited to browser, So it can't be very disastrous. But consider these:- JavaScript can make arbitrary modifications to the HTML and DOM.
- The implications:
- JavaScript can redirect you to a phishing site.
- JavaScript has access to some of the user's sensitive information, such as cookies.This is a step towards identity theft, and session fixation.
- JavaScript can send HTTP requests with arbitrary destinations by Ajax. This mean sensitive information in the wrong part.
To make thing even worse, victims may remain unaware of the attacks and every one who visits the XSS compromised page is a victim.
Some prevention Rules suggested by owasp.org:
- Never Insert Untrusted Data
- HTML Escape Before Inserting Untrusted Data into HTML Content
- JavaScript Escape Before Inserting Untrusted Data into JavaScript Data Values
- HTML escape JSON values
- Sanitize HTML.
All these are nothing but validation of Untrusted data.
http://excess-xss.com/ has a Great tutorial On XSS.