2014-04-30 74 views
1

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

我正在托管一个套接字服务器,我认为不是让它执行处理来检查泛滥 - 将它卸载到防火墙。我知道你可以防范SYN泛洪攻击,就像这里所说:

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可以做,所以这个问题是有点含糊。但由于网络套接字使用tcp我应该能够限制每秒字节数。并且标志连接超过这个限制或者只是放下它们,不管。

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

+0

“* data *”,你是否包含以太网帧和它们的头文件? – user2284570

+0

@ user2284570是的,我想 – FrostyFire

回答

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

STRONG NOTE:iptables是Linux专用的,自Linux 2.4起可用。用户空间工具的内核实现在2.0和2.2之前确实发生了变化。
3.13版本引入了new change,它将取代ipset; arptables; ebtables; ip6tables和iptables与一个单一的工具。
与以前的版本一样,它们将是一个过渡期,像vuurmuur这样的前端可以与内核保持兼容,但不要指望将来会使用iptables。