V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
gouchaoer2
V2EX  ›  程序员

iptables 的一个疑问

  •  
  •   gouchaoer2 · 2019-01-13 17:12:17 +08:00 · 1740 次点击
    这是一个创建于 1923 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我现在需要设置 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 访问外网啊?

    12 条回复    2019-01-14 15:37:41 +08:00
    zbinlin
        1
    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
        2
    swsh007  
       2019-01-13 17:35:09 +08:00 via Android
    公网 ip 的话 22 就算了吧
    得多少人在那里扫啊
    换一个端口
    sodora
        3
    sodora  
       2019-01-13 17:35:40 +08:00 via Android
    回程被挡了
    wd
        4
    wd  
       2019-01-13 20:08:12 +08:00 via iPhone
    我看你的 forward 是 drop 是这里的问题吧?
    leaflxh
        5
    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
        6
    leaflxh  
       2019-01-14 09:34:00 +08:00
    然后就很尴尬的发现自己算错了....
    julyclyde
        7
    julyclyde  
       2019-01-14 12:06:04 +08:00
    @zbinlin -P 会坑死很多人的
    zbinlin
        8
    zbinlin  
       2019-01-14 13:08:31 +08:00
    @julyclyde 何出此言?
    gouchaoer2
        9
    gouchaoer2  
    OP
       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
        10
    zbinlin  
       2019-01-14 15:16:10 +08:00
    @gouchaoer2 为什么要用 `iptables -F` 呢?/汗

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

    我用 php 写了一些运维脚本,这些脚本 IMAP 去读取邮件,SMTP 发送邮件,解析邮件增加 ip 白名单,然后去管理代码仓库,做一些备份管理啥的,细节挺多的,花了几天时间。。。。我现在比较满意了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1007 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 19:01 · PVG 03:01 · LAX 12:01 · JFK 15:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.