我有一台服务器,并且正在运行一些 docker 容器。这些容器有一些未开放的端口。我添加了 INPUT 规则,只允许从 1 个 ip 访问这些端口,之后我有一个 DROP 规则。
这应该会阻止除我的家庭 IP 之外的所有端口访问。现在事实证明,docker 只允许一切,并且 docker 能够以某种方式覆盖输入规则,因为输入规则不起作用。
我相信这与转发规则有关,如下所示:
Chain FORWARD (policy ACCEPT)
target prot opt source destination
DOCKER-USER all -- anywhere anywhere
DOCKER-ISOLATION-STAGE-1 all -- anywhere anywhereACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
DOCKER all -- anywhere anywhere
ACCEPT all -- anywhere anywhere
ACCEPT all -- anywhere anywhere
ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
DOCKER all -- anywhere anywhere
ACCEPT all -- anywhere anywhere
ACCEPT all -- anywhere anywhere
我已经刷新了所有规则,然后我从 docker 中得到了非常奇怪的错误,例如:
driver failed programming external connectivity on endpoint gogs (b62814647bf440e923c009da0ca76185fac2f89a9534eb11792dbcb07ef3ffbf): (iptables failed: iptables --wait -t filter -A DOCKER ! -i br-5dd41982af68 -o br-5dd41982af68 -p tcp -d 172.18.0.6 --dport 3000 -j ACCEPT: iptables: No chain/target/match by that name.
如何确保输入规则覆盖所有内容,以便 INPUT 链上的端口过滤阻止除我之外的其他人访问?
Mar*_*tra -1
我找到了更好的选择。一起避免使用 DOCKER-USER。
我意识到我要保护的是网卡接口。假设我有一个 VPS,它有一个连接到互联网的接口 eth0,那么我需要做的就是阻止除我想要的端口之外的 eth0 的所有传入流量。例如 80、443。然后我还可以为 eth0 添加 IP 例外,例如我的家庭和办公室 ip 号码。然后我允许其他一切。
简而言之,阻止除某些端口之外的互联网接口并允许其他一切。
我用来快速设置 iptable 规则的示例 bash 文件:
#!/bin/bash
# Set temporary all to accept otherwise the next flush might block us out
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
# Flush all the existing rules
iptables -F
# Allow internal connections.
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
# Allow established connections
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m conntrack --ctstate ESTABLISHED -j ACCEPT
iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
# Allow IP address
iptables -A INPUT -p tcp -s IP
iptables -A FORWARD -p tcp -s IP -j ACCEPT
# Allow http and https
iptables -A INPUT -p tcp -m multiport --dports 80,443 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp -m multiport --dports 80,443 -m conntrack --ctstate ESTABLISHED -j ACCEPT
# Block all on eth0 or any other
#iptables -A INPUT -i eth0 -j DROP
#iptables -A FORWARD -i eth0 -j DROP
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4811 次 |
| 最近记录: |