🌊

TrivyでKubernetesのリソースをチェックしてみる

に公開

Trivyがコンテナイメージの脆弱性スキャンツールだとばかり思っていましたが
"Simplify Cloud Native Security with Trivy" での登壇を聞いて、できることがかなり増えてそうだったので、触ってみました。

普段は、Kubernetes周りのことを主に取り組んでいるので、Kubernetes関連でできることを試そうと思います。

脆弱性スキャン

皆さんご存知だと思われますが、コンテナイメージの脆弱性スキャンで
Quick Startに書かれているものを実行してみました。

スキャンしたコンテナイメージ(python:3.4-alpine)に存在する脆弱性を出力してくれています。

$ trivy image python:3.4-alpine
2022-08-27T16:46:23.051+0900    INFO    Vulnerability scanning is enabled
2022-08-27T16:46:23.051+0900    INFO    Secret scanning is enabled
2022-08-27T16:46:23.051+0900    INFO    If your scanning is slow, please try '--security-checks vuln' to disable secret scanning
2022-08-27T16:46:23.051+0900    INFO    Please see also https://aquasecurity.github.io/trivy/v0.31.2/docs/secret/scanning/#recommendation for faster secret detection
2022-08-27T16:46:25.589+0900    INFO    Detected OS: alpine
2022-08-27T16:46:25.589+0900    INFO    Detecting Alpine vulnerabilities...
2022-08-27T16:46:25.591+0900    INFO    Number of language-specific files: 1
2022-08-27T16:46:25.591+0900    INFO    Detecting python-pkg vulnerabilities...
2022-08-27T16:46:25.595+0900    WARN    This OS version is no longer supported by the distribution: alpine 3.9.2
2022-08-27T16:46:25.595+0900    WARN    The vulnerability detection may be insufficient because security updates are not provided

python:3.4-alpine (alpine 3.9.2)

Total: 37 (UNKNOWN: 0, LOW: 4, MEDIUM: 16, HIGH: 13, CRITICAL: 4)

┌──────────────┬────────────────┬──────────┬───────────────────┬───────────────┬──────────────────────────────────────────────────────────────┐
   Library Vulnerability Severity Installed Version Fixed Version                            Title
├──────────────┼────────────────┼──────────┼───────────────────┼───────────────┼──────────────────────────────────────────────────────────────┤
 expat CVE-2018-20843 HIGH 2.2.6-r0 2.2.7-r0 expat: large number of colons in input makes parser consume
 high amount...
 https://avd.aquasec.com/nvd/cve-2018-20843
              ├────────────────┤                   ├───────────────┼──────────────────────────────────────────────────────────────┤
 CVE-2019-15903 2.2.7-r1 expat: heap-based buffer over-read via crafted XML input
 https://avd.aquasec.com/nvd/cve-2019-15903
├──────────────┼────────────────┼──────────┼───────────────────┼───────────────┼──────────────────────────────────────────────────────────────┤
 libbz2 CVE-2019-12900 CRITICAL 1.0.6-r6 1.0.6-r7 bzip2: out-of-bounds write in function BZ2_decompress
 https://avd.aquasec.com/nvd/cve-2019-12900
...
├──────────────┼────────────────┼──────────┼───────────────────┼───────────────┼──────────────────────────────────────────────────────────────┤
 sqlite-libs CVE-2019-8457 CRITICAL 3.26.0-r3 3.28.0-r0 sqlite: heap out-of-bound read in function rtreenode()       
 https://avd.aquasec.com/nvd/cve-2019-8457
              ├────────────────┼──────────┤                   ├───────────────┼──────────────────────────────────────────────────────────────┤
 CVE-2019-19244 HIGH 3.28.0-r2 sqlite: allows a crash if a sub-select uses both DISTINCT
 and window...
 https://avd.aquasec.com/nvd/cve-2019-19244
              ├────────────────┤                   ├───────────────┼──────────────────────────────────────────────────────────────┤
 CVE-2019-5018 3.28.0-r0 sqlite: Use-after-free in window function leading to remote
 code execution
 https://avd.aquasec.com/nvd/cve-2019-5018
              ├────────────────┤                   ├───────────────┼──────────────────────────────────────────────────────────────┤
 CVE-2020-11655 3.28.0-r3 sqlite: malformed window-function query leads to DoS
 https://avd.aquasec.com/nvd/cve-2020-11655
              ├────────────────┼──────────┤                   ├───────────────┼──────────────────────────────────────────────────────────────┤
 CVE-2019-16168 MEDIUM 3.28.0-r1 sqlite: Division by zero in whereLoopAddBtreeIndex in
 sqlite3.c
 https://avd.aquasec.com/nvd/cve-2019-16168
              ├────────────────┤                   ├───────────────┼──────────────────────────────────────────────────────────────┤
 CVE-2019-19242 3.28.0-r2 sqlite: SQL injection in sqlite3ExprCodeTarget in expr.c
 https://avd.aquasec.com/nvd/cve-2019-19242
