Avoid SQL Injection attacks

Rate It (2)

Last post 04-14-2008 2:10 PM by shashishsingh. 10 replies.

Sort Posts:

  • Avoid SQL Injection attacks

    12-19-2004, 11:32 AM
    • Loading...
    • bdesmet
    • Joined on 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

    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
    • Loading...
    • ivanatanasov
    • Joined on 05-09-2007, 12:49 PM
    • Bulgaria
    • Posts 276

    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
    • Loading...
    • vinit_4u
    • Joined on 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
    • Loading...
    • sudipta
    • Joined on 02-25-2008, 3:33 PM
    • India
    • Posts 340

    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
    • Loading...
    • uwspstar
    • Joined on 03-23-2008, 8:36 PM
    • Milwaukee
    • Posts 109

    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 !

    owner of AskBargains.com
    MCAD & MCSD


    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
  • Re: Avoid SQL Injection attacks

    04-12-2008, 4:03 PM
    • Loading...
    • TATWORTH
    • Joined on 02-04-2003, 1:34 PM
    • England
    • Posts 5,094

    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
    • Loading...
    • mod84
    • Joined on 04-02-2007, 7:30 PM
    • Posts 252

    also avoid the 'exec' sql command 

    EXPERT .NET DEVELOPER
  • Re: Avoid SQL Injection attacks

    04-13-2008, 11:48 AM
    • Loading...
    • mbanavige
    • Joined on 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 6,852
    • 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
    ~~~~~~~~~~~~
    Dont 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-14-2008, 2:10 PM

     Big Smile

Page 1 of 1 (11 items)