我的服务器安全复盘,如何从挖矿木马入侵到Docker Rootless加固?

摘要:前言 最近我连续几台服务器被挂了挖矿木马,CPU、带宽、磁盘 IO 被拉满,服务器直接卡死无法连接。 排查后发现,核心诱因是 Docker 权限过高 + 服务漏洞暴露,导致攻击者通过容器突破权限控制。 PS:本来想写一篇文
前言 最近我连续几台服务器被挂了挖矿木马,CPU、带宽、磁盘 IO 被拉满,服务器直接卡死无法连接。 排查后发现,核心诱因是 Docker 权限过高 + 服务漏洞暴露,导致攻击者通过容器突破权限控制。 PS:本来想写一篇文章介绍排查过程的,不过还是嫌麻烦没写,放在本文一起讲吧~ 重装系统后,我在部署 Docker 时注意到官方提示的 Rootless(无根)模式 —— 这一模式能从根本上降低容器逃逸风险,遂深入研究并落地配置,现将完整过程整理分享,希望能帮到同样关注 Docker 安全的开发者。 Rootless 模式是什么 普通情况下,Docker 守护进程(dockerd)是用 root 权限运行的,哪怕你用普通用户执行 docker run,底层还是 root 权限,这有安全风险(比如容器逃逸可能拿到主机 root)。 Rootless 模式让 Docker 守护进程以普通用户权限运行,哪怕容器出问题,也无法获取主机的 root 权限,安全性大幅提升。 有好处自然有代价,rootless 的代价是配置复杂,且部分功能受限(比如无法端口映射 < 1024)。 不过没关系,这些也可以通过配置解决。先从安装开始吧。 安装docker 本来安装是很简单的,不过加个定语:在国内网络环境,那就非常复杂了。 本文介绍最简单的安装方式:使用docker官方脚本+清华镜像。 export DOWNLOAD_URL="https://mirrors.tuna.tsinghua.edu.cn/docker-ce" # 如您使用 curl curl -fsSL https://ghfast.top/https://raw.githubusercontent.com/docker/docker-install/master/install.sh | sh # 如您使用 wget wget -O- https://ghfast.top/https://raw.githubusercontent.com/docker/docker-install/master/install.sh | sh 注意 raw.githubusercontent.com 这个域名也是无法访问的,可以使用 ghproxy 来加速。 安装完成提示 安装完成会有一个提示,这也是开启 Rootless 模式的关键入口: ================================================================================ To run Docker as a non-privileged user, consider setting up the Docker daemon in rootless mode for your user: dockerd-rootless-setuptool.sh install Visit https://docs.docker.com/go/rootless/ to learn about rootless mode. To run the Docker daemon as a fully privileged service, but granting non-root users access, refer to https://docs.docker.com/go/daemon-access/ WARNING: Access to the remote API on a privileged Docker daemon is equivalent to root access on the host. Refer to the 'Docker daemon attack surface' documentation for details: https://docs.docker.com/go/attack-surface/ ================================================================================ 我就是在这里开始使用 rootless 模式的。
阅读全文