Analyze vulnerabilities in a Docker image
In this step, we will learn how to analyze vulnerabilities in a Docker image using Trivy. Trivy is a comprehensive and versatile security scanner. It can detect vulnerabilities in operating system packages (Alpine, RHEL, CentOS, Debian, Ubuntu, etc.) and application dependencies (Bundler, Composer, npm, yarn, etc.). In addition, Trivy supports various scan targets, such as container images, filesystems, and Git repositories.
First, let's pull a vulnerable Docker image that we will use for analysis. We will use the library/ubuntu:18.04
image, which is known to have some vulnerabilities.
docker pull library/ubuntu:18.04
You should see output indicating that the image is being pulled and downloaded.
18.04: Pulling from library/ubuntu
...
Status: Downloaded newer image for ubuntu:18.04
docker.io/library/ubuntu:18.04
Now that we have the image, we need to install Trivy. Since Trivy is not pre-installed in the LabEx environment, we will download and install it. We will download the latest release from the official GitHub repository.
wget https://github.com/aquasecurity/trivy/releases/download/v0.50.1/trivy_0.50.1_Linux-64bit.deb
This command downloads the Trivy Debian package. You should see output similar to this:
--2023-10-27 08:00:00-- https://github.com/aquasecurity/trivy/releases/download/v0.50.1/trivy_0.50.1_Linux-64bit.deb
Resolving github.com (github.com)... 140.82.113.4
Connecting to github.com (github.com)|140.82.113.4|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://objects.githubusercontent.com/github-production-release-asset-2e65be/140000000/...?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=... [following]
--2023-10-27 08:00:00-- https://objects.githubusercontent.com/github-production-release-asset-2e65be/140000000/...
Resolving objects.githubusercontent.com (objects.githubusercontent.com)... 185.199.108.133, 185.199.109.133, 185.199.110.133, ...
Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.108.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 50000000 (48M) [application/octet-stream]
Saving to: ‘trivy_0.50.1_Linux-64bit.deb’
trivy_0.50.1_Linux-64bit.deb 100%[=================================================>] 47.68M --.-MB/s in 0.5s
2023-10-27 08:00:01 (95.3 MB/s) - ‘trivy_0.50.1_Linux-64bit.deb’ saved [50000000/50000000]
Now, install the downloaded package using dpkg
.
sudo dpkg -i trivy_0.50.1_Linux-64bit.deb
You should see output indicating the installation process.
Selecting previously unselected package trivy.
(Reading database ... 100000 files and directories currently installed.)
Preparing to unpack trivy_0.50.1_Linux-64bit.deb ...
Unpacking trivy (0.50.1) ...
Setting up trivy (0.50.1) ...
With Trivy installed, we can now scan the library/ubuntu:18.04
Docker image for vulnerabilities.
trivy image library/ubuntu:18.04
Trivy will start scanning the image. This might take a few moments as it downloads vulnerability databases and analyzes the image layers. You will see output showing the progress and then a detailed report of the vulnerabilities found.
2023-10-27T08:00:05.000+0000 INFO Need to update DB
2023-10-27T08:00:05.000+0000 INFO Downloading DB...
...
2023-10-27T08:00:10.000+0000 INFO Detected OS: ubuntu
2023-10-27T08:00:10.000+0000 INFO Detecting Ubuntu vulnerabilities...
...
library/ubuntu:18.04 (ubuntu 18.04)
===================================
Total: 100 (UNKNOWN: 0, LOW: 50, MEDIUM: 30, HIGH: 15, CRITICAL: 5)
┌───────────────────────────────────────────────────┬─────────────────────────────────┬──────────┬───────────────────┬───────────────────────────────────────────────────────────┐
│ Library/Package │ Vulnerability │ Severity │ Installed Version │ Fixed Version │
├───────────────────────────────────────────────────┼─────────────────────────────────┼──────────┼───────────────────┼───────────────────────────────────────────────────────────┤
│ apt │ CVE-YYYY-XXXX │ HIGH │ 1.6.12 │ 1.6.14 │
│ ... │ ... │ ... │ ... │ ... │
└───────────────────────────────────────────────────┴─────────────────────────────────┴──────────┴───────────────────┴───────────────────────────────────────────────────────────┘
The output provides a summary of the total vulnerabilities found, categorized by severity (UNKNOWN, LOW, MEDIUM, HIGH, CRITICAL). It then lists each vulnerability with details like the affected package, the vulnerability ID (e.g., CVE-YYYY-XXXX), the severity level, the installed version of the package, and the fixed version if available. This report helps you understand the security posture of your Docker image and identify areas that need remediation.