Placed in: Home arrow Useful arrow Features arrow How you can prevent an SQL injection
How you can prevent an SQL injection

About the author:
The write up has been done by Joanna who works with WebHostingSearch and has been been part of web industry from quite some time now.

How you can prevent an SQL injection

An SQL Injection (also known as "Failure to Preserve SQL Query Structure") is one of the most common and most dangerous security issues. SQL injections are dangerous because they are a door wide open to hackers to enter your system through your Web interface and to do whatever they please - i.e. delete tables, modify databases, even get hold of your corporate network. SQL injections are a programming error and they have nothing to do with your web site hosting provider. So, if you have been searching for a secure JSP hosting, PHP hosting or any other type of web hosting packages, you need to know that prevention of an SQL injection is not a responsibility of your web site hosting provider but of your web developers.

Why an SQL Injection Occurs

SQL injections might be common but, ironically, they are also easy to prevent. SQL injections are common because SQL injection vulnerabilities are abundant (i.e. it is so easy to do) and because if an SQL injection is successful, the reward for the hacker could be substantial (i.e. a whole database to tamper with).

SQL injection risks arise every time when a programmer creates a dynamic database query, which contains user input. This means that the ways to prevent an SQL injection are two:

  • Don't write dynamic database queries
  • Don't allow user input in your queries.

The solution might be simple but it is impractical because dynamic queries are too useful to be avoided and user input is inevitable. This doesn't mean that SQL injections can't be prevented. With some coding techniques applicable in any programming language, SQL injections can be prevented.

What Can Be Done to Prevent an SQL Injection

Though the exact code differs depending on the programming language you use, the basic principles to prevent an SQL injection are similar. Here are some examples how you can do it:

  • Use dynamic SQL only if absolutely necessary.

    Dynamic SQL can almost always be replaced with prepared statements, parameterized queries, or stored procedures. For instance, instead of dynamic SQL, in Java you can use PreparedStatement() with bind variables, in .NET you can use parameterized queries, such as SqlCommand() or OleDbCommand() with bind variables, and in PHP you can use PDO with strongly typed parameterized queries (using bindParam()).

    In addition to prepared statements, you can use stored procedures. Unlike prepared statements, stored procedures are kept in the database but both require first to define the SQL code, and then to pass parameters.

  • Escape user input.

    Escaping user input is less effective than parameterized queries and stored procedures but if parameterized queries and stored procedures can't be used, escaping user input is still more than nothing. The exact syntax for escaping user input varies depending on the database, so you need to check your DB docs for the correct syntax and examples.

  • Assume magic quotes is always off.

    When the magic_quotes_gpc variable is off, this can prevent some (but not all) SQL injection attacks. Magic quotes are not an ultimate defense and what is worse - sometimes they are off and you don't know about it. This is why it is necessary to have code for the substitution of quotes with slashes. Here is a neat piece of code Jon Lee suggests:

     
    $username = $_POST['username'];
    $password = $_POST['password'];
    if (!get_magic_quotes_gpc()) {
       $username = addslashes($username);
       $password = addslashes($password);
    }
  • Install patches regularly and timely.

    Even if your code doesn't have SQL vulnerabilities, when the database server, the operating system, or the development tools you use have vulnerabilities, this is also risky. This is why you should always install patches, especially SQL vulnerabilities patches, right after they become available.

  • Remove all functionality you don't use.

    Database servers are complex beasts and they have much more functionality than you need. As far as security is concerned, more is not better. For instance, the xp_cmdshell extended stored procedure in MS SQL gives access to the shell and this is just what a hacker dreams of. This is why you should disable this procedure and any other functionality, which can easily be misused.

  • Use automated test tools for SQL injections.

    Even if developers follow the rules above and do their best to avoid dynamic queries with unsafe user input, you still need to have a procedure to confirm this compliance. There are automated test tools to check for SQL injections and there is no excuse for not using them to check all the code of your database applications.

    One of the easiest tools (and a more or less a reliable one) to test SQL injections is the Firefox extension named SQL Inject ME. After you install the extension, the tool is available in the right-click context menu, as well as from Tools → Options. The sidebar of SQL Inject ME is shown in the next screenshot and as you can see there are many test you can run:

    SQL Inject 1

    SQL Inject 2

    You can choose which tests to run and which values to test. When you press one of the Test buttons, the selected tests will start. When the tests are done, you will see a report of how the tests ended.

    There are many options you can set for the SQL Inject ME extension, as shown in the next two screenshots:

    SQL Inject 3

    SQL Inject 4

As you see, there are many (and above all - simple) steps you can take in order to clean your code from potential SQL injection vulnerabilities. Don't neglect these simple steps because if you do, you will compromise the security not only of your sites but also of all the sites that are hosted with your web hosting provider.

About the author:
The write up has been done by Joanna who works with WebHostingSearch and has been been part of web industry from quite some time now.


Tags:  sql injection webhost security guest post

Interested in this topic? You might enjoy another article I've written called

Did you like this article? Subscribe to my feed or email to keep updated on new articles.

