Skip to content

Commit f28afd4

Browse files
committed
Add container image scanning script
Added tools/wazuh-scan-images.sh to scan all the container images running on a host. The script will be used in the future to scan images on a schedule using Wazuh.
1 parent 3616595 commit f28afd4

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
features:
3+
- |
4+
Added ``wazuh-scan-images.sh``, a script to scan container images for
5+
vulnerabilities. In a future release, this script can be integrated into
6+
Wazuh for continuous scanning.

tools/wazuh-scan-images.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/bash
2+
3+
# SBOM directory path
4+
SBOM_DIR="/opt/kayobe/stackhpc/sboms"
5+
6+
# Ensure the SBOM directory exists
7+
mkdir -p "$SBOM_DIR"
8+
9+
# Ensure the custom output template exists
10+
cat <<EOL > "$SBOM_DIR/trivy-custom.tmpl"
11+
"Package","Version Installed","Vulnerability ID","Severity","Title"
12+
{{- range \$ri, \$r := . }}
13+
{{- range \$vi, \$v := .Vulnerabilities }}
14+
"{{ $v.PkgName }}","{{$v.InstalledVersion }}","{{ $v.VulnerabilityID }}","{{$v.Severity }}","{{$v.Title }}"
15+
{{- end}}
16+
{{- end }}
17+
EOL
18+
19+
# Loop through each container image and process its SBOM
20+
docker image ls --format "{{.Repository}}:{{.Tag}}" | sort | uniq | while read -r image; do
21+
# Generate SBOM filename
22+
sbom_file="$SBOM_DIR/$(echo "$image" | tr '/:' '_').sbom"
23+
24+
# Generate SBOM if missing
25+
if [[ ! -f "$sbom_file" ]]; then
26+
echo "Generating SBOM for $image"
27+
if ! trivy image --quiet --format spdx-json --output "$sbom_file" "$image"; then
28+
echo "Failed to generate SBOM for $image. Skipping."
29+
continue
30+
fi
31+
fi
32+
33+
echo "Scanning SBOM: $sbom_file"
34+
# Scan SBOM and prepend image info to each output line
35+
trivy sbom \
36+
--scanners vuln \
37+
--severity CRITICAL,HIGH \
38+
--ignore-unfixed \
39+
--quiet \
40+
--format template \
41+
--template "@$SBOM_DIR/trivy-custom.tmpl" \
42+
"$sbom_file" | \
43+
awk -v img="$image" '{print "Trivy:\"" img "\"," $0}'
44+
done

0 commit comments

Comments
 (0)