Saturday, January 31, 2009
Tuesday, January 27, 2009
Wednesday, January 21, 2009
SQL Injection
This article will show you how you can hack in to vulnerable websites, and to check your own website for one specific vulnerability. It's OK to play around with this on your own site but do not be tempted to try it out on a site you do not own. If the site is properly managed, an attempt to log in using this or similar methods will be detected and you might find yourself facing charges under the Cyber Law. Penalties under this act are severe, including heavy fines or even imprisonment.
What is SQL Injection?
SQL stands for Structured Query Language, and it is the language used by most website databases. SQL Injection is a technique used by hackers to add their own SQL to your site's SQL to gain access to confidential information or to change or delete the data that keeps your website running. I'm going to talk about just one form of SQL Injection attack that allows a hacker to log in as an administrator - even if he doesn't know the password.
If your website has a login form for an administrator to log in, go to your site now, in the username field type the administrator user name.In the password field, type or paste this:
x' or 'a' = 'a
If the website didn't let you log in using this string you can relax a bit; this article probably doesn't apply to you. However you might like to try this alternative:
x' or 1=1--
Or you could try pasting either or both of the above strings into both the login and password field. Or if you are familiar with SQL you could try a few other variations. A hacker who really wants to get access to your site will try many variations before he gives up.
If you were able to log in using any of these methods then get your web tech to read this article, and to read up all the other methods of SQL Injection. The hackers and "skript kiddies" know all this stuff; your web techs need to know it too.
The technical stuff about SQL Injection
If you were able to log in, then the code which generates the SQL for the login looks something like this:
SELECT * FROM users WHERE username='" & username & "' AND Password='" & password & "'
When you log in normally, let's say using userid admin and password secret, what happens is the admin is put in place of username and secret is put in place of password.
The SQL that is generated then looks like this:
SELECT * FROM users WHERE username = 'admin' and PASSWORD = 'secret'
But when you enter
as the password, the SQL which is generated looks like this:
SELECT * FROM users WHERE username = 'admin' and PASSWORD = 'x' or 'a' = 'a'
Notice that the string: x' or 'a' = 'a has injected an extra phrase into the WHERE clause: or 'a' = 'a'.This means that the WHERE is always true, and so this query will return a row contain the user's details.
If there is only a single user defined in the database, then that user's details will always be returned and the system will allow you to log in. If you have multiple users, then one of those users will be returned at random. If you are lucky, it will be a user without administration rights.
How to defend against this type of attack
The best practice is to strip special characters in username & password field of login form. The parsing of characters from user input fields removes the required characters to successfully exploit a SQL injection hole. The usual suspects for potentially dangerous SQL injection characters include: ("*^';&><'/') One of the easiest mechanisms for removing these characters from user-supplied strings is to create a removal or substitution regular expression, which will search, find and remove the desired dangerous characters.
