过滤docker流量

过滤docker流量

我的系统上有许多带有自己端口的 docker 容器。 Docker 使用 IPTables 做了一些魔法,因此很难为其定义一些自定义规则。
仅对于 http 连接来说,反向代理不是问题 - 因此我可以使用在主机上运行的 nginx 定义自己的规则(如 SYN 洪水保护)。

但现在我想将名称服务器作为 docker 容器运行。为了保护放大攻击我写了一个规则,但是 docker-iptables-magic 绕过了这个规则。

所以我就尝试着欺骗。我将名称服务器的已发布端口更改为其他端口(5353)。我的计划是制定这样的规则

# rules defined via ferm

table filter {
   chain INPUT {
      POLICY DROP;

      # drop any-query for nameserver
      proto udp dport (53) {
         mod string from 40 algo bm hex-string "|0000ff0001|" DROP;

         # my old rule would jump to ACCEPT now
         ACCEPT;

         # but I think would be nice, when can route the packet now
         # routing isn't allowed here
         REDIRECT to-ports 5353;
      }
   }
}


table nat {
   chain PREROUTING {
      # I also tried to preroute the packet
      # but then will match the docker-rule again 
      # and I cant protect the port
      proto udp dport 53 REDIRECT to-ports 5353;
   }
}


有人有想法吗?我也完全接受其他解决方案 - UDP 中继是一个选项吗?

答案1

我做到了!@preserve是关键词。这告诉 ferm 保持这些链不变。在这里找到的https://www.lullabot.com/articles/convincing-docker-and-iptables-play-nicely

这是我的新配置

#vars
@def $WG_PORT = XXX;
@def $TCP_PORTS = (80 443 22);


table filter {

    # keep docker-chains
    chain (DOCKER DOCKER-INGRESS DOCKER-ISOLATION-STAGE-1 DOCKER-ISOLATION-STAGE-2 FORWARD KUBE-FIREWALL DOCKER-USER) @preserve;    

    chain mainRules {
        #allow local and wireguard
        interface (lo wg0 docker0 br+) ACCEPT;
        source 127.0.0.1 ACCEPT;

        #keep connected
        mod conntrack ctstate (ESTABLISHED RELATED) jump ACCEPT;

        #icmp
        proto icmp {
            mod limit limit  10/minute limit-burst 10 ACCEPT;
            DROP;
        }

        # allow wireguard-traffic instant
        proto udp dport ($WG_PORT) ACCEPT;


        # drop any-query for nameserver when udp
        proto udp dport (53) {
            mod string from 40 algo bm hex-string "|0000ff0001|" DROP;
            ACCEPT;
        }

        #tcp
        proto tcp dport ($TCP_PORTS) {

            #prevent syn-flood, but accept other
            syn {
                mod limit limit 10/sec limit-burst 10 jump PREACCEPT;
                DROP;
            }

            jump ACCEPT;
        }
    }


    chain INPUT {
        policy DROP;
        jump mainRules;
    }

    chain OUTPUT {
        policy ACCEPT;
    }

    chain FORWARD {
        policy ACCEPT;
    }
}

table nat {

    chain (DOCKER DOCKER-INGRESS PREROUTING POSTROUTING OUTPUT DOCKER-USER KUBE-POSTROUTING) @preserve;
}

现在 ferm 和 docker 配合得很好。

相关内容