Trivy でコンテナイメージ、IaC、Dockerfile リポジトリをセキュリティスキャン

概要

Trivy を利用してコンテナのセキュリティチェックを行いたい。

  • Trivy v0.38 (2023/03/11)

公式リポジトリ。

公式ドキュメント。

【WIP】Trivy コマンドまとめ

スキャン対象と内容、目的でコマンドが異なる。

コマンド目的対象チェック項目
trivy config設定不備チェックIaCファイル ( Terraform )
Dockerfile
trivy imageコンテナイメージからコンテナイメージ
(イメージ内のファイル、メタデータ)
Vulnerabilities
Misconfigurations
Secrets
Licenses
trivy fsファイルシステムチェックファイルシステムVulnerabilities
Misconfigurations
Secrets
Licenses
S
trivy repoリポジトリチェックリモートリポジトリVulnerabilities
Misconfigurations
Secrets
Licenses
[WIP] trivy rootfs
[WIP] trivy aws
[WIP] trivy k8s

Trivy 準備

Mac なら brew で入る

brew install trivy

もしくは docker で。

docker run aquasec/trivy

Trivy CLI 使い方

設定不備チェック ( Misconfiguration )

設定不備を発見する。

IaC

IaC ファイルを静的解析して設定不備をチェックすることが可能
対象は Terraform, CloudFormation, Helm など。 CSPM/KSPM 機能。

下記は Terraform ファイルをスキャンした場合の結果。
16 件テストし、14 の FAILURES(大量に出力されたため一部省略)。

$ trivy config . | head -n 50
2023-03-01T08:31:29.382+0900	INFO	Misconfiguration scanning is enabled
2023-03-01T08:31:29.949+0900	INFO	Detected config files: 14

modules/alb/main.tf (terraform)
===============================
Tests: 16 (SUCCESSES: 2, FAILURES: 14, EXCEPTIONS: 0)
Failures: 14 (UNKNOWN: 0, LOW: 4, MEDIUM: 0, HIGH: 4, CRITICAL: 6)

HIGH: Application load balancer is not set to drop invalid headers.
═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
Passing unknown or invalid headers through to the target poses a potential risk of compromise.

By setting drop_invalid_header_fields to true, anything that doe not conform to well known, defined headers will be removed by the load balancer.

