文章目录

  • ​​kube-bench​​
  • ​​运行​​
  • ​​kube-hunter​​
  • ​​运行​​
  • ​​结论​​
  • ​​其他可选工具​​
  • ​​参考​​
  • ​​关注​​


随着 Kubernetes 越来越受欢迎,门槛也越来越低。但是安全问题仍然存在,下面介绍两个审计集群安全的开源工具。

kube-bench

​kube-bench​​​ 是一个 Go 应用程序,拥有 4.5k star。用于检查 Kubernetes 集群是否符合​​CIS Kubernetes Benchmark​​指南。


CIS:互联网安全中心。这是一个非营利组织,利用来自社区的反馈来解决网络安全问题。建立一个免受网络攻击的环境的建议列表。这些建议包括寻找过于宽松的配置文件权限、集群组件的潜在风险设置、识别未受保护的帐户以及检查常规网络策略。


通过配置 YAML 文件进行测试,这样做的好处是该工具很容易随着测试的规范化而更新。

运行

运行 kube-bench 有​​多种方法​

  • 在容器内运行
  • 在 Kubernetes 集群中运行
  • 在 AKS 集群中运行
  • 在 EKS 集群中运行
  • 在 OpenShift 上运行
  • 在 GKE 集群中运行
  • 在 ACK 集群中运行

下面演示在 Docker 容器中运行 Kube-bench。它需要访问主机的 PID 名称空间,以便检查正在运行的进程,以及访问主机上配置文件等。

docker run --rm --pid=host -v /etc:/etc:ro -v /var/lib/etcd:/var/lib/etcd:ro -v /var/lib/kubelet/config.yaml:/var/lib/kubelet/config.yaml:ro  -v $(which kubectl):/usr/local/mount-from-host/bin/kubectl -v $HOME/.kube:/.kube -e KUBECONFIG=/.kube/config -it aquasec/kube-bench:latest run

运行后会输出检查报告,该报告分为五个主题块,从标题中即可看出:

  • 主节点安全配置;
  • Etcd 节点配置;
  • 控制平面配置;
  • 工作节点安全配置;
  • Kubernetes Policy

针对 Master Node 的配置文件检查结果如下:

[INFO] 1 Master Node Security Configuration
[INFO] 1.1 Master Node Configuration Files
[PASS] 1.1.1 Ensure that the API server pod specification file permissions are set to 644 or more restrictive (Automated)
# 省略
[FAIL] 1.1.7 Ensure that the etcd pod specification file permissions are set to 644 or more restrictive (Automated)
[FAIL] 1.1.8 Ensure that the etcd pod specification file ownership is set to root:root (Automated)

== Remediations master ==
1.1.7 Run the below command (based on the file location on your system) on the master node.
For example,
chmod 644 /etc/kubernetes/manifests/etcd.yaml

1.1.8 Run the below command (based on the file location on your system) on the master node.
For example,
chown root:root /etc/kubernetes/manifests/etcd.yaml

针对 Etcd 的检查:

[INFO] 2 Etcd Node Configuration
[INFO] 2 Etcd Node Configuration Files
[FAIL] 2.1 Ensure that the --cert-file and --key-file arguments are set as appropriate (Automated)
[FAIL] 2.2 Ensure that the --client-cert-auth argument is set to true (Automated)

== Remediations etcd ==
2.1 Follow the etcd service documentation and configure TLS encryption.
Then, edit the etcd pod specification file /etc/kubernetes/manifests/etcd.yaml
on the master node and set the below parameters.
--cert-file=</path/to/ca-file>
--key-file=</path/to/key-file>

2.2 Edit the etcd pod specification file /etc/kubernetes/manifests/etcd.yaml on the master
node and set the below parameter.
--client-cert-auth="true"

检查结果为​​FAIL、WARN​​的,会对应序号给出修改方案。但是修改方案是否试用于你的集群,需要自行综合去判断,应该全面了解修改会对集群产生什么影响。

kube-hunter

是一个 Python 工具,旨在发现 Kubernetes 集群中的漏洞。它从​​攻击者​​的角度评估集群安全。拥有 3.5K star。

运行

  • 二进制
  • 容器
  • Pod

下面演示使用 docker 容器运行。

docker run -it --rm --network host aquasec/kube-hunter

运行之后需要选择扫描方式:

Choose one of the options below:
1. Remote scanning (scans one or more specific IPs or DNS names)
2. Interface scanning (scans subnets on all local network interfaces)
3. IP range scanning (scans a given IP range)
  1. 远程扫描

要指定远程机器进行扫描,选项 1 或使用​​--remote​​选项,容器方式运行:

docker run --rm aquasec/kube-hunter --remote 192.168.28.72
  1. 接口扫描

要指定接口扫描,可以使用​​--interface​​选项(这将扫描机器的所有网络接口),容器方式运行:

docker run --rm aquasec/kube-hunter --interface
  1. 网段扫描

要指定要扫描的特定 CIDR,使用​​--cidr​​选项,容器方式运行:

docker run --rm aquasec/kube-hunter --cidr 192.168.0.0/24

以下是运行检查之后输出的报告:

+--------+---------------------+----------------------+----------------------+----------------------+----------------------+
| KHV002 | 192.168.28.154:6443 | Initial Access // | K8s Version | The kubernetes | v1.19.8 |
| | | Exposed sensitive | Disclosure | version could be | |
| | | interfaces | | obtained from the | |
| | | | | /version endpoint | |
+--------+---------------------+----------------------+----------------------+----------------------+----------------------+
| KHV052 | 192.168.28.194:10255| Discovery // Access | Exposed Pods | An attacker could | count: 9 |
| | | Kubelet API | | view sensitive | |
| | | | | information about | |
| | | | | pods that are | |
| | | | | bound to a Node | |
| | | | | using the /pods | |
| | | | | endpoint | |
+--------+---------------------+----------------------+----------------------+----------------------+----------------------+

列出两个漏洞:

  • 攻击者可以通过​​/version​​获取到k8s的版本;
  • 攻击者可以通过​​/pods​​接口获取到集群节点上运行的Pod数量。

Kubernetes 节点自动发现

设置​​--k8s-auto-discover-nodes​​​标志查询 Kubernetes 集群中的所有节点,然后尝试扫描所有节点。默认情况下,它将使用​​集群内配置​​​连接到 Kubernetes API。如果您想使用显式 kubeconfig 文件,请设置​​--kubeconfig /root/.kube/kubeconfig​​.

查看命令帮助,可以运行:

# docker run -it --rm --network host aquasec/kube-hunter --help

kube-hunter 是用于 Kubernetes 集群渗透测试的可选择工具。它易于安装和运行,并展示集群在攻击者视角的样子。但输出的报告,没有修复方案提示,需要运维人员自行评估方案。

结论

这两个实用程序都非常成熟且易于上手,并提供了集群安全性的良好描述。尽管存在一些缺陷,但对关注集群安全的人来说,它们是一个不错的选择。

安全性不仅限于集群配置,让容器中运行的应用程序保持最新状态、防止未经授权访问镜像注册表、使用安全网络协议、监控应用程序活动等也同样值得注意。

其他可选工具

除了这两个分析工具。​​ kubescape ​​也是值得推荐的选择。Kubescape 提供了风险分析、安全合规、RBAC 可视化工具和镜像漏洞扫描等功能,截至目前已有超过 5.1K star。

使用 kube-bench 和 kube-hunter 进行 Kubernetes 集群安全评估_kubernetes

参考

​Kubernetes cluster security assessment with kube-bench and kube-hunter​

关注

微信公众号【我的小碗汤】