└──────────────┴────────────────┴──────────┴───────────────────┴───────────────┴──────────────────────────────────────────────────────────────┘
2022-08-27T16:46:25.613+0900    INFO    Table result includes only package filenames. Use '--format json' option to get the full path to the package file.

Python (python-pkg)

Total: 2 (UNKNOWN: 0, LOW: 0, MEDIUM: 1, HIGH: 1, CRITICAL: 0)

┌────────────────┬────────────────┬──────────┬───────────────────┬───────────────┬─────────────────────────────────────────────────────────────┐
    Library Vulnerability Severity Installed Version Fixed Version                            Title
├────────────────┼────────────────┼──────────┼───────────────────┼───────────────┼─────────────────────────────────────────────────────────────┤
 pip (METADATA) │ CVE-2019-20916 │ HIGH     │ 19.0.3            │ 19.2          │ python-pip: directory traversal in _download_http_url()     │
 function in src/pip/_internal/download.py
 https://avd.aquasec.com/nvd/cve-2019-20916
                ├────────────────┼──────────┤                   ├───────────────┼─────────────────────────────────────────────────────────────┤
 CVE-2021-3572 MEDIUM 21.1 python-pip: Incorrect handling of unicode separators in git
 references
 https://avd.aquasec.com/nvd/cve-2021-3572
└────────────────┴────────────────┴──────────┴───────────────────┴───────────────┴─────────────────────────────────────────────────────────────┘

脆弱性スキャンもできることが増えていて
Container Image, Filesystem, Rootfs, Git Repositoryの脆弱性を見つけてくれます。

Misconfigurationスキャン

Terraform, CloudFormation, Dockerfile のようなIaCファイルのMisconfigurationを見つけてくれて、Kubernetes manifest でも確認できました。
以下では、Kubernetes の公式ドキュメントに記載されているDeploymentを作成するページからコピペしてきたDeploymentをスキャンしています。

Kustomize化してしまっていますが、以下にPublic Repository として公開したので、よかったら使ってください。
https://github.com/dubs11kt/trivy-sample

$ pwd
/Users/dubs11kt/git/trivy-sample

$ trivy config  .
2022-08-27T16:56:02.609+0900    INFO    Misconfiguration scanning is enabled
2022-08-27T16:56:02.794+0900    INFO    Detected config files: 3

kustomize/base/deployment.yaml (kubernetes)

Tests: 78 (SUCCESSES: 66, FAILURES: 12, EXCEPTIONS: 0)
Failures: 12 (UNKNOWN: 0, LOW: 10, MEDIUM: 2, HIGH: 0, CRITICAL: 0)

MEDIUM: Container 'nginx' of Deployment 'nginx-deployment' should set 'securityContext.allowPrivilegeEscalation' to false
═════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
A program inside the container can elevate its own privileges and run as root, which might give the program control over the container and node.

See https://avd.aquasec.com/misconfig/ksv001
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 kustomize/base/deployment.yaml:18-21
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
  18       - name: nginx
  19         image: nginx:1.14.2
  20         ports:
  21         - containerPort: 80
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
...

出力されたものから分かる通り、78件テストされ、12件報告されています。

Tests: 78 (SUCCESSES: 66, FAILURES: 12, EXCEPTIONS: 0)
Failures: 12 (UNKNOWN: 0, LOW: 10, MEDIUM: 2, HIGH: 0, CRITICAL: 0)

