我正在尝试在我的 Ubuntu VM 上打开端口 80、443 和 SSH。我正在运行 Docker,我认为这是导致端口 21 和 5222 可见的原因。
telnet HOST 21
Trying HOST...
Connected to HOST.
Escape character is '^]'.
sudo iptables --list --line-numbers -v
Chain INPUT (policy DROP 23 packets, 1878 bytes)
num pkts bytes target prot opt in out source destination
1 2013 350K ACCEPT all -- lo any anywhere anywhere
2 1063 614K ACCEPT all -- any any anywhere anywhere ctstate RELATED,ESTABLISHED
3 0 0 DROP all -- any any anywhere anywhere ctstate INVALID
4 1 60 ACCEPT tcp -- any any anywhere anywhere tcp dpt:ssh ctstate NEW,ESTABLISHED
5 28 1644 ACCEPT tcp -- any any anywhere anywhere multiport dports http,https ctstate NEW,ESTABLISHED
Chain FORWARD (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 DOCKER-USER all -- any any anywhere anywhere
Chain OUTPUT (policy ACCEPT 43 packets, 3082 bytes)
num pkts bytes target prot opt in out source destination
1 2013 350K ACCEPT all -- any lo anywhere anywhere
2 816 236K ACCEPT all -- any any anywhere anywhere ctstate ESTABLISHED
3 0 0 ACCEPT tcp -- any any anywhere anywhere tcp spt:ssh ctstate ESTABLISHED
4 0 0 ACCEPT tcp -- any any anywhere anywhere multiport dports http,https ctstate ESTABLISHED
Chain DOCKER-USER (1 references)
num pkts bytes target prot opt in out source destination
1 0 0 RETURN all -- any any anywhere anywhere
sudo iptables-保存
# Generated by iptables-save v1.6.1 on Sun Mar 3 05:57:34 2019
*nat
:PREROUTING ACCEPT [286:14463]
:INPUT ACCEPT [29:1704]
:OUTPUT ACCEPT [273:16843]
:POSTROUTING ACCEPT [273:16843]
:DOCKER - [0:0]
COMMIT
# Completed on Sun Mar 3 05:57:34 2019
# Generated by iptables-save v1.6.1 on Sun Mar 3 05:57:34 2019
*filter
:INPUT DROP [23:1878]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [43:3082]
:DOCKER-USER - [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A INPUT -m conntrack --ctstate INVALID -j DROP
-A INPUT -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m multiport --dports 80,443 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
-A FORWARD -j DOCKER-USER
-A OUTPUT -o lo -j ACCEPT
-A OUTPUT -m conntrack --ctstate ESTABLISHED -j ACCEPT
-A OUTPUT -p tcp -m tcp --sport 22 -m conntrack --ctstate ESTABLISHED -j ACCEPT
-A OUTPUT -p tcp -m multiport --dports 80,443 -m conntrack --ctstate ESTABLISHED -j ACCEPT
-A DOCKER-USER -j RETURN
COMMIT
# Completed on Sun Mar 3 05:57:34 2019
# Generated by iptables-save v1.6.1 on Sun Mar 3 05:57:34 2019
*mangle
:PREROUTING ACCEPT [3829:1086443]
:INPUT ACCEPT [3617:1077407]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [3380:702245]
:POSTROUTING ACCEPT [3380:702245]
COMMIT
# Completed on Sun Mar 3 05:57:34 2019
我已经在启动时禁用了docker服务并禁用了它修改iptables(但它仍然插入DOCKER-USER)。
为什么我仍然可以 telnet 到端口 21 和 5222?
答案1
正如docker网站所述:
如果您需要添加在 Docker 规则之前加载的规则,请将它们添加到 DOCKER-USER 链中。这些规则在 Docker 自动创建的任何规则之前加载。
默认情况下,所有外部源 IP 都被允许连接到 Docker 守护进程。
因此,你编写的规则必须添加到 DOCKER-USER 链中,而不是 INPUT,因为 INPUT 将过滤流向主机而不是 docker 守护进程的流量
答案2
根据您的 iptables 规则,您无法从本地主机以外的任何方式在端口 21 上建立会话。
关于您的 FORWARD 规则:
- 你只有一个,那就是跳转到DOCKER-USER。
- DOCKER-USER 什么也不做,然后返回 FORWARD。
- FORWARD 默认策略是 DROP。
您可以开始查看是否有某个进程正在监听 21 端口:
ss -antp | grep :21
然后,您可以尝试监视通过规则的数据包:
watch iptables -L -n -v
然后,再次尝试,看看如果您再次进行 telnet,规则数据包计数是否正在增加。根据您的规则集,您应该增加 DROP default INPUT 策略:
Chain INPUT (policy DROP 23 packets, 1878 bytes)
如果有另一条规则匹配,您能告诉我们吗?
如果都不是,那么有些愚蠢的问题:
- 你确定你访问的是好的服务器吗?
dig
您能否使用或解析您主机的 DNS并查看它是否与您服务器上的nslookup
相同?ip a