跳至内容

如何将 EventBridge 与 Amazon SNS 主题配合使用,以便为 Security Hub 调查发现设置自定义电子邮件通知?

3 分钟阅读
0

我想将 Amazon EventBridge 与 Amazon Simple Notification Service (Amazon SNS) 主题配合使用,以便为 AWS Security Hub 设置自定义电子邮件通知。

简短描述

以下解决方法向您展示了如何配置 Amazon EventBridge 与 Amazon SNS 以接收 Security Hub 通知。根据 EventBridge 规则,当事件发生时,Amazon SNS 会向订阅该主题的电子邮件地址发送通知。AWS Lambda 函数会创建格式经过优化的自定义警报消息。

解决方法

创建 SNS 主题和订阅

完成以下步骤:

  1. 打开 Amazon SNS 控制台
  2. 在导航窗格中,选择 Topics(主题)。
  3. 选择 Create topic(创建主题)。
  4. Details(详细信息)部分中,对于 Type(类型),选择 Standard(标准)。
  5. 对于 Name(名称),输入您的主题的名称。
  6. 选择 Create topic(创建主题)。
  7. 从创建的主题中,选择 Create subscription(创建订阅)。
  8. 对于 Topic ARN(主题 ARN),如果未自动填充,请选择您创建的主题的 Amazon 资源名称 (ARN)。
  9. 对于 Protocol(协议),选择 Email(电子邮件)。
  10. 对于 Endpoint(端点),输入要用来接收 SNS 通知的电子邮件地址。
  11. 选择 Create subscription(创建订阅)。
    **重要事项:**您必须在发送给订阅用户的确认电子邮件中确认订阅,才能使订阅状态从 PendingConfirmation 切换为 Confirmed
  12. (可选)您还可以创建经过身份验证的订阅,防止对您的主题执行取消订阅操作。

创建 Lambda 函数

完成以下步骤:

  1. 打开 Lambda 控制台
  2. 在导航窗格中,选择 Functions(函数)。
  3. 选择 Create function(创建函数)。
  4. 选择 Author from scratch(从头开始创作)。
  5. 对于 Function name(函数名称),输入您的函数名称。
  6. 选择 Runtime(运行时),然后选择 Python 3.14
  7. 对于 Architecture(架构),选择 x86_64
  8. 展开 Change default execution role(更改默认执行角色)。
  9. 对于 Execution role(执行角色),选择 Create a new role from AWS policy templates(从 AWS 策略模板创建新角色)。
  10. 对于 Role name(角色名称),输入角色的名称。
  11. 对于 Policy template(策略模板),选择 Amazon SNS publish policy(Amazon SNS 发布策略)。
  12. 选择 Create function(创建函数)。
  13. Code source(代码源)部分输入以下代码:
import json
import boto3

sns = boto3.client('sns')