上記のコマンド結果の1つを見てみると
nginx-deployment の nginx コンテナにsecurityContext.allowPrivilegeEscalationをfalse に設定するべきとあります。
Configure a Security Context for a Pod or Containerにある通り
プロセスが親プロセスより多くの特権を得ることができるかどうかを制御する設定となりますので、できる限りfalseにすべきです。
デフォルトはtrueとなりますので、明示的にfalseを設定してあげる必要があります。

Kubernetes Clusterのスキャン

Kubernetes Clusterの脆弱性、Misconfiguration、Secrets?(よくわかってない)のスキャンを行うことができます。
kind で立てたCluster全体にコマンドを実施してみました。

$ trivy k8s --report=summary cluster

163 / 163 [------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------] 100.00% 4 p/s

Summary Report for kind-kind
┌────────────────────┬────────────────────────────────────────────────┬─────────────────────────┬────────────────────┬───────────────────┐
     Namespace                    Resource     Vulnerabilities Misconfigurations      Secrets
                                                ├────┬─────┬────┬─────┬───┼───┬───┬───┬────┬───┼───┬───┬───┬───┬───┤
 C  H M  L U C H M L U C H M L U
├────────────────────┼────────────────────────────────────────────────┼────┼─────┼────┼─────┼───┼───┼───┼───┼────┼───┼───┼───┼───┼───┼───┤
 local-path-storage Deployment/local-path-provisioner 4  6 6 11 3 2 10
 kube-system Pod/kube-apiserver-kind-control-plane  1 4 19
 kube-system Deployment/coredns  4 1 2 3 5
 kube-system Pod/kube-scheduler-kind-control-plane  1 3 9
 kube-system DaemonSet/kube-proxy 7 11 3 56 2 4 10
 kube-system Service/kube-dns 1
 kube-system DaemonSet/kindnet 8 11 4 56 1 1 5 6
 kube-system Pod/kube-controller-manager-kind-control-plane  1 3 11
 kube-system Pod/etcd-kind-control-plane 12 4 4 1 3 7
 default Deployment/nginx-deployment 56 106 86 159 7 2 10
└────────────────────┴────────────────────────────────────────────────┴────┴─────┴────┴─────┴───┴───┴───┴───┴────┴───┴───┴───┴───┴───┴───┘
Severities: C=CRITICAL H=HIGH M=MEDIUM L=LOW U=UNKNOWN


Summary Report for kind-kind
┌─────────────┬─────────────────────────────────────────────────────────────────┬───────────────────┐
  Namespace                            Resource  RBAC Assessment
                                                                 ├───┬───┬───┬───┬───┤
 C H M L U
├─────────────┼─────────────────────────────────────────────────────────────────┼───┼───┼───┼───┼───┤
 kube-system Role/system::leader-locking-kube-controller-manager 1
 kube-system Role/system:controller:bootstrap-signer 1
 kube-system Role/system:controller:token-cleaner 1
 kube-system Role/system::leader-locking-kube-scheduler 1
 kube-system Role/system:controller:cloud-provider 1
 kube-public Role/system:controller:bootstrap-signer 1
 ClusterRole/edit 2 7 1
 ClusterRole/system:controller:endpointslicemirroring-controller 1
 ClusterRole/system:controller:generic-garbage-collector 1
 ClusterRole/system:controller:expand-controller 1
 ClusterRole/system:controller:job-controller 1
 ClusterRole/system:controller:root-ca-cert-publisher 1
 ClusterRole/system:controller:persistent-volume-binder 1 2
 ClusterRole/system:kube-scheduler 2
 ClusterRole/system:kube-controller-manager 5 2
 ClusterRole/system:controller:resourcequota-controller 1
 ClusterRole/system:controller:namespace-controller 1
 ClusterRole/system:controller:replicaset-controller 1
 ClusterRole/cluster-admin 2
 ClusterRole/admin 3 7 1
 ClusterRole/system:aggregate-to-admin 1
 ClusterRole/system:controller:replication-controller 1
 ClusterRole/system:controller:endpointslice-controller 1
 ClusterRole/system:controller:deployment-controller 2
 ClusterRole/system:controller:horizontal-pod-autoscaler 2
 ClusterRole/system:node 1
 ClusterRole/system:controller:endpoint-controller 1
 ClusterRole/system:controller:cronjob-controller 2
 ClusterRole/local-path-provisioner-role 1 1
 ClusterRole/system:aggregate-to-edit 2 7 1
