简体   繁体   English

iptables防止洪水泛滥

[英]Iptables Prevent Flooding

I know you can limit number of connections per ip, per time interval etc, but what I am wanting is amount of data. 我知道您可以限制每个ip,每个时间间隔等的连接数,但是我想要的是数据量。

I'm hosting a socket server, and I thought rather than making it do the processing to check for flooding - offload it to the firewall. 我正在托管一个套接字服务器,我想与其进行处理以检查是否泛洪,而不是将其卸载到防火墙。 I know you can guard against syn flooding attacks, like mentioned here: 我知道您可以防范Syn Flood攻击,如下所示:

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

For example: 例如:

# 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

I'm not sure what iptables can do, so the question is a bit vague. 我不确定iptables可以做什么,所以这个问题有点含糊。 But since web-sockets use tcp I should be able to limit number of bytes per second. 但是由于Web套接字使用tcp,所以我应该能够限制每秒的字节数。 And flag connections exceeding that limit or just drop them, whatever. 并标记超出该限制的连接,或者只是丢弃它们,无论如何。

I can't seem to find a good reference on this, as they are all about tracking connections etc, not data transfer. 我似乎在这方面找不到很好的参考,因为它们全都关于跟踪连接等,而不是数据传输。 Does anyone know of a good reference or how to do this? 有谁知道一个很好的参考书或如何做到这一点? Is iptables not a good firewall for this? iptables不是一个好的防火墙吗? if not what is? 如果不是,那是什么?

The kernel-side firewall is the fastest and the most secure software solution (difficult to kill the kernel isn't it?) . 内核侧防火墙是最快,最安全的软件解决方案(不是很难杀死内核吗?) Using it have also the advantage to use the hardware firewall found on some network controllers. 使用它还具有使用某些网络控制器上的硬件防火墙的优势。 Iptables is the primary tool for controlling it, but there are many others frontends with easier syntax. iptables是控制它的主要工具,但是还有许多其他的语法更简单的前端

If you want to configure easier, you should use this : 如果您想简化配置,则应使用以下命令: 流量整形配置的屏幕截图 .
Keep in mind tracking byte count for each IP can use lot of memory. 请记住,跟踪每个IP的字节数会占用大量内存。
In your case I would install ipset , which is developed by the same team of iptables : 在您的情况下,我将安装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

In this case you can check the list and edit it by ipset command. 在这种情况下,您可以检查列表并通过ipset命令对其进行编辑。
To show current list with counters and timeouts : ipset list IP_QUOTA_SET . 要显示带有计数器和超时的当前列表: ipset list IP_QUOTA_SET

STRONG NOTE : iptables is Linux specific and is available since linux 2.4. 注意: iptables是Linux特定的,自linux 2.4起可用。 The kernel implementation along the userspace tools did change in 2.0 and 2.2 previously. 以前,用户空间工具中的内核实现确实在2.0和2.2中有所变化。
The 3.13 version introduced a new change which will replace ipset; 3.13版本引入了一项新更改 ,它将取代ipset; arptables; arptables; ebtables; ebtables; ip6tables, and iptables with a single tool. ip6tables和iptables用一个工具。
As with previous versions, their will be a transition period where frontends like vuurmuur will remain compatible with the kernel, but don't expect to use iptables in the future. 与以前的版本一样,它们将是过渡期,其中像vuurmuur之类的前端将与内核保持兼容,但不要期望将来使用iptables。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM