Iptables防止洪水

时间:2014-04-30 00:36:29

标签: sockets ubuntu tcp iptables ddos

我知道你可以限制每个ip,每个时间间隔等的连接数,但我想要的是数据量。

我正在托管一个套接字服务器,我想而不是让它进行检查洪水的处理 - 将它卸载到防火墙。我知道你可以防范同步泛滥攻击,如下所述:

http://www.cyberciti.biz/tips/howto-limit-linux-syn-attacks.html

例如:

# Limit the number of incoming tcp connections
# Interface 0 incoming syn-flood protection
iptables -N syn_flood
iptables -A INPUT -p tcp --syn -j syn_flood
iptables -A syn_flood -m limit --limit 1/s --limit-burst 3 -j RETURN
iptables -A syn_flood -j DROP
#Limiting the incoming icmp ping request:
iptables -A INPUT -p icmp -m limit --limit  1/s --limit-burst 1 -j ACCEPT
iptables -A INPUT -p icmp -m limit --limit 1/s --limit-burst 1 -j LOG --log-prefix PING-DROP:
iptables -A INPUT -p icmp -j DROP
iptables -A OUTPUT -p icmp -j ACCEPT

我不确定iptables可以做什么,所以问题有点模糊。但由于web-socket使用tcp,我应该能够限制每秒的字节数。并且标记连接超过该限制或者只是丢弃它们,无论如何。

我似乎无法找到一个很好的参考,因为它们都是关于跟踪连接等,而不是数据传输。有谁知道一个很好的参考或如何做到这一点? iptables不是一个好的防火墙吗?如果不是什么?

2 个答案:

答案 0 :(得分:1)

内核端防火墙是最快且最安全的软件解决方案(难以杀死内核不是吗?)。使用它还有一个优点,就是使用某些网络控制器上的硬件防火墙。 Iptables是控制它的主要工具,但many others frontends语法更容易。

如果您想要更轻松地配置,请使用:screenshot of traffic shaping configuration 请记住,每个IP的跟踪字节数可以占用大量内存 在你的情况下,我会安装ipset,它由同一个iptables团队开发:

#create ipset for accounting with default lifetime 300 secs
ipset create IP_QUOTA_SET hash:ip timeout 300 counters

#create separated rule chain
iptables --new-chain PER_IP_QOUTING

#send packets to chain
iptables -t filter -A INPUT \
  -i <in-iface> --dst <ip>  \
  -p tcp --dport <dstport>  \
  -j PER_IP_QUOTING

#if ip doesn't exist in the set, add it
iptables -t filter -A PER_IP_QUOTING    \
  -m set ! --match-set IP_QUOTA_SET src \
  -j SET --add-set IP_QUOTA_SET src --timeout 300

#if packet exists in the set, check bytes
#if byte counter > quota then drop packet
iptables -t filter -A PER_IP_QUOTING    \
  -m set --match-set IP_QUOTA_SET src   \
  --bytes-gr 1000 -j DROP

#pass other packets (for debug purpose)
iptables -t filter -A PER_IP_QUOTING \
  -j RETURN

在这种情况下,您可以检查列表并通过ipset命令进行编辑 要显示包含计数器和超时的当前列表:ipset list IP_QUOTA_SET

强烈注意: iptables是特定于Linux的,自linux 2.4起可用。沿着用户空间工具的内核实现之前在2.0和2.2中确实发生了变化 3.13版本引入了new change,它将取代ipset; arptables; ebtables; ip6tables和iptables用一个工具。
与之前的版本一样,它们将是一个过渡时期,像vuurmuur这样的前端将与内核保持兼容,但不希望将来使用iptables。

答案 1 :(得分:0)

您可以尝试使用iptable命令标记和tc(流量整形):http://www.amiryan.org/2009/02/16/traffic-shaping-under-linux-with-tc-and-iptables/

相关问题