【发布时间】:2015-04-23 00:32:50
【问题描述】:
function addUser($username, $first_name, $last_name, $email, $pw, $type) {
include $_SERVER['DOCUMENT_ROOT'] . '/connection_library/connection_library.php';
$insertRow = NULL;
$connection = connectUserLogfiles();
try {
$sql = "INSERT INTO user_list (username, first_name, last_name, email, password, type) "
. "VALUES (:username, :first_name, :last_name, :email, :pw, :type)";
$stmt = $connection->prepare($sql);
$stmt->bindParam(':username', $username, PDO::PARAM_STR);
$stmt->bindParam(':first_name', $first_name, PDO::PARAM_STR);
$stmt->bindParam(':last_name', $last_name, PDO::PARAM_STR);
$stmt->bindParam(':email', $email, PDO::PARAM_STR);
$stmt->bindParam(':pw', $pw, PDO::PARAM_STR);
$stmt->bindParam(':type', $type, PDO::PARAM_STR);
$worked = $stmt->execute();
$stmt->rowCount();
$stmt->closeCursor();
} catch (Exception $ex) {
return FALSE;
}
return $worked;
}
我听说使用 bindParam 可以防止 SQL 注入攻击。这是真的?有没有办法对此代码执行 SQL 注入攻击?假设我没有对参数执行过滤或清理(密码除外,它已使用强单向加密方案加密),您将如何执行 SQL 注入攻击?
数据库为 MySQL 数据库,connectionUserLogfiles() 函数中使用的用户只有 SELECT、INSERT 和 UPDATE 权限。
【问题讨论】:
-
只要你使用参数化查询(像你一样),禁用模拟准备选项(如果在旧软件上运行,PDO 会默默地回退到转义参数),并使用 UTF-8(UTF- 7可能会给你带来问题),你很好。对于更短的代码,您可以尝试使用未命名的占位符 (?),然后将一组值发送到执行函数中。
-
我不介意稍微长一点的代码,因为这样一目了然更容易理解。 IMO:可读性>长度
-
是的。对我来说看起来很好。假设
$pw是一个经过适当加盐/哈希处理的密码(例如使用password_hash()),就没有什么可以敲响警钟了。 -
$pw 使用 MD5 和 SHA-512 以及伪随机生成的盐,但我不使用 password_hash(),我使用 crypt()。
-
使用
password_hash()。你会更开心的。
标签: php mysql security pdo sql-injection