Loading...
image
In part 2 of 'How websites get hacked' I'm going to take a look at cross site scripting (XSS) and SQL Injection as attack vectors. How do these methods work and how can we defend against them.

Ongoing Projects

Microcyte Content Management System
Snatch (Mac OS X)
Snatch is a website scraping tool which can be used to retrieve links, images and email addresses from a given webpage and linked pages.
image
Distribution (Mac OS X)
Distributions is a mailing list management tool for Mac OS X. It features support for Multiple Classifications and some CRM functions.
image
??? (Mac OS X)
This is a new project I am working on for OS X (leopard). more soon!
image

Opinions & Views

image Previous | Next image

You may already have heard of Cloud Computing, Cloud Hosting is an exciting extension of this relatively new area.
I'm now back online after an outage in America caused my Hosting Provider some downtime.
Recently I had to convert a shed load on WMA's to work with iTunes on a Mac, heres how I did it at no cost.
As I work towards the 2.0 release of MicroCyte I wonder whether I should scrap XML/static files in favor of a database?
I've recently had the opportunity make extensive use of a 24" iMac 2.8Ghz, here's what I thought!
Recently I went to see Nizlopi perform at the Norwich arts centre, here's what I thought
MicroCyte has been released! head on over to microcyte.co.uk to checkout the demo and download your copy!
Very soon Firefox 3 will be released. This update will mean support for Microcyte CMS.
Regulars may have noticed that the site has been quiet for the last few days, click through to find out why!
In the last couple of days I've implemented a comments plugin for my Microcyte CMS
In Part 3 of building a DAL in Classic ASP I look at how I implemented my Dynamic SQL module to fulfull my DAL requirements
In part 2 of building a DAL in Classic ASP I look at how to create a functional Dynamic SQL module.
Classic ASP is not known for its rich data access tools, so I look at how you can build a strong Data Access Layer.
Are there compelling reasons for an old school scripting house to move to a new fangled framework?
After 9 months of work, the W3C has published the first working draft of HTML 5.
E-shots can help drive targeted traffic to your website, but how do you avoid making them look like SPAM?
A/B testing can be used to dramically increase conversions on your e-commerce site. Here is basic overview.
In the final part of 'how websites get hacked' I'm going to look at Social Engineering, the non technical hack.
In part 2 of 'how sites get hacked' we look at XSS and SQL Injection
In this brief primer we look at how websites get hacked and what to do to protect yourself (part 1)
After the hype has cooled down, what are the pro's and con's of the new Apple MacBook Air
Opinion: why is the Macbook Air a full 300 pounds (600 dollars) more expensive than in the US?

Security - XSS and SQL injection

In part 1 of 'How websites get hacked' I started off by talking about why sites are hacked these days, what common attack methods there are and then I took a closer look at server level security and database security. In this part I'm going to focus on site code attacks which are the most common attack method.

CrossSite Scripting
CrossSite Scripting or XSS is the more complex of these two attack methods and requires a little more work by the attacker. There are three main types of XSS attack which all vary in difficulty. We'll focus on the easier and more common two attacks.

The concept of a XSS attack is simple: get some malious code to run in the victims web browser to steal or redirect data such as log-in credentials or sensitive data. This can be achieved because the browser is intellegent and can execute code independantly of the server. If you can trick the browser into executing code not sent from your server, or even trick the server into sending code to the browser that you didn't intend then you can execute an XSS attack.

XSS scenario A (Persistant)
In this scenario the attacker uses a form (such as a comments or contact form) to send some javascript or clever HTML to the server which is then stored in the backend database. When someone else (another logged in user for example) looks at the submitted comment, the malicious code is downloaded to their browser and run. This code might be a block of javascript which picks up the users session data (or even just what is showing on the screen at that time) and then sends it on to the attacker. This will happen for every user that views that comment potentially effecting a large group of people.

