GET parameters vulnerable to SQL Injection - PHP

后端 未结 5 847
长情又很酷
长情又很酷 2020-12-21 08:05

I\'ve been asked to handle a security issue for a site which was set up by another programmer. As of yet, I haven\'t seen any of the code, so I\'m going off of assumptions a

相关标签:
5条回答
  • 2020-12-21 08:17

    Yes, mysql_real_escape_string() will escape any potentially hazardous characters. If you know that the arguments are numeric, it would not hurt to verify this aswell using is_numeric()

    You should also look at mysql::prepare -- This will ensure that only 1 statement is executed, and prevent additional SQL vulnerabilities.

    0 讨论(0)
  • 2020-12-21 08:22

    That function should be fine - your variables are inside single quotes in the SQL statement, and any single, or double quotes will be escaped.

    This means that none of the variables can "break out" of the statement.

    0 讨论(0)
  • 2020-12-21 08:36

    If ID and PID are integer fields, why not casting them to int.

    That way, you are sure to have a number, an no SQL injection :

    $pid = (int) $pid;
    $id = (int) $id;
    
    0 讨论(0)
  • 2020-12-21 08:40

    Skip the old mysql_* stuff if you can and use PDO.

    $pdo = new PDO('mysql:host=localhost;dbname=whatever', $username, $password);
    
    $statement = $pdo->prepare('SELECT * FROM table WHERE pid=:pid AND ID=:id');
    
    $statement->bindParam(':pid', $_GET['pid']);
    
    $statement->bindParam(':id', $_GET['id']);
    
    $results = $statement->execute();
    
    var_dump($results->fetchAll());
    
    0 讨论(0)
  • That should be fine, but i'd always recommend using prepared statements.

    0 讨论(0)
提交回复
热议问题