IP地址SQL注入
用户是否可以伪造从 PHP 中的 $_SERVER['REMOTE_ADDR']
返回的结果,以便理论上他们可以在数据库上使用 SQL 注入?
这有点愚蠢,但我对 PHP 还很陌生,我想知道它是否可以完成,当 SELECT 语句从 IP 地址中选择时是否需要清理数据库输入从 $_SERVER['REMOTE_ADDR']
返回。所以,如果我想使用类似 $query = "SELECT * FROM users WHERE IP='" 的东西。 $_SERVER['REMOTE_ADDR'] 。 "'";
,我这样做会有危险吗?
再说一次,这可能是一个“菜鸟”问题,但我觉得必须问。
谢谢
Is it possible for a user to forge the result that is returned from $_SERVER['REMOTE_ADDR']
in PHP so they could in theory use SQL injection on a database?
This is a bit dumb, but I'm still new enough to PHP that I want to know if it can be done, whether or not I need to sanitize database input when the SELECT
statement chooses from IP addresses returned from $_SERVER['REMOTE_ADDR']
. So, if I wanted to use something like $query = "SELECT * FROM users WHERE IP='" . $_SERVER['REMOTE_ADDR'] . "'";
, would there be any danger to my doing this?
Again, probably a "nooby" question, but I feel it must be asked.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这是一个延伸,而且不太可能,但我不会说这是不可能的。所以...
无论如何都要使用参数化查询。
即使您从未通过 IP 地址字段受到攻击,您仍然可以通过缓存获得更快查询的额外好处。
It's a stretch, and unlikely, but I wouldn't go as far as to say it's impossible. So....
Use parameterized queries anyways.
Even if you never get attacked via the IP address field, you will still get the added benefit of faster queries through caching.
您不能依赖 REMOTE_ADDR 为真...由于匿名代理或某些此类技巧,它可能是错误的地址。您可以相信它始终是一个IP地址,因此不可能通过此路径进行SQL注入。
在堆栈的底部,它是从与服务器建立 TCP 连接的数据包上的源地址转换而来的。这意味着 a) 它必须是 IP 地址,b) 它必须路由回客户端才能发生连接。
You can't rely on REMOTE_ADDR being true... it could be the wrong address due to anonymising proxies or some such trick. You can rely on it always being an IP address, so SQL injection by this path is impossible.
Way down at the bottom of the stack, that's been converted from the source address on the packets making the TCP connection to your server. That means a) it has to be an IP address and b) it has to route back to the client for the connection to happen at all.
我认为有人伪造
$_SERVER['REMOTE_ADDR']
的唯一方法是构造一个带有假 IP 地址的 IP 数据包(因为它是由服务器而不是客户端设置的),哪些案例响应将被路由回伪造的地址。如果您担心注入攻击,我想您没问题,因为 IP 数据包中的地址字段只留有地址空间。I think the only way for someone to forge
$_SERVER['REMOTE_ADDR']
would be to construct a IP packet with a fake IP address (because it is set by the server, not the client), in which case responses would be routed back to the faked address. If you're worried about injection attacks, I think you're ok because the address fields in IP packets only have room for addresses.始终清理所有外部输入 - 使用
mysql_real_escape_string
,或者更好的是,准备好声明Always sanitize all external inputs - use
mysql_real_escape_string
, or better still, prepared statements您可以使用 PHP 的数据过滤功能。
filter_var() 和 FILTER_VALIDATE_IP 将验证远程 IP。如果远程 IP 有效,则在 SQL 中使用它。
编辑:带有 INPUT_SERVER 的 filter_input() 是另一种选择;)
http://www. php.net/manual/en/book.filter.php
http://www.php.net/manual/en/filter.filters.validate.php
http://www.php.net/manual/en/function.filter-var.php
http://www.php.net/manual/en/function.filter-input.php
希望这会有所帮助,
Simeon
You could use PHP's Data Filtering Functions.
filter_var() with FILTER_VALIDATE_IP will validate the remote IP. If the remote IP is valid, use it in SQL.
EDIT: filter_input() with INPUT_SERVER is another option ;)
http://www.php.net/manual/en/book.filter.php
http://www.php.net/manual/en/filter.filters.validate.php
http://www.php.net/manual/en/function.filter-var.php
http://www.php.net/manual/en/function.filter-input.php
Hope this helps,
Simeon
REMOTE_ADDR 不是由客户端发送的,而是由服务器设置的。虽然技术上可以在网络级别欺骗 IP 地址,但攻击者必须盲目工作。最棘手的部分是猜测序列号。 (顺便说一句,在 Coldfusion 下,它是 另一个故事。)
正如其他人所说,使用准备好的语句,您不需要担心 SQL 注入(尽管其他类型的注入攻击也是可能的)。
REMOTE_ADDR isn't sent by the client, it's set by the server. While it's technically possible to spoof an IP address at the network level, the attacker has to work blind. The trickiest part is guessing the sequence number. (Under Coldfusion, by the way, it's another story.)
As others have said, use prepared statements and you don't need to worry about SQL injection (though other types of injection attacks are possible).
我总是把这段代码放在我的所有项目中(一些初始输入清理),以防止出现伪造 IP 的情况。
我不确定他们是否可以伪造 REMOTE_ADDR。
I always put this code in all my projects (some initial input sanitization), to prevent something nasty with faked IPs going on.
I am not sure whether they can fake the REMOTE_ADDR anyway.
为了效率和安全,您可能希望将 IP 作为整数存储在数据库中并使用。
存储 IP 地址的直接方法是在数据库中使用 varchar 字段。然而,另一种表示 IP 的方法是用整数表示。以这种方式转换提供的 IP 可以对其进行清理,并且还可以使您的存储和查询更加高效。存储 INT 在数据库中占用的空间更少,并且更适合索引,并且我相信查询缓存。
查看 ip2long 和 long2ip 用于 PHP 函数转换,以及 inet_aton 和 inet_ntoa 在 MySQL 中执行此操作。
因此,该过程可能会像这样
您还可以通过组合两者来清理 IP 并将其保留为原始的点分四边形形式
For efficiency and safety you may wish to store and work with IPs as ints in your database.
The straightforward way to store IP addresses is to use a varchar field in your DB. However, another way to represent IPs is as an integer. Converting the supplied IP in this way will sanitize it, and also make your storage and queries more efficient. Storing an INT takes up less space in a DB, and works better for indexing and I believe query caching.
Check out ip2long and long2ip for PHP functions to convert, and inet_aton and inet_ntoa to do it in MySQL.
So, the process might go like
You can also sanitize an IP and keep it in the original dotted quad form by combining the two