Complete a 3 Question Survey and Earn a re:Post Badge
Help improve AWS Support Official channel in re:Post and share your experience - complete a quick three-question survey to earn a re:Post badge!
CloudWatch Logs Insights を使用して CloudTrail ログを取得して分析するにはどうすればよいですか?
Amazon CloudWatch Logs Insights を使用して Amazon CloudTrail ログを取得して分析したいと考えています。
簡単な説明
CloudWatch ログを記録するように CloudTrail を設定したら、CloudWatch Logs Insights でクエリを使用して CloudTrail ログを取得できます。取得後、特定のアカウントアクティビティをモニタリングできます。
解決策
以下のクエリを使用して CloudWatch ログを取得し、Amazon Simple Storage Service (Amazon S3) のバケットとオブジェクトのアクティビティを分析して調べます。デフォルトでは、CloudTrail は Amazon S3 データイベントをキャプチャしないことに注意してください。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" (output truncated) |
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": (output truncated) |
クエリ 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 | Management | sts.amazonaws.com | AssumeRole | AwsApiCall | cloudtrail.amazonaws.com | AWSService |
2022-02-18 18:00:09.647 | ca-central-1 | Management | sts.amazonaws.com | AssumeRole | AwsApiCall | cloudtrail.amazonaws.com | AWSService |
クエリ 3: Amazon Elastic Compute Cloud (Amazon EC2) 実行インスタンスでフィルタリングする
目的
- 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 | Role | AssumedRole | AWSServiceRoleForAutoScaling |
2022-02-18T13:45:18Z | RunInstances | i-04d17a8425b7cb59a | Role | AssumedRole | AWSServiceRoleForAutoScaling |
クエリ 4: 最新のコンソールログインでフィルタリングする
目的
- CloudTrail イベント内の特定のフィールドを取得します。
- フィールド名を変更して、よりわかりやすいラベルを使用します。
- API コールに基づいてコンソールを介した最新のログインをフィルタリングします。
クエリ
#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 | Response | ARN | User_Type |
---|---|---|---|---|
2022-02-18T17:35:44Z | ConsoleLogin | Success | arn:aws:iam::123456789012:user/test_user | IAMUser |
2022-02-17T13:53:58Z | ConsoleLogin | Success | arn:aws:sts::123456789012:assumed-role/Admin/test_user | AssumedRole |
クエリ 5: 認証に失敗したコンソールログインでフィルタリングする
目的
- CloudTrail イベント内の特定のフィールドを取得します。
- フィールド名を変更して、よりわかりやすいラベルを使用します。
- コンソールを介した、失敗した最新のログインをフィルタリングします。
クエリ
#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 | Response | User | User_Type | sourceIPAddress | errorMessage |
---|---|---|---|---|---|---|
2022-02-18T20:10:55Z | ConsoleLogin | Failure | echo | IAMUser | 12.34.56.89 | Failed authentication |
2022-02-18T20:10:43Z | ConsoleLogin | Failure | echo | IAMUser | 12.34.56.89 | Failed authentication |
クエリ 6: Amazon Simple Storage Service (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 | Key | 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 アクティビティを要約する
目的
- Amazon 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 | Key | Hits |
---|---|---|---|
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 KMS の復号アクティビティを要約する
目的
- AWS Key Management Service (AWS KMS) サービスと Decrypt API に基づいてフィルタリングします。
- fields コマンドを使用して、フィールドの名前を変更し、わかりやすい名前で集約します。
- count 統計に基づいて、一致するすべてのイベントを集計します。
- AWS 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 | User | Hits |
---|---|---|
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 コールをエラーコードで要約する
目的
- Amazon 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 | Hits |
---|---|---|
AccessDenied | Access Denied | 86 |
NoSuchBucketPolicy | The bucket policy does not exist | 80 |
NoSuchPublicAccessBlockConfiguration | The public access block configuration was not found | 79 |
ObjectLockConfigurationNotFoundError | Object Lock configuration does not exist for this bucket | 3 |
ServerSideEncryptionConfigurationNotFoundError | The server side encryption configuration was not found | 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: AWS KMS の 1 時間あたりのコール量
目的
- AWS KMS サービスと Decrypt API に基づいてフィルタリングします。
- 一致するすべてのイベントを 1 時間のビンに集約します。
- 結果を折れ線グラフで視覚化します。
クエリ
#KMS: Hourly Decrypt Call Volume filter eventSource='kms.amazonaws.com' and eventName='Decrypt' | stats count(*) as Hits by bin(1h)
結果
bin(1h) | Hits |
---|---|
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 |
関連情報
関連するコンテンツ
- 質問済み 6ヶ月前lg...
- 質問済み 6ヶ月前lg...
- 質問済み 3ヶ月前lg...