2011-06-17 2179 views
14

多端口擴展對於可以指定的端口有一個限制(15)。Iptables在一個規則中設置多個多端口

但我需要在一個單一的規則來指定更多的端口號,所以我試圖用幾個多端像一個規則:

iptables -A INPUT -p tcp -m multiport --destination-ports 59100 -m multiport --destination-ports 3000 -m state --state NEW -j REJECT --reject-with tcp-reset 

iptables -L INPUT -n結果是

Chain INPUT (policy ACCEPT) 
target  prot opt source    destination   
REJECT  tcp -- 0.0.0.0/0   0.0.0.0/0   multiport dports 59100 multiport dports 3000 state NEW reject-with tcp-reset 

但事實證明,當我嘗試從客戶端連接時,兩個端口都不會被拒絕。

該版本是v1.4.2-rc1。

是否有解決方法,或者當我需要在一個規則中指定超過15個端口時應該怎麼做。

回答

0

據我所知,寫多個匹配是邏輯與操作;所以你的規則意味着如果目的端口是「59100」和「3000」,那麼拒絕tcp-reset連接;解決方法是使用-mport選項。注意手冊頁。

+1

沒有導入選項。 – 2012-03-30 16:43:48

3

您需要使用多個規則來實現類OR語義,因爲匹配總是在規則內進行「並列」操作。或者,您可以對端口索引ip套餐(ipset create blah bitmap:port)進行匹配。

24

作爲解決此限制的一種解決方法,我使用兩條規則來涵蓋所有情況。

例如,如果我想允許或拒絕這些18個端口:

465,110,995,587,143,11025,20,21,22,26,80,443,3000,10000,7080,8080,3000,5666 

我用以下規則:

iptables -A INPUT -p tcp -i eth0 -m multiport --dports 465,110,995,587,143,11025,20,21,22,26,80,443 -j ACCEPT 

iptables -A INPUT -p tcp -i eth0 -m multiport --dports 3000,10000,7080,8080,3000,5666 -j ACCEPT 

以上規則應該爲你的工作情況也。如果您在第一條和第二條規則上均達到15個端口限制,您可以創建另一條規則。

+1

您還可以添加一個範圍:`--dports 20:11025`(這不會是您的案例中的最佳解決方案)。是否有可能在該列表中混合範圍和單個端口,如「20:26,80,143,110,443:465,995,587,3000,7080,8080,5666,10000,11025」?另請參見:http://serverfault.com/a/751074/128892 – rubo77 2016-03-09 08:19:18

-3
enable_boxi_poorten 

} 

enable_boxi_poorten() { 
SRV="boxi_poorten" 
boxi_ports="427 5666 6001 6002 6003 6004 6005 6400 6410 8080 9321 15191 16447 17284 17723 17736 21306 25146 26632 27657 27683 28925 41583 45637 47648 49633 52551 53166 56392 56599 56911 59115 59898 60163 63512 6352 25834" 


case "$1" in 
    "LOCAL") 
     for port in $boxi_ports; do $IPT -A tcp_inbound -p TCP -s $LOC_SUB --dport $port -j ACCEPT -m comment --comment "boxi specifieke poorten";done 
    # multiports gaat maar tot 15 maximaal :((
    # daarom maar for loop maken 
    # $IPT -A tcp_inbound -p TCP -s $LOC_SUB -m state --state NEW -m multiport --dports $MULTIPORTS -j ACCEPT -m comment --comment "boxi specifieke poorten" 
    echo "${GREEN}Allowing $SRV for local hosts.....${NORMAL}" 
    ;; 
    "WEB") 
    for port in $boxi_ports; do $IPT -A tcp_inbound -p TCP -s 0/0 --dport $port -j ACCEPT -m comment --comment "boxi specifieke poorten";done 
    echo "${RED}Allowing $SRV for all hosts.....${NORMAL}" 
    ;; 
    *) 
    for port in $boxi_ports; do $IPT -A tcp_inbound -p TCP -s $LOC_SUB --dport $port -j ACCEPT -m comment --comment "boxi specifieke poorten";done 
    echo "${GREEN}Allowing $SRV for local hosts.....${NORMAL}" 
    ;; 
esac 

} 
+1

您至少應該留下一些關於您的代碼的評論,並避免使用其他語言撰寫評論,而不是使用英文。 – akluth 2012-10-05 13:18:37