└─────────────┴─────────────────────────────────────────────────────────────────┴───┴───┴───┴───┴───┘
Severities: C=CRITICAL H=HIGH M=MEDIUM L=LOW U=UNKNOWN

めちゃめちゃ報告されています。。。。。
業務でこんなにでると気絶しそうです。

あと、単体リソースに対しても実行することができます。
先程同様、nginx の Deploymentに対してコマンドを実行してみます。

脆弱性、Misconfiguration、Secretsのスキャンを一度に実行することができました。
個別にチェックするより便利そうです。が、静的、動的チェックはケースによって要否があると思いますので
ケースに応じて利用するのがよいのではないかな、と思います。

$ trivy kubernetes deployment/nginx-deployment
1 / 1 [----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------] 100.00% 0 p/s

nginx:1.14.2 (debian 9.8)

Total: 414 (UNKNOWN: 7, LOW: 159, MEDIUM: 86, HIGH: 106, CRITICAL: 56)

┌────────────────────────┬──────────────────┬──────────┬────────────────────────┬────────────────────────┬──────────────────────────────────────────────────────────────┐
        Library  Vulnerability Severity   Installed Version     Fixed Version                            Title
├────────────────────────┼──────────────────┼──────────┼────────────────────────┼────────────────────────┼──────────────────────────────────────────────────────────────┤
 apt CVE-2020-27350 MEDIUM 1.4.9 1.4.11 apt: integer overflows and underflows while parsing .deb
 packages
 https://avd.aquasec.com/nvd/cve-2020-27350
                        ├──────────────────┤                        ├────────────────────────┼──────────────────────────────────────────────────────────────┤
 CVE-2020-3810 1.4.10 Missing input validation in the ar/tar implementations of
 APT before v ......
 https://avd.aquasec.com/nvd/cve-2020-3810
                        ├──────────────────┼──────────┤                        ├────────────────────────┼──────────────────────────────────────────────────────────────┤
 CVE-2011-3374 LOW It was found that apt-key in apt, all versions, do not
 correctly...
 https://avd.aquasec.com/nvd/cve-2011-3374
...
├────────────────────────┼──────────────────┼──────────┼────────────────────────┼────────────────────────┼──────────────────────────────────────────────────────────────┤
 zlib1g CVE-2022-37434 CRITICAL 1:1.2.8.dfsg-5 zlib: a heap-based buffer over-read or buffer overflow in
 inflate in inflate.c...
 https://avd.aquasec.com/nvd/cve-2022-37434
                        ├──────────────────┼──────────┤                        ├────────────────────────┼──────────────────────────────────────────────────────────────┤
 CVE-2018-25032 HIGH 1:1.2.8.dfsg-5+deb9u1 zlib: A flaw found in zlib when compressing (not             
 decompressing) certain inputs...                             │
 https://avd.aquasec.com/nvd/cve-2018-25032
└────────────────────────┴──────────────────┴──────────┴────────────────────────┴────────────────────────┴──────────────────────────────────────────────────────────────┘

default-Deployment-nginx-deployment-941555502.yaml (kubernetes)

Tests: 78 (SUCCESSES: 66, FAILURES: 12, EXCEPTIONS: 0)
Failures: 12 (UNKNOWN: 0, LOW: 10, MEDIUM: 2, HIGH: 0, CRITICAL: 0)

MEDIUM: Container 'nginx' of Deployment 'nginx-deployment' should set 'securityContext.allowPrivilegeEscalation' to false
══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
A program inside the container can elevate its own privileges and run as root, which might give the program control over the container and node.

See https://avd.aquasec.com/misconfig/ksv001
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 default-Deployment-nginx-deployment-941555502.yaml:129-137
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 129                 - image: nginx:1.14.2
 130                   imagePullPolicy: IfNotPresent
 131                   name: nginx
 132                   ports:
 133                     - containerPort: 80
 134                       protocol: TCP
 135                   resources: {}
 136                   terminationMessagePath: /dev/termination-log
 137                   terminationMessagePolicy: File
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
...

あとは、SBOMLicenseのスキャン
最近リリースされたクラウドのスキャンもありますね。

気づいたらいろんな事ができるようになっていたのでびっくりしました。
気が向いたら、他のオプションも試してみたいと思います。

GitHubで編集を提案

Discussion