如何使用 iptables 和 openvpn 防止 docker 暴露自身

如何使用 iptables 和 openvpn 防止 docker 暴露自身

目标是:

  1. 允许 VPN 客户端访问互联网
  2. 可以访问 docker 子网(例如 178.18.0.0/24)
  3. 通过修改 iptables 防止 docker 自动暴露自身
  4. 手动允许 Docker 端口暴露给互联网

我已经用示例配置解决了 1这里,2 通过将子网推入server.conf

问题:如何防止 docker 自动绕过 INPUT DROP iptable 链暴露端口,而不会断开 eth0 和 tun0 上的互联网连接?

尝试:

  • 我已经尝试过记录的docker方式:https://docs.docker.com/network/iptables/一旦我应用它,它就会断开我的 vpn 客户端的所有互联网连接- 他们可以正常访问 docker 的子网,但无法访问互联网。如果我将 DROP 反转为 ACCEPT,则情况正好相反:互联网可以访问,但 docker 子网无法访​​问,并且会暴露。
  • iptables -I DOCKER-USER -i eth0 -m state --state ESTABLISHED,RELATED -j ACCEPT我也尝试过按此处所述添加:https://riptutorial.com/docker/topic/9201/iptables-with-docker- 遗憾的是,这并没有改变任何事情

我的 docker 相关 iptables 条目目前如下所示:

iptables -I DOCKER-USER -i eth0 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -I DOCKER-USER -i eth0 ! -s 10.0.0.0/24 -j DROP

我的网络如下所示:

eth0 - publicly facing
tun0 - vpn on 10.0.0.0/24
docker - 172.18.0.0/24

截至目前的完整配置:

#!/bin/bash

# A Sample OpenVPN-aware firewall.

# eth0 is connected to the internet.
# eth1 is connected to a private subnet.

# Change this subnet to correspond to your private
# ethernet subnet.  Home will use 10.0.1.0/24 and
# Office will use 10.0.0.0/24.
PRIVATE=10.0.0.0/24

# Loopback address
LOOP=127.0.0.1

# Delete old iptables rules
# and temporarily block all traffic.
iptables -P OUTPUT DROP
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -F

# Set default policies
iptables -P OUTPUT ACCEPT
iptables -P INPUT DROP
iptables -P FORWARD DROP

# Prevent external packets from using loopback addr
iptables -A INPUT -i eth0 -s $LOOP -j DROP
iptables -A FORWARD -i eth0 -s $LOOP -j DROP
iptables -A INPUT -i eth0 -d $LOOP -j DROP
iptables -A FORWARD -i eth0 -d $LOOP -j DROP

# Anything coming from the Internet should have a real Internet address
iptables -A FORWARD -i eth0 -s 192.168.0.0/16 -j DROP
iptables -A FORWARD -i eth0 -s 172.16.0.0/12 -j DROP
iptables -A FORWARD -i eth0 -s 10.0.0.0/8 -j DROP
iptables -A INPUT -i eth0 -s 192.168.0.0/16 -j DROP
iptables -A INPUT -i eth0 -s 172.16.0.0/12 -j DROP
iptables -A INPUT -i eth0 -s 10.0.0.0/8 -j DROP

# Block outgoing NetBios (if you have windows machines running
# on the private subnet).  This will not affect any NetBios
# traffic that flows over the VPN tunnel, but it will stop
# local windows machines from broadcasting themselves to
# the internet.
iptables -A FORWARD -p tcp --sport 137:139 -o eth0 -j DROP
iptables -A FORWARD -p udp --sport 137:139 -o eth0 -j DROP
iptables -A OUTPUT -p tcp --sport 137:139 -o eth0 -j DROP
iptables -A OUTPUT -p udp --sport 137:139 -o eth0 -j DROP

# Check source address validity on packets going out to internet
iptables -A FORWARD -s ! $PRIVATE -i eth1 -j DROP

# Allow local loopback
iptables -A INPUT -s $LOOP -j ACCEPT
iptables -A INPUT -d $LOOP -j ACCEPT

# Allow incoming pings (can be disabled)
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT

# Allow services such as www and ssh (can be disabled)
iptables -A INPUT -p tcp --dport http -j ACCEPT
iptables -A INPUT -p tcp --dport ssh -j ACCEPT

# Allow incoming OpenVPN packets
# Duplicate the line below for each
# OpenVPN tunnel, changing --dport n
# to match the OpenVPN UDP port.
#
# In OpenVPN, the port number is
# controlled by the --port n option.
# If you put this option in the config
# file, you can remove the leading '--'
#
# If you taking the stateful firewall
# approach (see the OpenVPN HOWTO),
# then comment out the line below.

iptables -A INPUT -p udp --dport 1194 -j ACCEPT

# Allow packets from TUN/TAP devices.
# When OpenVPN is run in a secure mode,
# it will authenticate packets prior
# to their arriving on a tun or tap
# interface.  Therefore, it is not
# necessary to add any filters here,
# unless you want to restrict the
# type of packets which can flow over
# the tunnel.

iptables -A INPUT -i tun+ -j ACCEPT
iptables -A FORWARD -i tun+ -j ACCEPT
iptables -A INPUT -i tap+ -j ACCEPT
iptables -A FORWARD -i tap+ -j ACCEPT

# Allow packets from private subnets
iptables -A INPUT -i eth1 -j ACCEPT
iptables -A FORWARD -i eth1 -j ACCEPT

# Keep state of connections from local machine and private subnets
iptables -A OUTPUT -m state --state NEW -o eth0 -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -m state --state NEW -o eth0 -j ACCEPT
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT

# Docker allow only VPN by default
    iptables -I DOCKER-USER -i eth0 -m state --state ESTABLISHED,RELATED -j ACCEPT
    iptables -I DOCKER-USER -i eth0 ! -s 10.0.0.0/24 -j DROP

# Masquerade local subnet
iptables -t nat -A POSTROUTING -s $PRIVATE -o eth0 -j MASQUERADE

谢谢!

答案1

解决方案是将 docker 正确地放在 ufw 后面,一篇很棒的文章被发布到 stack overflow:https://stackoverflow.com/a/58098930/11821602源自:https://github.com/moby/moby/issues/4737#issuecomment-419705925

在 /etc/ufw/after.rules 末尾附加以下内容(将 eth0 替换为面向外部的接口):

# Put Docker behind UFW
*filter
:DOCKER-USER - [0:0]
:ufw-user-input - [0:0]

-A DOCKER-USER -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A DOCKER-USER -m conntrack --ctstate INVALID -j DROP
-A DOCKER-USER -i eth0 -j ufw-user-input
-A DOCKER-USER -i eth0 -j DROP
COMMIT

并撤消以下任何或所有操作:

  • 从 /etc/docker/daemon.json 中删除“iptables”:“false”
  • 在 /etc/default/ufw 中恢复为 DEFAULT_FORWARD_POLICY="DROP"
  • 删除对 /etc/ufw/before.rules 的所有 docker 相关更改

确保测试重启后一切正常。

相关内容