logo

How I Hacked a Popular Website Using SQL Injection

logo

sql injection 150x150 How I Hacked a Popular Website Using SQL InjectionFew people really mind security matters before anything bad happens to them. Once they get to feel how it is to lose data or give away precious information due to security faults, they begin to get more concerned over protecting their data. When it comes to websites, SQL security is one of the most important thing that haunts them. Of course, the most important one, is the php/asp or whatever code it uses.

The problem with almost every security fault is trust. In the world of websites and user related data, nothing is to be taken for granted. Do you ask your users to register for your website under a given name ? Then, you should check what that input. Never, ever assume that the website user will not be a malicious person. Do not trust your users !

What is SQL injection ?

This idea extends to lots of different exploits, but certainly one of the most well known and easily exploitable ones is definately mySQL injections. As with any other input devices, databases handle user input as well, and heavily. In this post, i assume that you’re at least familiar with SQL. If not, please take a look at this tutorial about mysql programming.

The scenario is simple. There is a part of a website that handles some user input and then makes a simple query back to the database. Let’s suppose that in this webpage, a user gets to view his/her emails. The url of the page is like “www.website.com?index.php?page=mail&uid=3″.

Now, as you see, there is a GET parameter passed to a php script. This corresponds to our user id, being 3. So, in the actual php script, the code that gets a user emails from the database would be like :


$query = "SELECT * FROM mails WHERE uid='$_GET['uid']' ";
mysql_query($query);

Now this seems ok, doesn’t it ? Actually, it is a very vulnerable execution flow. Using that, an attacker can even delete the whole database, in some cases. What they could easily do, is get the mails of other users apart from the ones of their legitimate account. As you see, we specify the uid value through the url. What would happen if that value was 4 instead of 3 ? Well, we would just be able to see every email, that a user who was unlucky enough to have the user id 4, has stored inside the system.

How I Hacked a Popular Website

Well, there are actually hundreds of websites that follow this scheme. However, i was surprised to see that a very well known website had this trivial security problem. I will not specify the actual url, but i will present you with the test case. One of the very first things that i do when i want to test a website for vulnerabilities, is check if there is an /admin page.

In this case, there was one. A typical one, needing a username and a password. The next step is checking for sql vulnerabilities. One of the easiest ways to do so is just input a single quote (‘) as a username or password. Doing so, you get this error :

“error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ”” and password=” )’ at line 1″

This now makes it sure for us that there is a big mySQL vulnerability here. Why this error comes up ? Well, think of the previous query specified above. If you just input a single quote, the query becomes faulty and mySQL returns an error code.We know that this is vulnerable, but what can we do ? First of all, let’s think how the actual registration check may look like. It could be like this :


$query = "SELECT COUNT(*) as count FROM users WHERE password='$password' ";
 $result = mysql_query($query);

if ($result['count'] > 0)  ..... // you are admin

If somebody enters the correct admin password, they become an administrator. While this would be fair, the implementation is not secure. We could make it so that the query returns more than 0 results. But how ? What about selecting all the users of the system ? If the query was like :


$query = "SELECT COUNT(*) as count FROM users WHERE password='$password' OR 1=1";

This selects every user, if the password correct or 1=1. But 1 will be 1 for every user ! Therefore, every user will be selected. This principle is what makes it easy for us now to exploit. The key would be an input value like 1′ or ’1′=’1 . This makes the sql command like :


$query = "SELECT COUNT(*) as count FROM users WHERE password='1' or '1'='1'  ";

Do you see it ? It returns as a count the total number of all the users in the database, meaning that we effectively get to be admins. Once you input that, welcome to the administration panel ..

In The End, Sanitize

If you do not that to happen to your programs, make sure that you check what the user has inputted. First of all, escape special characters. Quotes should always be escaped for instance. Take a look at magic quotes for mysql. Then, also make sure that if you need a numeric value, never accept strings. Limit user input and guess what their “out of the ordinary” input can be. Play safe to be safe.

pixelstats trackingpixel
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • MySpace
  • Technorati
  • Slashdot
  • StumbleUpon
  • Print
  • Add to favorites
If you enjoyed this post, make sure you subscribe to my RSS feed!

Random Posts

14 Responses to “How I Hacked a Popular Website Using SQL Injection”

  1. You are right on the money with this post, keep up the good work!

    Like or Dislike: Thumb up 0 Thumb down 0

  2. I really enjoy your site, thanks for posting!

    Like or Dislike: Thumb up 0 Thumb down 0

  3. Extremely interesting blog post thank you for writing it I just added your website to my favorites and will check back :) By the way this is a little off subject but I really like your blogs layout.

    Like or Dislike: Thumb up 0 Thumb down 0

  4. Couldn’t be written any better. Reading this post reminds me of my old room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thanks for sharing!

    Like or Dislike: Thumb up 0 Thumb down 0

  5. gives use a great website decent Gives thanks for the hard work to support people

    Like or Dislike: Thumb up 0 Thumb down 0

  6. Full Game says:

    shares use a terrific web page decent Gives bless you for the working hard to help out myself

    Like or Dislike: Thumb up 0 Thumb down 0

  7. Hi, i must fantastic site you have, i stumbled across it in Yahoo. Does you get much traffic?

    Like or Dislike: Thumb up 0 Thumb down 0

  8. Alan says:

    special characters are always a killer
    Alan´s last blog ..Clone High – A Show That Was Canceled Too Soon My ComLuv Profile

    Like or Dislike: Thumb up 0 Thumb down 0

  9. Sueann Sides says:

    Incredibly wonderful website! Had a great deal of fun to read it. Thank you! :)

    Like or Dislike: Thumb up 0 Thumb down 0

  10. Incredibly wonderful man! Keep posting :)

    Like or Dislike: Thumb up 0 Thumb down 0

  11. Okay article. I just became aware of your blog and desired to say I have really enjoyed reading your opinions. Any way I¡¯ll be subscribing in your feed and Lets hope you post again soon.

    Like or Dislike: Thumb up 0 Thumb down 0

  12. Simply want to say your article is as tonishing. The clearness in your post is simply spectacular and i can assume you are an expert on this subject. Well with your permission allow me to grab your rss feed to keep up to date with forthcoming post. Thanks a million and please keep up the gratifying work.

    Like or Dislike: Thumb up 0 Thumb down 0

  13. Guy Zalar says:

    Hello, great post.

    Like or Dislike: Thumb up 0 Thumb down 0

  14. liked this article!

    Like or Dislike: Thumb up 0 Thumb down 0

Leave a Reply

CommentLuv Enabled
logo
logo
Copyright 2009 All Rights Reserved