|
||||||
| Security Tutorials Tutorials on how to protect your software against crackers. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||||
|
In today's world of rapidly expanding technology, most of us have become completely dependent on the internet. For many of us, it serves as our main method of banking, holiday shopping, people use it to find their soul mate, and some of us even use it as our main method of social interaction [thats not me]. So it is a no-brainer to understand why keeping our data secure is important. The scary part is, all of these actions involve the storage of your personal information on a remote server and the security of your private information, is completely dependent on the security of the environment in which you interact. Which is why webmasters need to take into account the strength of their database queries. One of the most common security flaws is the ability of code to be injected into a database query and perform malicious actions. This is known as a SQL Injection.
What is an SQL injection? Before I fully divulge what an SQL injection is, lets setup a simple scenario. Bob is a freelance programmer heired by the Extreme Banking Association to development an online banking system, which stores the credit card information of its users. Bob creates a mysql database similar to the following structure: SQL Code:
He then imports some data into the database: SQL Code:
At this point, the manager of the project should see Bob's incompetence and lack of skill. What kind of idiot would store passwords in plain text? Anyway, Bob continues with this code and creates a basic login system, and it has a structure similar to the following: PHP Code:
At this point, it does everything the manager of the project wants, enter your username/password and the users credit card number is displayed. So, Bob withdraws his money from the escrow account and goes on his merry way. One day an ub3r l33t h4x0r comes along, and attempts an SQL injection. He sees that the form uses a GET method request, and attempts to alter the data that is sent to the server. He enters the following line into his address bar: Quote:
Quote:
Quote:
PHP Code:
How do I prevent SQL injections? The answer is simple – never trust user input. That is the single most important concept in developing a secure application. The concept is simple, but taking action is a little more difficult. The easiest way to prevent SQL injections, is to escape data. The PHP developers implemented a feature(?) called Magic Quotes, which escapes quotes, NULL characters, backslashes, among other characters. This was designed as a security feature to help prevent SQL injections, however has caused both me, and many developers headaches, which is probably why as of PHP 6.0, Magic Quotes will be depreciated. As a result, it is a poor idea to rely on Magic Quotes as a solution to your SQL injections. In fact, even with Magic Quotes enabled, SQL injections are still possible. The code I am going to provide you is probably not the most secure code, as I am far from an expert on security, but I have found it as a decent solution for many of my problems regarding SQL injections. First thing we want to do is remove all of the damage done by Magic Quotes: PHP Code:
Next, take advantage of the mysql_real_escape_string() function in our query: PHP Code:
PHP Code:
Finally, strengthen the session validation, and the final script will look like this: PHP Code:
As far as SQL injections go, you should be pretty safe!
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall Last edited by John; 12-15-2007 at 10:39 PM. |
| Sponsored Links |
|
|
|
|||||
|
Pretty good article.
There's a little bug in your SQL injection though. I don't think the SQL injection you come up with would work correctly. Code:
' OR 1 = '1 Code:
' OR '1' = '1 I actually made a blogpost on this topic a while ago, if anyone should be interested. Last edited by v0id; 09-24-2007 at 07:38 AM. |
|
|||||
|
Thanks very much! When I saw this I thought that my website was hacked because of it, but I tested it and at least it is not vulnerable to this threat. So I can eliminate an SQL Injection. Thanks and great tutorial/article.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall ![]() Business Directory | Technology Blog | Windows Help |
|
|||||
|
Excellent article! Thank you for the Tutorial.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
|
|||||
|
Quote:
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
| Sponsored Links |
|
|
|
|||||
|
Touché. I was actually going to create a login system and host it here as a demo, but I didn't have time. Although I would still like to do so in the future.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |