mcpField
S
securing-container-registry-images

securing-container-registry-images

github.com/mukul975安全35.2k99 次安装8 周前更新
代码仓库

关于 securing-container-registry-images

面向容器安全与 DevOps 团队的镜像仓库安全技能,指导扫描漏洞、生成 SBOM、签名镜像并配置仓库控制。它还涵盖在 CI/CD 流程中设置安全门禁,以支持镜像晋级前的检查。

主要功能

  1. 01用 Trivy 和 Grype 扫描镜像
  2. 02生成 SPDX 或 CycloneDX SBOM
  3. 03用 Cosign 签名与验证镜像
  4. 04配置镜像扫描和标签不可变性
  5. 05在 CI/CD 中设置安全门禁

适用场景

  1. 01建立安全镜像晋级流程
  2. 02审计仓库中的未扫描镜像
  3. 03在发布前检查镜像漏洞
  4. 04为部署镜像生成 SBOM

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/mukul975/anthropic-cybersecurity-skills --skill securing-container-registry-images

安装后在 Claude Code 中输入/securing-container-registry-images确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

LICENSE
references/
api-reference.md
scripts/
agent.py

详细介绍

保护容器镜像仓库安全

适用场景

  • 为容器镜像仓库(ECR、ACR、GCR、Docker Hub)建立安全控制时
  • 构建在镜像推送前强制执行漏洞扫描的 CI/CD 流水线时
  • 实施镜像签名与验证以防范供应链攻击时
  • 审计现有仓库中易受攻击、未扫描或未签名的镜像时
  • 合规性要求为已部署的容器镜像提供软件物料清单(SBOM)时

不适用于运行时容器安全(请使用 Falco 或 Sysdig),不适用于 Kubernetes 准入控制(请在建立仓库控制后使用 OPA Gatekeeper 或 Kyverno),也不适用于主机级漏洞扫描(请使用 Amazon Inspector 或 Qualys)。

前置条件

  • 已安装 Trivy(brew install trivy 或 apt install trivy)
  • 已安装 Grype(curl -sSfL https://raw.githubusercontent.com/anchore/grype/main/install.sh | sh)
  • 已安装 Cosign 用于镜像签名(go install github.com/sigstore/cosign/v2/cmd/cosign@latest)
  • 已安装 Syft 用于生成 SBOM(curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | sh)
  • 具备容器镜像仓库访问权限(ECR、ACR、GCR 或私有仓库)

工作流

步骤 1:使用 Trivy 扫描镜像漏洞

在推送到仓库之前和之后,对容器镜像运行全面的漏洞扫描。

# Scan a local image for vulnerabilities
trivy image --severity HIGH,CRITICAL myapp:latest

# Scan a remote registry image
trivy image --severity HIGH,CRITICAL 123456789012.dkr.ecr.us-east-1.amazonaws.com/myapp:latest

# Scan with JSON output for CI/CD processing
trivy image --format json --output trivy-results.json myapp:latest

# Scan for vulnerabilities AND misconfigurations
trivy image --scanners vuln,misconfig,secret myapp:latest

# Scan a specific image with SBOM output
trivy image --format spdx-json --output sbom.json myapp:latest

# Fail CI/CD if critical vulnerabilities found
trivy image --exit-code 1 --severity CRITICAL myapp:latest
步骤 2:使用 Grype 扫描以获得额外覆盖

将 Grype 用作补充扫描器,以获得更广泛的漏洞数据库覆盖。

# Scan image with Grype
grype myapp:latest

# Scan with severity threshold
grype myapp:latest --fail-on critical

# Output in JSON for processing
grype myapp:latest -o json > grype-results.json

# Scan an SBOM instead of the image directly
syft myapp:latest -o spdx-json > sbom.json
grype sbom:sbom.json

# Scan a directory-based image export
grype dir:/path/to/image-rootfs
步骤 3:生成软件物料清单(SBOM)

为所有镜像创建 SBOM,以维护软件组件和依赖项的清单。

# Generate SBOM with Syft in SPDX format
syft myapp:latest -o spdx-json > sbom-spdx.json

# Generate SBOM in CycloneDX format
syft myapp:latest -o cyclonedx-json > sbom-cyclonedx.json

# Attach SBOM to the image as an OCI artifact
cosign attach sbom --sbom sbom-spdx.json \
  123456789012.dkr.ecr.us-east-1.amazonaws.com/myapp:latest

# Verify SBOM contents
syft myapp:latest -o table | head -50
步骤 4:使用 Cosign 和 Sigstore 对镜像签名

实施镜像签名,以确保供应链中镜像的完整性和真实性。

# Generate a key pair for signing
cosign generate-key-pair

# Sign an image in the registry
cosign sign --key cosign.key \
  123456789012.dkr.ecr.us-east-1.amazonaws.com/myapp:latest

# Sign using keyless signing with Sigstore (OIDC-based)
cosign sign --yes \
  123456789012.dkr.ecr.us-east-1.amazonaws.com/myapp:latest

# Verify image signature
cosign verify --key cosign.pub \
  123456789012.dkr.ecr.us-east-1.amazonaws.com/myapp:latest

# Verify keyless signature
cosign verify \
  --certificate-identity [email protected] \
  --certificate-oidc-issuer https://accounts.google.com \
  123456789012.dkr.ecr.us-east-1.amazonaws.com/myapp:latest

# Add attestation with scan results
cosign attest --predicate trivy-results.json \
  --key cosign.key \
  123456789012.dkr.ecr.us-east-1.amazonaws.com/myapp:latest
步骤 5:配置仓库级安全控制

为 ECR、ACR 和 GCR 设置仓库特定的安全功能。

# AWS ECR: Enable image scanning on push
aws ecr put-image-scanning-configuration \
  --repository-name myapp \
  --image-scanning-configuration scanOnPush=true

# ECR: Set image tag immutability (prevent tag overwrites)
aws ecr put-image-tag-mutability \
  --repository-name myapp \
  --image-tag-mutability IMMUTABLE

# ECR: Set lifecycle policy to clean up untagged images
aws ecr put-lifecycle-policy \
  --repository-name myapp \
  --lifecycle-policy-text '{
    "rules": [{
      "rulePriority": 1,
      "description": "Remove untagged images after 7 days",
      "selection": {"tagStatus": "untagged", "countType": "sinceImagePushed", "countUnit": "days", "countNumber": 7},
      "action": {"type": "expire"}
    }]
  }'

# ECR: Get scan findings for an image
aws ecr describe-image-scan-findings \
  --repository-name myapp \
  --image-id imageTag=latest \
  --query 'imageScanFindings.findingSeverityCounts'

# Azure ACR: Enable Defender for container registries
az security pricing create --name ContainerRegistry --tier standard

# GCR: Enable Container Analysis
gcloud services enable containeranalysis.googleapis.com
gcloud artifacts docker images list-vulnerabilities \
  LOCATION-docker.pkg.dev/PROJECT/REPO/IMAGE@sha256:DIGEST
步骤 6:构建带安全门禁的 CI/CD 流水线

将扫描和签名作为强制门禁集成到 CI/CD 流水线中。

# GitHub Actions: Scan, sign, and push image
name: Container Security Pipeline
on: push

jobs:
  build-scan-sign:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Build image
        run: docker build -t myapp:${{ github.sha }} .

      - name: Trivy vulnerability scan
        uses: aquasecurity/trivy-action@master
        with:
          image-ref: myapp:${{ github.sha }}
          format: json
          output: trivy-results.json
          severity: CRITICAL,HIGH
          exit-code: 1

      - name: Generate SBOM
        run: syft myapp:${{ github.sha }} -o spdx-json > sbom.json

      - name: Push to ECR
        run: |
          aws ecr get-login-password | docker login --username AWS --password-stdin $ECR_REGISTRY
          docker tag myapp:${{ github.sha }} $ECR_REGISTRY/myapp:${{ github.sha }}
          docker push $ECR_REGISTRY/myapp:${{ github.sha }}

      - name: Sign image with Cosign
        run: |
          cosign sign --key env://COSIGN_PRIVATE_KEY \
            $ECR_REGISTRY/myapp:${{ github.sha }}

      - name: Attach SBOM
        run: |
          cosign attach sbom --sbom sbom.json \
            $ECR_REGISTRY/myapp:${{ github.sha }}

