SQl injection

Discussion in 'PHP' started by dean5000v, Mar 25, 2009.

  1. #1
    well ive been working with logins and stuff, and i just thought if you store values in cookies on the users machine, could the user possibly inject a SQL query in there cookie instead to exploit the login depending on how it is coded ?
     
    dean5000v, Mar 25, 2009 IP
  2. it career

    it career Notable Member

    Messages:
    3,562
    Likes Received:
    155
    Best Answers:
    0
    Trophy Points:
    270
    #2
    Better store cookie in a separate database .
     
    it career, Mar 25, 2009 IP
  3. bpasc95

    bpasc95 Active Member

    Messages:
    196
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    70
    #3
    You need to clean any data that comes from the user that is going to "touch" the database. The best practice would be to clean any data that touches the database, internal or external, to ensure you won't run into issues.

    -Bing
     
    bpasc95, Mar 25, 2009 IP
  4. skyfe

    skyfe Active Member

    Messages:
    256
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    63
    #4
    You could use mysql_real_escape_string.

    example for SELECT query:

    
    <?php
    
    $userinput_1 = "skyfe";
    $bad_userinput = " ' OR 1 = 1"; //however this userinput isn't so bad yet
    
    $bad_query = mysql_query("SELECT id FROM accounts WHERE username = '$userinput_1' AND password = '$bad_userinput' ");
    //this would be an query that could be abbused by user input
    
    $good_query = mysql_query("SELECT id FROM accounts WHERE username = '".mysql_real_escape_string($userinput_1)."' AND password = '".mysql_real_escape_string($bad_userinput)."' ");
    //good protected query, no SQL injections possible
    
    ?>
    
    Code (markup):
    Skyfe.
     
    skyfe, Mar 25, 2009 IP
  5. AdnanAhsan

    AdnanAhsan Well-Known Member

    Messages:
    601
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    110
    #5
    limit cookie expiration time, use ip tracking with cookies . i mean track ip in db with associated cookie, there is too many ways to prevent injections i hope my tips will be useful for you :) best of luck
     
    AdnanAhsan, Mar 25, 2009 IP
  6. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #6
    A cookie is like any other input from the user: you have to sanitize it before using it for ANYTHING.

    In the case of the cookie, it's very easy because you created the values in the cookie, so you know exactly what form they should take. Just make sure they do indeed take that form, and strip or escape any nasty characters, and you're fine.
     
    SmallPotatoes, Mar 25, 2009 IP
  7. jazzcho

    jazzcho Peon

    Messages:
    326
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Simply use prepared statement and let them try to hack it. :)
     
    jazzcho, Mar 27, 2009 IP
  8. kusal

    kusal Peon

    Messages:
    91
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    use mysql_real_escape_string() function and you'll be safe from injection attacks
     
    kusal, Mar 27, 2009 IP
  9. TomSh

    TomSh Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I'm looking for a web application firewall that will protect my website from all those attacks.
    I don't have the time to secure my code - any suggestion?
    I did a quick research and found a few solution out there:
    Imperva - dotDefender - Sentry - Deny-All - ModSecurity and a few others.
    Do you have any experience with one of the products above?
    Thanks for any guideline or direction,
    Tom
     
    TomSh, May 18, 2009 IP
  10. bpasc95

    bpasc95 Active Member

    Messages:
    196
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    70
    #10
    Tom,

    Sorry, but I almost fell off my chair when I saw this. You really should take the time to review your code and filter inputs from the users. Third party systems that sit between your application and the rest of the world were not intended to secure security issues that should have otherwise been handled at the code level.

    My suggestion - take the time to secure what you write. If you don't have the time, then sit the code offline until you have the time to audit your code.

    Security of your code should be a requirement of any development.

    -Bing
     
    bpasc95, May 18, 2009 IP