从 DevOps 兽进化成 DevSecOps 兽:Kube-bench 跟 Falco 浅谈

最近浏览了一下其他先进的文章,发现大家都很有梗耶,今天标题来致敬一下这位大大:Docker兽 究极进化 ~~ Kubernetes兽 系列

回到正题,有些公司可能没有独立的 Security Team,因此这责任就变成 DevOps 要去帮忙注意,今天介绍两个工具帮忙可怜的 DevOps 去检查系统的安全性设定和监控系统中有风险的行为。

Kube-bench


kube-bench 由 Go 语言写成.它可以帮忙就查 Kubernetes 是否有根据 CIS Kubernetes Benchmark 做好安全性设定。

注意这工具只帮忙检查 CIS 规范中标示为 Automation 的部分,标示为 Manual 的规范你仍须自己手动检查,但没鱼虾也好, 你可以在 这里 找到完整的 CIS Kubernetes Benchmark,然後手动检查 kube-bench 没有帮忙检查到的部分。

安装:

请参考 官方文件,笔者本身是在运算节点安装 Binary 档。

$ curl -L https://github.com/aquasecurity/kube-bench/releases/download/v0.3.1/kube-bench_0.3.1_linux_amd64.rpm -o kube-bench_0.3.1_linux_amd64.rpm

$ sudo yum install kube-bench_0.3.1_linux_amd64.rpm -y

用 kube-bench 检查 OpenShift 3.11 设定。

很不幸的 kube-bench 目前不支援 OpenShift 4.x
底下的测试范例是跑在我们之前的 OpenShift 3.11 系统。

在 Compute nodes:

$ /usr/local/bin/kube-bench node --version ocp-3.11 
[INFO] 2 Worker Node Security Configuration
[INFO] 7 Kubelet
[INFO] 7.1 Use Security Context Constraints to manage privileged containers as needed
[INFO] 7.2 Ensure anonymous-auth is not disabled
[PASS] 7.3 Verify that the --authorization-mode argument is set to WebHook
[PASS] 7.4 Verify the OpenShift default for the client-ca-file argument
[PASS] 7.5 Verify the OpenShift default setting for the read-only-port argument
[PASS] 7.6 Adjust the streaming-connection-idle-timeout argument
[INFO] 7.7 Verify the OpenShift defaults for the protect-kernel-defaults argument
[PASS] 7.8 Verify the OpenShift default value of true for the make-iptables-util-chains argument
[FAIL] 7.9 Verify that the --keep-terminated-pod-volumes argument is set to false
[INFO] 7.10 Verify the OpenShift defaults for the hostname-override argument
[PASS] 7.11 Set the --event-qps argument to 0
[PASS] 7.12 Verify the OpenShift cert-dir flag for HTTPS traffic
[PASS] 7.13 Verify the OpenShift default of 0 for the cadvisor-port argument
[PASS] 7.14 Verify that the RotateKubeletClientCertificate argument is set to true
[PASS] 7.15 Verify that the RotateKubeletServerCertificate argument is set to true
[INFO] 8 Configuration Files
[PASS] 8.1 Verify the OpenShift default permissions for the kubelet.conf file
[PASS] 8.2 Verify the kubeconfig file ownership of root:root
[FAIL] 8.3 Verify the kubelet service file permissions of 644
[WARN] 8.4 Verify the kubelet service file ownership of root:root
[PASS] 8.5 Verify the OpenShift default permissions for the proxy kubeconfig file
[PASS] 8.6 Verify the proxy kubeconfig file ownership of root:root
[PASS] 8.7 Verify the OpenShift default permissions for the certificate authorities file.
[PASS] 8.8 Verify the client certificate authorities file ownership of root:root

在 master nodes:

