0

Possible Duplicate:
Best way to stop SQL Injection in PHP

Recently, I got my database wiped through a hacker inserting a DROP table command through a signup form.

This annoyed me and got me thinking:

How can I prevent mysql injection in php, before the information is sent to the database, e.g is there a way to detect that the user is trying to inject bad code into the database that can wipe it, and if so, detect it and display an error?

Also, is there a way to detect the mysql injection after it has been added, so when I am displaying the query, if it is the delete injection code, don't display it.

Thanks again, I apologise for any waffle.

Community
  • 1
  • 1
H Bellamy
  • 22,405
  • 23
  • 76
  • 114

5 Answers5

8

Use either PDO or Mysqli with the binding syntax. This alone will prevent most injection attacks.

Example:

    $stmt = $db->prepare(
                'UPDATE users ' .
                'SET userEmail=:email, userSalt=:salt, userPass=:pass ' .
                'WHERE userId=:userId LIMIT 1' );
    $stmt->bindParam( ':email',  $this->_email,    \PDO::PARAM_STR );
    $stmt->bindParam( ':salt',   $this->_salt,     \PDO::PARAM_STR );
    $stmt->bindParam( ':pass',   $this->_password, \PDO::PARAM_STR );
    $stmt->bindParam( ':userId', $this->_id,       \PDO::PARAM_INT );
    $stmt->execute();

In the above example, trying to escape the :email binding to insert a DROP TABLE won't work.

You still need to be careful with user-provided data. For instance, if the user provides a $docId for a get document query, make sure they're authorized for the document being requested. (And not just guessing a $docId belonging to some other user).

Idris
  • 997
  • 2
  • 10
  • 27
nsanders
  • 12,250
  • 2
  • 40
  • 47
0

If you do not want to use PDO or mysqli I would suggest using transactions and only commiting to the database when you are sure that everything is correct.

Naftali
  • 144,921
  • 39
  • 244
  • 303
0

Use PDO or mysqli's bind parameters functionality:

http://php.net/manual/en/mysqli-stmt.bind-param.php

http://www.php.net/manual/en/pdostatement.bindparam.php

The PDO syntax is easier to use, but either will work:

$pdo = new PDO($stuff);

$stmt = $pdo->prepare('SELECT * FROM foo WHERE bar = :baz');
$stmt->bindParam(':baz', $baz);
$stmt->execute();
Jonathan Rich
  • 1,740
  • 10
  • 11
0
<?php

function hashPassword($str)
{
        return hash("sha512", $str . "salt");
        //Change so it fits your database configuration.
}

$username = mysql_real_escape_string($_POST['username']);
$password = hashPassword($_POST['password']);

?>

This should do it.

Griffin
  • 644
  • 6
  • 18
0

is there a way to detect that the user is trying to inject bad code?

It is useless and wrong approach.
You have to format your data properly, not hunt for some odd codes.

Besides that, I have a feeling that your problem cannot be solved with prepared statements.
If so, you will find the solution in my answer in the question linked in the comments.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345