Showing posts with label Hacking. Show all posts
Showing posts with label Hacking. Show all posts

Sunday, May 20, 2012

Hacking ASP.Net Applications with XSS


This blog post I want to talk about preventing the hacker from using XSS attack in ASP.NET applications. 
The first defense is using ValidateRequest. By default Microsoft sets the ValidateRequest to true.


<%@ Page Title="" Language="C#" ValidateRequest="true" MasterPageFile="~/Demo.Master" AutoEventWireup="true" CodeBehind="Demo.aspx.cs" Inherits="XSS_Demo.Demo" %>


That is a good thing but it can cause of problems later on; the reason is it gives the programmer a false sense of security. Writing secure code is about intent. When we rely on default settings to control the behavior of how web pages validate input we rely on other people to make our code is secure and we are no longer coding with intent. Second issue is we are no longer relying on a proven security principle of  “Defense-in-Depth”. We understand that with any security mechanism it can and will be broken. The security mechanism may not have a current vulnerability known at the present time but over time the cyber criminal’s have proven they can find one or a work-around. So we always want a backup security mechanism. Our goal is simple and proven. When one security defense mechanism fails we have another. Making the effort for the cyber criminal increasing difficulty so he goes elsewhere.


Another reason why we don’t want to rely on ValidateRequest is web applications have a trend to become more complex with consecutive deployments into production with more and more complex user interaction. Down the road someone is going to need the user to be able to input the following characters “</>into a user control. As soon as that happens we get the error message “A potentially dangerous Request.Form value was detected from the client”. To resolve this error we need to set ValidateRequest to false and now our application is open to  XSS attacks. Now we have a decision, how are we going to provide the security against XSS attack vector? In my opinion when we bolt on security to an application already in production we have already lost some of the potential to make our application secure. I understand with an ever-changing production environment, business requirements changing, etc, some decisions will affect the architecture of an application in production. However that is not the case here.


Regular Expressions:
As a defense-in-depth mechanism we need to sanitize all data from our users. All data from users should be considered to be untrusted data. It should not be dependent on if the application is external or internal facing to our organization. If you don not have initial control/creation of the data it is untrusted.
Our next defense-in-depth mechanism that we should have in place from the beginning of the application is to verify the data is correctly formatted using a regular expression. We have two choices here; we can validate against bad input characters or validate for expected input. The best choice is to validate for expected input.
The other choice leaves us open to the cyber criminal’s expertise in obfuscating his attack to our regex filter. The following is just a sample of how the cyber criminal can obfuscate his attack.


<SCRIPT>alert(String.fromCharCode(88,83,83))</SCRIPT>
<SCRIPT SRC=http://ha.ckers.org/xss.js></SCRIPT>
<<SCRIPT>alert("XSS");//<</SCRIPT>
</TITLE><SCRIPT>alert("XSS");</SCRIPT>
<BODY ONLOAD=alert('XSS')>
<BR SIZE="&{alert('XSS')}">
<META HTTP-EQUIV="refresh" CONTENT="0;url=data:text/html;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4K">
<IFRAME SRC="javascript:alert('XSS');"></IFRAME>
<FRAMESET><FRAME SRC="javascript:alert('XSS');"></FRAMESET>
<DIV STYLE="width: expression(alert('XSS'));">
<OBJECT TYPE="text/x-scriptlet"
<HTML><BODY><?xml:namespace prefix="t" ns="urn:schemas-microsoft-com:time"><?import namespace="t" implementation="#default#time2"><t:set attributeName="innerHTML" to="XSS&lt;SCRIPT DEFER&gt;alert(&quot;XSS&quot;)&lt;/SCRIPT&gt;"></BODY></HTML>
<SCRIPT>document.write("<SCRI");</SCRIPT>PT <A HREF="http://66.102.7.147/">XSS</A>
<A HREF="http://%77%77%77%2E%67%6F%6F%67%6C%65%2E%63%6F%6D">XSS</A>
<A HREF="http://1113982867/">XSS</A>
<A HREF="htt    p://6&#9;6.000146.0x7.147/">XSS</A>
<A HREF="//www.google.com/">XSS</A>
<A HREF="//google">XSS</A>
<A HREF="http://google.com/">XSS</A>
<A HREF="http://www.google.com./">XSS</A>
<A HREF="javascript:document.location='http://www.google.com/'">XSS</A>
<A HREF="http://www.gohttp://www.google.com/ogle.com/">XSS</A>
// "\%3C|<") // catches first <
// "\%3E|>") // catches first >
// "((\%2F)|\/)") // catches forward slash inside closing tag
static Regex regExample = new Regex(@"((\%3C)|<)((\%2F)|\/)*[a-z0-9\%]+((\%3E)|>)");