def lambda_handler(event, context):

    #Extract details from JSON event
    detailType= event["detail-type"]
    region = event["region"]
    accountId = event["account"]

    #Security Hub Insight Results
    if (detailType == "Security Hub Insight Results"):

        action = event["detail"]["actionDescription"]

        message = "Alert: %s in %s for account: %s\n Action description: %s" % (detailType, region,accountId,action)

    elif  ("Security Hub Findings" in detailType):

        finding = event["detail"]["findings"][0]
        findingTime = finding["FirstObservedAt"]
        findingType = finding["Types"][0]
        findingDescription = finding["Description"]
        remediation = finding["Remediation"]["Recommendation"]["Text"]

        #Security Hub Findings - Custom finding
        if(detailType == "Security Hub Findings - Custom"):
            complianceStatus = finding["Compliance"]["Status"]
            severity = finding["Severity"]["Label"]
            remediationUrl = finding["Remediation"]["Recommendation"]["Url"]

            message = "Alert: %s in %s for account: %s\n\nFinding regarding: [%s] %s\n Severity: %s\nDescription: %s\nFirst observed at: %s\n%s: %s" % (detailType, region, accountId, complianceStatus, findingType,
            severity, findingDescription, findingTime, remediation, remediationUrl)

        #Security Hub Findings - Imported finding
        else:
            message = "Alert: %s in %s for account: %s\n\nFinding regarding: %s\nFirst observed at: %s\nRemediation recommendation: %s" % (detailType, region, accountId, findingDescription,findingTime, remediation)

    #AWS API Call via CloudTrail finding
    elif (detailType == "AWS API Call via CloudTrail"):

        time = event["detail"]["eventTime"]
        eventName = event["detail"]["eventName"]
        requestParameters = event["detail"]["requestParameters"]

        message = "Alert: %s in %s for account: %s at time: %s\n\n Event: %s \n Request parameters: %s" % (detailType, region, accountId, time, eventName, requestParameters)


    #If the event doesn't match any of the above, return the event    
    else:
        message = str(event)

    response = sns.publish(
            TopicArn = "arn:aws:sns:eu-west-1:111122233333:your-arn",
            Message = message
            )

    return {
      'statusCode': 200,
      'body': json.dumps('Success!')
}

**注意:**将 arn:aws:sns:eu-west-1:111122233333:your-arn 替换为您的主题 ARN。 选择 Deploy(部署)。

创建并配置 EventBridge 规则

完成以下步骤:

  1. 打开 EventBridge 控制台
  2. 在导航窗格中,选择 Rules(规则),然后选择 Create rule(创建规则)。
  3. 输入规则的 Name(名称),然后选择 Next(下一步)。
  4. 对于 Creation method(创建方法),选择 Use pattern form(使用模式表单)。
  5. 对于 Event source(事件源),选择 AWS services(AWS 服务)。
  6. 对于 AWS service(AWS 服务),选择 Security Hub
  7. 对于 Event type(事件类型),选择 All Events(所有事件),然后选择 Next(下一步)。
    **注意:**您还可以为特定事件配置警报,而不是为所有事件配置警报。
  8. 对于 Target types(目标类型),选择 AWS service(AWS 服务)。
  9. 对于 Select a target(选择目标),选择 Lambda function(Lambda 函数)。
  10. 对于 Function(函数),选择您之前创建的函数,然后选择 Next(下一步)。
  11. Configure tags(配置标签)页面上,选择 Next(下一步)。
  12. 选择 Create rule(创建规则)。

接收自定义通知

当配置的事件发生时,您会收到一封来自 no-reply@sns.amazonaws.com 的自定义通知电子邮件。

默认的 Security Hub 事件将被重新格式化为更易读的自定义格式。

Security Hub 见解结果消息示例

默认:

{"version": "0", "id": "ac844908-d14e-05b1-4b7b-836d85110e26", "detail-type": "Security Hub Insight Results", "source": "aws.securityhub", "account": "123456789012", "time": "2019-04-11T21:31:57Z", "region": "us-east-1", "resources": ["arn:aws:securityhub:us-east-1:123456789012:action/custom/slackMessaging"], "detail": {"actionName": "SendToSlack", "actionDescription": "Send Findings to Slack", "insightName": "5.AWS users with the most suspicious activity", "insightArn": "arn:aws:securityhub:::insight/securityhub/default/9", "resultType": "ResourceAwsIamAccessKeyUserName", "insightResults": [{"Admin": 7}, {"DenySlr_UI_User": 1}]}}

自定义:

警报: 以下账户在 us-east-1 中的 Security Hub 见解结果: 123456789012

操作描述: 将调查发现发送到 Slack

**注意:**您可以根据自己的使用案例为每种安全调查发现类型编辑消息。

相关信息

如何为 GuardDuty 配置 EventBridge 规则,以便针对特定服务调查发现类型发送自定义 SNS 通知?

教程: 使用输入转换器自定义 EventBridge 向事件目标传递的内容

为什么我的 EventBridge 规则未调用我的 Lambda 函数?