最近给家里路由器升级时决定使用docker来部署一些常用软件,今天编译出来了一个带docker的固件就开始测试了。
一开始的功能测试都没问题,结果安全测试时我才发现一个深坑:
因为我知道docker在暴露端口时,会将容器内的端口暴露给所有的网卡,比如你在用 docker run .... -p 9000:9000 时,就是说在所有的网卡收到的目标端口为9000的入站数据都转发进容器。
然而openwrt作为路由器,肯定是不能接受所有网卡的入站数据的,至少你得吧wan口的入站数据给屏蔽掉,而这一部分是通过防火墙读取iptables规则来实现的。
但是万万没想到,docker暴露端口也是操作iptables,我测试时发现只要docker镜像暴露的端口全都暴露到公网上去了。
然后我上网上查了各种办法,也许是我搜索技巧问题,大多数的答案都指向了在暴露端口时手动指定ip,比如 -p 192.168.1.1:9000:9000
这不开玩笑呢吗?假如我要暴露给两张网卡怎么办(虽然一般来说只可能暴露给一个lan ip)
最后我去看dockerd的配置文件才发现,一切的一切都是默认配置在搞鬼,不知道为什么,openwrt的默认配置竟然允许了dockerd操作iptables
- # The following settings require a restart of docker to take full effect, A reload will only have partial or no effect:
- # bip
- # blocked_interfaces
- # extra_iptables_args
- # device
- config globals 'globals'
- # option alt_config_file '/etc/docker/daemon.json'
- option data_root '/opt/docker/'
- option log_level 'warn'
- option iptables '1'
- # list hosts 'unix:///var/run/docker.sock'
- # option bip '172.18.0.1/24'
- # option fixed_cidr '172.17.0.0/16'
- # option fixed_cidr_v6 'fc00:1::/80'
- # option ipv6 '1'
- # option ip '::ffff:0.0.0.0'
- # list dns '172.17.0.1'
- # list registry_mirrors 'https://<my-docker-mirror-host>'
- # list registry_mirrors 'https://hub.docker.com'
- # Docker ignores fw3 rules and by default all external source IPs are allowed to connect to the Docker host.
- # See https://docs.docker.com/network/iptables/ for more details.
- # firewall config changes are only additive i.e firewall will need to be restarted first to clear old changes,
- # then docker restarted to load in new changes.
- config firewall 'firewall'
- option device 'docker0'
- list blocked_interfaces 'wan'
- # option extra_iptables_args '--match conntrack ! --ctstate RELATED,ESTABLISHED' # allow outbound connections
复制代码
只要把 option iptables '1' 改成 0 再重启dockerd就好了
感觉这是一个非常大的安全隐患啊,不知道openwrt维护者是出于什么心态设置的这个选项。
请不要胡乱输入以及粘贴、复制等方式灌水
请尊重作者、并共同维护网站的正常阅读,否则账户将会被限制发帖、回帖,站内短信以及阅读权限等都会受到影响,谢谢。
具体限制方式:https://www.right.com.cn/forum/thread-8307840-1-1.html
|