iptables 的一个疑问

2019-01-13 17:12:17 +08:00
 gouchaoer2

我现在需要设置 iptables 为:允许 22 端口访问,然后禁止外网的访问+允许局域网访问,iptables 如下:

iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh
ACCEPT     tcp  --  localhost/8          anywhere            
ACCEPT     tcp  --  10.0.0.0/8           anywhere            
ACCEPT     tcp  --  172.16.0.0/12        anywhere            
ACCEPT     tcp  --  192.168.0.0/16       anywhere            
DROP       tcp  --  anywhere             anywhere            

Chain FORWARD (policy DROP)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain DOCKER (0 references)
target     prot opt source               destination         

Chain DOCKER-ISOLATION-STAGE-1 (0 references)
target     prot opt source               destination         

Chain DOCKER-ISOLATION-STAGE-2 (0 references)
target     prot opt source               destination         

Chain DOCKER-USER (0 references)
target     prot opt source               destination  

现在问题来了,我设置好之后发现在这台机器上无法 tcp 去访问外网,我先清空 iptables 之后用 wget 先建立 tcp 连接然后 netstat 查看连接如下:

netstat -anp | grep wget
tcp        0      0 172.23.54.173:34670     52.216.134.43:443       ESTABLISHED 7727/wget 

可以看到 wget 的 source 是 172.23.54.173 的内网私有 ip,而这个私有 ip 我在 iptables 里面的ACCEPT tcp -- 172.16.0.0/12 anywhere已经覆盖了啊,为啥我无法 tcp 访问外网啊?

1747 次点击
所在节点    程序员
12 条回复
zbinlin
2019-01-13 17:32:06 +08:00
加上

```
iptables -t filter -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
```
试试。

另外建议不要使用 rule 来禁止,而是直接设置 policy 为 DROP:

```
iptables -t filter -P INPUT DROP
```
swsh007
2019-01-13 17:35:09 +08:00
公网 ip 的话 22 就算了吧
得多少人在那里扫啊
换一个端口
sodora
2019-01-13 17:35:40 +08:00
回程被挡了
wd
2019-01-13 20:08:12 +08:00
我看你的 forward 是 drop 是这里的问题吧?
leaflxh
2019-01-14 08:40:51 +08:00
172.16.0.0 与 255.240.0.0 进行与运算得到的结果是 172.10.0.0
172.23.54.173 与 255.240.0.0 进行与运算得到的结果是 172.20.0.0
leaflxh
2019-01-14 09:34:00 +08:00
然后就很尴尬的发现自己算错了....
julyclyde
2019-01-14 12:06:04 +08:00
@zbinlin -P 会坑死很多人的
zbinlin
2019-01-14 13:08:31 +08:00
@julyclyde 何出此言?
gouchaoer2
2019-01-14 14:22:48 +08:00
@zbinlin 不太敢设置默认的 policy 为 DROP,原因很简单,当你设置 policy 为 DROP 的那一瞬间外网的 tcp 流量其实都是断开的,这就意味着你必须保证先设置了允许 22 端口,如果一个不注意 iptables -F 了,那就彻底完蛋了

现在我设置 OK 了,我现在的 iptables -L 如下,我还有个问题,如果别人用 ipv6 的 ip 来访问我是不是我的防火墙就失效了呢?

```
iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
ACCEPT tcp -- localhost/8 anywhere
ACCEPT tcp -- 10.0.0.0/8 anywhere
ACCEPT tcp -- 172.16.0.0/12 anywhere
ACCEPT tcp -- 192.168.0.0/16 anywhere
DROP tcp -- anywhere anywhere
```

然后这台主机是可以连外网的
zbinlin
2019-01-14 15:16:10 +08:00
@gouchaoer2 为什么要用 `iptables -F` 呢?/汗

ipv6 有 `ip6tables` 来设置,规则差不多的。
zbinlin
2019-01-14 15:17:08 +08:00
@zbinlin 另外,即使 ssh 被挡了,不是还有 VNC 吗?
gouchaoer2
2019-01-14 15:37:41 +08:00
@zbinlin 我 netstat 看了一下,端口没有绑定 ipv6 的地址,似乎不用设置,3q

我用 php 写了一些运维脚本,这些脚本 IMAP 去读取邮件,SMTP 发送邮件,解析邮件增加 ip 白名单,然后去管理代码仓库,做一些备份管理啥的,细节挺多的,花了几天时间。。。。我现在比较满意了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/526620

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX