我有來自 2 個子網路的資料包進入我的防火牆 eth1:
10.111.12.0/24 和 10.10.100.0/24
我這樣設定 iptables 規則:
iptables -A INPUT -i eth1 ! -s 10.111.12.0/24 -j DROP
iptables -A INPUT -i eth1 ! -s 10.10.100.0/24 -j DROP
但是 - 第一個規則不允許檢查第二個規則,因為 10.10.100.0/24 不是 10.111.12.0/24 並且它與第一個規則相符。如何修復它?
答案1
最簡單的方法是標記允許的資料包,然後丟棄所有未標記的資料包:
iptables -t mangle -A PREROUTING -i eth1 -s 10.111.12.0/24 -j MARK --set-mark 1
iptables -t mangle -A PREROUTING -i eth1 -s 10.10.100.0/24 -j MARK --set-mark 1
iptables -t mangle -A PREROUTING -i eth1 -m mark ! --mark 1 -j DROP
您應該考慮到可能還有您需要/想要通過的其他資料包。
答案2
鏈在這裡將是一個有用的方法。鏈的作用有點像子程式或函數。 RETURN 只是意味著對該資料包的控制將返回到先前的鏈,並且規則將繼續被評估。
# create a new chain
iptables -t filter -N in_validnet
# all incoming packets on eth1 are evaluated by the chain
iptables -t filter -A INPUT -i eth1 -j in_validnet
# packets from these networks are returned for further evaluation
iptables -A in_validnet -s 10.111.12.0/24 -j RETURN
iptables -A in_validnet -s 10.10.100.0/24 -j RETURN
# everything else gets dropped
iptables -A in_validnet -j DROP