Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: mssql SQL Injection

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default mssql SQL Injection

    Hey,

    Anyone have any ideas on how I should go about preventing SQL injections on a microsoft sql database? I've used settype to integers for values that are only IDs but I'm know using string inputs and am not sure how to pass them. Thanks.
    Corrections to my coding/thoughts welcome.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    If none of your queries involve any direct user input except for values within quotes, then all you need to escape, generally speaking, is quotes.

    So just do str_replace("'","\'",$userinput);

    Of course this won't handle anything except that which you place directly in single quotes, such as:
    $query = "SELECT * FROM `table` WHERE `col`='$value';";

    However, I expect that there may be more info available on google for this since mssql is fairly popular. There may be a nice function like mysql_real_escape_string, but I don't know.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default

    This area of awareness is similar , as it has been already answered in this forum.
    If we will escape some strings, we will be able to avoid this. In mysql we use mysql_real_escape_string to do this, similarly there may be some functions, if not we may adopt our own methods as shown in this forum , in order to escape these string.

    Hope this much will help.

  4. #4
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Okay, I've returned to this again...

    If I were to input

    O'Brien
    into a database of users

    would using

    $user_first = $_POST['fname'];
    $user_last = $_POST['lname'];
    $user_first = str_replace("'", "''", $user_first);
    $user_last = str_replace("'", "''", $user_last);
    // those has a ' for the first value and two for the second (in each string replacement).

    I want to use these values in a query such as

    PHP Code:
    $query "INSERT into Users_Table (F_Name, L_Name) values ($user_first$user_last)"
    or possibly

    PHP Code:
    $query "update Users_Table set F_Name = '$user_first', L_Name = '$user_last' where users_id_is = $####"
    Thanks.
    Corrections to my coding/thoughts welcome.

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Why are you replacing it with two single quotes? You should use \' instead. The slash escapes it. The doubling just duplicates the problem.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. The Following User Says Thank You to djr33 For This Useful Post:

    bluewalrus (10-12-2010)

  7. #6
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Okay, thanks I think I was thinking the \ was to escape the php not the sql. Thanks for clearing that up. Would this be about the equivalent of mysql_real_escape or are there others I should escape? In the examples I see online all injections are triggered from a single quote being parsed in.
    Corrections to my coding/thoughts welcome.

  8. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You could have a similar situation with a double quote (if you were using those instead). There are also some special key-symbols (keywords?) including _ and % as wildcards. I'm not sure about the rest. Simply put, it's a lot easier and safer to use mysql_real_escape_string(). Of course it's annoying to type that, but it's reliable.


    In short, more or less, it's the same thing. But there might be some situations where you can't predict what sort of other characters might have an effect. Whatever you do, make sure to escape the single quotes... and the rest if you can...
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. The Following User Says Thank You to djr33 For This Useful Post:

    bluewalrus (10-13-2010)

  10. #8
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Oh, okay thanks. I'm not using mysql though do you know if the function is still avaliable?

    I read the mysql_real_escape documentation and it escapes for the following things

    \x00, \n, \r, \, ', " and \x1a
    Do you know what the \x00, and the \x1a do? I figure I can replace the \n and \r with <br />.

    It doesn't escape for those other wild cards you mentioned. It says those are used with LIKE, GRANT, or REVOKE. Those are contained in single quotes to be triggered which would already be escaped correct?
    Corrections to my coding/thoughts welcome.

  11. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    They're some sort of control characters. I'm not sure on the details.

    From my minimal understanding, mysql_real_escape_sting() is actually a variable function in that it may work differently in different configurations. I have no idea if this is really true or how much it varies, but the idea is that it is related to your current mysql connection. That said, you probably can't use it or wouldn't want to rely on it.

    If you look around you may find a substitute for mssql because this must be a popular function.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  12. The Following User Says Thank You to djr33 For This Useful Post:

    bluewalrus (10-14-2010)

  13. #10
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Okay, thanks. I think I'll write a number of string replaces. I'm not really finding any results for a mssql equvilant of mysql_real_escape_string, googling mysql_real_escape_string mssql first result brings me to this thread.

    If an input were \' it would then become \\'. Would this still escape the single quote or would the first slash be escaped and make the second quote active?
    Corrections to my coding/thoughts welcome.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •