mcpField
C
container-scanning

container-scanning

github.com/bagelhole安全1.2k365 次安装23 周前更新
代码仓库

关于 container-scanning

container-scanning 是一项指导容器镜像安全扫描的技能,面向负责构建、部署和保护容器的团队。内容涵盖多种扫描工具、CI 集成、镜像仓库扫描及准入控制。

主要功能

  1. 01扫描镜像漏洞与配置问题
  2. 02比较 Trivy、Grype 等工具
  3. 03扫描文件系统与基础设施配置
  4. 04集成 CI/CD 与镜像仓库
  5. 05配置策略和准入控制

适用场景

  1. 01在构建流程中检查镜像
  2. 02为镜像仓库启用漏洞扫描
  3. 03在 CI/CD 中设置安全门禁
  4. 04部署前制定镜像准入策略

安装

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

$npx skills add https://github.com/bagelhole/devops-security-agent-skills --skill container-scanning

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

详细介绍

容器扫描

扫描容器镜像,发现漏洞和安全配置错误。

何时使用此技能

在以下情况下使用此技能:

  • 构建容器镜像
  • 实施容器安全门禁
  • 扫描镜像仓库中的镜像
  • 满足合规要求
  • 加固容器部署

前提条件

  • 容器运行时(Docker、Podman)
  • 待扫描的容器镜像
  • 已安装扫描工具

工具对比

工具许可证速度特性
TrivyOSS快全面,IaC
GrypeOSS快准确,SBOM
ClairOSS中等镜像仓库集成
Snyk Container商业版快修复建议
Docker Scout商业版快GitHub 集成

Trivy

安装
# Linux
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin

# macOS
brew install trivy

# Docker
docker pull aquasec/trivy
镜像扫描
# Scan local image
trivy image myapp:latest

# Scan remote image
trivy image nginx:1.25

# JSON output
trivy image --format json -o results.json myapp:latest

# Filter by severity
trivy image --severity HIGH,CRITICAL myapp:latest

# Ignore unfixed vulnerabilities
trivy image --ignore-unfixed myapp:latest

# Exit code on vulnerability
trivy image --exit-code 1 --severity CRITICAL myapp:latest
文件系统扫描
# Scan project directory
trivy fs /path/to/project

# Scan Dockerfile
trivy config Dockerfile

# Scan Kubernetes manifests
trivy config k8s/
配置
# trivy.yaml
timeout: 10m
severity:
  - HIGH
  - CRITICAL
ignore-unfixed: true
exit-code: 1

vulnerability:
  type:
    - os
    - library

scan:
  file-patterns:
    - "Dockerfile"
    - "*.yaml"
CI 集成
# GitHub Actions
name: Container Security

on:
  push:
    branches: [main]
  pull_request:

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

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

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

      - name: Upload results
        uses: github/codeql-action/upload-sarif@v3
        with:
          sarif_file: 'trivy-results.sarif'

Grype

安装
# Linux/macOS
curl -sSfL https://raw.githubusercontent.com/anchore/grype/main/install.sh | sh -s -- -b /usr/local/bin

# Homebrew
brew install grype
使用方法
# Scan image
grype myapp:latest

# Scan from SBOM
grype sbom:./sbom.json

# JSON output
grype myapp:latest -o json > results.json

# Filter severity
grype myapp:latest --fail-on high

# Scan directory
grype dir:/path/to/project
配置
# .grype.yaml
check-for-app-update: false
fail-on-severity: high
output: "json"
scope: "Squashed"

ignore:
  - vulnerability: CVE-2023-12345
    reason: "False positive"
    expires: "2024-12-31"

Docker Scout

使用方法
# Enable Docker Scout
docker scout quickview myapp:latest

# Full CVE report
docker scout cves myapp:latest

# Compare images
docker scout compare myapp:v1 myapp:v2

# Recommendations
docker scout recommendations myapp:latest
CI 集成
- name: Docker Scout
  uses: docker/scout-action@v1
  with:
    command: cves
    image: ${{ env.IMAGE_NAME }}
    sarif-file: scout-results.sarif
    summary: true

镜像仓库集成

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

# Get scan findings
aws ecr describe-image-scan-findings \
  --repository-name myapp \
  --image-id imageTag=latest

# Start manual scan
aws ecr start-image-scan \
  --repository-name myapp \
  --image-id imageTag=latest
Azure ACR
# Enable Defender for Containers
az security pricing create \
  --name Containers \
  --tier Standard

# View scan results in Azure Portal or:
az acr repository show \
  --name myregistry \
  --image myapp:latest
Google Artifact Registry
# Enable vulnerability scanning
gcloud artifacts repositories update myrepo \
  --location=us-central1 \
  --enable-vulnerability-scanning

# View vulnerabilities
gcloud artifacts docker images describe \
  us-central1-docker.pkg.dev/project/myrepo/myapp:latest \
  --show-package-vulnerability

准入控制器

OPA Gatekeeper
apiVersion: templates.gatekeeper.sh/v1beta1
kind: ConstraintTemplate
metadata:
  name: k8sallowedrepos
spec:
  crd:
    spec:
      names:
        kind: K8sAllowedRepos
      validation:
        openAPIV3Schema:
          properties:
            repos:
              type: array
              items:
                type: string
  targets:
    - target: admission.k8s.gatekeeper.sh
      rego: |
        package k8sallowedrepos
        
        violation[{"msg": msg}] {
          container := input.review.object.spec.containers[_]
          satisfied := [good | repo = input.parameters.repos[_]; good = startswith(container.image, repo)]
          not any(satisfied)
          msg := sprintf("container <%v> has an invalid image repo <%v>", [container.name, container.image])
        }
Kyverno
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: require-image-scan
spec:
  validationFailureAction: enforce
  rules:
    - name: check-vulnerabilities
      match:
        resources:
          kinds:
            - Pod
      verifyImages:
        - image: "*"
          attestations:
            - predicateType: cosign.sigstore.dev/attestation/vuln/v1
              conditions:
                - all:
                    - key: "{{ scanner.result.summary.criticalCount }}"
                      operator: Equals
                      value: "0"

扫描策略

策略定义
# scan-policy.yaml
policies:
  - name: critical-vulnerabilities
    description: Block images with critical CVEs
    severity: CRITICAL
    action: block
    
  - name: high-vulnerabilities
    description: Warn on high severity CVEs
    severity: HIGH
    action: warn
    max_count: 5
    
  - name: age-policy
    description: Block images older than 30 days
    max_age_days: 30
    action: block
    
  - name: base-image
    description: Only allow approved base images
    allowed_bases:
      - alpine:3.18
      - ubuntu:22.04
      - python:3.11-slim

常见问题

问题:误报

问题:扫描器报告了无法利用的漏洞 解决方案:使用忽略文件,并结合上下文进行验证

问题:扫描速度慢

问题:扫描耗时过长 解决方案:使用缓存、增量扫描并优化镜像层

问题:漏洞无法修复

问题:没有可用于 CVE 的补丁 解决方案:更新基础镜像,并实施补偿性控制措施

最佳实践

  • 在 CI/CD 流水线中扫描
  • 使用精简的基础镜像(Alpine、distroless)
  • 定期更新基础镜像
  • 实施准入控制
  • 跟踪漏洞变化
  • 设置严重性阈值
  • 记录已接受的风险
  • 使用多阶段构建

相关技能

评论

支持 Markdown