What is inside an image you inherit?
Scanning a container image means listing every package inside it and checking each version against known vulnerabilities. Trivy, an open-source scanner, does this in seconds.
Most of an image is somebody else's work: a base system, libraries and tools. A scan is how you find out what came with it.
By the end of this lesson
Note
I can scan a container image and a project with Trivy and read what it found.
Key words
| Word | What it means | Example |
|---|---|---|
| Image scanner | a tool that lists an image's packages and their known flaws | |
| Package database | the file where a system records what is installed | |
| Lock file | a file pinning the exact versions a project uses | requirements.txt |
| Fixed version | the first version without the flaw | |
| End of life | no more security fixes from the maker |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsA scanner reads the package database
An image scanner does not run the image or attack it. It reads files. First it reads which system the image is, such as Alpine 3.18.6. Then it reads the package database, which names every installed package and version.
Last it looks each one up in advisories for that system. The running web server holds the same files, so you can read them yourself.
Which Alpine is the web server?
docker exec web cat /etc/alpine-releaseShown here, not run. Press Start lab to type it into your own lab’s terminal.
Read libssl3 from the package database
docker exec web grep -A1 -x P:libssl3 /lib/apk/db/installedShown here, not run. Press Start lab to type it into your own lab’s terminal.
What the web server's files say
| File | Line | Meaning |
|---|---|---|
| /etc/alpine-release | 3.18.6 | The system and its release |
| /lib/apk/db/installed | P:libssl3 | A package called libssl3 |
| /lib/apk/db/installed | V:3.1.4-r5 | Its installed version |
The cells students fill in are left blank in this preview.
Check
Have a go
Check
3 questionsYour turn: read libexpat's version
docker exec web grep -A1 -x P:libexpat /lib/apk/db/installedShown here, not run. Press Start lab to type it into your own lab’s terminal.
Findings, CVEs and packages differ
Trivy reports a finding for each package and CVE. One OpenSSL flaw appears twice, once in libssl3 and once in libcrypto3, because both packages come from it. So count CVEs and packages too.
Trivy also says when a system has reached its end of life. Alpine 3.18 gets no more fixes, so a fixed version may only exist in a newer release.
Try it: findings against CVEs
findings = [
["libcrypto3", "CVE-2024-6119"],
["libssl3", "CVE-2024-6119"],
["curl", "CVE-2024-2398"],
["libcurl", "CVE-2024-2398"],
["libexpat", "CVE-2024-45492"],
]
cves = []
for f in findings:
if f[1] not in cves:
cves.append(f[1])
print(len(findings), "findings", len(cves), "CVEs")
# Count the packages instead: how many different ones are there?
Make a free account to save your work and track your progress.
Write the helper that reads reports, part 1
docker exec tools sh -c "printf '%s\n' 'import csv' 'import gzip' 'import json' '' 'EPSS_FILE = \"/work/snapshot/epss-2026-11-20.csv.gz\"' 'KEV_FILE = \"/work/snapshot/kev-2026-11-20.json\"' '' 'def findings(name):' ' with open(\"/work/reports/\" + name + \".json\") as f:' ' report = json.load(f)' ' rows = []' ' for result in report.get(\"Results\", []):' ' rows = rows + (result.get(\"Vulnerabilities\") or [])' ' return rows' '' 'def cve_list(rows):' ' cves = []' ' for v in rows:' ' if v[\"VulnerabilityID\"] not in cves:' ' cves.append(v[\"VulnerabilityID\"])' > steps/snap.py"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the helper that reads reports, part 2
docker exec tools sh -c "printf '%s\n' ' return cves' '' 'def epss(wanted):' ' scores = {}' ' with gzip.open(EPSS_FILE, \"rt\") as f:' ' for row in csv.reader(f):' ' if row[0] in wanted:' ' scores[row[0]] = [float(row[1]), float(row[2])]' ' return scores' '' 'def kev():' ' with open(KEV_FILE) as f:' ' catalogue = json.load(f)' ' return {v[\"cveID\"]: v for v in catalogue[\"vulnerabilities\"]}' '' 'def nvd(v):' ' return v.get(\"CVSS\", {}).get(\"nvd\", {}).get(\"V3Score\")' >> steps/snap.py"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the script that counts a report
docker exec tools sh -c "printf '%s\n' 'import sys' 'import snap' 'import json' '' 'with open(\"/work/reports/\" + sys.argv[1] + \".json\") as f:' ' os = json.load(f).get(\"Metadata\", {}).get(\"OS\")' 'if os:' ' print(os[\"Family\"], os[\"Name\"], \"end of life:\", os.get(\"EOSL\", False))' 'rows = snap.findings(sys.argv[1])' 'packages = []' 'fixable = 0' 'for v in rows:' ' if v[\"PkgName\"] not in packages:' ' packages.append(v[\"PkgName\"])' ' if v.get(\"FixedVersion\"):' ' fixable = fixable + 1' 'print(\"findings\", len(rows), \"CVE\", len(snap.cve_list(rows)), \"packages\", len(packages), \"with a fix\", fixable)' > steps/count.py"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Scan the old image to JSON
docker exec scanner trivy image --input /targets/web-1.25.3.tar --format json -o /work/reports/web.jsonShown here, not run. Press Start lab to type it into your own lab’s terminal.
Scan the new image to JSON
docker exec scanner trivy image --input /targets/web-1.31.9.tar --format json -o /work/reports/web-new.jsonShown here, not run. Press Start lab to type it into your own lab’s terminal.
Count the old image
docker exec tools python3 steps/count.py webShown here, not run. Press Start lab to type it into your own lab’s terminal.
Count the new image
docker exec tools python3 steps/count.py web-newShown here, not run. Press Start lab to type it into your own lab’s terminal.
What count.py printed for the two images
| Image | Alpine | End of life | Findings | CVEs | Packages | With a fix |
|---|---|---|---|---|---|---|
| nginx 1.25.3 | 3.18.6 | True | 74 | 44 | 13 | 74 |
| nginx 1.31.9 | 3.24.4 | False | 12 | 7 | 4 | 12 |
The cells students fill in are left blank in this preview.
A common mistake
Warning
Many people think a clean scan means no vulnerabilities. A scanner finds only packages it can identify and flaws already in its database. A program copied in by hand, or a flaw not yet published, stays invisible.
Check
Have a go
Check
3 questionsLock files show a project's libraries
Your own code pulls in libraries too, and its lock file lists them with exact versions. Trivy reads requirements.txt for Python and gradle.lockfile for Java with trivy fs, a scan of a folder.
The shop is a Flask app pinned in 2022. Its requirements.txt gives 49 findings: 49 CVEs in 7 packages, each with a fix.
Scan the shop's requirements.txt
docker exec scanner trivy fs /targets/shop --format json -o /work/reports/shop.jsonShown here, not run. Press Start lab to type it into your own lab’s terminal.
Count the shop
docker exec tools python3 steps/count.py shopShown here, not run. Press Start lab to type it into your own lab’s terminal.
Your turn: scan the Java service
docker exec scanner trivy fs /targets/reports --format json -o /work/reports/reports.jsonShown here, not run. Press Start lab to type it into your own lab’s terminal.
Count the Java service
docker exec tools python3 steps/count.py reportsShown here, not run. Press Start lab to type it into your own lab’s terminal.
Assignment: count a scan
Count a scan's findings. Each finding is a dictionary with cve, pkg, severity and fixed, an empty string when there is no fix yet.
by_severity(findings) returns a dictionary from each severity to how many findings have it. unique_cves(findings) returns each CVE once, in the order first seen. fixable(findings) returns how many findings have a fixed version.
def by_severity(findings):
return {}
def unique_cves(findings):
return []
def fixable(findings):
return 0
Make a free account to save your work and track your progress.
Exit quiz
Have a go