(int) php 和 mysql 注入
(int) php and mysql injection
我知道保护您的网站免受 SQL 注入攻击的最安全方法是使用准备好的语句。但是,如果将用户输入转换为数字,人们如何解决这个问题?
$id = (int)$_GET['id'];
But how does someone can go around this if user input is converted to a number?
简单地说,他们不能,story/hits 砖墙的尽头。由于它被转换为 integer,因此不能对其进行修改或操作。
只有 您 有权修改它,如果您 wanted/had 可以。
例如:
$string = "String";
$id = (int)$_GET['id'];
echo $new_value = $string . "" . $id;
正在生产 String123
。
A var_dump($new_value);
将产生 string(9) "String123"
.
但是现在,那将是一个字符串,所以您不想要它,但这只是为了向您展示事后 可以 做什么。
由于 SQL 注入通常包含 '
、-
或 DELETE
等字符或任何其他非整数且由潜在黑客,该声明将被拒绝。
file.php?id=123
和URL中的修改如file.php?id=String'); DROP TABLE USERS; --
不会"pass GO",原样。
您可以做的是检查数组中传递的内容是否确实是整数。
有几个函数可以检查这一点。
- https://secure.php.net/manual/en/function.is-int.php
- http://php.net/manual/en/function.ctype-digit.php
- https://php.net/manual/en/function.is-numeric.php
filter_input()
与 FILTER_VALIDATE_INT 过滤器
https://secure.php.net/manual/en/filter.filters.validate.php
但是,使用准备好的语句也是有益的。 mysqli_ 和 PDO apis 都提供了这个。
- https://php.net/manual/en/mysqli.quickstart.prepared-statements.php
- https://php.net/manual/en/pdo.prepared-statements.php
例如使用 mysqli_ api 然后使用诸如:
bind_param("i", $id)
("integer" 的 i
)。即使有人确实设法传递了整数以外的东西,mysql 仍然会拒绝让它通过,因为它也通过绑定参数转换为整数。
然而,如果那是 bind_param("s", $id)
-("s" 对应 "string"),那么情况可能会有所不同。
PDO 也有这个,PDO::PARAM_INT
。
话虽如此;坚持使用准备好的语句,无论如何。
我知道保护您的网站免受 SQL 注入攻击的最安全方法是使用准备好的语句。但是,如果将用户输入转换为数字,人们如何解决这个问题?
$id = (int)$_GET['id'];
But how does someone can go around this if user input is converted to a number?
简单地说,他们不能,story/hits 砖墙的尽头。由于它被转换为 integer,因此不能对其进行修改或操作。
只有 您 有权修改它,如果您 wanted/had 可以。
例如:
$string = "String";
$id = (int)$_GET['id'];
echo $new_value = $string . "" . $id;
正在生产 String123
。
A var_dump($new_value);
将产生 string(9) "String123"
.
但是现在,那将是一个字符串,所以您不想要它,但这只是为了向您展示事后 可以 做什么。
由于 SQL 注入通常包含 '
、-
或 DELETE
等字符或任何其他非整数且由潜在黑客,该声明将被拒绝。
file.php?id=123
和URL中的修改如file.php?id=String'); DROP TABLE USERS; --
不会"pass GO",原样。
您可以做的是检查数组中传递的内容是否确实是整数。
有几个函数可以检查这一点。
- https://secure.php.net/manual/en/function.is-int.php
- http://php.net/manual/en/function.ctype-digit.php
- https://php.net/manual/en/function.is-numeric.php
filter_input()
与 FILTER_VALIDATE_INT 过滤器
https://secure.php.net/manual/en/filter.filters.validate.php
但是,使用准备好的语句也是有益的。 mysqli_ 和 PDO apis 都提供了这个。
- https://php.net/manual/en/mysqli.quickstart.prepared-statements.php
- https://php.net/manual/en/pdo.prepared-statements.php
例如使用 mysqli_ api 然后使用诸如:
bind_param("i", $id)
("integer" 的 i
)。即使有人确实设法传递了整数以外的东西,mysql 仍然会拒绝让它通过,因为它也通过绑定参数转换为整数。
然而,如果那是 bind_param("s", $id)
-("s" 对应 "string"),那么情况可能会有所不同。
PDO 也有这个,PDO::PARAM_INT
。
话虽如此;坚持使用准备好的语句,无论如何。