0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

trivyを理解する

Last updated at Posted at 2025-04-10

目的

  • trivyを理解する

手段

killercodaで手を動かす

環境

killercodaで手を動かす

trivyとは

trivy はセキュリティスキャナです。trivy はコンテナイメージ・ファイルシステム・リポジトリに対して脆弱性やSBOMのスキャンを実行できます。例えば、現在利用中のコンテナイメージに対して特定 CVE が該当しているかどうかを確認できます。

手を動かす

コマンドフォーマットは以下の通りです。

controlplane:~$ trivy --help
NAME:
   trivy - A simple and comprehensive vulnerability scanner for containers

USAGE:
   trivy [global options] command [command options] target

VERSION:
   0.22.0

COMMANDS:
   image, i          scan an image
   filesystem, fs    scan local filesystem for language-specific dependencies and config files
   rootfs            scan rootfs
   repository, repo  scan remote repository
   client, c         client mode
   server, s         server mode
   config, conf      scan config files
   plugin, p         manage plugins
   help, h           Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --quiet, -q        suppress progress bar and log output (default: false) [$TRIVY_QUIET]
   --debug, -d        debug mode (default: false) [$TRIVY_DEBUG]
   --cache-dir value  cache directory (default: "/root/.cache/trivy") [$TRIVY_CACHE_DIR]
   --help, -h         show help (default: false)
   --version, -v      print the version (default: false)
# trivy {実行対象} |grep -E "CVE-XXXX-XXXX" のような形式で利用します

既存 deployment の image を確認します。

controlplane:~$ k -n applications get po -oyaml |grep image:
    - image: nginx:1.19.1-alpine-perl
      image: docker.io/library/nginx:1.19.1-alpine-perl
    - image: nginx:1.19.1-alpine-perl
      image: docker.io/library/nginx:1.19.1-alpine-perl
    - image: nginx:1.20.2-alpine
      image: docker.io/library/nginx:1.20.2-alpine
# -oyaml でyamlフォーマットで出力すれば一覧で確認できます

特定イメージに対して trivy でスキャンします。

controlplane:~$ trivy image nginx:1.19.1-alpine-perl | grep "CVE-2021-28831"      
| busybox      | CVE-2021-28831   |          | 1.31.1-r9         | 1.31.1-r10    | busybox: invalid free or segmentation |
| ssl_client   | CVE-2021-28831   | HIGH     | 1.31.1-r9         | 1.31.1-r10    | busybox: invalid free or segmentation |
# スキャン対象のコンテナイメージは当該 CVE に該当しています。

trivy スキャン結果は出力形式やフィルリングが可能です。
まずは出力形式を指定します。

trivy image -f json nginx:latest > json_format.txt
# ポイントはコマンド構文ですね。`trivy image [OPTIN] イメージ名 となります` 

正しく json 形式で出力されているか確認します。

controlplane:~$ more json_format.txt 
2025-09-22T11:18:14.065Z        INFO    Detected OS: debian
2025-09-22T11:18:14.065Z        INFO    Detecting Debian vulnerabilities...
2025-09-22T11:18:14.096Z        INFO    Number of language-specific files: 0
{
  "SchemaVersion": 2,
  "ArtifactName": "nginx:latest",
  "ArtifactType": "container_image",
  "Metadata": {
#OKですね

続いて実行結果をファイルに出力します

controlplane:~$ trivy image -o nginx-tabel-format.txt nginx:latest
2025-09-22T11:23:44.840Z        INFO    Detected OS: debian
2025-09-22T11:23:44.840Z        INFO    Detecting Debian vulnerabilities...
2025-09-22T11:23:44.858Z        INFO    Number of language-specific files: 0

nginx:latest (debian 12.12)
===========================
Total: 88 (UNKNOWN: 8, LOW: 72, MEDIUM: 6, HIGH: 2, CRITICAL: 0)

controlplane:~$ more nginx-tabel-format.txt 
+------------------+------------------+----------+------------------------+---------------+-----------------------------------------+
|     LIBRARY      | VULNERABILITY ID | SEVERITY |   INSTALLED VERSION    | FIXED VERSION |                  TITLE                  |
+------------------+------------------+----------+------------------------+---------------+-----------------------------------------+
| apt              | CVE-2011-3374    | LOW      | 2.6.1                  |               | It was found that apt-key in apt,       |
|                  |                  |          |                        |               | all versions, do not correctly...       |
|                  |                  |          |                        |               | -->avd.aquasec.com/nvd/cve-2011-3374    |
# 標準出力をリダイレクトする必要はないですね

最後に脆弱性のSeverityでフィルタリングします。

controlplane:~$ trivy image -s CRITICAL nginx:latest
2025-09-22T11:26:43.540Z        INFO    Detected OS: debian
2025-09-22T11:26:43.540Z        INFO    Detecting Debian vulnerabilities...
2025-09-22T11:26:43.557Z        INFO    Number of language-specific files: 0

nginx:latest (debian 12.12)
===========================
Total: 0 (CRITICAL: 0)
#CRITICALは該当無しです

controlplane:~$ trivy image -s HIGH nginx:latest
2025-09-22T11:27:02.507Z        INFO    Detected OS: debian
2025-09-22T11:27:02.508Z        INFO    Detecting Debian vulnerabilities...
2025-09-22T11:27:02.531Z        INFO    Number of language-specific files: 0

nginx:latest (debian 12.12)
===========================
Total: 2 (HIGH: 2)

+---------+------------------+----------+-------------------+---------------+--------------------------------------+
| LIBRARY | VULNERABILITY ID | SEVERITY | INSTALLED VERSION | FIXED VERSION |                TITLE                 |
+---------+------------------+----------+-------------------+---------------+--------------------------------------+
| libssl3 | CVE-2023-0286    | HIGH     | 3.0.17-1~deb12u2  |               | X.400 address type confusion         |
|         |                  |          |                   |               | in X.509 GeneralName                 |
|         |                  |          |                   |               | -->avd.aquasec.com/nvd/cve-2023-0286 |
+---------+                  +          +                   +---------------+                                      +
| openssl |                  |          |                   |               |                                      |
|         |                  |          |                   |               |                                      |
|         |                  |          |                   |               |                                      |
+---------+------------------+----------+-------------------+---------------+--------------------------------------+
# HIGH は1件のみです

-o オプションと -s を駆使すれば、大分効率的にコマンド実行できます。

あとがき

kubernetestは銀河系の太陽系ですね...広大だ...

ソース

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?