Spread the word and submit to:
Digg!Reddit!Del.icio.us!Facebook!StumbleUpon!
Comments
Add NewSearchRSS
yasmani   2010-04-03 01:54:10
Gravatar image I find this information very useful, security is always critical to a website.
Robin - You might want to check this out   2010-04-09 22:16:48
Gravatar image For everyone who's interested in preventing SQL injection from user input, you might want to check our newly released opensource library ValidForm Builder.

Some of the key features are:
- Fully CSS and webstandards based forms (no tables)
- Prevent SQL Injection using both clientside and serverside validation
- Check http://www.validformbuilder.org/ for more information, tutorials, complete API reference guide and ofcourse the source download!

This is no commercial; it's a free to use opensource library for creating webforms.
Robin - By the way   2010-04-09 22:29:25
Gravatar image By the way, Marco, are you aware of the fact that website linking is broken in your comments? :unsure:
Anonymous - thanks   2010-07-12 07:58:29
Gravatar image i have to say thanks for this useful information
Pink Laptops - Nice   2010-07-28 14:02:57
Gravatar image Good job. You have to be careful these days, especially when it comes to programming. Great article.
Praveen Kumar P.V - SQA Analyst   2010-08-31 10:34:01
Gravatar image Thank you for the valuable post. U done a very good job. Its appreciated. Expecting more good posts from you Joanna
Pink Laptops - MySQL & PHP   2010-11-12 23:00:07
Gravatar image I've recently switched to using PHP PDO for interfacing with the database, it certainly makes things more secure but is taking a bit of getting used too.
Anonymous   2010-11-24 17:54:32
Gravatar image '; DESC users; --
Anonymous   2010-11-24 17:54:33
Gravatar image 1' AND 1=(SELECT COUNT(*) FROM tablenames); --
Anonymous   2010-11-24 17:54:33
Gravatar image 1 AND USER_NAME() = 'dbo'
Anonymous   2010-11-24 17:54:35
Gravatar image 1 EXEC XP_
Anonymous   2010-11-24 17:54:41
Gravatar image 1' OR '1'='1
www.top-sale-shop.com   2011-04-26 02:39:02
Gravatar image As the leading character in fashion elegant watches, Ebel Watches has always been crowned as the superior symbol of performance and prestigiousness for over a century.Each of the Replica Ebel Watches we acquit has been carefully chose on the basis of quality and value.Perfect appear, high-grade and approachable costs.superior quality and first-class service can guarantee you leverage your wanted replica watches at ease on our website.These low-priced imitations make you look rich at a fraction of the price. It is no wonder that so many people prefer to purchase replicas.
over weight pills information - over weight pills information   2011-06-09 07:03:09
Gravatar image I am strongly associated with this site. As this site has inspired me a lot always in a new way and made my work easy by every time highlighting on the new issue and make me pleased. Thanks you people rock!!!!!!
pharmacy tech blog - pharmacy tech blog   2011-06-09 07:03:56
Gravatar image I am frequent reader of the articles and new knowledgeable post about new things always and would be searching new stuff for that. And I really thank you people for providing us new articles and post. Thanks a lot!
diabetes pills info - diabetes pills info   2011-06-09 07:04:53
Gravatar image It was really surprising to see such a wonderful post that is inspiring and informative and caught the attention of many people. I am a regular visitor of the blog and love the work of these people.
live birth control pills revie - live birth control pills reviews   2011-06-09 07:05:26
Gravatar image This is a wonderful website that has great info and is helpful for one and all. I always look forward for your website to gather any kind of information. Hope you people do like this only wonderful job. Cheers
drug store club info - drug store club info   2011-06-09 07:06:01
Gravatar image I often like surfing on net and find info on new things and this time I got a new website which has great info and is quite brilliantly written. Am just thrilled and excited to see this and hope to see more work of you people in future.
cheap online stores pharmacy - cheap online stores pharmacy   2011-06-09 07:06:39
Gravatar image You guys are really wonderful who search and bring such a wonderful info, I am glad to see this time also a useful stuff that had inspired me. Thanks a lot do keep giving us genuine stuff
overweight usage guides - overweight usage guides   2011-06-09 07:07:18
Gravatar image I enjoy reading the post and have become a great fan of yours. Keep up with the good job and please provide us with great blogs. I really appreciate the research you people take for the posts.
anti smoking pills center - anti smoking pills center   2011-06-09 07:07:56
Gravatar image It was perfect collection of such useful information. This was a helpful post foe me. Thanks for sharing such nice information. Thanks a lot!
discount online store pharmacy - discount online store pharmacy   2011-06-09 07:08:33
Gravatar image I was a great experience to read your post. I found your site from Google and thank a lot for this nice and wonderful information. The information posted was useful and interesting.
sleep better birth control pil - sleep better birth control pills   2011-06-09 08:07:43
Gravatar image Thanks a lot for such a wonderful post, the stuff posted were really interesting and useful. The quality of the content was good and clear. Thanks for the post
top drug store reviews - top drug store reviews   2011-06-09 08:08:33
Gravatar image I appreciate the ideas posted on your site; they were very informative and innovative. It was worth visiting your site. Thanks a lot for such valuable information.
diet side effects journal arti - diet side effects journal articles   2011-06-09 08:09:57
Gravatar image It is amazing to such useful information at one place. I was looking for the same information from a long time, at last I found it. Thanks for such innovative and amazing information.
hot pharmacy advisor - hot pharmacy advisor   2011-06-09 08:10:32
Gravatar image I was very pleased to visit your site; I was definitely a wonderful site. The post was worth reading. I enjoyed each bit of your post. Thanks for such excellent post.
clinic pharmacy spot - clinic pharmacy spot   2011-06-09 08:11:09
Gravatar image I recently came across your blog and have been reading along. I have no words except to say that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.
rajendra - WebHostingPad Reviews   2011-08-02 09:47:22
Gravatar image I was searching for some info is visible in this blog and useful info is visible in this blog. I am really very happy for providing the lot info in this blog. I am searching for some different info in this blog. This is very much impressive with this website. I had really like it very much for useful info in this blog. Thanks a lot for sharing the nice info in this blog.
Kapil Malhotra - Prevention of Sql Injection in Asp.Net   2011-11-12 09:45:42
Gravatar image Great stuff. Its really helpful for me. Here I a have found another nice post over the internet which explain nicely about the Prevention of Sql Injection in Asp.Net, You may check it by clicking on this link...
http://mindstick.com/Blog/228/Preventing%20SQL%20Injection

