コンテンツにスキップ

trivy@脆弱性診断

はじめに

本サイトにつきまして、以下をご認識のほど宜しくお願いいたします。


01. trivyの仕組み

検出項目

アプリのパッケージ、IaC (Kubernetes、Terraform、Dockerfile) 、イメージ (コンテナイメージ、マシンイメージ) で報告されたCVEに基づいて、ファイルの実装方法に起因する脆弱性を検証する。

Regoでカスタムポリシーを実装できる。


02. セットアップ

インストール

▼ バイナリとして

$ brew install trivy

▼ コンテナとして

$ docker run aquasec/trivy

▼ チャートとして

trivy-operatorをインストールする。

$ helm repo add <チャートリポジトリ名> https://aquasecurity.github.io/helm-charts/

$ helm repo update

$ kubectl create namespace trivy

$ helm install <Helmリリース名> <チャートリポジトリ名>/trivy-operator -n trivy


03. 設定ファイル

.trivyignoreファイル

検証したくないCVEの項目を設定する。

$ cat .trivyignore.yaml

vulnerabilities:
  - id: CVE-2022-40897
...


04. trivyコマンド

config

▼ configとは

▼ --debug

デバッグモードを有効化する。

$ trivy config --debug <IaCファイル>

2023-08-02T04:52:32.908Z    DEBUG   Severities: ["HIGH" "CRITICAL"]
2023-08-02T04:52:32.912Z    DEBUG   cache dir:  /root/.cache/trivy
2023-08-02T04:52:32.912Z    INFO    Misconfiguration scanning is enabled
2023-08-02T04:52:32.912Z    DEBUG   Failed to open the policy metadata: open /root/.cache/trivy/policy/metadata.json: no such file or directory
2023-08-02T04:52:32.912Z    INFO    Need to update the built-in policies
2023-08-02T04:52:32.912Z    INFO    Downloading the built-in policies...
2023-08-02T04:52:32.912Z    DEBUG   Using URL: ghcr.io/aquasecurity/defsec:0 to load policy bundle
41.66 KiB / 41.66 KiB [-----------------------------------------------------------] 100.00% ? p/s 0s2023-08-02T04:52:33.743Z    DEBUG   Digest of the built-in policies: sha256:*****
2023-08-02T04:52:33.743Z    DEBUG   Policies successfully loaded from disk
2023-08-02T04:52:33.776Z    DEBUG   Walk the file tree rooted at '<IaCファイル>' in parallel
2023-08-02T04:52:33.776Z    DEBUG   Scanning Kubernetes files for misconfigurations...
2023-08-02T04:52:36.173Z    DEBUG   Scanning Helm files for misconfigurations...
2023-08-02T04:52:36.192Z    DEBUG   OS is not detected.
2023-08-02T04:52:36.192Z    INFO    Detected config files: 1
2023-08-02T04:52:36.192Z    DEBUG   Scanned config file: <IaCファイル>

▼ --exit-code

脆弱性が検出された時の終了コードを設定する。

デフォルトでは、いずれの結果でも終了コードが0になる。

$ trivy config --exit-code 1 <IaCファイル>

マニフェスト管理ツール (Helm、Kustomize) の作成したマニフェストファイルを渡しても良い。

$ helm template . -f foo-values.yaml -f foo-secrets.yaml > manifest.yaml

$ trivy config --exit-code 1 manifest.yaml

▼ --include-non-failures

もし解析結果に何も問題がなければ、成功したことを表示できるようにする。

デフォルトであると、成功が表示されない。

$ trivy config --include-non-failures <IaCファイル>

▼ --quiet

処理中のプログレスバー処理ログの両方を非表示にする。

# 非表示の場合
$ trivy config --quiet <IaCファイル>

# 表示する場合
$ trivy config <IaCファイル>

2023-08-02T04:50:04.261Z    INFO    Misconfiguration scanning is enabled
2023-08-02T04:50:04.261Z    INFO    Need to update the built-in policies
2023-08-02T04:50:04.261Z    INFO    Downloading the built-in policies...
41.66 KiB / 41.66 KiB [-----------------------------------------------------------] 100.00% ? p/s 0s
2023-08-02T04:50:08.303Z    INFO    Detected config files: 1

▼ --severity

検出する下限の重要度レベル (UNKNOWN、LOW、MEDIUM、HIGH、CRITICAL) を設定する。

$ trivy config --severity HIGH,CRITICAL <IaCファイル>