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

Thread: combatting SQL injection?

  1. #1
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    combatting SQL injection?

    This is something I brought up that has been kicked around in the IRC channel.

    So now, I am giving this idead a wider audience.

    What about using urlencode() before inserting a string to combat SQL injection? (then using urldecode() before printing it)
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  2. #2

    Re: combatting SQL injection?

    May work with the most popular database, but seems like an ugly hack.

    A good way to do it is by using the DBMS native functions to it. PostgreSQL, by example, have a function to escape strings and have also parametrized queries that do this automatically.

    P.S: Also, urlencode can handle other codifications than ASCII?
    Last edited by kknd; November 28th, 2008 at 10:22 PM.

  3. #3
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: combatting SQL injection?

    I believe so.

    The idea is to use some encoding that has a limited character set.
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  4. #4
    Join Date
    Oct 2006
    Location
    Ireland
    Beans
    416
    Distro
    Kubuntu 8.04 Hardy Heron

    Re: combatting SQL injection?

    I imagine there would probably be relatively easy ways around this? Best just to use the escaping functions provided (such as mysql_real_escape_string() I think in PHP) AND cast any GET variables to the correct types?.

    A very common error on sites is that they just mysql_real_escape_string($_GET[whatever]) and assume it's safe, but when the value given by POST or GET is an integer this provides no safety at all (unless perhaps the integer is surrounded by quotes in the SQL query anyway).
    Saying that you "could care less" about something is implying that you DO care about it, regardless of common usage.
    Using it as a way of saying you don't care is simply wrong.

  5. #5
    Join Date
    Dec 2007
    Location
    UK
    Beans
    571
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: combatting SQL injection?

    I'm creating joomla mods atm and I just mysql_real_escape_string my queries. I can understand people shoving in modified IDs, but how would casting help? Just curious.

  6. #6
    Join Date
    Oct 2006
    Location
    Ireland
    Beans
    416
    Distro
    Kubuntu 8.04 Hardy Heron

    Re: combatting SQL injection?

    Quote Originally Posted by mike_g View Post
    I'm creating joomla mods atm and I just mysql_real_escape_string my queries. I can understand people shoving in modified IDs, but how would casting help? Just curious.
    If you have "news.php?id=2" and the SQL function was
    Code:
    "SELECT title, whatever, blah FROM news WHERE id="+mysql_real_escape_string($GET[id])
    Then someone could send
    Code:
    news.php?id=2 UNION ALL SELECT concat(username, char(58,58,58), password) FROM users
    NONE of that would get escaped because it contains no apostrophes, special characters or quotation marks, it would execute
    Code:
    SELECT title, whatever, blah FROM news WHERE id=2 UNION ALL SELECT concat(username, char(58,58,58), password) FROM users
    Whereas since "id" will always be an integer in database you could cast it before executing statement, if you did "$id = (int)$_GET[id]" then the above example of injection would not work because when casting you an int you're ignoring everything but the "2" at the start.

    Not sure if I'm explaining clearly.
    Saying that you "could care less" about something is implying that you DO care about it, regardless of common usage.
    Using it as a way of saying you don't care is simply wrong.

  7. #7
    Join Date
    Dec 2007
    Location
    UK
    Beans
    571
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: combatting SQL injection?

    Not sure if I'm explaining clearly.
    Yeah, dunno. I'm too drunk tom pay attention so I'll thank you anyway; although I'm sure you're right on this

    But its going to mean more work for me tho

  8. #8
    Join Date
    Oct 2006
    Location
    Ireland
    Beans
    416
    Distro
    Kubuntu 8.04 Hardy Heron

    Re: combatting SQL injection?

    Quote Originally Posted by mike_g View Post
    But its going to mean more work for me tho
    Only really affects integers, for strings it's fine to just use mysql_real_escape_string() AFAIK.
    Saying that you "could care less" about something is implying that you DO care about it, regardless of common usage.
    Using it as a way of saying you don't care is simply wrong.

  9. #9
    Join Date
    Dec 2007
    Location
    The last place I look
    Beans
    Hidden!
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: combatting SQL injection?

    personally, I avoid inline SQL in my source. everything runs through parameterized stored procedures. between that and sanitation of all input, it gives me several levels of protection.

    good luck,
    franklin

  10. #10
    Join Date
    May 2007
    Beans
    880
    Distro
    Ubuntu Development Release

Page 1 of 2 12 LastLast

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
  •