$ /usr/local/bin/kube-bench master --version ocp-3.11
[INFO] 1 Securing the OpenShift Master
[INFO] 1 Protecting the API Server
[INFO] 1.1 Maintain default behavior for anonymous access
[PASS] 1.2 Verify that the basic-auth-file method is not enabled
[INFO] 1.3 Insecure Tokens
[PASS] 1.4 Secure communications between the API server and master nodes
[PASS] 1.5 Prevent insecure bindings
[PASS] 1.6 Prevent insecure port access
[PASS] 1.7 Use Secure Ports for API Server Traffic
[INFO] 1.8 Do not expose API server profiling data
[PASS] 1.9 Verify repair-malformed-updates argument for API compatibility
[PASS] 1.10 Verify that the AlwaysAdmit admission controller is disabled
[FAIL] 1.11 Manage the AlwaysPullImages admission controller
...

Falco


Falco 可以用来监控 Kubernetes runtime security。详细说明请参阅 Falco 官方说明

我们可以透过 OperatorHub 来安装 Falco Operator 到 OpenShift 上。

  1. 安装 Falco Operator。

https://ithelp.ithome.com.tw/upload/images/20201014/201303215hVOevTjOv.png

  1. 安装 Falco DaemonSet

https://ithelp.ithome.com.tw/upload/images/20201014/20130321k7yxIoj5NC.png

  1. 设定客制化的规则 (Rule)

例如,我们想要接收到通知当有人在 Pod 里面运行 bash 指令时.

把下列 Rule 加到 Falco DaemonSet 的 YAML 档。

          - macro: shell_procs
            condition: proc.name in (shell_binaries)
            output: shell in a container (user=%user.name container_id=%container.id container_name=%container.name shell=%proc.name parent=%proc.pname cmdline=%proc.cmdline)
            priority: WARNING

https://ithelp.ithome.com.tw/upload/images/20201014/20130321NcZ4Sl5MQu.png

  1. 测试客制化的规则是否有用,

我们在别的 console 透过 "oc rsh" 连进某个 Pod, 然後你可以找到对应的 logs 出现在 Falco DaemonSet Pod 内,如果你有用 Splunk 或 EFK Stack 在监控 Pod logs,你可以很轻易的设定对应的 Alerts。

$ oc logs -f ds/example-falco

...

04:54:40.373142233: Notice A shell was spawned in a container with an attached terminal (user=<NA> k8s.ns=myproject k8s.pod=nginx-pod container=d165df35e317 shell=sh parent=runc cmdline=sh -c TERM="xterm-256color" /bin/sh terminal=34817 container_id=d165df35e317 image=<NA>) k8s.ns=myproject k8s.pod=nginx-pod container=d165df35e317
04:54:40.375631721: Notice A shell was spawned in a container with an attached terminal (user=<NA> k8s.ns=myproject k8s.pod=nginx-pod container=d165df35e317 shell=sh parent=runc cmdline=sh terminal=34817 container_id=d165df35e317 image=<NA>) k8s.ns=myproject k8s.pod=nginx-pod container=d165df35e317

结论啦


简单整理一下, 要进化成 DevSecOps 兽来保护OpenShift 系统及应用程序,你应该

  • 加密及备份 etcd 资料。
  • 确定系统设定有遵守 CIS Benchmark (可藉由 kube-bench 辅助)。
  • 在 CI/CD Pipeline 扫描映像档的 vulnerabilities,没有风险才可以 Release 到 OpenShift。
  • 透过 container-security-operator 持续监控 Pod 的 vulnerabilities。
  • 透过 Falco 持续监控系统的 runtime activity。

<<:  《Day29》Oracle Database 实体档案

>>:  YouTube 推动全球电信商建立私有云

个人or团队这回事(下)

先写警语:这篇...会是一个半成品思绪,而且是一个自我的幼稚心路历程回顾…因为组织起来心会有点情绪,...

我想当工程师!要念资讯相关科系吗?

在业界蛮多如何成为工程师的课程,至於要不要念本科系,以现今的社会来说不一定是必要条件。相关科系从事相...

予焦啦!Golang 当中的讯号(signal)机制

予焦啦!正如 Golang 自己维护了记忆体管理机制(竞技场、记忆体抽象层、垃圾回收、...)般,让...

[ 卡卡 DAY 4 ] - React Native 专案结构

我的资料结构 . ├── App.js // App root component,the star...

观注的系列

之後会将铁人赛里的系列文做一个介绍 不过分类是依照我的想法 Google Assistant Goo...