ZDNet Asia - Where technology means business
HomeNewsInsightBlogsTechJobsTechGuidesDownloads
Advertisement
Toolkits for Developers
  Security
Advertisement
Builder > Architect > Database > Article
Talkback
Print story
Email Story
Prevent SQL injection
By Arthur Fuller, Builder.com
Monday, March 28 2005 01:06 PM

SQL injection is a strategy for attacking databases. The attacker "injects" a SQL statement into another statement--often to inflict damage upon your database. Web sites that interface with databases are particularly vulnerable to SQL injection because they often rely on dynamic SQL. Here's a simple example.

An ASP page asks the user for a name and a password, and then sends the following string to the database:

SELECT FROM users WHERE username = 'whatever' AND password = 'mypassword'

It seems safe, but it isn't. A user might enter something like this as her user name:

' OR 1>0 --

When this is plugged into the SQL statement, the result looks like this:

SELECT FROM users WHERE username = '' OR 1>0 -- AND password = ''

This injection comments out the password portion of the statement. It results in a list of all the names in the users table, so any user could get into your system.

The easiest way to prevent this sort of injection is to parse the SQL string and remove any occurrences of "--" before passing the statement.

You also have to beware of injections that contain semicolons because semicolons delimit SQL statements. Think about the implications of a user name like this:

' OR 1>0 ; DELETE Customers ; --

There are numerous ways a malicious user might penetrate your system using SQL injection and various defenses, but the simplest approach is to avoid dynamic SQL. Instead, use stored procedures everywhere. Thanks to the way SQL passes parameters, injections such as those above will produce errors, and the stored procedure will not execute.

Arthur Fuller has been developing database applications for more than 20 years. He frequently works with Access ADPs, Microsoft SQL 2000, MySQL, and .NET.


   Back to Database  
Print story  Email Story  


 Sponsored Links
Data Center Secrets   Discover what makes a great data center – and be rewarded.
Compare your IT salary   Sign-up for free download of IT salary benchmark report 2008
ZDNet Asia:  News  |  Insight  |  Blogs  |  SMB  |  IT Library  |  TechGuides Toolkits  |  Downloads  |  Premium Newsletters  |  RSS feeds
Search  
Around the World:     ZDNet AU  |   ZDNet China  |   ZDNet Taiwan  |   ZDNet India  |   ZDNet Korea  |   ZDNet Japan  |   ZDNet.com  |
  ZDNet UK  |   ZDNet Germany  |   ZDNet France  |   CNET Asia  |   CNET.de  |   CNET Australia  |   CNET France  |   CNET Japan  |   CNET Taiwan  |   CNET UK  |   CNET.com  |   News.com  |   activeTechPros  |   BNET  |   businessMOBILE.fr  |   Download.com  |   TechRepublic  |   Silicon.com  |   Builder  |   MySimon  |
  GameSpot  |   GameSpot Korea  |   MP3.com  |   TV.com
Advertise  |   About CNET Networks  |   About ZDNet Asia  |   Go to CNET Asia  |   Jobs @ CNET in Asia
Copyright © 2009 CNET Networks, Inc. All rights reserved.  Privacy Policy.