Microsoft KB Archive/253119

= How To Review ASP Code for CSSI Vulnerability =

Article ID: 253119

Article Last Modified on 7/8/2005

-

APPLIES TO


 * Microsoft Active Server Pages 4.0

-



This article was previously published under Q253119



SUMMARY
This article describes how to identify and correct Active Server Pages (ASP) applications that are susceptible to Cross-Site Scripting Security Issues (CSSI). Only input that is not correctly validated or formatted makes your application vulnerable to attack.



MORE INFORMATION
The following steps help you identify and correct ASP applications that are susceptible to CSSI:   Look for ASP code that generates HTML to be displayed. ASP writes HTML to the output in two ways: Response.Write

-and-

<% =                    Determine whether the HTML output includes input parameters. These parameters can come from a variety of sources. The following list includes common input sources:

 When you find ASP code that generates HTML using some input, you need to evaluate solutions for your specific application. The solutions below present some general concepts to help you begin prevention of CSSI.

Please note that when filtering or encoding, you need to specify a character set for your Web pages to ensure that your filter is checking for the appropriate special characters. The data inserted into your Web pages should filter out byte sequences that are considered special based on the specific character set (charset). A popular charset is ISO 8859-1, which is the default in early versions of HTML and HTTP. You must take into account localization issues when you change these parameters.   Use the HTMLEncode method to encode input parameters when generating display.

In general, most CSSI attacks can be prevented simply by using HTMLEncode on input parameters. Using HTMLEncode works by replacing characters that have special meanings in HTML to HTML variables that represent those characters; (for example, & = &, " = "). Please note that only the data needs to be encoded, and not the full strings. <% Response.Write("Hello visitor " +     Server.HTMLEncode(Request.Form("UserName")) +      ""); %>                          HTTP_REFERER can be used to limit the domain from which requests can be submitted.

HTTP_REFERER returns a string that contains the URL of the original request when a redirect has occurred. Web servers can check the referrer field when they receive a filled-in form and reject it if it does not come from the right place. You can check the HTTP_REFERER in the following way: <%  If (Request.ServerVariables("HTTP_REFERER") = "") Or _ (Left(Request.ServerVariables("HTTP_REFERER"),42) <> _     "http://www.myserver.com/AppDir/mainfrm.asp") Then Response.Redirect "http://www.myserver.com/AppDir/mainfrm.asp" End If  %> NOTE: The referrer field has some limitations:  You risk blocking legitimate form submissions.</li> The link may come from an e-mail or bookmark that does not have a URL.</li> Browsers may deliberately clear the referrer field, such as during an HTTPS request.</li></ul> </li>  Use URLEncode to encode URLs received as input parameters.

The URLEncode method applies URL encoding rules, including escape characters, to a specified string. You should encode incoming URLs before displaying them. Here is a sample for URLEncode: <%     var BaseURL = http://www.mysite.com/search2.asp?searchagain=; Response.write("<a href=\"" + BaseUrl +     Server.URLEncode(Request.QueryString("SearchString")) +      "\">click-me</a>"); %>                       </li>  Strip or modify special characters from input parameters. Special characters include the following: < > " ' % ; ) ( & + You can strip or modify characters when you read them in or when you display them to the browser depending on your application.

This sample uses JavaScript to filter special characters: function RemoveBad(strTemp) { strTemp = strTemp.replace(/\<|\>|\"|\'|\%|\;|\(|\)|\&|\+|\-/g,""); return strTemp; } This sample uses Visual Basic Scripting Edition version 5.0 regular expressions to filter special characters:  Function ValidateTags(QueryString)    Dim o    Set o = CreateObject("VBScript.RegExp") ' -> VB Script 5.0

Dim sBad

sBad = "(<\s*(script|object|applet|embed|form)\s*>)"  ' <  script xxx > sbad = sbad & "|" & "(<.*>)" ' >xxxxx< warning includes hyperlinks and stuff between > and < sbad = sbad & "|" & "(&.{1,5};)"  ' &xxxx; sbad = sbad & "|" & "eval\s*\("   ' eval  ( sbad = sbad & "|" & "(event\s*=)" ' event  = 'Now lets check for encoding sbad = Replace(sbad,"<", "(<|%60|<)") sbad = Replace(sbad,">", "(>|%62|>)") o.IgnoreCase = True 'ignore case of string o.Global =False 'stop on first hit

o.Pattern = sBad

ValidateTags = o.Test(QueryString)

Set o = Nothing End Function </li></ul> </li></ol>

<div class="references_section">