The reader can try for him self to see if the above regex expression catches the above XSS script injections.
To see a more extensive list on ways to obfuscating XSS attacks please see http://ha.ckers.org/xss.html  or OWASP XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet 


AntiXSS/Web Protection Library:
We have one more option in our arsenal to help make our application secure that we should be using from the beginning. That is encoding. We can use HtmlEncode. HtmlEncode part of standard .NET framework that uses a blacklist approach to encoding, filtering out known bad characters, like <> and “ characters. We can give our self’s extra protection and greater flexibility by using Microsoft AntiXSS library; which has been rename “Web Protection Library WPL”.  Web Protection Library has a lot of encoding functions that will help you prevent XSS attacks in ASP.NET applications.  AntiXSS library uses a whitelist of known good characters. AntiXSS also has protection in place spanning characters sets in over different languages. Due to this approach, AntiXSS is inherently safer against new XSS attacks. Furthermore to my first point in this blog by using Microsoft Web Protection Library you are now coding with intent on writing secure code.


Resources Web Protection Library:
http://msdn.microsoft.com/en-us/security/aa973814
http://msdn.microsoft.com/en-us/library/aa973813.aspx
http://wpl.codeplex.com/





Monday, March 12, 2012

Currently reading


I am currently reading “Ghost in the Wires: My Adventures as the World's Most Wanted Hacker”. I am enjoying reading the book. It’s a little early for me to recommend the book but it is better then the previous book I read about Kenvin Mitnick.

I picked up a copy of “Takedown: The Pursuit and Capture of America's Most Wanted Computer Outlaw -- By The Man Who Did It” at a used bookstore and it was a total waste of time to read. Besides not covering the technical details well Shimomura comes off as a egomaniac. One good thing is Shimomura did was to step up and help end Kevin Mitnick’s rampage. Well not so good if you were Kevin Mitnick. If you are reading this to better under stand hackers like Kevin or technical details in how to prevent their intrusions into your systems then read something else.

I have read other accounts about the personalities involved in bring Kevin down including Kevin’s own accounts in interviews. The one thing I can say for sure is everyone seems to have his or her own perspectives of whom and what Kevin did and the amount of damage he is responsible for.

Another drawback to this story is Shimomura insistences in his bringing too much of his personal life into the story. His relationship with his girlfriend was a complete mystery to me why this filler was included in the story.

When I read these types of books I am looking for three things. One is an understanding of the events and a timeline so I can out things into perspective, Second is an understanding of the individuals involved and their motives and lastly is an understanding of the tools, how the hacks worked, and other technical details. I am not looking for a how to hack book but I do expect a good technical discussion. This book did not meet any of those objectives for me.

The police work and forensic data where not discussed in detail at all. To make matters worse the FBI and other law enforcement individuals do not come off in a good light when compared to Shimomura’s ego. The book would have benefited from a more team approach in using individuals along with law enforcement. I am at the opinion that everyone from Kevin, Shimomura to John Markoff  all saw dollar signs in stopping Kevin. A book written in a more honest approach about dealing with law enforcement individuals would have gone further in educating everyone into the dangers that Kevin and other hacker’s pose. I would not recommend this book to anyone.

I did read “Fatal System Error: The Hunt for the New Crime Lords Who are Bringing Down the Internet” and found that many of the complaints I listed in Takedown above were overcome. I was able to take away knowledge that was useful and the book was very entertaining story without being dishonest. The relationship with the main character and law enforcement was not without issues but it gave an honest account on getting law enforcement involved. I would recommend this book to anyone.




Tsutomu with John Markoff Shimomura. “Takedown: The Pursuit and Capture of Kevin Mitnick, America's Most Wanted Computer Outlaw-By the Man Who Did It Tsutomu Shimomura” Hyperion Books 1996.

Joseph Menn. “Fatal System Error: The Hunt for the New Crime Lords Who Are Bringing Down the Internet” PublicAffairs 2010

Kevin Mitnick. “Ghost in the Wires: My Adventures as the World's Most Wanted Hacker” Little, Brown and Company 2011