
我遇到一個問題,儘管 iptables 配置旨在限制訪問,但暴露於 docker 容器中系統上運行的應用程式的連接埠仍然對世界開放。
在我看來,該問題可能與 docker 守護程式在啟動時為 iptables 新增規則有關。我也知道這些標誌--icc=true|false
,--ip-forward=true|false
但--iptables=true|false
我不確定應該應用這些標誌的哪種組合。我已經嘗試過--icc=false
,--ip-forward=false
但都沒有達到預期的效果。我不願意使用,--iptables=false
因為 docker 守護程式顯然添加了許多規則,如果仍然需要它們,我必須手動配置。
這是 docker 守護程式啟動之前規則的狀態:
Chain INPUT (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 ACCEPT all -- lo any anywhere anywhere
0 0 REJECT all -- !lo any anywhere loopback/8 reject-with icmp-port-unreachable
0 0 DROP tcp -- any any anywhere anywhere tcpflags:! FIN,SYN,RST,ACK/SYN state NEW
0 0 DROP all -f any any anywhere anywhere
0 0 DROP tcp -- any any anywhere anywhere tcpflags: FIN,SYN,RST,PSH,ACK,URG/FIN,SYN,RST,PSH,ACK,URG
0 0 DROP tcp -- any any anywhere anywhere tcpflags: FIN,SYN,RST,PSH,ACK,URG/NONE
82 8831 ACCEPT all -- any any anywhere anywhere state RELATED,ESTABLISHED
0 0 ACCEPT icmp -- any any anywhere anywhere icmp echo-request
0 0 ACCEPT tcp -- any any anywhere anywhere multiport dports ssh
0 0 ACCEPT tcp -- any any <IP ADDRESS RANGE 1> anywhere multiport dports ssh,http,https,7990,7999,tproxy,8090,8095,18080
0 0 ACCEPT tcp -- any any <IP ADDRESS RANGE 2> anywhere multiport dports ssh,http,https,7990,7999,tproxy,8090,8095,18080
0 0 LOG all -- any any anywhere anywhere limit: avg 5/min burst 5 LOG level debug prefix "iptables denied: "
0 0 REJECT all -- any any anywhere anywhere reject-with icmp-port-unreachable
Chain FORWARD (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
24 2489 REJECT all -- any any anywhere anywhere reject-with icmp-port-unreachable
Chain OUTPUT (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
77 10080 ACCEPT all -- any any anywhere anywhere
這就是 docker 守護程式運行時的情況:
Chain INPUT (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 ACCEPT all -- lo any anywhere anywhere
0 0 REJECT all -- !lo any anywhere loopback/8 reject-with icmp-port-unreachable
0 0 DROP tcp -- any any anywhere anywhere tcpflags:! FIN,SYN,RST,ACK/SYN state NEW
0 0 DROP all -f any any anywhere anywhere
0 0 DROP tcp -- any any anywhere anywhere tcpflags: FIN,SYN,RST,PSH,ACK,URG/FIN,SYN,RST,PSH,ACK,URG
0 0 DROP tcp -- any any anywhere anywhere tcpflags: FIN,SYN,RST,PSH,ACK,URG/NONE
1335 230K ACCEPT all -- any any anywhere anywhere state RELATED,ESTABLISHED
1 32 ACCEPT icmp -- any any anywhere anywhere icmp echo-request
7 380 ACCEPT tcp -- any any anywhere anywhere multiport dports ssh
0 0 ACCEPT tcp -- any any <IP ADDRESS RANGE 1> anywhere multiport dports ssh,http,https,7990,7999,tproxy,8090,8095,18080
0 0 ACCEPT tcp -- any any <IP ADDRESS RANGE 2> anywhere multiport dports ssh,http,https,7990,7999,tproxy,8090,8095,18080
35 2016 LOG all -- any any anywhere anywhere limit: avg 5/min burst 5 LOG level debug prefix "iptables denied: "
62 3672 REJECT all -- any any anywhere anywhere reject-with icmp-port-unreachable
Chain FORWARD (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
54492 21M DOCKER all -- any docker0 anywhere anywhere
51882 20M ACCEPT all -- any docker0 anywhere anywhere ctstate RELATED,ESTABLISHED
58371 9122K ACCEPT all -- docker0 !docker0 anywhere anywhere
0 0 DROP all -- docker0 docker0 anywhere anywhere
1186 121K REJECT all -- any any anywhere anywhere reject-with icmp-port-unreachable
Chain OUTPUT (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
2090 263K ACCEPT all -- any any anywhere anywhere
Chain DOCKER (1 references)
pkts bytes target prot opt in out source destination
86 7048 ACCEPT tcp -- !docker0 docker0 anywhere 172.17.0.2 tcp dpt:7990
1639 395K ACCEPT tcp -- !docker0 docker0 anywhere 172.17.0.2 tcp dpt:7999
791 151K ACCEPT tcp -- !docker0 docker0 anywhere 172.17.0.3 tcp dpt:http-alt
20 1898 ACCEPT tcp -- !docker0 docker0 anywhere 172.17.0.4 tcp dpt:8090
49 4561 ACCEPT tcp -- !docker0 docker0 anywhere 172.17.0.5 tcp dpt:18080
25 3642 ACCEPT tcp -- !docker0 docker0 anywhere 172.17.0.6 tcp dpt:8095
iptables -L
還有一些 POSTROUTING & MASQUERADE 規則,只有在您使用 時才顯示iptables-save
。我也不確定這些的意義。
我懷疑 FORWARD 鏈中的 DOCKER 目標規則是問題的根源,但我不知道如何解決這個問題,因為它似乎是由 docker 守護程式插入到鏈的開頭。
那麼,有人可以告訴我,我需要做什麼來確保運行 docker 時連接埠 7990、8090 等不會暴露在外面嗎?
謝謝
理查
答案1
該DOCKER
鍊是在 FORWARD 鏈上定義的自訂鏈。當封包到達any
介面並綁定到 docker0 橋接器時,它會被傳送到自訂 DOCKER 鏈。
pkts 位元組目標 prot 選擇輸出來源目標 54492 21M DOCKER all -- 任何地方任何地方的任何 docker0
現在,DOCKER 鏈將取得所有傳入封包(來自 docker0 的封包除外),並將它們傳送到容器 IP (172.xxx) 和端口,在本例中為 7990。
pkts 位元組目標 prot 選擇輸出來源目標 86 7048 接受 tcp -- !docker0 docker0 任何地方 172.17.0.2 tcp dpt:7990
如果您要發佈 的輸出iptables -t nat -L -n
,您將看到DNAT
執行主機到容器連接埠轉送的規則,假設到達 49154 上主機介面的封包將被連接埠轉送到容器 IP 172.17.0.2 和連接埠 7990。
DNAT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:49154 到:172.17.0.2:7990
例如,您可以透過將任何 0.0.0.0 的來源 IP 限制為僅允許來自內部網路的封包來阻止封包到達容器。若要僅允許從內部網路(例如 192.168.1.0/24)連接到容器連接埠 7990,您可以執行以下命令 -
/sbin/iptables -I 轉送 '!' -s 192.168.1.0/24 -d 172.17.0.2 -p tcp --dport 7990 -j 刪除
這將阻止將任何封包轉送到指定 IP:Port 的容器,除非它們來自內部網路。您可以根據您的設定修改來源/目標 IP 和連接埠。
答案2
所以,這將是一條評論,但沒有 stree.. serverfault 代表。
您是否嘗試過此處發布的解決方案:http://blog.viktorpetersson.com/post/101707677489/the-dangers-of-ufw-docker
--iptables=false
像這樣使用這個標誌DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4 --iptables=false"
?