MySQL 查询中是否应该引用用户输入的数字来帮助避免 SQL 注入攻击?
假设我在页面上有一个表格询问某人的年龄。他们输入年龄并点击提交。以下 php 代码处理表单提交:(age 是 db 表中的 int 字段。)
$Number = mysqli_real_escape_string($dbc, $_POST["age"]);
$Query = "INSERT INTO details (age) VALUES ($Number)";
$Result = mysqli_query($dbc, $Query);
除此之外,将用户输入括在单引号中(即使它不是字符串)是否可以获得任何好处?像这样:
...
$Query = "INSERT INTO details (age) VALUES ('$Number')"; <-- quotes
...
执行 SELECT
怎么样?这是:
$ID = mysqli_real_escape_string($dbc, $_POST["id"]);
$Query = "SELECT * FROM users WHERE id = '$ID'";
$Result = mysqli_query($dbc, $Query);
比:
$ID = mysqli_real_escape_string($dbc, $_POST["id"]);
$Query = "SELECT * FROM users WHERE id = $ID"; <-- no quotes
$Result = mysqli_query($dbc, $Query);
注意:我知道准备好的语句,并且通常在字符串连接上使用它们,但这是我正在处理的遗留代码。我想尽我所能保护它。
Should numbers from user input be quoted in MySQL queries to help avoid SQL injection attacks?
Say i have a form on a page asking for someone's age. They enter their age and hit submit. The following php code deals with the form submission: (age is an int field in the db table.)
$Number = mysqli_real_escape_string($dbc, $_POST["age"]);
$Query = "INSERT INTO details (age) VALUES ($Number)";
$Result = mysqli_query($dbc, $Query);
Instead of this, is there anything to be gained to enclosing the user input in single quotes, even though it is not a string? Like this:
...
$Query = "INSERT INTO details (age) VALUES ('$Number')"; <-- quotes
...
What about performing a SELECT
? Is this:
$ID = mysqli_real_escape_string($dbc, $_POST["id"]);
$Query = "SELECT * FROM users WHERE id = '$ID'";
$Result = mysqli_query($dbc, $Query);
better than:
$ID = mysqli_real_escape_string($dbc, $_POST["id"]);
$Query = "SELECT * FROM users WHERE id = $ID"; <-- no quotes
$Result = mysqli_query($dbc, $Query);
NOTE: I am aware of prepared statements and usually use them over string concatenation but this is legacy code i'm dealing with. I want to secure it as best as i can.
发布评论
评论(3)
如果添加数字,请使用
intval
/floatval
函数,不要使用mysql_real_escape_string
函数。对于您使用 mysql_real_escape_string 的所有内容,您必须使用引号,例如:
If you add numbers, use the
intval
/floatval
functions, don't usemysql_real_escape_string
for those.For everything you use
mysql_real_escape_string
for, you must use quotes, example:你真的应该使用 sprintf,即使在遗留代码中需要 2 分钟来修改,并且在我看来完全值得花时间。
无耻地从 php.net 中窃取:
您的查询现在非常安全,不会将错误的类型传递到其字段和未转义的字符。
You really shoud use sprintf, even if in legacy code it takes 2 mins to modify and is in my opinion totally worth the time.
Shamelessly ripped from php.net:
Your query is now pretty much safe from being passed the wrong types to it's fields and unescaped caracters.
您应该使用 PHP 过滤器,并过滤数字 - 甚至范围、正则表达式;使用默认值、失败时为 NULL 等。
http://hu.php.net /manual/en/ref.filter.php
如果值来自请求变量,例如 $_POST,请参阅:
http://hu.php.net/manual/en/function.filter-input.php
You SHOULD use the PHP filters, and filter for numbers - even for ranges, regular expressions; with default values, NULL on failure, etc.
http://hu.php.net/manual/en/ref.filter.php
if the values come from a request variable, e.g. $_POST, see:
http://hu.php.net/manual/en/function.filter-input.php