You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用boto3按ECR仓库名称过滤AWS Security Hub检测结果

解决AWS Security Hub筛选特定ECR仓库检测结果的问题

要筛选特定ECR仓库的Security Hub检测结果,你需要使用嵌套字段路径作为过滤器键,因为ECR镜像的仓库信息存储在ResourceDetails.AwsEcrContainerImage结构下,而非直接的顶层字段。以下是正确的实现方式:

正确的过滤器构造与代码示例

import boto3

def lambda_handler(event, context):
    securityhub_client = boto3.client('securityhub')
    
    # 构造过滤器:筛选指定ECR仓库的镜像检测结果
    filters = {
        "ResourceType": [
            {
                "Comparison": "EQUALS",
                "Value": "AwsEcrContainerImage"
            }
        ],
        # 关键:使用完整的嵌套字段路径匹配仓库名
        "ResourceDetails.AwsEcrContainerImage.RepositoryName": [
            {
                "Comparison": "EQUALS",
                "Value": "nginx"  # 替换为你的目标ECR仓库名
            }
        ]
    }
    
    # 处理分页获取所有结果(get_findings默认最多返回100条)
    findings = []
    next_token = None
    while True:
        params = {"Filters": filters}
        if next_token:
            params["NextToken"] = next_token
        
        response = securityhub_client.get_findings(**params)
        findings.extend(response["Findings"])
        
        next_token = response.get("NextToken")
        if not next_token:
            break
    
    # 后续处理导出逻辑
    print(f"获取到{len(findings)}条目标ECR仓库的检测结果")
    return findings

关键说明

  1. 字段路径正确性:Security Hub的过滤器支持通过点分隔的路径引用嵌套字段,ResourceDetails.AwsEcrContainerImage.RepositoryName是对应ECR仓库名的正确字段路径,你之前尝试的ResourceContainerImageName等字段并非Security Hub标准过滤器字段。
  2. 多仓库匹配:如果需要匹配多个ECR仓库,可将Comparison改为IN,Value传入仓库名数组:
    "ResourceDetails.AwsEcrContainerImage.RepositoryName": [
        {
            "Comparison": "IN",
            "Value": ["nginx", "mysql", "redis"]
        }
    ]
    
  3. 分页处理:get_findings接口单次最多返回100条结果,若结果数量较多,必须通过NextToken循环获取所有数据。

内容的提问来源于stack exchange,提问作者Shadow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 00:32:19