新手问题 iptables 过滤了端口, 为什么 netstat 还是可以查看到过滤的端口链接?

wppurking · 2013年06月28日 · 最后由 wppurking 回复于 2013年06月28日 · 4979 次阅读

我在 Host DB 上设置的 iptables

Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     udp  --  anywhere             anywhere             udp dpts:60000:61000
ACCEPT     tcp  --  li413-134.members.linode.com  anywhere             tcp dpt:mysql
ACCEPT     tcp  --  server1.electronixcraft.com  anywhere             tcp dpt:mysql
REJECT     all  --  anywhere             127.0.0.0/8          reject-with icmp-port-unreachable
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:https
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
ACCEPT     icmp --  anywhere             anywhere
LOG        all  --  anywhere             anywhere             limit: avg 5/min burst 5 LOG level debug prefix "iptables denied: "
DROP       all  --  anywhere             anywhere

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
DROP       all  --  anywhere             anywhere

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere

按照上面的信息来说, 除开打开 http/https/mysql 就应该没有其他端口, 服务器监听的端口为:

root@db:/var/lib/mysql# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 *:mysql                 *:*                     LISTEN
tcp        0      0 *:ssh                   *:*                     LISTEN
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN
Active UNIX domain sockets (only servers)
....

可是通过 netstat 查看建立链接的 tcp 的时候看到

root@db# netstat -t
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 db:mysql         server1.electroni:34875 ESTABLISHED
tcp        0      0 db:mysql         server1.electroni:35233 ESTABLISHED
tcp        0      0 db:mysql         server1.electroni:34879 ESTABLISHED
tcp        0      0 db:mysql         server1.electroni:34890 ESTABLISHED
tcp        0      0 db:mysql         server1.electroni:34891 ESTABLISHED
tcp        0      0 db:mysql         server1.electroni:34885 ESTABLISHED
tcp        0      0 db:mysql         server1.electroni:34869 ESTABLISHED
tcp        0      0 db:mysql         server1.electroni:34873 ESTABLISHED
tcp        0      0 db:mysql         server1.electroni:35234 ESTABLISHED
tcp        0      0 db:mysql         server1.electroni:34886 ESTABLISHED
tcp        0      0 db:mysql         server1.electroni:34889 ESTABLISHED
tcp        0      0 db:mysql         server1.electroni:34881 ESTABLISHED

我能够猜到这些 tcp 链接是应用服务器链接到当前服务器 3306 端口访问 MySQL 建立起来的, 可这个不是与 iptables 中的设置想违背了吗? 在 34xxx 端口上的 tcp 应该会被 DROP 掉, 怎么又链接成功了呢?

还有求推荐这方面有什么书可以参考 - -|| 这问题把我搞糊涂了.

共收到 2 条回复

Chain INPUT (policy ACCEPT)

意思不是全部接受吗?

@windwiny Chain INPUT (policy ACCEPT) 这个和下面的 Chain FORWARD (policy ACCEPT)Chain OUTPUT (policy ACCEPT) 都是标题, 具体的过滤条件为这些标题之间的内容.

例如:

Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             anywhere

iptables -I INPUT 1 -p tcp -j ACCEPT

这条语句表明所有 tcp 请求无论来自哪里, 去哪里都允许连接.

DROP all -- anywhere anywhere 的意思和其相反

iptables -A INPUT -p tcp -j DROP

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册