XSS scenario B (non-persistant)
This is slightly more difficult and requires some social engineering. The idea is that you might have something like a search engine on your site. When you type something into the search engine the results page might show you what you searched for (the untreated text you entered). An attacker could use this fact to send some code to the search engine which is then executed directly on the search results page.

You might think this is not a problem because only the person using the search engine is going to see the results page right? well, not quite. How about this scenario:

  1. Bad GuyA gains access to part of a site which has billing details and also has some sort of order ID search.
  2. Bad Guy A works out that the search facility can be used for XSS and constucts a URL which will submit malicious code to the order ID search facility processing funtion.
  3. Good User B logs into the website to check the status of their order and whilst they are logged in Bad Guy B sends them an email containing the malicious link to click saying there has been a problem with their order and they have to fill in an information form
  4. Good User B clicks the malicious link and it submits the XSS attack to the seach facility.
  5. Using Good User B's authenticated session the XSS attack is run on Good User B's browser and accesses the billing details pages sending Good User B's billing details to Bad Guy A.

Follow that? its a little tricky to get your head around and requires some co-ordination and social engineering but it does work and has been used successfully by attackers on many websites.

SQL injection
This type of attack is a bit more common and is easier to execute on many sites. It relies on the target site having a backend database which contains user information or sensitive data. SQL is a programming language which is generally used to tell databases what data to select and send back to the user.

This method of attack sends malicious SQL to the database through a web form such as a contact form, comments form or search form. The idea is that the malicious SQL will subvert the normal database actions of the form and force the database to return something sensitive like passwords or billing data which will then be sent back to the browser.

For the attack to be successful several factors must be present:

  • If the SQL fails and the database sends an error back to the browser the attacker can use this error message to deduce the structure of the database through a trial and error method. If the error message is hidden then the attacker has no idea if they are getting close or not.
  • The output from the target form could be subverted and forced to display data that the developer never intended like usernames and passwords instead of telephone numbers and addresses. Make sure the output script is designed only to output certain information and that the SQL the form uses only returns the information that is absolutely required from the database instead of getting all the columns in the database table.

Defending against these attacks - Validation
You can take several simple steps to defend against both of these attack methods. The simplest and most effective method of shoring up your application code is to thoroughly validate any input that comes from the browser through forms and query strings (the bit after ? in the URL). You can do this by taking an exclusive approach, this means:

  • Only letting through values and data-types you expect to see and discarding anything which doesn't fit the pattern (e.g. dont accept a sting value when you're expecting a numeric).
  • Actively strip any input of HTML tags using regular expressions.
  • Check specifically for script blocks and strip them out.
  • Use regular expressions to match email addresses and postcodes/ZIP code form inputs.
  • Escape quotes at all costs, many scripting languages will have a built-in method for doing this
  • Insert data in to the database using something like a prepared query rather than constructing raw SQL strings. This should automatically help to strip quotes and other nasties.
  • If your script/application does error for any reason, be sure to hide any error messages from the user. This gives the hacker far less information to play with.
Database Access
You can mitigate damage to the database by restricting the database access rights that the website code has. If a part of your web app will only ever need access a handful of tables, create a database user specifically for those tables with only the privileges it needs to work properly.
 
Stored Procedures
If you can, using stored procedures can also help boost database security because they (and the input they take) must be validated before they can be created and run. The work they do can also be restricted using the database security model. This makes it almost impossible to inject SQL into a stored procedure. The downside is that you may find you loose a certain degree of development flexibility moving the SQL out of your application.
 
Conclusion
It can be easier than you think to do SQL injection or XSS but by using at least some of the methods above you make life much harder for any potential crackers. Validating input is crucial and that will go a long way to preventing the bad guys from getting in.
 
In the next part I'm going to take a look at social engineering and see what practical measures you can take to stop your staff from give away the keys to the kingdom.
 
If you have any comments of would like to talk to me directly about any of the issues discussed here then please drop me a line.