Home > Security Tips > > Preventing SQL injections
Security Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 


Preventing SQL injections


Mike Chapple, CISSP
09.21.2005
Rating: -5.00- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


Technical professionals responsible for the security of publicly accessible Internet servers often focus (justifiably) on maintaining the operating system and server software that they purchase from vendors. Indeed, these packages often contain significant security vulnerabilities and it is incumbent upon every security administrator to ensure that their servers are patched with the most recent vendor security patches and hotfixes.

However, that's not the extent of the administrator's responsibilities. As you probably know, the custom code developed within your organization to power dynamic Web site functionality has the potential to open significant vulnerabilities in your Web server. These flaws are especially dangerous when you're using Web applications to provide an interface to a back-end database. One particular type of attack against database-driven applications is the SQL injection. In this attack, the intruder manipulates a site's Web-based interfaces to force the database to execute undesirable SQL code.

SQL injection attacks are best learned through example. Imagine a simple Web site set up by a package delivery company to provide delivery status information to anyone who knows the tracking number associated with a particular package. The application may simply ask the user for the tracking number and then look it up in a database table using the following SQL code:

SELECT * FROM Shipments WHERE TrackingID='@tracking'

Where @tracking is a variable passed in from the Web application. Under normal circumstances, this application may function perfectly normally. For example, if a user enters the tracking number 1A2123ZC2, the corresponding query would be:

SELECT * FROM Shipments WHERE TrackingID='1A2123ZC2'

That ideal...



situation makes one flawed assumption -- that the user will only enter a valid tracking number. Malicious individuals are not likely to be so cooperative. Suppose that the user instead enters the string shown below in the tracking number field:

1A2123ZC2' or true The corresponding query will now be: SELECT * FROM Shipments WHERE TrackingID='1A2123ZC2' or true

Which will have the unintended consequence of retrieving all of the tracking information stored in the database. Now assume that we have an even more malicious user who enters the following string:

1A2123ZC2'; DELETE FROM Shipments This would cause the database to execute the following query: SELECT * FROM Shipments WHERE TrackingID='1A2123ZC2'; DELETE FROM Shipments

Which would have the clearly undesirable result of deleting all of the tracking information from the database!

There are several steps that you can take to reduce the possibility of a SQL injection attack against your database:

  • Escape single quotation marks. Include code within your Web applications that replaces single apostrophes with double apostrophes. This will force the database server to recognize the apostrophe as a literal character rather than a string delimiter.

  • Limit the privileges available to the account that executes Web application code. In the example above, if the account only had permission to perform the intended action (retrieving records from the Shipping table), the deletion would not be possible.

  • Reduce or eliminate debugging information. When an error condition occurs on your server, the Web user should not see technical details of the error. This type of information could aid an intruder seeking to explore the structure of your database.

  • Educate your developers. Make sure that the people responsible for developing code within your organization are aware of the seriousness of the threat and the simple steps that they may take to help safeguard your servers.

  • Test your Web applications. Spot-check the work done by your developers. One simple check that you can do is to place single quotation marks within the data sent to your server. If you receive an error response of any kind, chances are you're vulnerable to an SQL injection attack.

    If you take the time to implement these simple steps, you'll be well on your way to securing your Web/database interaction.

    About the Author
    Mike Chapple, CISSP is an IT Security Professional with the University of Notre Dame. He previously served as an information security researcher with the National Security Agency and the U.S. Air Force. Mike is a frequent contributor to SearchSecurity, a technical editor for Information Security magazine and the author of several information security titles including the CISSP Prep Guide and Information Security Illuminated.

    [IMAGE]
    [IMAGE]SQL INJECTION PROTECTION GUIDE
    [IMAGE]  SQL injection protection introduction
    [IMAGE]  How to prevent SQL injection attacks
    [IMAGE]  SQL injection attack defense
    [IMAGE]  Automate SQL injection testing
    [IMAGE]  Defenses for automated SQL injection attacks

    Rate this Tip
    To rate tips, you must be a member of SearchSecurity.com.
    Register now to start rating these tips. Log in if you are already a member.




    BROWSE BY TAG
    Application and Platform Security,   Application Attacks (Buffer Overflows, Cross-Site Scripting),   Database Security Management,   VIEW ALL TAGS

    Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



    RELATED CONTENT
    Application Attacks (Buffer Overflows, Cross-Site Scripting)
    Quiz: How to build secure applications
    Black box and white box testing: Which is best?
    Adobe warns of critical update for Reader, Acrobat 9.1.3
    9 Ways to Improve Application Security After an Incident
    Developers Need Help with Security Errors
    Buffer overflow tutorial: How to find vulnerabilities, prevent attacks
    SQL injection protection: A guide on how to prevent and stop attacks
    Experts rebuke programmers who use SQL injection as feature
    SANS: Application threats, website flaws pose biggest security threats
    Mozilla helps Adobe push out faster patches
    Application Attacks (Buffer Overflows, Cross-Site Scripting) Research

    Database Security Management
    What is the best database patch management process?
    Unpatched vulnerability discovered in Microsoft SQL Server
    SQL injection continues to trouble firms, lead to breaches
    Oracle issues quarterly patches, fixes database flaws
    Database monitoring, encryption vital in tight economy, Forrester says
    Oracle to buy Sun Microsystems for $7.4 billion
    Oracle issues 43 updates, fixes serious database flaws
    Imperva assigns security risk levels to databases
    How to create configuration management plans to install DLP
    Information security book excerpts and reviews
    Database Security Management Research

    RELATED GLOSSARY TERMS
    Terms from Whatis.com − the technology online dictionary
    buffer overflow  (SearchSecurity.com)
    cache poisoning  (SearchSecurity.com)
    cyberterrorism  (SearchSecurity.com)
    dictionary attack  (SearchSecurity.com)
    directory harvest attack  (SearchSecurity.com)
    distributed denial-of-service attack  (SearchSecurity.com)
    JavaScript hijacking  (SearchSecurity.com)
    ping of death  (SearchSecurity.com)
    stack smashing  (SearchSecurity.com)
    SYN flooding  (SearchSecurity.com)

    RELATED RESOURCES
    2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
    Search Bitpipe.com for the latest white papers and business webcasts
    Whatis.com, the online computer dictionary

    DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



  • Research Solutions for Network Security, Access Control and Security Threats
    TechTarget Security Media
    Information Security View this month\\'s issue and subscribe today.
    Information Security Decisions Apply online for free conference admission.
    SearchSecurity.com
    HomeNewsMagazineMultimediaWhite PapersLearningAdviceTopicsEventsAbout Us

    About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
    TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

    TechTarget Corporate Web Site  |  Media Kits  |  Site Map




    All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
      TechTarget - The IT Media ROI Experts