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:
Input Source Sample of ASP Code Access Method Request.Form Collection Response.Write Request.Form("username")
Request.QueryString Collection <%=Request.QueryString("username")%>
Response.Write Request.QueryString("username")
<a href="http://mysite/showuser.asp?username=<% = Request.QueryString("username") %>">mypage</a>
<a href="http://mysite/mypage.asp"><%=Request.QueryString("url")%></a>
Request Object Response.Write("username")
Databases / Data Access Methods Do While Not rst.EOF Response.Write rst("myfield") & "<br>" rst.MoveNext Loop
Cookies Collection Response.Write Request.Cookie("username")
Session and Application Variables Response.Write Session("username")
- 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 <I>" + Server.HTMLEncode(Request.Form("UserName")) + "</I>"); %>
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.
- The link may come from an e-mail or bookmark that does not have a URL.
- Browsers may deliberately clear the referrer field, such as during an HTTPS request.
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>"); %>
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
REFERENCES
For more information, see the following advisory from the Computer Emergency Response Team (CERT) at Carnegie Mellon University:
For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:
252985 How To Prevent Cross-Site Scripting Security Issues For Web Applications
253121 How To Review MTS/ASP Code for CSSI Vulnerability
253120 How To Review Visual InterDev Generated Code for CSSI Vulnerability
253117 How To Prevent Internet Explorer and Outlook Express CSSI Vulnerability
Microsoft provides third-party contact information to help you find technical support. This contact information may change without notice. Microsoft does not guarantee the accuracy of this third-party contact information.
Keywords: kbhowto kbcssi kbsecvulnerability kbcodesnippet kbsecurity KB253119