[英]iptables rules to permit GitHub Actions from breaking (chains default to DROP)
假设我的 iptables 规则在 INPUT 和 OUTPUT 链上默认为 DROP,那么我必须添加到我的链中以防止在 GitHub Actions 中运行的脚本无限期停止的最低限度的规则集是什么?
我正在为我的开源应用程序的 CI/CD 基础设施使用(免费)GitHub Actions。 当我将更改推送到 github.com 时,它会自动启动 Microsoft 云中的 Ubuntu 18.04 linux 服务器,该服务器检查我的存储库并执行 BASH 脚本来构建我的应用程序。
出于安全原因,在构建脚本的早期,我安装并设置了一些非常严格的iptables
规则,这些规则在INPUT
和OUTPUT
链上默认为DROP
。 我在防火墙中为127.0.0.1
戳了一个洞,在INPUT
上RELATED/ESTABLISHED
,并且只允许_apt
用户通过OUTPUT
发送流量。
当我在本地系统上的 docker 容器中运行构建脚本时,这非常有效。 但是——正如我刚刚了解到的——当它与 GitHub Actions 一起运行时,它会无限期地停止。 显然,实例本身需要能够与 GitHub 的服务器进行通信才能完成。 我似乎已经打破了这一点。
所以问题是:我应该将什么-j ACCEPT
规则添加到我的iptables
INPUT
和OUTPUT
链中,以只允许 GitHub Actions 执行的基本要求照常进行?
作为参考,这是我的构建脚本中设置防火墙的片段:
##################
# SETUP IPTABLES #
##################
# We setup iptables so that only the apt user (and therefore the apt command)
# can access the internet. We don't want insecure tools like `pip` to download
# unsafe code from the internet.
${SUDO} iptables-save > /tmp/iptables-save.`date "+%Y%m%d_%H%M%S"`
${SUDO} iptables -A INPUT -i lo -j ACCEPT
${SUDO} iptables -A INPUT -s 127.0.0.1/32 -j DROP
${SUDO} iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
${SUDO} iptables -A INPUT -j DROP
${SUDO} iptables -A OUTPUT -s 127.0.0.1/32 -d 127.0.0.1/32 -j ACCEPT
${SUDO} iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
${SUDO} iptables -A OUTPUT -m owner --uid-owner 100 -j ACCEPT # apt uid = 100
${SUDO} iptables -A OUTPUT -j DROP
${SUDO} ip6tables-save > /tmp/ip6tables-save.`date "+%Y%m%d_%H%M%S"`
${SUDO} ip6tables -A INPUT -i lo -j ACCEPT
${SUDO} ip6tables -A INPUT -s ::1/128 -j DROP
${SUDO} ip6tables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
${SUDO} ip6tables -A INPUT -j DROP
${SUDO} ip6tables -A OUTPUT -s ::1/128 -d ::1/128 -j ACCEPT
${SUDO} ip6tables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
${SUDO} ip6tables -A OUTPUT -m owner --uid-owner 100 -j ACCEPT
${SUDO} ip6tables -A OUTPUT -j DROP
# attempt to access the internet as root. If it works, exit 1
curl -s 1.1.1.1
if [ $? -eq 0 ]; then
echo "ERROR: iptables isn't blocking internet access to unsafe tools. You may need to run this as root (and you should do it inside a VM)"
exit 1
fi
您正在做的事情不会可靠地工作,您应该采用不同的解决方案。 为了使其工作,您需要知道您正在操作的网络的布局以及运行相关 GitHub Actions 进程的用户,GitHub 既不记录也不保证该设置的一致性。
因此,即使您确实找到了解决方案,GitHub 也可能会通过在新数据中心或其他网络上运行您的代码,或通过更改运行其进程的用户,或与您的某些相关的其他属性来破坏它。设置。
如果您担心正在运行的代码会下载您不想要的东西,最好将其配置为不这样做,首先不运行代码,或者验证您想要运行的代码. 例如,在使用 C 库的 Rust 程序中,如果我担心的话,我可能会验证二进制文件是否动态链接到系统库,而不是让 Cargo 构建自己的版本。 如果您只想使用系统包,您可以配置任何每种语言的包管理器来查看 localhost 的镜像,如果他们尝试访问 Internet,这将失败,如果您愿意,甚至可以进行测试。
这可以通过在泊坞窗容器中运行构建脚本和应用该容器,这不会影响到主机内的iptables规则来实现runner
的连接。
例如,如果在 GitHub Actions 作业(在 Ubuntu 18.04 GitHub 共享运行docker_script.sh
)中执行以下脚本,它将在没有互联网连接的 debian docker_script.sh
容器中运行构建脚本 ( docker_script.sh
),除了_apt
用户。
#!/bin/bash
set -x
###################
# INSTALL DEPENDS #
###################
apt-get -y install docker.io
##################
# DOWNLOAD IMAGE #
##################
# At the time of writing, Docker Content Trust is 100% security theater without
# explicitly adding the root public keys to the $HOME/.docker/trust/ directory
#
# * https://github.com/BusKill/buskill-app/issues/6#issuecomment-700050760
# * https://security.stackexchange.com/questions/238529/how-to-list-all-of-the-known-root-keys-in-docker-docker-content-trust
# * https://github.com/docker/cli/issues/2752
docker -D pull debian:stable-slim
#################
# CREATE SCRIPT #
#################
tmpDir=`mktemp -d`
pushd "${tmpDir}"
cat << EOF > docker_script.sh
#!/bin/bash
set -x
# SETTINGS #
SUDO=/usr/bin/sudo
# DEPENDS #
${SUDO} apt-get update
${SUDO} apt-get install iptables curl
# IPTABLES #
# We setup iptables so that only the apt user (and therefore the apt command)
# can access the internet. We don't want insecure tools like `pip` to download
# unsafe code from the internet.
${SUDO} iptables-save > /tmp/iptables-save.`date "+%Y%m%d_%H%M%S"`
${SUDO} iptables -A INPUT -i lo -j ACCEPT
${SUDO} iptables -A INPUT -s 127.0.0.1/32 -j DROP
${SUDO} iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
${SUDO} iptables -A INPUT -j DROP
${SUDO} iptables -A OUTPUT -s 127.0.0.1/32 -d 127.0.0.1/32 -j ACCEPT
${SUDO} iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
${SUDO} iptables -A OUTPUT -m owner --uid-owner 100 -j ACCEPT # apt uid = 100
${SUDO} iptables -A OUTPUT -j DROP
${SUDO} ip6tables-save > /tmp/ip6tables-save.`date "+%Y%m%d_%H%M%S"`
${SUDO} ip6tables -A INPUT -i lo -j ACCEPT
${SUDO} ip6tables -A INPUT -s ::1/128 -j DROP
${SUDO} ip6tables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
${SUDO} ip6tables -A INPUT -j DROP
${SUDO} ip6tables -A OUTPUT -s ::1/128 -d ::1/128 -j ACCEPT
${SUDO} ip6tables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
${SUDO} ip6tables -A OUTPUT -m owner --uid-owner 100 -j ACCEPT
${SUDO} ip6tables -A OUTPUT -j DROP
# attempt to access the internet as root. If it works, exit 1
curl 1.1.1.1
if [ $? -eq 0 ]; then
echo "ERROR: iptables isn't blocking internet access to unsafe tools. You may need to run this as root (and you should do it inside a VM)"
exit 1
fi
# BUILD #
# ...
# <DO BUILD HERE>
# ...
exit 0
EOF
chmod +x docker_script.sh
##############
# DOCKER RUN #
##############
docker run --rm --cap-add "NET_ADMIN" -v "${tmpDir}:/root/shared_volume" debian:stable-slim /bin/bash -c "cd /root/shared_volume && docker_script.sh"
# exit cleanly
exit 0
注意:
您必须手动执行docker run
命令,而不仅仅是在 GitHub Actions yaml 文件中指定container:
以添加NET_ADMIN
功能。 另请参阅如何在具有附加功能的 docker 容器中运行脚本(docker exec ... --cap-add ...)
这是一个安全风险,除非您在调用docker pull
之前固定根签名密钥。 另见https://security.stackexchange.com/questions/238529/how-to-list-all-of-the-known-root-keys-in-docker-docker-content-trust
上面的脚本应该以 root 身份执行。 例如,在 GitHub Actions 工作流程中的步骤的run:
key 中添加sudo
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.