我正在尝试在shell上使用cURL来连接到本地主机。 我使用curl http://localhost:80来连接。 不幸的是,它被iptables阻止。 以下是iptables日志中的错误原因:
IPTables-Dropped: IN=lo OUT= MAC=00:00:00:00:00:00:00:00:00:00:00:00:08:00 SRC=127.0.0.1 DST=127.0.0.1 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=0 DF PROTO=TCP SPT=80 DPT=58617 WINDOW=32768 RES=0x00 ACK SYN URGP=0
这里的iptables:
iptables -L -v Chain INPUT (policy DROP 314 packets, 19725 bytes) pkts bytes target prot opt in out source destination 30731 4342K ACCEPT all -- eth0 any anywhere anywhere state RELATED,ESTABLISHED 255 31984 ACCEPT all -- eth1 any anywhere anywhere state RELATED,ESTABLISHED 6 360 ACCEPT tcp -- any any anywhere anywhere tcp dpt:http 0 0 ACCEPT tcp -- any any anywhere anywhere tcp dpt:https 207 28142 ACCEPT tcp -- any any localhost.localdomain anywhere tcp dpt:6379 173 9634 ACCEPT tcp -- any any localhost.localdomain anywhere tcp spt:6379 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 31748 packets, 2974K bytes) pkts bytes target prot opt in out source destination
我想我已经缩小了这个事实,即cURL似乎正在使用不是80的目标端口。每当我做一个新的cURL请求,它也会改变。 您可以在日志条目中看到: SPT=80 DPT=58617 。 解决这个(我想象)应该解决这个问题。
一些说明:
更新:添加以下规则-A INPUT -p tcp -m tcp --sport 80 -j ACCEPT修复了这个问题。 允许基于源端口而不是目的端口的连接是否存在安全问题?
你只有接口eth0和eth1的RELATED,ESTABLISHED规则,因此web服务器对cURL连接请求的回答将被iptables丢弃,因为它不被任何规则所接受。 更改RELATED,ESTABLISHED规则,使其适用于所有接口应解决该问题。
不要使用规则接受来自特定源端口的stream量,除非您需要这些规则,具体原因如下:有一个规则接受来自端口80的所有stream量意味着任何人都可以连接到您的任何服务,如果他/她可以打开连接端口80(对任何攻击者来说都是微不足道的)。