CloudWatch Logs Insights を使用して CloudTrail ログを取得して分析するにはどうすればよいですか?

所要時間7分
0

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

結果

@timestampawsRegioneventCategoryeventSourceeventNameeventTypesourceIPAddressuserIdentity.type
2022-02-18 18:00:09.647ca-central-1Managementsts.amazonaws.comAssumeRoleAwsApiCallcloudtrail.amazonaws.comAWSService
2022-02-18 18:00:09.647ca-central-1Managementsts.amazonaws.comAssumeRoleAwsApiCallcloudtrail.amazonaws.comAWSService

クエリ 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

結果

eventTimeAPIInstanceIDIssuerTypeIdentityTypeuserName
2022-02-18T17:36:38ZRunInstancesi-0325b4d6ae4e93c75RoleAssumedRoleAWSServiceRoleForAutoScaling
2022-02-18T13:45:18ZRunInstancesi-04d17a8425b7cb59aRoleAssumedRoleAWSServiceRoleForAutoScaling

クエリ 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

結果

eventTimeeventNameResponseARNUser_Type
2022-02-18T17:35:44ZConsoleLoginSuccessarn:aws:iam::123456789012:user/test_userIAMUser
2022-02-17T13:53:58ZConsoleLoginSuccessarn:aws:sts::123456789012:assumed-role/Admin/test_userAssumedRole

クエリ 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

結果

eventTimeeventNameResponseUserUser_TypesourceIPAddresserrorMessage
2022-02-18T20:10:55ZConsoleLoginFailureechoIAMUser12.34.56.89Failed authentication
2022-02-18T20:10:43ZConsoleLoginFailureechoIAMUser12.34.56.89Failed 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

結果

@timestampAPIBucketNameKeyUserName
2022-02-12 17:16:07.415PutObjecttest_bucket1w4r9Hg4V7g.jpg
2022-02-12 16:29:43.470PutObjecttest_bucket26wyBy0hBoB.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

結果

APIBucketNameKeyHits
ListAccessPoints44
GetBucketAclteam1-ctrail-multi-region27
GetBucketAclteam2-dub-cloudtrail27
GetBucketAclaws-cloudtrail-logs-123456789012-ba940dd726
GetObjectdevsupport-prodrdscr/individual/12345678901218

クエリ 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_KeyUserHits
arn:aws:kms:us-east-1:123456789012:key/03f2923d-e213-439d-92cf-cbb444bd85bdAWSServiceRoleForConfig12
arn:aws:kms:us-east-1:123456789012:key/03f2923d-e213-439d-92cf-cbb444bd85bdFoxTrot-1UQJBODTWZYZ68

クエリ 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_ServiceAPIerrorCodeNum_of_Events
s3.amazonaws.comGetBucketPublicAccessBlockNoSuchPublicAccessBlockConfiguration79
lambda.amazonaws.comGetLayerVersionPolicy20181031ResourceNotFoundException66
s3.amazonaws.comGetBucketPolicyStatusNoSuchBucketPolicy60
s3.amazonaws.comHeadBucketAccessDenied47
logs.amazonaws.comCreateLogStreamResourceNotFoundException21

クエリ 10: S3 API コールをエラーコードで要約する

目的

  • Amazon S3 サービスと ErrorCode フィールドの存在に基づいてフィルタリングします。
  • count 統計に基づいて、一致するすべてのイベントを集計します。
  • errorCodeerrorMessage に基づいて結果を分割します。
  • 最も一致が多い順に並べ替えます。

クエリ

#S3: Summarize Error Codes
filter eventSource = 's3.amazonaws.com' and ispresent(errorCode)
| stats count(*) as Hits by errorCode, errorMessage
| sort Hits desc
| limit 5

結果

errorCodeerrorMessageHits
AccessDeniedAccess Denied86
NoSuchBucketPolicyThe bucket policy does not exist80
NoSuchPublicAccessBlockConfigurationThe public access block configuration was not found79
ObjectLockConfigurationNotFoundErrorObject Lock configuration does not exist for this bucket3
ServerSideEncryptionConfigurationNotFoundErrorThe server side encryption configuration was not found3

クエリ 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

結果

errorCodeAWS_ServiceAPIIdentityTypeInvokedByNumberOfEvents
AccessDenieds3.amazonaws.comHeadBucketAWSServicedelivery.logs.amazonaws.com83
AccessDenieds3.amazonaws.comGetObjectAssumedRole9

クエリ 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.00016
2022-02-18 18:00:00.00025
2022-02-18 17:00:00.00028
2022-02-18 16:00:00.00014
2022-02-18 15:00:00.00016

関連情報

Amazon CloudWatch で AWS CloudTrail のログデータをモニタリングする (動画)

ダッシュボードにクエリを追加する、またはクエリ結果をエクスポートする

コメントはありません