2014-04-30 73 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。