跳至內容

如何為 AWS 帳戶根使用者登入警示建立 EventBridge 規則?

4 分的閱讀內容
0

作為帳戶安全措施的一部分,我希望在有人使用 AWS 管理主控台存取我的 AWS 帳戶根使用者時,收到電子郵件通知。

解決方法

若要監控帳戶根使用者的登入活動,請建立一個 Amazon EventBridge 規則,監控 AWS CloudTrail 日誌中的 userIdentity 元素。EventBridge 規則會使用 Amazon Simple Notification Service (Amazon SNS),在根使用者登入 AWS 管理主控台時傳送通知。

若要讓 CloudTrail 將 API 呼叫傳送到 EventBridge,您必須在與 EventBridge 規則相同的 AWS 區域中建立追蹤Configure the trail's management events (將追蹤的管理事件) 設定為 Read and Write (讀取與寫入) 或僅限 Write (寫入)。

若要建立 EventBridge 規則和 SNS 主題,您可以使用 Amazon SNS 和 EventBridge。或使用 AWS CloudFormation。

使用 Amazon SNS 和 EventBridge

建立 Amazon SNS 主題與訂閱

請完成下列步驟:

  1. 建立 Amazon SNS 主題
  2. 將端點訂閱至該主題。
  3. 檢查您的電子郵件收件匣,尋找 AWS 確認電子郵件。
  4. 在電子郵件中,選擇 Confirm subscription (確認訂閱) 以確認 SNS 訂閱請求。接著您將收到「訂閱已確認!」訊息。

建立 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. 記下包含登入事件詳細資訊的 userIdentitysourceIPAddressMFAUsed CloudTrail 記錄

**注意:**若不想再收到通知,請刪除您建立的 CloudFormation 堆疊

相關資訊

如何在有人使用 AWS 帳戶的根存取金鑰時接收通知

監控和通知 AWS 帳戶根使用者活動

AWS::CloudWatch::Alarm

AWS 官方已更新 7 個月前