在通用服务器上运行 tarpit 确实存在风险。如果您知道风险是什么,您可以根据您的舒适程度减轻它们。
值得庆幸的是,这一切都是可能的,并且使用常规的 iptables 和 ipset 非常容易。
您可以使用 iptables 来限制您 TARPIT 的主机数量,而不会使用太多系统资源。 请参阅下面的示例。 这包括网络带宽、系统内存、状态稳定条目和其他系统资源。获得太多缓动连接,开始忽略它们。如果您以正确的顺序组织规则集,则所有缓送连接都不会出现在您的状态表中。还要确保您不要登录,除非您使用自定义 ulog 之类的东西进行实时统计——直接 iptables tarpit 日志可以快速填满磁盘。
根据我的经验,我当前的主机可以轻松地在一个 tarpit 中容纳 200 多台主机,对内存使用、流量使用或 CPU 使用几乎没有明显影响。可能我可以进一步推动这一点,但到目前为止,我在任何给定时刻平均只能捕获大约 130 台主机。
我实施限制的原因正如另一个建议中所述,因为我的第一个 tarpit 主机被淹没了。这是一个微不足道的解决方法。从那以后我没有任何问题。
ipset是一个很棒的小工具,它允许您创建要在 iptables 规则中使用的对象组。不仅如此,而且由于它可以将对象保存在哈希表中,因此与等效的线性 iptables 规则集相比,ipset 越大越快。
除此之外,列表还可以包括基于每个对象的计数器(数据包/字节)、超时和排除。
您可以使用大多数自动阻止的程序从 ipset 添加/删除,例如 fail2ban、ossec 等。因为您可以设置默认超时,所以您可以确保条目过期,无论哪个程序设置条目。
下面是一个基于我在我管理的服务器上使用的东西的例子,它可以减轻上面列出的风险:
### Note: This does not account for all possible traffic you might need or want
### This is only an example of mitigating common risks of using a tarpit
### Use at your own risk
Run Code Online (Sandbox Code Playgroud)
# Create the ipset blacklist
# options:
# - create : create a new ipset
# - blacklist : Name of the ipset we'll reference in the iptables rule
# - hash:net : Make blacklist type hash to hold network (ip/mask) data
# - family inet : This is for IPv4 data (inet6 for IPv6)
# - counters : We want packet/byte stats counted for each entry
# - comment : So we can add a comment with each entry (handy)
# - timeout 604800 : Set a default timeout of 604800 seconds (1 week) for each new entry
# - nomatch : Allow us to enter exclusion entries (never match an entry)
ipset create blacklist hash:net family inet counters comment timeout 604800 nomatch
# Create an entry to never blacklist a trusted network
# options:
# - timeout 0 : entry never expires
# - nomatch : Tells IPset to never match this entry (whitelist, in our usage)
ipset add blacklist 123.45.67.0/24 comment "Trusted subnet, causes breakage if blocked" timeout 0 nomatch
# Example to blacklist hosts
# no netmask implies /32 (or /128 for ipv6)
ipset add blacklist 34.56.78.90 comment "SSH Bruteforce"
ipset add blacklist 23.45.67.89 comment "SQL Injection" timeout 12345
# etc...
Run Code Online (Sandbox Code Playgroud)
# Flush the input table
iptables -F INPUT
# Drop the custom flow TAR
iptables -X TAR
# Set default INPUT policy to DROP
iptables -P INPUT DROP
# Create the chain TAR
iptables -N TAR
# Send all blacklisted hosts to the tarpit
iptables -A INPUT -m set --match-set blacklist src -j TAR
# Allow established connections
# Note: after the blacklist so newly blacklisted threats are immediately ignored
# Yes, that will cause the server to hold the state open until it times out.
# Would you rather have a malicious host continuing its attack?
iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
# Allow the services we want
# Note: These create new state table entries/use up memory
# Note: We do not account for synflood prevention in this example
iptables -A INPUT -p tcp -m multiport --dports 22,80,443 -m tcp --syn -m conntrack --ctstate NEW -j ACCEPT
iptables -A INPUT -p icmp -m icmp --icmp-type 8 -m conntrack --ctstate NEW -j ACCEPT
# Send everything else to tarpit chain
iptables -A INPUT -j TAR
# This is the tarpit chain
# Tarpit up to 10 unique connections a second, any more, and pass to next rule
# Note: 10/s limit is arbitrary, adjust to your preference (experiment)
iptables -A TAR -p tcp -m limit --limit 10/sec -j TARPIT --tarpit
# Drop everything else that makes it this far
# You can also set to REJECT which will immediately tell all connections to buzz off
iptables -A TAR -j DROP
Run Code Online (Sandbox Code Playgroud)
$ sudo tcpdump -npi eth0 'src YOUR.HOST.IP and (tcp[14] = 0 && tcp[15] = 0)'
Run Code Online (Sandbox Code Playgroud)
我曾经认为这是个好主意。但现在我知道不幸的是这是一个非常糟糕的主意。
您是否曾经运行过像 apache bench 这样的 http 基准测试应用程序。在一台机器上,您可以将其设置为每秒创建数百个到目标服务器的连接。让其中一些客户端运行并连接到启用缓送的服务器,我想你会看到一个问题。
考虑一下,如果每个连接都被困在一个 tarpit 中,那么每秒为您的服务器创建数千个连接将如何影响服务器。
您的服务器将迅速消耗所有可用资源(或文件句柄),因此不再允许连接。这比仅仅关闭连接更糟糕。与其试图占用他的资源,不如暂时放弃罪犯。这就是像fail2ban这样的脚本实现的。
此外,您永远不希望您的普通用户陷入困境。至少对于交互式会话。你如何决定哪些人可以被允许,哪些人不能提前?对于某些协议,您不能。例如,HTTP 流量。你必须假设一个客户是好的,直到你从他们那里得到告诉你其他情况的活动。然后你可以决定把它当作坏的,下次它会陷入困境。除了这些攻击中的许多可能来自刚好获得最新蠕虫病毒的动态 adsl 用户等之外,这似乎没问题。
鉴于蠕虫病毒对 PC 的许多攻击“在用户甚至不知道和运行动态地址的情况下发生”,您最终可能会建立一个容易过时的 tarpit 黑名单。您是否开始发现一些问题?
归档时间: |
|
查看次数: |
8047 次 |
最近记录: |