【DevOps】docker 通过 sbom 获取组件版本和漏洞编号

最近做 SCA 测试,需要获取镜像的组件和漏洞,使用 docker scout 工具,通过 sbom 获取组件名称、版本和漏洞编号。

docker scout

获取组件信息

bash 复制代码
docker scout sbom --format spdx --output 2025.05.25.sbom.spdx.json nginx:stable-perl
    v SBOM of image already cached, 244 packages indexed
    v Report written to 2025.05.25.sbom.spdx.json

使用 SCA 工具处理组件信息。

获取 CVE 编号

bash 复制代码
docker scout cves --format sbom nginx:stable-perl > sbom.cve.json

按行输出 CVE 编号

python 复制代码
import json
import re
from charset_normalizer import detect

def extract_cve_ids(file_path):
    """
    Extract all CVE IDs from a JSON file.
    """
    cve_pattern = re.compile(r'CVE-\d{4}-\d{4,}')  # Regex pattern for CVE IDs
    cve_ids = []

    # Detect the file encoding
    with open(file_path, 'rb') as file:
        raw_data = file.read()
        detected = detect(raw_data)
        encoding = detected['encoding']

    # Read the JSON file with the detected encoding
    with open(file_path, 'r', encoding=encoding) as file:
        data = json.load(file)

    # Recursively search for CVE IDs in the JSON structure
    def search_cve(obj):
        if isinstance(obj, dict):
            for key, value in obj.items():
                search_cve(value)
        elif isinstance(obj, list):
            for item in obj:
                search_cve(item)
        elif isinstance(obj, str):
            matches = cve_pattern.findall(obj)
            cve_ids.extend(matches)

    search_cve(data)
    return cve_ids

# File path to 1.json
file_path = "sbom.cve.json"

# Extract CVE IDs and print them
cve_ids = extract_cve_ids(file_path)
print("\n".join(cve_ids))
相关推荐
mortimer2 小时前
一次搞懂 rsync:从入门到解决真实世界中的权限难题
linux·运维·centos
wb1893 小时前
服务器的Mysql 集群技术
linux·运维·服务器·数据库·笔记·mysql·云计算
天上掉下来个程小白4 小时前
Docker-07.Docker基础-数据卷挂载
运维·docker·微服务·容器
迷失蒲公英4 小时前
Docker容器中文PDF生成解决方案
docker·容器·pdf
whabc1004 小时前
ssh连接VirtualBox中的Ubuntu24.04(win11、putty、NAT 模式)
运维·ssh
热爱生活的五柒4 小时前
服务器突然之间特别卡,什么原因?
运维·服务器
zly35005 小时前
Linux(centos)安全狗
linux·运维·服务器
星辰云-5 小时前
Nginx笔记
运维·笔记·nginx
失因6 小时前
Linux 权限管理与 ACL 访问控制
linux·运维·服务器·数据库·centos
云攀登者-望正茂6 小时前
Azure DevOps — Kubernetes 上的自托管代理 — 第 5 部分
kubernetes·azure·devops