Avoid SQL Injection attacks

Rate It (5)

Last post 10-05-2009 7:29 AM by slavik118. 110 replies.

Sort Posts:

  • Avoid SQL Injection attacks

    12-19-2004, 11:32 AM
    • Star
      8,255 point Star
    • bdesmet
    • Member since 08-04-2002, 10:39 AM
    • Belgium
    • Posts 1,651
    Don't ever use string concatenation (or a StringBuilder) to create SQL commands. An example is this:
    string sql = "SELECT * FROM Products WHERE Category=" + cat;

    There are a lot of reasons why not to do this:
    1. Strings inside the command text needs to be enclosed between ' and '. You can have a problem when the value of cat contains a ' itself. You can avoid this by doubling all single quotes inside the cat string, but it still is not recommended.
    2. SQL Injection attacks!!! Don't be tricked by this one, it's easy to avoid. Think of a string cat that contains the following value:
    1; DROP TABLE Products; --

    -- is the comment operator in T-SQL. So, the resulting command is this:
    SELECT * FROM Products WHERE Category=1; DROP TABLE Products; --

    The result: the Products table is droppe. Thus, pretty simple to do if the cat value comes from the querystring or from a form input.

    How to avoid this:
    1. Don't ever ever connect to the database as "sa" or another db owner with full access to the underlying database. Always connect with the least privileges needed to do the job.
    2. Don't use string concat, but use parameterized commands instead, like this:
    string query = "SELECT * FROM Products WHERE Category=@Category";
    
    SqlCommand cmd = new SqlCommand(query, conn);
    cmd.Parameters.Add("@Category", SqlDbType.NVarChar, 50);
    cmd.Parameters["@Category"].Value = cat;
    //...

    This will make sure the anomalities with quotes are solved for you, as well as avoid basic injections and perform checkings for the input length of the strings (+ type checking etc).
    3. Even better, use a stored procedure with parameters on the server and call it using SqlCommand. The idea is the same, but the SQL command with params itself is stored on the server. This allows better performance and even better security.
    Bart De Smet [MVP]



    Visit www.msdn.be, www.bartdesmet.net
  • Re: Avoid SQL Injection attacks

    03-07-2008, 3:44 AM
    • Member
      103 point Member
    • joydipkanjilal
    • Member since 05-09-2006, 4:18 AM
    • Hyderabad
    • Posts 25

    Good post.

    Here is my article on this: http://www.aspnetpro.com/newsletterarticle/2006/12/asp200612jk_l/asp200612jk_l.asp

    Best,

    Joydip

    Author: ASP.NET Data Presentation Controls Essentials (Packt Publishing)

    http://www.amazon.com/ASP-NET-Data-Presentation-Controls-Essentials/dp/1847193951

    Joydip Kanjilal
    Microsoft Most Valuable Professional (ASP.NET)
    http://aspadvice.com/blogs/joydip
  • Re: Avoid SQL Injection attacks

    03-07-2008, 3:55 AM
    • Participant
      1,790 point Participant
    • ivanatanasov
    • Member since 05-09-2007, 12:49 PM
    • Bulgaria
    • Posts 333

    nice post, why you don't write same community article and submit here 


    My blog is here.


    Please remember to 'Mark as Answer' if this post answered your question!
    Filed under:
  • Re: Avoid SQL Injection attacks

    03-16-2008, 2:55 AM
    • Member
      130 point Member
    • vinit_4u
    • Member since 03-13-2008, 1:07 PM
    • Posts 27

    Nice Article,Your article is very useful to novice users who are fully unaware of sql injection attack

  • Re: Avoid SQL Injection attacks

    03-20-2008, 6:51 AM
    • Contributor
      2,216 point Contributor
    • sudipta
    • Member since 02-25-2008, 3:33 PM
    • India
    • Posts 448

    The author of the thread Joydip made a good post to demonstrate the novice users what sqlinjection is all about and how a profound damage can be done by deleting the tables in the database. We should take this discussion a little further by citing how to use xp_cmdshell, xp_makewebtask etc. Also, how to enable xp_cmdshell if it is disabled thorugh SqlInjection. Also, if a form contains a display for outputting a single value and displays multiple values in a grid, how to extract multiple pieces of information like the whole database schema, tables, columns, values etc. Also, some Sql Injection tools that really work to find lopholes in your site etc.

    Please click "Mark As Answer" if this hepled in solving your problem.
  • Re: Avoid SQL Injection attacks

    04-10-2008, 10:25 AM
    • Member
      733 point Member
    • uwspstar
    • Member since 03-23-2008, 8:36 PM
    • Milwaukee
    • Posts 210

    it is a nice for list this topic, if you could post a vidoe stuff to explain what you did here, it will be great !

    founder of AskBargains.com



    If you mark as "Answer"other people can use this answer as a reference
  • Re: Avoid SQL Injection attacks

    04-12-2008, 3:53 PM

     always use sql helper n oracle helper to be in touch wid any database.

    error handling is easy n less eror pron.

    bind parameters with command object.

    n use properties to avoid sql injection. 

    Hope it helps.

    -Manas

    =======================================
    If this post is useful to you, please mark it as answer.
  • Re: Avoid SQL Injection attacks

    04-12-2008, 4:03 PM
    • All-Star
      62,414 point All-Star
    • TATWORTH
    • Member since 02-04-2003, 8:34 AM
    • England
    • Posts 12,177
    • TrustedFriends-MVPs

    Why not write up your notes into an article for www.codeproject.com? 

    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Avoid SQL Injection attacks

    04-13-2008, 2:08 AM
    • Participant
      797 point Participant
    • mod84
    • Member since 04-02-2007, 7:30 PM
    • Posts 275

    also avoid the 'exec' sql command 

    EXPERT .NET DEVELOPER
  • Re: Avoid SQL Injection attacks

    04-13-2008, 11:48 AM
    • All-Star
      97,135 point All-Star
    • mbanavige
    • Member since 11-06-2003, 8:29 AM
    • New England, USA
    • Posts 10,263
    • Moderator
      TrustedFriends-MVPs

    exec in and of itself is not necessarily unsafe.  rather it is how exec is used that can open you up to injection attacks.

    for example: http://www.dotnetjunkies.com/WebLog/chris.taylor/archive/2004/10/13/28370.aspx

     

    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: Avoid SQL Injection attacks

    06-26-2008, 6:16 PM
    • Member
      26 point Member
    • Yankee
    • Member since 12-28-2007, 10:52 AM
    • Posts 41

    Thanks for the tips! I saw another tutorial and I see that everybody recommends to avoid string concatenation in SQL queries.

  • Re: Avoid SQL Injection attacks

    06-26-2008, 9:58 PM
    • Member
      26 point Member
    • bryanpaling
    • Member since 06-27-2008, 1:45 AM
    • Posts 12

    hmm.. nice post... can somebody update us on this? Maybe someone can post some sample sql inject parameters like (' or 1=1--)  so we can also test this on the GUI itself.

    thanks a lot.. .nice to be here.. Stick out tongue

  • Re: Avoid SQL Injection attacks

    07-22-2008, 7:10 AM
    • All-Star
      62,414 point All-Star
    • TATWORTH
    • Member since 02-04-2003, 8:34 AM
    • England
    • Posts 12,177
    • TrustedFriends-MVPs

      There is an excellant article on SQL Injection attacks at http://forums.asp.net/t/1254125.aspx

    Like you I wish there some guidance on hack strings, so we can include them in the general abuse we include in test scripts.

    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Avoid SQL Injection attacks

    07-22-2008, 6:13 PM
    • All-Star
      29,532 point All-Star
    • Naom
    • Member since 12-31-2007, 2:08 PM
    • Wisconsin
    • Posts 6,597

    Thanks for the link. Just to make sure - we need to add the information into web.config file for <system.web>, right?

    Looking for a job opportunity.

    Beware of bugs in the above code; I have only proved it correct, not tried it.
    (Donald Knuth)

    Visit my blog

    PluralSight Learning Library
  • Re: Avoid SQL Injection attacks

    07-23-2008, 1:09 AM
    • All-Star
      62,414 point All-Star
    • TATWORTH
    • Member since 02-04-2003, 8:34 AM
    • England
    • Posts 12,177
    • TrustedFriends-MVPs

     Naom>Just to make sure - we need to add the information into web.config file for <system.web>
    Which information please?

    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
Page 1 of 8 (111 items) 1 2 3 4 5 Next > ... Last »