阻止或允许带有“/etc/hosts”的网站真的被弃用了吗?用什么代替?

Block or allow websites with "/etc/hosts" is really deprecated? What to use instead?

我正在为我的大学创建一个 Java 项目,我的项目的一个特点是阻止/允许由老师设置的网站(它是一个开源实验室监控软件)。 实际上我需要更简单的方法: 阻止所有网站但只允许少数(允许大约 2 或 3 个网站,必须阻止所有其他网站)。

我找到了 this excellent tutorial that uses /etc/hosts.allow and /etc/hosts.deny to do exactly what I need. However I discovered that these files and this method of blocking / allowing websites is deprecated

我不认为 IPTables 是实现我的目标的好方法,因为要允许访问单个网站,我需要允许一个 IP 地址 - 但请记住,一个主机名可以有多个 IP 地址(就像任何Google服务,Facebook甚至我大学的Moodle)。

那么,阻止所有网站并只允许少数网站的最佳方法是什么?

IPtables 使用 IP 地址而不是 DNS 地址的原因是坏人相对容易阻止或欺骗 DNS 服务器,从而逃避您设置的阻止。

最好的方法使用 iptables(或等效的)因为它们根据 IP 地址进行阻止。


I don't think IPTables is a good way to achieve my aim, because to allow the access to a single website I need allow an IP address - but remember that a single hostname can have several IP addresses (like any Google service, Facebook and even Moodle of my university).

IPTables 很乐意允许(或阻止)属于单个服务的多个 IP 地址。您必须 告诉 这些 IP 地址是什么,但是一旦您这样做了,它就会允许/阻止它们。

事实上,这就是防火墙管理问题的核心。 无从知晓:

  • Facebook(比如说)将使用什么 IP 地址,或者
  • 如果 Facebook 以前使用的 IP 仍然(名义上)被 Facebook 使用,或者
  • 如果有人劫持了 Facebook IP 地址。

不能依赖 DNS 来回答这些问题。 Facebook 不需要在 DNS 中公布他们的 IP 地址,因为他们不需要依赖 DNS 查找。 他们可能不知道他们的主机是否被劫持了。


您还询问是否真的不推荐使用“/etc/hosts.allow”和“/etc/hosts.deny”来阻止或允许网站。

好吧,这取决于你所说的“弃用”是什么意思。当我 运行 "man hosts.allow" 在我的 (Fedora 20) Linux 框中时,它没有提到 "deprecated" 这个词,而当我 Google "hosts.allow deprecated”,我没有看到任何可信的东西。 (提示:将 SO 答案视为权威是不明智的。)

我对软件“弃用”的理解是,这意味着供应商建议您停止使用它。我没有看到发生这种情况的迹象。

然而,“hosts.allow”方法(或更准确地说,“tcpd(8)”)确实有其局限性。引用自 "HOWTO - Limiting Access to TCP-wrapped Services with hosts.allow"

"Hosts.allow is quite an interesting and useful facility that can help increase the security of the host, but typos and poorly written rules can make the host as or more susceptible to exploits than without those rules. Typos could also end up locking yourself out of the box when you least expect it. As stated above, using the access control facility for "TCP wrapped" services and daemons can and probably will take a hit on the host's performance and possibly limit the response rate for the client."

"The facility is not a complete security solution nor should it be treated as such, rather it's a compliment to packet filtering and firewall solutions available (be it gratis, free-speech or commercial). There are some features that are available only by using the hosts.allow facility such as redirecting specific clients to other services, or returning specific error messages, and banners specific to the service and/or client. Nonetheless, this is a great facility that some people overlook as a method to provide a simple means to deny access to specific services to specific hosts."

还需要注意的是,该 HOWTO 的源文章相当过时。它写于 2002 年,此后发生了很多变化。

最后,请注意,如果您将 DNS 名称放入“hosts.allow”和“hosts.deny”文件中,您就会面临我上面解释的问题。