跳至內容

如何使用 CloudTrail 追蹤帳戶中的安全群組和資源變更?

2 分的閱讀內容
0

我想使用 AWS CloudTrail 來追蹤 AWS 帳戶中的安全群組和資源變更。

解決方法

您可以使用 AWS CloudTrailAmazon AthenaAWS Config 檢視和監控 AWS 帳戶中的安全群組事件記錄。

先決條件:

使用 CloudTrail 事件歷史記錄查看安全群組活動

**注意:**您可以使用 CloudTrail 搜尋過去 90 天的事件歷史記錄。

  1. 開啟 CloudTrail console (CloudTrail 主控台)。
  2. 選擇 Event history (事件歷史記錄)。
  3. Filter (篩選器) 的下拉式清單中,選擇 Resource name (資源名稱)。
  4. Enter resource name (輸入資源名稱) 文字方塊中,輸入您的資源名稱。例如,sg-123456789
  5. Time range (時間範圍) 中,請輸入所需的時間範圍。然後,選擇 Apply (套用)。
  6. 在結果清單中選擇一個事件。

如需詳細資訊,請參閱使用主控台檢視最近的管理事件

CloudTrail 事件的範例:

**注意:**在此範例中,傳入規則允許來自 192.168.0.0/32 的 TCP 連接埠 998。

{
    "eventVersion": "1.05",
    "userIdentity": {
        "type": "AssumedRole",
        "principalId": "123456789:Bob",
        "arn": "arn:aws:sts::123456789:assumed-role/123456789/Bob",
        "accountId": "123456789",
        "accessKeyId": "123456789",
        "sessionContext": {
            "attributes": {
                "mfaAuthenticated": "false",
                "creationDate": "2019-08-05T07:15:25Z"
            },
            "sessionIssuer": {
                "type": "Role",
                "principalId": "123456789",
                "arn": "arn:aws:iam::123456789:role/123456789",
                "accountId": "123456789",
                "userName": "Bob"
            }
        }
    },
    "eventTime": "2019-08-05T07:16:31Z",
    "eventSource": "ec2.amazonaws.com",
    "eventName": "AuthorizeSecurityGroupIngress",
    "awsRegion": "us-east-1",
    "sourceIPAddress": "111.111.111.111",
    "userAgent": "console.ec2.amazonaws.com",
    "requestParameters": {
        "groupId": "sg-123456789",
        "ipPermissions": {
            "items": [
                {
                    "ipProtocol": "tcp",
                    "fromPort": 998,
                    "toPort": 998,
                    "groups": {},
                    "ipRanges": {
                        "items": [
                            {
                                "cidrIp": "192.168.0.0/32"
                            }
                        ]
                    },
                    "ipv6Ranges": {},
                    "prefixListIds": {}
                }
            ]
        }
    },
    "responseElements": {
        "requestId": "65ada3c8-d72f-4366-a583-9a9586811111",
        "_return": true
    },
    "requestID": "65ada3c8-d72f-4366-a583-9a9586811111",
    "eventID": "6c604d53-d9c3-492e-a26a-a48ac3f711111",
    "eventType": "AwsApiCall",
    "recipientAccountId": "123456789"
}

使用 Athena 查詢檢閱安全群組活動

  1. 開啟 Athena console (Athena 主控台)。
  2. 選擇 Query Editor (查詢編輯器)。
  3. 在 Athena 查詢編輯器中,根據您的使用案例輸入查詢。然後,選擇 Run query (執行查詢)。

如需詳細資訊,請參閱了解 CloudTrail 日誌和 Athena 資料表

傳回安全群組建立和刪除事件的查詢範例:

**重要:**將 example table name (範例表名稱) 替換為您的資料表名稱。

SELECT *
FROM example table name
WHERE (eventname = 'CreateSecurityGroup' or eventname = 'DeleteSecurityGroup')
and eventtime > '2019-02-15T00:00:00Z'
order by eventtime asc

針對特定安全群組所做變更,傳回所有 CloudTrail 事件的範例:

SELECT *
FROM example table name
WHERE (eventname like '%SecurityGroup%' and requestparameters like '%sg-123456789%')
and eventtime > '2019-02-15T00:00:00Z'
order by eventtime asc;

使用 AWS Config 組態歷史記錄檢閱安全群組活動

  1. 開啟 CloudTrail console (CloudTrail 主控台)。
  2. 選擇 Event history (事件歷史記錄)。
  3. Filter (篩選器) 的下拉式清單中,選擇 Event name (事件名稱)。
  4. Enter event name (輸入事件名稱) 文字方塊中,輸入事件類型。例如,CreateSecurityGroup。然後,選擇 Apply (套用)。
  5. 在結果清單中選擇一個事件。
  6. Resources Referenced (參考的資源) 窗格中,選擇 View AWS Config resource timeline (查看 AWS Config 資源時間表) 以查看組態時間表。

如需詳細資訊,請參閱檢視使用 AWS Config 參考的資源

AWS 官方已更新 6 個月前