如何使用 CloudWatch Logs Insights 分析 CloudTrail 日誌?
7 分的閱讀內容
0
我想使用 Amazon CloudWatch Logs Insights 分析 Amazon CloudTrail 日誌。該如何進行?
簡短描述
您可以將 CloudTrail 設定為記錄到 CloudWatch Logs。這樣做允許您使用 CloudWatch Logs Insights 分析 CloudTrail 日誌以監控特定帳戶活動。
為了示範 Logs Insights 分析 CloudTrail 日誌的能力,以下解決方案包含範例查詢。這些查詢涵蓋了最常見的使用案例:
- 隔離特定的日誌欄位。
- 根據不同的條件進行篩選。
- 匯總事件。
- 建置時間序列。
解決方案
以下查詢探索 Amazon Simple Storage Service (Amazon S3) 儲存貯體和物件活動。依預設,CloudTrail 不擷取 S3 資料事件。您可以在 CloudTrail 中開啟事件記錄。如需詳細資訊,請參閱為 S3 儲存貯體和物件啟用 CloudTrail 事件記錄。
您可以在這些範例查詢的基礎上建立更多且更複雜的 Logs Insights 查詢,以滿足您使用案例的需要。您還可以將查詢與您的 CloudWatch 儀表板 整合,以將查詢及相關指標視覺化為圖表和圖形。
查詢 1:最新事件
目標
使用預設的 @timestamp 和 @message 欄位擷取最新的 CloudTrail 日誌事件。
查詢
#Retrieve the most recent CloudTrail events fields @timestamp, @message | sort @timestamp desc | limit 2
結果
@timestamp | @message |
2022-02-18 17:52:31.118 | {"eventVersion":"1.08","userIdentity":{"type":"AssumedRole","principalId":"AROAWZKRRJU47ARZN7ECC:620d7d78144334d6933c27195cae2a98", "arn":"arn:aws:sts::123456789012:assumed- role/Amazon_EventBridge_Invoke_Run_Command_371790151/620d7d78144334d6933c27195cae2a98","accountId":"123456789012", "accessKeyId":"ASIAWZKRRJU4Y45M4SC6","sessionContext":{"sessionIssuer": {"type":"Role","principalId":"AROAWZKRRJU47ARZN7ECC","arn":"arn:aws:iam::123456789012:role/service- role/Amazon_EventBridge_Invoke_Run_Command_371790151","accountId":"123456789012","userName": "Amazon_EventBridge_Invoke_Run_Command_371790151" (輸出截斷) |
2022-02-18 17:51:52.137 | {"eventVersion":"1.08","userIdentity":{"type":"AssumedRole","principalId":"AROAWZKRRJU43YP4FHR2N:StateManagerService","arn":"arn:aws:sts::123456789012:assumed-role/AWSServiceRoleForAmazonSSM/StateManagerService","accountId":"123456789012","sessionContext":{"sessionIssuer":{"type":"Role","principalId":"AROAWZKRRJU43YP4FHR2N","arn":"arn:aws:iam::123456789012:role/aws-service-role/ssm.amazonaws.com/AWSServiceRoleForAmazonSSM","accountId":"123456789012","userName":"AWSServiceRoleForAmazonSSM"}, "webIdFederationData":{},"attributes":{"creationDate":"2022-02-18T17:50:06Z","mfaAuthenticated":"false"}},"invokedBy":"ssm.amazonaws.com"},"eventTime":"2022-02-18T17:50:06Z","eventSource":"ec2.amazonaws.com","eventName":"DescribeInstances","awsRegion":"eu-west-1","sourceIPAddress":"ssm.amazonaws.com","userAgent":"ssm.amazonaws.com","requestParameters":{"maxResults":50,"instancesSet": (輸出截斷) |
查詢 2:拆分單個欄位
目標
- 隔離 @message 中的各個欄位。
- 在 CloudTrail 事件 中顯示選取欄位。
查詢
#Breakout Individual Fields fields @timestamp, awsRegion, eventCategory, eventSource, eventName, eventType, sourceIPAddress, userIdentity.type | sort @timestamp desc | limit 2
結果
@timestamp | awsRegion | eventCategory | eventSource | eventName | eventType | sourceIPAddress | userIdentity.type |
---|---|---|---|---|---|---|---|
2022-02-18 18:00:09.647 | ca-central-1 | 管理 | sts.amazonaws.com | AssumeRole | AwsApiCall | cloudtrail.amazonaws.com | AWSService |
2022-02-18 18:00:09.647 | ca-central-1 | 管理 | sts.amazonaws.com | AssumeRole | AwsApiCall | cloudtrail.amazonaws.com | AWSService |
查詢 3:依 Amazon Elastic Compute Cloud (Amazon EC2) RunInstances 進行篩選
目標
- 擷取 Cloudtrail 事件中的特定欄位。
- 使用更有意義的標籤重新命名欄位。
- 根據 API 呼叫篩選此帳戶中啟動的最新 EC2 執行個體。
查詢
#EC2: Recently Launched Instances fields eventTime, eventName as API, responseElements.instancesSet.items.0.instanceId as InstanceID, userIdentity.sessionContext.sessionIssuer.type as IssuerType, userIdentity.type as IdentityType, userIdentity.sessionContext.sessionIssuer.userName as userName | filter eventName = 'RunInstances' | sort eventTime desc | limit 2
結果
eventTime | API | InstanceID | IssuerType | IdentityType | userName |
---|---|---|---|---|---|
2022-02-18T17:36:38Z | RunInstances | i-0325b4d6ae4e93c75 | 角色 | AssumedRole | AWSServiceRoleForAutoScaling |
2022-02-18T13:45:18Z | RunInstances | i-04d17a8425b7cb59a | 角色 | AssumedRole | AWSServiceRoleForAutoScaling |
查詢 4:依主控台登入進行篩選:最近
目標
- 擷取 Cloudtrail 事件中的特定欄位。
- 使用更有意義的標籤重新命名欄位。
- 根據 API 呼叫篩選透過 AWS 主控台的最新登入。
查詢
#Console Login: Most Recent API Calls fields eventTime, eventName, responseElements.ConsoleLogin as Response, userIdentity.arn as ARN, userIdentity.type as User_Type | filter eventName = 'ConsoleLogin' | sort eventTime desc | limit 10
結果
eventTime | eventName | 回應 | ARN | User_Type |
---|---|---|---|---|
2022-02-18T17:35:44Z | ConsoleLogin | 成功 | arn:aws:iam::123456789012:user/test_user | IAMUser |
2022-02-17T13:53:58Z | ConsoleLogin | 成功 | arn:aws:sts::123456789012:assumed-role/Admin/test_user | AssumedRole |
查詢 5:依主控台登入進行篩選:身分驗證失敗
目標
- 在 Cloudtrail 事件中擷取特定欄位 (例如,使用者名稱、使用者類型、來源 IP)。
- 使用更有意義的標籤重新命名欄位。
- 篩選透過 AWS 主控台的最新不成功登入。
查詢
#ConsoleLogin: Filter on Failed Logins fields eventTime, eventName, responseElements.ConsoleLogin as Response, userIdentity.userName as User, userIdentity.type as User_Type, sourceIPAddress, errorMessage | filter eventName = 'ConsoleLogin' and responseElements.ConsoleLogin = 'Failure' | sort eventTime desc | limit 10
結果
eventTime | eventName | 回應 | 使用者 | User_Type | sourceIPAddress | errorMessage |
---|---|---|---|---|---|---|
2022-02-18T20:10:55Z | ConsoleLogin | 故障 | echo | IAMUser | 12.34.56.89 | 身分驗證失敗 |
2022-02-18T20:10:43Z | ConsoleLogin | 故障 | echo | IAMUser | 12.34.56.89 | 身分驗證失敗 |
查詢 6:依 Amazon Simple Storage Solution (Amazon S3) 物件上傳進行篩選
目標
- 擷取 Cloudtrail 事件中的特定欄位。
- 使用更有意義的標籤重新命名欄位。
- 篩選 API 呼叫 和目標 S3 儲存貯體。
查詢
#Filter PutObject API Calls on a specific S3 Bucket fields @timestamp, eventName as API, requestParameters.bucketName as BucketName, requestParameters.key as Key, userIdentity.sessionContext.sessionIssuer.userName as UserName | filter eventName = 'PutObject' and BucketName = 'target-s3-bucket' | sort @timestamp desc | limit 2
結果
@timestamp | API | BucketName | 索引鍵 | UserName |
---|---|---|---|---|
2022-02-12 17:16:07.415 | PutObject | test_bucket1 | w4r9Hg4V7g.jpg | |
2022-02-12 16:29:43.470 | PutObject | test_bucket2 | 6wyBy0hBoB.jpg |
查詢 7:匯總 S3 活動
目標
- 根據 S3 服務進行篩選。
- 根據 count 統計數字匯總所有匹配事件。
- 根據 API、S3 儲存貯體和索引鍵拼接結果。
- 使用 stats 命令重新命名欄位。
- 依遞減順序排序。
查詢
#S3 Activity: Bucket & Key Details filter eventSource = 's3.amazonaws.com' | stats count(*) as Hits by eventName as API, requestParameters.bucketName as BucketName, requestParameters.key as Key | sort Hits desc | limit 5
結果
API | BucketName | 索引鍵 | 命中 |
---|---|---|---|
ListAccessPoints | 44 | ||
GetBucketAcl | team1-ctrail-multi-region | 27 | |
GetBucketAcl | team2-dub-cloudtrail | 27 | |
GetBucketAcl | aws-cloudtrail-logs-123456789012-ba940dd7 | 26 | |
GetObject | devsupport-prod | rdscr/individual/123456789012 | 18 |
查詢 8:匯總 AWS Key Management Service (AWS KMS) 解密活動
目標
- 根據 KMS 服務和 Decrypt API 進行篩選。
- 使用 fields 命令重新命名欄位,並以使用者友好的名稱進行彙總。
- 根據 count 統計數字匯總所有匹配事件。
- 根據 KMS 金鑰和使用者拼接結果。
- 依遞減順序排序。
查詢
#KMS Decrypt Activity: Key & User Details fields resources.0.ARN as KMS_Key, userIdentity.sessionContext.sessionIssuer.userName as User | filter eventSource='kms.amazonaws.com' and eventName='Decrypt' | stats count(*) as Hits by KMS_Key, User | sort Hits desc | limit 2
結果
KMS_Key | 使用者 | 命中 |
---|---|---|
arn:aws:kms:us-east-1:123456789012:key/03f2923d-e213-439d-92cf-cbb444bd85bd | AWSServiceRoleForConfig | 12 |
arn:aws:kms:us-east-1:123456789012:key/03f2923d-e213-439d-92cf-cbb444bd85bd | FoxTrot-1UQJBODTWZYZ6 | 8 |
查詢 9:匯總遇到錯誤的 API 呼叫
目標
- 根據 errorCode 欄位是否存在進行篩選。
- 根據 count 統計數字匯總所有匹配事件。
- 根據 AWS 服務、API 和 errorCode 拼接結果。
- 使用 stats 命令重新命名欄位。
- 依最大匹配數排序。
查詢
#Summarize API Calls with Errors filter ispresent(errorCode) | stats count(*) as Num_of_Events by eventSource as AWS_Service, eventName as API, errorCode | sort Num_of_Events desc | limit 5
結果
AWS_Service | API | errorCode | Num_of_Events |
---|---|---|---|
s3.amazonaws.com | GetBucketPublicAccessBlock | NoSuchPublicAccessBlockConfiguration | 79 |
lambda.amazonaws.com | GetLayerVersionPolicy20181031 | ResourceNotFoundException | 66 |
s3.amazonaws.com | GetBucketPolicyStatus | NoSuchBucketPolicy | 60 |
s3.amazonaws.com | HeadBucket | AccessDenied | 47 |
logs.amazonaws.com | CreateLogStream | ResourceNotFoundException | 21 |
查詢 10:使用錯誤碼匯總 S3 API 呼叫
目標
- 根據 S3 服務和 errorCode 欄位是否存在進行篩選。
- 根據 count 統計數字匯總所有匹配事件。
- 根據 errorCode 和 errorMessage 拼接結果。
- 依最大匹配數排序。
查詢
#S3: Summarize Error Codes filter eventSource = 's3.amazonaws.com' and ispresent(errorCode) | stats count(*) as Hits by errorCode, errorMessage | sort Hits desc | limit 5
結果
errorCode | errorMessage | 命中 |
---|---|---|
AccessDenied | 存取遭拒 | 86 |
NoSuchBucketPolicy | 儲存貯體政策不存在 | 80 |
NoSuchPublicAccessBlockConfiguration | 未找到公有存取區塊組態 | 79 |
ObjectLockConfigurationNotFoundError | 此儲存貯體不存在物件鎖定組態 | 3 |
ServerSideEncryptionConfigurationNotFoundError | 未找到伺服器端加密組態 | 3 |
查詢 11:根據 AWS 服務、API 和 AWS Identity and Access Management (IAM) 使用者匯總 AccessDenied/UnauthorizedOperation API 呼叫
目標
- 篩選 AccessDenied 或 UnauthorizedOperation CloudTrail 事件。
- 根據 count 統計數字匯總所有匹配事件。
- 根據 errorCode、AWS 服務、API 和 IAM 使用者/角色拼接結果。
- 使用 stats 命令重新命名欄位。
- 依遞減順序排序。
查詢
#Summarize AccessDenied/UnauthorizedOperation API Calls by AWS Service, API, IAM User filter (errorCode='AccessDenied' or errorCode='UnauthorizedOperation') | stats count(*) as NumberOfEvents by errorCode, eventSource as AWS_Service, eventName as API, userIdentity.type as IdentityType, userIdentity.invokedBy as InvokedBy | sort NumberOfEvents desc | limit 10
結果
errorCode | AWS_Service | API | IdentityType | InvokedBy | NumberOfEvents |
---|---|---|---|---|---|
AccessDenied | s3.amazonaws.com | HeadBucket | AWSService | delivery.logs.amazonaws.com | 83 |
AccessDenied | s3.amazonaws.com | GetObject | AssumedRole | 9 |
查詢 12:時間序列:KMS 每小時呼叫量
目標
- 根據 KMS 服務和 Decrypt API 進行篩選。
- 將所有匹配事件匯總到 1 小時的筒。
- 在折線圖上視覺化結果。
查詢
#KMS: Hourly Decrypt Call Volume filter eventSource='kms.amazonaws.com' and eventName='Decrypt' | stats count(*) as Hits by bin(1h)
結果
筒 (1 小時) | 命中 |
---|---|
2022-02-18 19:00:00.000 | 16 |
2022-02-18 18:00:00.000 | 25 |
2022-02-18 17:00:00.000 | 28 |
2022-02-18 16:00:00.000 | 14 |
2022-02-18 15:00:00.000 | 16 |
相關資訊
AWS 官方已更新 2 年前
沒有評論
相關內容
- 已提問 4 個月前lg...
- 已提問 6 個月前lg...
- AWS 官方已更新 2 年前
- AWS 官方已更新 2 年前
- AWS 官方已更新 1 年前
- AWS 官方已更新 2 年前