关键概念

术语定义
容器镜像扫描自动分析容器镜像层,以识别操作系统软件包和应用依赖项中的已知漏洞
镜像签名使用 Cosign 或 Notation 验证容器镜像真实性和完整性的加密证明
SBOM软件物料清单,容器镜像中软件组件、库和依赖项的完整清单
标签不可变性仓库设置,可防止覆盖现有镜像标签,确保标签始终指向同一镜像摘要
Sigstore开源项目,为软件供应链安全提供无密钥签名、透明度日志和验证工具
镜像证明附加到镜像的加密签名元数据(扫描结果、SBOM、构建来源),可在部署前进行验证

工具与系统

  • Trivy:面向容器镜像、文件系统、git 仓库和 Kubernetes 资源的全面漏洞扫描器
  • Grype:Anchore 的漏洞扫描器,对容器镜像和 SBOM 具有广泛的漏洞数据库覆盖
  • Cosign:Sigstore 工具,可通过基于密钥或无密钥工作流对容器镜像进行签名、验证和证明
  • Syft:SBOM 生成工具,支持容器镜像和文件系统的 SPDX 和 CycloneDX 格式
  • AWS ECR:内置扫描、标签不可变性和生命周期策略的容器镜像仓库

常见场景

场景:实现安全的镜像晋升流水线

背景:一个开发团队在没有安全管控的情况下将镜像推送到开发镜像仓库。安全团队需要实现一条晋升流水线,对镜像进行扫描、签名,并仅将通过审批的镜像晋升到生产镜像仓库。

方案:

  1. 为开发镜像仓库配置推送时触发的 ECR 扫描
  2. 添加 Trivy 扫描作为 CI/CD 门禁,阻止包含 CRITICAL 漏洞的镜像
  3. 使用 Syft 生成 SBOM,并与镜像扫描结果一起存储
  4. 扫描通过后,使用 Cosign 对已审批的镜像进行签名
  5. 将生产镜像仓库配置为所有推送都要求镜像签名
  6. 在生产 Kubernetes 中设置 Kyverno 或 OPA Gatekeeper,在创建 Pod 之前验证签名
  7. 实现生命周期策略,清理两个镜像仓库中未打标签的镜像和旧镜像

易踩的坑:漏洞数据库在不断更新。今天通过扫描的镜像,明天可能会被发现新的 CRITICAL 漏洞。要对已部署的镜像实施持续扫描,而不只是在构建时扫描。镜像签名密钥必须安全存储在 KMS 或 Vault 中,而不是放在 CI/CD 环境变量里。

输出格式

Container Registry Security Report
=====================================
Registry: 123456789012.dkr.ecr.us-east-1.amazonaws.com
Repositories: 24
Report Date: 2026-02-23

IMAGE INVENTORY:
  Total images: 342
  Images scanned: 298 (87%)
  Images signed: 156 (46%)
  Images with SBOM: 134 (39%)

VULNERABILITY SUMMARY:
  Critical vulnerabilities:    23 (across 8 images)
  High vulnerabilities:       145 (across 34 images)
  Medium vulnerabilities:     456 (across 67 images)
  Images with no vulns:       89

CRITICAL IMAGES REQUIRING REMEDIATION:
  myapp:1.2.3           - 5 CRITICAL (CVE-2026-xxxx in openssl)
  api-gateway:2.0.1     - 3 CRITICAL (CVE-2026-yyyy in log4j)
  worker:latest         - 4 CRITICAL (CVE-2026-zzzz in glibc)

REGISTRY CONFIGURATION:
  Scan on push enabled:     18 / 24 repositories
  Tag immutability:         12 / 24 repositories
  Lifecycle policies:       20 / 24 repositories
  Image signing enforced:    8 / 24 repositories

评论

支持 Markdown