Results 1 to 7 of 7

Thread: Would allowing " and \ cause SQL Injection

  1. #1

    Thread Starter
    Addicted Member kill_bill_gates's Avatar
    Join Date
    Oct 2004
    Posts
    222

    Would allowing " and \ cause SQL Injection

    I was wondering if allowing " and \ in the username or password field would cause SQL injection. Could somebody explain me this one and tell me what does magicquotes do?

    Thanks a lot in advance...
    "Quis custodiet ipsos custodes?"
    Juvenal
    Mete the Hun wanted to live in peace with the Chinese. So he gave the Chinese Emperor his favorite horse, best swords in his armory, and lots of other cool stuff. But then the Chinese Emperor asked for one thing. A useless land through the north. It was a small, useless, unproductive, uninhabited piece of land. But Mete the Hun's answer was certain:
    I gave you horses, weapons and much more which belonged to me. But the lands are not mine, it's my nation's and I'm ready to fight, kill and die for just an inch my country
    -=Joey Jordison R0CKS!! =-

  2. #2
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765

    Re: Would allowing " and \ cause SQL Injection

    Ahhh... no I don't think it would as long as you use Mysql_real_escape_string() around the input data or whatever it is, as it'll escape every " or \ in the string.

    visualAd is the pro on this though I think. :\
    Don't Rate my posts.

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Would allowing " and \ cause SQL Injection

    Magic quotes is evil. It auto magically adds slashes to any quotes found in outside data, such as POST, GET and COOKIE. The problem is, you cannot always rely on it being on.

    Worse, if you are relying on magic quotes to protect you form SQL injection exploits, it will give you a false sense of security. While it works ok with MySql some other databases such as MsSql, Access and Foxpro do not use a backslash to escape quotes.

    Fortunatly you can test whether magic quotes is on and undo its damage if neccessary. Two versions of the stripslashes and addslashes functions which test for the magic qutoes:
    PHP Code:
    /**
     * Remove slashes form a previously escaped string
     * only if magic quotes is turned on.
     * @param string $string
     */
    function stripslashes_safe($string)
    {
        if (
    get_magic_quotes_gpc()) {
            return 
    stripslashes($string);
        } else {
            return 
    $string;
        }
    }

    /**
     * Escape quotes in a string using backslashes only if
     * magic quotes is turned off.
     * @param string $string
     */
    function addslashes_safe($string)
    {
        if (! 
    get_magic_quotes_gpc()) {
            return 
    addslashes($string);
        } else {
            return 
    $string;
        }

    Last edited by visualAd; Dec 22nd, 2005 at 06:46 PM.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Would allowing " and \ cause SQL Injection

    Generally, I treat everything I get from the user to a stripslashes_safe-like function and then use prepared queries for the actual action. PEAR:B and PHP DBO can emulate prepared queries for databases that don't support them (such as MySQL pre-4.1).

    A prepared query is a query that contains question marks:
    Code:
    SELECT * FROM table WHERE name = ?
    Then you can substitute these parameters with actual values. The cool thing about this is that the database driver takes care of correct quoting and escaping. All you have to do is make sure the parameters aren't already quoted.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Addicted Member kill_bill_gates's Avatar
    Join Date
    Oct 2004
    Posts
    222

    Re: Would allowing " and \ cause SQL Injection

    (I'm using MySQL)

    Would using addslashes() work?
    "Quis custodiet ipsos custodes?"
    Juvenal
    Mete the Hun wanted to live in peace with the Chinese. So he gave the Chinese Emperor his favorite horse, best swords in his armory, and lots of other cool stuff. But then the Chinese Emperor asked for one thing. A useless land through the north. It was a small, useless, unproductive, uninhabited piece of land. But Mete the Hun's answer was certain:
    I gave you horses, weapons and much more which belonged to me. But the lands are not mine, it's my nation's and I'm ready to fight, kill and die for just an inch my country
    -=Joey Jordison R0CKS!! =-

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Would allowing " and \ cause SQL Injection

    addslashes() correctly escapes strings for MySQL, but beware the stuff we talked about.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774

    Re: Would allowing " and \ cause SQL Injection

    I mostly allow every last character in passwords and the first thing I do is MD5 (one way encrypt) so it doesn't matter but no one can read the password if they get the database.

    With user names again this is a special case and I would carefully do a replace for all the characters I wanted to allow and replace with HTML escape codes so that no HTML injection can hurt things (HTML injection also called a site to site scriptting attack is used with javascript and often used to steal cookies.)

    In all other cases I would clean up all input that might come from a user (even hidden values in forms (trust nothing)) so that the SQL sequence is safe.
    ?
    'What's this bit for anyway?
    For Jono

Posting Permissions

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



Click Here to Expand Forum to Full Width