我正在运行带有 nginx 网络服务器的 Ubuntu 15.10 服务器。我使用以下 iptables 配置来允许端口 80 和端口 443 连接:
*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
# Allow ping.
-A INPUT -p icmp -m state --state NEW --icmp-type 8 -j ACCEPT
# Allow SSH connections.
-A INPUT -p tcp --dport 950 -m state --state NEW -j ACCEPT
# Allow HTTP and HTTPS connections from anywhere
# (the normal ports for web servers).
-A INPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT
-A INPUT -p tcp --dport 443 -m state --state NEW -j ACCEPT
# Allow mail
-A INPUT -p tcp --dport 25 -m state --state NEW -j ACCEPT
# Allow inbound traffic from established connections.
# This includes 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
经确认iptables -L
:
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere
REJECT all -- 127.0.0.0/8 anywhere reject-with icmp-port-unreachable
ACCEPT icmp -- anywhere anywhere state NEW icmp echo-request
ACCEPT tcp -- anywhere anywhere tcp dpt:950 state NEW
ACCEPT tcp -- anywhere anywhere tcp dpt:http state NEW
ACCEPT tcp -- anywhere anywhere tcp dpt:https state NEW
ACCEPT tcp -- anywhere anywhere tcp dpt:smtp state NEW
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
LOG all -- anywhere anywhere limit: avg 5/min burst 5 LOG level debug prefix "iptables_INPUT_denied: "
REJECT all -- anywhere anywhere reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT)
target prot opt source destination
LOG all -- anywhere anywhere limit: avg 5/min burst 5 LOG level debug prefix "iptables_FORWARD_denied: "
REJECT all -- anywhere anywhere reject-with icmp-port-unreachable
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
这通常有效。无论如何,到端口 80 的某些连接已被阻止(/var/log/kern.log
):
Apr 15 11:50:47 while kernel: [6038916.249973] iptables_INPUT_denied: IN=eth0 OUT= MAC=04:01:a4:e4:4d:01:84:b5:9c:fa:10:30:08:00 SRC=82.101.237.77 ST=xxx.xxx.xxx.xxx LEN=40 TOS=0x08 PREC=0x00 TTL=54 ID=2368 DF PROTO=TCP SPT=51182 DPT=80 WINDOW=0 RES=0x00 RST URGP=0
iptables 为什么这样做?
答案1
您的示例日志条目是针对具有重置标志的 TCP 数据包。
对于 TCP 连接,Linux 倾向于使用“半双工”关闭序列,其中会话的任何一方都可以通过单次双向 FIN-ACK 握手(将连接置于 CLOSE_WAIT 状态)来启动连接终止,而不是完整的 4 次 FIN-ACK 握手。
这种情况很可能很常见,即您认为之前的 TCP 会话已关闭并忘记了此事。然后 82.101.237.77 尝试重置连接,结果显示为“iptables_INPUT_denied”。
总结:没什么不对。