我們正在為一台機器設置防火牆,該防火牆應允許:
- 來自特定 IP 位址的傳入 SSH 連線;
- ICMP,重定向除外。
我們選擇使用 REJECT 而不是 DROP,至少在我們完成目前的測試之前是如此。
*filter
# Allow all loopback (lo0) traffic and reject traffic to localhost that does not originate from lo0.
-A INPUT -i lo -j ACCEPT
-A INPUT ! -i lo -s 127.0.0.0/8 -j REJECT
# Reject ICMP redirect.
-A INPUT -p icmp --icmp-type 5 -j REJECT
# Allow ICMP (the types which are not rejected).
-A INPUT -p icmp -j ACCEPT
# Allow SSH connections from specific address
-A INPUT -p tcp --dport 22 -s x.x.x.x -j ACCEPT
# Allow inbound traffic from established connections, including ICMP error returns.
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Log what was incoming but denied (optional but useful).
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables_INPUT_denied: " --log-level 7
# Reject all other inbound.
-A INPUT -j REJECT
# Log any traffic which was sent to you for forwarding (optional but useful).
-A FORWARD -m limit --limit 5/min -j LOG --log-prefix "iptables_FORWARD_denied: " --log-level 7
# Reject all traffic forwarding.
-A FORWARD -j REJECT
COMMIT
該機器位於 NAT 路由器後面,連接埠 22 轉送給它。
當嘗試從未經授權的 IP 位址進行連線時,與主機的連線會在一分鐘左右後逾時。
ssh: connect to host x.x.x.x port 22: Connection timed out
這是預期的嗎?我認為 REJECT 會立即導致連線失敗。是否是因為 REJECT ICMP 封包沒有返回到連接電腦?