IP地址SQL注入

发布于 2024-08-16 21:59:01 字数 365 浏览 3 评论 0原文

用户是否可以伪造从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

萤火眠眠 2024-08-23 21:59:01

这是一个延伸,而且不太可能,但我不会说这是不可能的。所以...

无论如何都要使用参数化查询。

即使您从未通过 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.

桃酥萝莉 2024-08-23 21:59:01

您不能依赖 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.

不再让梦枯萎 2024-08-23 21:59:01

我认为有人伪造 $_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.

注定孤独终老 2024-08-23 21:59:01

始终清理所有外部输入 - 使用 mysql_real_escape_string,或者更好的是,准备好声明

Always sanitize all external inputs - use mysql_real_escape_string, or better still, prepared statements

鸩远一方 2024-08-23 21:59:01

您可以使用 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

谁人与我共长歌 2024-08-23 21:59:01

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).

牵强ㄟ 2024-08-23 21:59:01

我总是把这段代码放在我的所有项目中(一些初始输入清理),以防止出现伪造 IP 的情况。
我不确定他们是否可以伪造 REMOTE_ADDR。

function validate_ip($ip) {
    // Try IPv4 First, as its the most used method right now
    if(!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
        // Oops... try v6
        if(!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
            return false; // Sorry...
        }
        else {
            return true;
        }
    }
    else {
        return true;
    }
}
if(!isset($_SERVER['REMOTE_ADDR'])) # wtf?
    die("Could not find your IP Address...");
else {
    if(validate_ip($_SERVER["REMOTE_ADDR"]))
        die("Could not validate your IP Address...");
}

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.

function validate_ip($ip) {
    // Try IPv4 First, as its the most used method right now
    if(!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
        // Oops... try v6
        if(!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
            return false; // Sorry...
        }
        else {
            return true;
        }
    }
    else {
        return true;
    }
}
if(!isset($_SERVER['REMOTE_ADDR'])) # wtf?
    die("Could not find your IP Address...");
else {
    if(validate_ip($_SERVER["REMOTE_ADDR"]))
        die("Could not validate your IP Address...");
}
挽袖吟 2024-08-23 21:59:01

为了效率和安全,您可能希望将 IP 作为整数存储在数据库中并使用。

存储 IP 地址的直接方法是在数据库中使用 varchar 字段。然而,另一种表示 IP 的方法是用整数表示。以这种方式转换提供的 IP 可以对其进行清理,并且还可以使您的存储和查询更加高效。存储 INT 在数据库中占用的空间更少,并且更适合索引,并且我相信查询缓存。

查看 ip2longlong2ip 用于 PHP 函数转换,以及 inet_aton 和 inet_ntoa 在 MySQL 中执行此操作。

因此,该过程可能会像这样

$user_ip=ip2long($_SERVER['REMOTE_ADDR']);
if(!$user_ip){ //returned false due to odd input
   echo 'wtf, yo';
   }
else{
   //do your query
   }

您还可以通过组合两者来清理 IP 并将其保留为原始的点分四边形形式

$user_ip=long2ip(ip2long($_SERVER['REMOTE_ADDR']));

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

$user_ip=ip2long($_SERVER['REMOTE_ADDR']);
if(!$user_ip){ //returned false due to odd input
   echo 'wtf, yo';
   }
else{
   //do your query
   }

You can also sanitize an IP and keep it in the original dotted quad form by combining the two

$user_ip=long2ip(ip2long($_SERVER['REMOTE_ADDR']));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文