See https://avd.aquasec.com/misconfig/avd-aws-0052
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 modules/alb/main.tf:8-22
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   8 ┌ resource "aws_lb" "for_webserver" {
   9 │   name               = "webserver-alb"
  10 │   internal           = false
  11 │   load_balancer_type = "application"
  12 │
  13 │   security_groups = [
  14 │     aws_security_group.alb.id
  15 │   ]
  16 └
  ..

ALB で HIGH: Application load balancer is not set to drop invalid headers. と指摘。
無効なヘッダーを叩き落としてないため、drop_invalid_header_fields = true を追加。

Terraformを修正し再度スキャン。
FAILURES が 14 -> 12 と減った。

$ trivy config . | head -n 50
2023-03-01T08:34:17.463+0900	INFO	Misconfiguration scanning is enabled
2023-03-01T08:34:18.016+0900	INFO	Detected config files: 14

modules/alb/main.tf (terraform)
===============================
Tests: 15 (SUCCESSES: 3, FAILURES: 12, EXCEPTIONS: 0)
Failures: 12 (UNKNOWN: 0, LOW: 4, MEDIUM: 0, HIGH: 2, CRITICAL: 6)

Dockerfile

Dockerfile をスキャンした場合。

$ trivy config .
2023-03-01T08:13:54.040+0900	INFO	Misconfiguration scanning is enabled
2023-03-01T08:13:54.538+0900	INFO	Detected config files: 1

Dockerfile (dockerfile)

Tests: 24 (SUCCESSES: 23, FAILURES: 1, EXCEPTIONS: 0)
Failures: 1 (UNKNOWN: 0, LOW: 1, MEDIUM: 0, HIGH: 0, CRITICAL: 0)

LOW: Add HEALTHCHECK instruction in your Dockerfile
═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
You shoud add HEALTHCHECK instruction in your docker container images to perform the health check on running containers.

See https://avd.aquasec.com/misconfig/ds026
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

LOW: Add HEALTHCHECK instruction in your Dockerfile とヘルスチェックがないとの結果。

Dockle の結果とは観点が異なる。

脆弱性チェック ( Vulnerability )

コンテナイメージを対象としたスキャン。

OS/言語パッケージ

コンテナイメージを対象にスキャンする。OS/言語の脆弱性をチェックできる。

$ trivy image aquasec/trivy
2023-03-02T07:33:35.108+0900	INFO	Vulnerability scanning is enabled
2023-03-02T07:33:35.108+0900	INFO	Secret scanning is enabled
2023-03-02T07:33:35.108+0900	INFO	If your scanning is slow, please try '--scanners vuln' to disable secret scanning
2023-03-02T07:33:35.108+0900	INFO	Please see also https://aquasecurity.github.io/trivy/v0.37/docs/secret/scanning/#recommendation for faster secret detection
2023-03-02T07:33:46.879+0900	INFO	Detected OS: alpine
2023-03-02T07:33:46.879+0900	INFO	Detecting Alpine vulnerabilities...
2023-03-02T07:33:46.882+0900	INFO	Number of language-specific files: 1
2023-03-02T07:33:46.882+0900	INFO	Detecting gobinary vulnerabilities...

aquasec/trivy (alpine 3.17.1)

Total: 21 (UNKNOWN: 0, LOW: 0, MEDIUM: 8, HIGH: 13, CRITICAL: 0)

┌────────────┬────────────────┬──────────┬───────────────────┬───────────────┬─────────────────────────────────────────────────────────────┐
│  Library   │ Vulnerability  │ Severity │ Installed Version │ Fixed Version │                            Title                            │
├────────────┼────────────────┼──────────┼───────────────────┼───────────────┼─────────────────────────────────────────────────────────────┤
│ git        │ CVE-2023-23946 │ HIGH     │ 2.38.3-r1         │ 2.38.4-r0     │ git: git apply: a path outside the working tree can be      │
│            │                │          │                   │               │ overwritten...                                              │
│            │                │          │                   │               │ https://avd.aquasec.com/nvd/cve-2023-23946                  │
│            ├────────────────┼──────────┤                   │               ├─────────────────────────────────────────────────────────────┤
│            │ CVE-2023-22490 │ MEDIUM   │                   │               │ git: data exfiltration with maliciously crafted repository  │
│            │                │          │                   │               │ https://avd.aquasec.com/nvd/cve-2023-22490

機密情報 ( Secrets )

AWS アクセキー、GCP サービスアカウント、 Github トークンなどのチェックも可能。

trivy image --security-checks コマンドで実行する。

ライセンス

ライセンスチェックすら可能。

trivy image --scanners license コマンド。

Filesystem

ファイルシステムを対象としたスキャン。

OS /言語パッケージ

ローカルにあるファイルシステムを対象にスキャンする。OS/言語の脆弱性をチェックできる。

以下は Next.js プロジェクトをチェックした結果。node_modules がない状態だとチェックされなかった。

$ trivy fs .
2023-03-02T07:40:33.626+0900	INFO	Vulnerability scanning is enabled
2023-03-02T07:40:33.626+0900	INFO	Secret scanning is enabled
2023-03-02T07:40:33.626+0900	INFO	If your scanning is slow, please try '--scanners vuln' to disable secret scanning
2023-03-02T07:40:33.626+0900	INFO	Please see also https://aquasecurity.github.io/trivy/v0.37/docs/secret/scanning/#recommendation for faster secret detection
2023-03-02T07:40:33.728+0900	INFO	Number of language-specific files: 2
2023-03-02T07:40:33.728+0900	INFO	Detecting yarn vulnerabilities...
2023-03-02T07:40:33.731+0900	INFO	Detecting npm vulnerabilities...

node_modules/uri-js/yarn.lock (yarn)

Total: 14 (UNKNOWN: 0, LOW: 3, MEDIUM: 3, HIGH: 7, CRITICAL: 1)

┌──────────────────────┬─────────────────────┬──────────┬───────────────────┬────────────────────────────┬──────────────────────────────────────────────────────────────┐
│       Library        │    Vulnerability    │ Severity │ Installed Version │       Fixed Version        │                            Title                             │
├──────────────────────┼─────────────────────┼──────────┼───────────────────┼────────────────────────────┼──────────────────────────────────────────────────────────────┤
│ ansi-regex           │ CVE-2021-3807       │ HIGH     │ 3.0.0             │ 3.0.1, 4.1.1, 5.0.1, 6.0.1 │ nodejs-ansi-regex: Regular expression denial of service      │
│                      │                     │          │                   │                            │ (ReDoS) matching ANSI escape codes                           │
│                      │                     │          │                   │                            │ https://avd.aquasec.com/nvd/cve-2021-3807                    │
│                      │                     │          ├───────────────────┤                            │                                                              │
│                      │                     │          │ 4.1.0             │                            │                                                              │
│                      │                     │          │                   │                            │                                                              │
│                      │                     │          │                   │                            │                                                              │

イメージスキャン時に比べて、OSパッケージ分少ない。

Git リポジトリ (Github/Gitlab)

Git リポジトリに対するスキャンも実行可能 (Vulnerabilities, Misconfigurations, Secrets, Licenses)。
非同期的なチェックに利用できる。

パブリックリポジトリへのスキャンには Token が不要。

$ trivy repo <your private GitHub repo URL>

プライベートリポジトリへのスキャンには Token が必要。

$ export GITHUB_TOKEN="your_private_github_token"
$ trivy repo <your private GitHub repo URL>

Vulnerabilities

package-lock.json などを参照し、脆弱性のチェックを行う。
同様に Secrets のチェックも行ってくれる。

$ trivy repo https://github.com/runble1/next-ecs-terraform
2023-03-15T14:52:07.145+0900	INFO	Vulnerability scanning is enabled
2023-03-15T14:52:07.145+0900	INFO	Secret scanning is enabled
2023-03-15T14:52:07.145+0900	INFO	If your scanning is slow, please try '--scanners vuln' to disable secret scanning
2023-03-15T14:52:07.145+0900	INFO	Please see also https://aquasecurity.github.io/trivy/v0.38/docs/secret/scanning/#recommendation for faster secret detection
Enumerating objects: 95, done.
Counting objects: 100% (95/95), done.
Compressing objects: 100% (88/88), done.
Total 95 (delta 16), reused 50 (delta 0), pack-reused 0
2023-03-15T14:52:07.854+0900	INFO	Number of language-specific files: 1
2023-03-15T14:52:07.854+0900	INFO	Detecting npm vulnerabilities...

ブランチを指定してのスキャンも可能。

$ trivy repo --branch <branch-name> <repo-name>

Misconfigurations

IaC ファイルと Dockerfile を参照し、設定不備チェックを行える。

$ trivy repo https://github.com/runble1/next-ecs-terraform  --scanners config | head -n 50
2023-03-15T14:49:10.680+0900	INFO	Misconfiguration scanning is enabled
Enumerating objects: 95, done.
Counting objects: 100% (95/95), done.
Compressing objects: 100% (88/88), done.
Total 95 (delta 16), reused 50 (delta 0), pack-reused 0
2023-03-15T14:49:12.421+0900	INFO	Detected config files: 9

nextjs-docker/Dockerfile (dockerfile)
=====================================
Tests: 24 (SUCCESSES: 23, FAILURES: 1, EXCEPTIONS: 0)
Failures: 1 (UNKNOWN: 0, LOW: 1, MEDIUM: 0, HIGH: 0, CRITICAL: 0)

LOW: Add HEALTHCHECK instruction in your Dockerfile
═════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
You shoud add HEALTHCHECK instruction in your docker container images to perform the health check on running containers.

See https://avd.aquasec.com/misconfig/ds026
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────



resource/modules/alb/main.tf (terraform)

json 形式でファイルに出力する。

$ trivy repo https://github.com/runble1/next-ecs-terraform  \
--scanners config \
--format json \
--output ./result.txt

severity を HIGH と CRITICAL のみに制御する。

$ trivy repo https://github.com/runble1/next-ecs-terraform  \
--scanners config \
--severity HIGH,CRITICAL

ライセンス

以下のコマンドでもチェックできる。

$ trivy repo --scanners license https://github.com/runble1/next-ecs-terraform
2023-03-15T14:48:08.100+0900	INFO	License scanning is enabled
Enumerating objects: 95, done.
Counting objects: 100% (95/95), done.
Compressing objects: 100% (88/88), done.
Total 95 (delta 16), reused 50 (delta 0), pack-reused 0

参考

Overview - Trivy
A Simple and Comprehensive Vulnerability Scanner for Containers and other Artifacts, Suitable for CI

コメント

タイトルとURLをコピーしました