How to secure code & queries from mysql injections?

Discussion in 'PHP' started by HungryMinds, Jul 5, 2011.

  1. #1
    How to secure code & queries from mysql injections?
     
    HungryMinds, Jul 5, 2011 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    http://php.net/manual/en/security.database.sql-injection.php

    Easiest way is to use mysql_real_escape_string or equivalent. You can also use an external ORM for database interaction or use prepared statements.

    #1, just don't ever trust any input from a user. Never allow any unsanetized $_POST, $_REQUEST, or $_GET variables anywhere in your code, especially in a database query.
     
    jestep, Jul 5, 2011 IP
  3. ataaso

    ataaso Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    use mysql_real_escape_string function for avoid sql injection
     
    ataaso, Jul 6, 2011 IP
  4. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    AND always wrap single quotes around any data passed. "SELECT * FROM users WHERE userid=".mysql_real_escape_string($userid) would still allow an injection
     
    Last edited: Jul 6, 2011
    Kyosys, Jul 6, 2011 IP
  5. elixiusx

    elixiusx Peon

    Messages:
    65
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Kyosys maybe i'm wrong but i really don't think your sample is a secure sample:
    "SELECT * FROM users WHERE userid=".mysql_real_escape_string($userid) ?

    I can do something like:
    -1 union select...

    I mean mysql_real_escape_string add a backslash to this characters \x00, \n, \r, \, ', " y \x1a.
    But what is my sql injection don't include this characters?

    You can do something like "SELECT * FROM users WHERE userid=".(int)$userid and problem solved (in this case i mean).
     
    elixiusx, Jul 7, 2011 IP
  6. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #6
    I think what he's saying is that this is not secure:

    
    $query = "SELECT * FROM users WHERE userid=".mysql_real_escape_string($userid);
    
    PHP:
    But something like this is:

    
    $query = "SELECT * FROM users WHERE userid='".mysql_real_escape_string($userid)."'";
    
    PHP:
     
    jestep, Jul 7, 2011 IP
  7. elixiusx

    elixiusx Peon

    Messages:
    65
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #7
    Yes if you use quotes mysql_real_escape_string works fine...
     
    elixiusx, Jul 7, 2011 IP
  8. Curt200518

    Curt200518 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    If the variable is a number then you can use abs((int)$_POST['x']) or simply $_POST['x']+=0;

    If the variable contains more than just numbers then you will need to use mysql_real_escape_string.
     
    Curt200518, Jul 7, 2011 IP
  9. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Yeah, that's what I was saying. Always wrap it around quotes as well, because my example would still allow an injection.
     
    Kyosys, Jul 8, 2011 IP
  10. akshat.gl

    akshat.gl Member

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #10
    you can filter all your variables in $_POST/$_GET/$_REQUEST simply by a foreach loop :

    foreach($_REQUEST as $key=>$value)
    $_REQUEST[$key] = mysql_real_escape_string($value);
     
    akshat.gl, Jul 8, 2011 IP
  11. vruvishal

    vruvishal Member

    Messages:
    22
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #11
    Hi,

    You need to use mysql_real_escape_string($yourvalue) in sql query.
     
    vruvishal, Jul 9, 2011 IP