I would like to say thanks to everyone,

:( Thanks All
Vijayasabapathi.P   2011-12-08 02:56:02
Gravatar image The information is new and useful to me. Thanks.
richa - SQL Injection   2012-03-10 16:52:55
Gravatar image I am doing a project on sql injection prevention technique comparisons and one of the techniques that i want to use is AMNESIA and the paper that i referred states that there is a tool for this technique but i am not able to find this tool anywhere on the internet ,can anyone pls help me with this... please.....
Anonymous   2012-03-26 14:02:49
Gravatar image :woohoo: :huh: :whistle: ;) :s :pinch: :D :lol: <img src=illy:' /> :angry: :P B) :0 :0 :s ;) :whistle: <img src=ide:' /> <img src=hock:' /> <img src=ide:' /> <img src=ide:' /> <img src=ide:' /> <img src=ide:' /> <img src=ide:' /> <img src=ide:' /> <img src=ide:' /> <img src=ide:' /> <img src=ide:' /> <img src=ide:' /> :arrow: :idea: :?:
Raman - Junior Engineer   2012-04-21 09:30:58
Gravatar image Thank you Joanna! All this information is very new and great help to me as i start my career. Found this to be helpful in understanding prepared statements and sql injection:

[url]
http://www.programmerinterview.com/index.php/database-sql/example-of-prepared-statements-and-sql-injection-prevention/[/url]
Weeee - weeee   2012-09-19 03:35:10
Gravatar image :angry: :0 :confused: :cheer: B) :evil: <img src=illy:' /> :dry: :lol: :kiss: :D :pinch: :( <img src=hock:' /> :X <img src=ide:' /> :) :P :unsure: :woohoo: :huh: :whistle: ;) :s :!: :?: :idea: :arrow:

Weeeeeee
RosettaStone - Rosetta Stone cheap sell absolutely genuine   2012-11-19 04:16:53
Gravatar image http://www.rosettastonetrade.com/
So flowers, flesh out their ire to court inRosetta Stone accrument to disenchant acquiesce each public baseball, in additionally to look after in military talents your Rosetta Stone Cheap[url]

the masses via the result in into Rosetta Stone Onlineplay of all these dunks welling forth because Nike conglomeration: valorous, Nike manumitted bad feelings, irritated in

totalling to outstanding. Servicing goals warrant sports shoes or boots is normally clone: 1, the existent follow, your second participation in secondment

inspection service.
Parajumpers - Absolutely authentic down jacket cheap sale   2012-11-19 04:18:40
Gravatar image Thanks for taking the time to discuss this,Parajumpers
I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, Parajumpers Jakker would you

mind updating your blog with Parajumpers Long Bear [code][/code]more information? It is extremely helpful for me.
Kristina - Kristina   2013-01-03 19:44:52
Gravatar image Thanks for this post, it's given me a great starting point.

A small suggestion to you after paging through all those spam comments: Project Honeypot. Save your wonderful blog from discount watches and viagra! https://www.projecthoneypot.org/
Derwick - Derwick   2013-01-30 10:11:37
Gravatar image This is actually what I was looking for.I am feeling glad to came here.I would like to thank you for sharing such info with us,I like to suggest you that Keep sharing such ideas in the future as well.
jhj' - dsd   2013-03-25 07:20:06
Gravatar image dfdf
Read more...
Name:
Email:
  Gravatar enabled.
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Security Image
Please input the anti-spam code that you can read in the image.
Unsubscribe from e-mail notifications.
 
< Prev   Next >
Subscribe

Subscribe to Marcofolio