跳至内容

如何创建针对 AWS 账户根用户登录警报的 EventBridge 规则?

3 分钟阅读
0

作为账户安全的一部分,我希望在有人通过 AWS 管理控制台访问我的 AWS 账户根用户时收到电子邮件通知。

解决方法

要监控账户根用户的登录活动,请创建一条 Amazon EventBridge 规则来监控 AWS CloudTrail 日志中的 userIdentity 元素。当根用户登录 AWS 管理控制台时,EventBridge 规则会使用 Amazon Simple Notification Service (Amazon SNS) 发送通知。

要让 CloudTrail 向 EventBridge 发送 API 调用,必须在与 EventBridge 规则相同的 AWS 区域中创建跟踪将跟踪的管理事件配置读取和写入或仅写入

要创建 EventBridge 规则和 SNS 主题,您可以使用 Amazon SNS 和 EventBridge。或者,使用 AWS CloudFormation。

使用 Amazon SNS 和 EventBridge

创建 Amazon SNS 主题和订阅

完成以下步骤:

  1. 创建 Amazon SNS 主题
  2. 为该主题订阅端点
  3. 检查您的电子邮件收件箱中是否有 AWS 确认电子邮件。
  4. 在电子邮件中,选择 Confirm subscription(确认订阅),以确认 SNS 订阅请求。然后,您将收到“Subscription confirmed!”消息。

创建 EventBridge 规则

完成以下步骤:

  1. 在美国东部(弗吉尼亚州北部)区域打开 EventBridge 控制台

  2. 在导航窗格中,选择 Rules(规则),然后选择 Create rule(创建规则)。

  3. 对于 Name(名称)和 Description(描述),输入规则的名称和描述。

  4. 对于 Rule type(规则类型),选择 Rule with an event pattern(具有事件模式的规则),然后选择 Next(下一步)。

  5. 对于 Event source(事件来源),选择 AWS events or EventBridge partner events(AWS 事件或 EventBridge 合作伙伴事件)。

  6. Event pattern(事件模式)下,选择 Custom pattern (JSON editor)(自定义模式(JSON 编辑器))。

  7. Event pattern(事件模式)JSON 编辑器中,输入以下根用户登录模式:

    {
    
    "detail-type": ["AWS Console Sign In via CloudTrail"],
    
    "detail": {
    
    "userIdentity": {
    
    "type": ["Root"]
    
    }
    
    }
    
    }
  8. 选择 Next(下一步)。

  9. 为您的目标配置以下设置:
    对于 Target types(目标类型),选择 AWS service(AWS 服务)。
    对于 Select a target(选择目标),选择 SNS topic(SNS 主题)。
    对于 Topic(主题),选择您创建的主题。

  10. 选择 Next(下一步)。

  11. (可选)向规则添加标签

  12. 选择 Next(下一步)。

  13. 查看规则的详细信息,然后选择 Create rule(创建规则)。

使用 CloudFormation

创建 CloudFormation 模板

要创建 EventBridge 规则和 SNS 主题,请在文本编辑器中输入以下 YAML 模板,然后保存该文件:

# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

AWSTemplateFormatVersion: '2010-09-09'
Description: ROOT-AWS-Console-Sign-In-via-CloudTrail
Metadata:
  AWS::CloudFormation::Interface:
    ParameterGroups:
    - Label:
        default: Amazon SNS parameters
      Parameters:
      - Email Address
Parameters:
  EmailAddress:
    Type: String
    ConstraintDescription: Email address required.
    Description: Enter an email address you want to subscribe to the Amazon SNS topic
      that will send notifications if your account's AWS root user logs in.
Resources:
  RootActivitySNSTopic:
    Type: AWS::SNS::Topic
    Properties:
      DisplayName: ROOT-AWS-Console-Sign-In-via-CloudTrail
      Subscription:
      - Endpoint:
          Ref: EmailAddress
        Protocol: email
      TopicName: ROOT-AWS-Console-Sign-In-via-CloudTrail
  EventsRule:
    Type: AWS::Events::Rule
    Properties:
      Description: Events rule for monitoring root AWS Console Sign In activity
      EventPattern:
        detail-type:
        - AWS Console Sign In via CloudTrail
        detail:
          userIdentity:
            type:
            - Root
      Name:
        Fn::Sub: "${AWS::StackName}-RootActivityRule"
      State: ENABLED
      Targets:
      - Arn:
          Ref: RootActivitySNSTopic
        Id: RootActivitySNSTopic
    DependsOn:
    - RootActivitySNSTopic
  RootPolicyDocument:
    Type: AWS::SNS::TopicPolicy
    Properties:
      PolicyDocument:
        Id: RootPolicyDocument
        Version: '2012-10-17'
        Statement:
        - Sid: RootPolicyDocument
          Effect: Allow
          Principal:
            Service: events.amazonaws.com
          Action: sns:Publish
          Resource:
          - Ref: RootActivitySNSTopic
      Topics:
      - Ref: RootActivitySNSTopic
Outputs:
  EventsRule:
    Value:
      Ref: EventsRule
    Export:
      Name:
        Fn::Sub: "${AWS::StackName}-RootAPIMonitorEventsRule"
    Description: Event Rule ID.

创建 CloudFormation 堆栈

使用 CloudFormation 控制台创建 CloudFormation 堆栈。在 Create stack(创建堆栈)页面上,选择 Upload a template file(上传模板文件),以上传您创建的模板。在 Configure stack options(配置堆栈选项)页面的 Notifications options(通知选项)下,输入您希望 AWS 发送通知的电子邮件地址。

测试您的 SNS 设置

完成以下步骤:

  1. 退出 AWS 管理控制台,然后以账户根用户身份重新登录 AWS 管理控制台
  2. 检查您的电子邮件收件箱中是否有 AWS 通知。
  3. 记下包含登录事件详细信息的 CloudTrail 记录userIdentitysourceIPAddressMFAUsed

**注意:**要不再接收通知,请删除您创建的 CloudFormation 堆栈

相关信息

How to receive notifications when your AWS account's root access keys are used(当您的 AWS 账户的根访问密钥被使用时,如何接收通知?)

Monitor and notify on AWS account root user activity

AWS::CloudWatch::Alarm

AWS 官方已更新 3 个月前