使用 kube-bench 和 kube-hunter 对 Kubernetes 集群风险评估

点击上方蓝字关注 👆👆

↓推荐关注↓

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

kube-bench

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

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

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

运行

运行 kube-bench 有多种方法[3]

  • docker 容器运行

  • 在 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[4]

是一个 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 集群中的所有节点,然后尝试扫描所有节点。默认情况下,它将使用集群内配置[5]连接到 Kubernetes API。如果您想使用显式 kubeconfig 文件,请设置--kubeconfig /root/.kube/kubeconfig.

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

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

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

结论

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

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

其他可选工具

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

参考资料

[1]

kube-bench: https://github.com/aquasecurity/kube-bench

[2]

CIS Kubernetes Benchmark: https://www.cisecurity.org/benchmark/kubernetes/

[3]

多种方法: https://github.com/aquasecurity/kube-bench/blob/main/docs/running.md

[4]

kube-hunter: https://github.com/aquasecurity/kube-hunter

[5]

集群内配置: https://kubernetes.io/docs/reference/access-authn-authz/rbac/

[6]

kubescape : https://github.com/armosec/kubescape

[7]

Kubernetes cluster security assessment with kube-bench and kube-hunter: https://blog.flant.com/kubernetes-security-with-kube-bench-and-kube-hunter/



我发起一个抽奖,欢迎您的参与👇

3a3efdf3cc6293bf9ea6cbaf7dec5ec5.png


后台回复“加群”,带你进入高手如云交流群

欢迎小伙伴们投稿原创文章~

投稿格式:markdown格式的md文件

投稿邮箱: pub@kubeinfo.cn

推荐阅读

一款强大的Kubernetes API流量查看神器

Argo CD 发现高危漏洞,对 K8s 有何影响?

Golang 从零搭建 LevelDB

为什么云中的容器可以成为攻击者的天堂

52 张图,万字讲解 Linux,建议收藏!

Win11将迎来首次重大更新,终于要支持安卓应用了

快叫女朋友来一起看烟花了!超漂亮!!

22 款必备的命令行生产力工具,提高效率全靠它了

有了这篇 Shell 脚本实践指南,同事对我“刮目相看”!

090d44edf94934b72ac769aa43074638.png

全网粉丝20W的头部大号,专注云原生、Golang、Linux实用脚本,效率工具,免费CSDN下载,回复【golang】获取近 6 万 Star的资源回复【1024】获取全种类IT资料,回复【红包封面】获取超好看封面,回复【加群】进入高手如云技术交流群

fdffb982225fc6a6a4053518f51db0c2.png

分享、点赞和在看

支持我们分享更多好文章,谢谢!

e6c63991c1bede9e995495de2e359b14.gif点击“阅读原文”查看更多                  

 点个在看集群永保稳定👇

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值