スキップしてコンテンツを表示

CloudWatch Logs Insights で CloudTrail ログを取得および分析する方法を教えてください。

所要時間5分
0

Amazon CloudWatch Logs Insights を使用して AWS CloudTrail ログをクエリおよび分析したいです。

簡単な説明

CloudTrail が CloudWatch Logs ロググループにログを送信するように設定した後、そのロググループに対してクエリを実行できます。API アクティビティをイベントタイプ、ユーザーアイデンティティ、AWS サービス、または時間範囲でフィルタリングできます。

この記事では、CloudWatch Logs を取得して Amazon S3 バケットとオブジェクトのアクティビティを分析および調査するためのサンプルクエリを提供します。

**注: ** デフォルトでは、CloudTrail は Amazon Simple Storage Service (Amazon S3) のデータイベントを記録しません。Amazon S3 バケットとオブジェクトのイベントログを取得するには、CloudTrail でイベントロギングを有効にする必要があります。

解決策

CloudWatch Logs Insights を使用して CloudTrail ログをクエリする

CloudWatch コンソールを使用して、CloudTrail ログを受信するロググループを選択します。

次のクエリ例を基に、ユースケースに合わせて、追加のより複雑な Logs Insights クエリを作成できます。また、クエリを CloudWatch ダッシュボードと統合して、関連するメトリクスと共にクエリをチャートやグラフとして可視化することもできます。

最新のイベントを取得する

デフォルトの @timestamp フィールドと @message フィールドで最近の CloudTrail ログイベントを取得するには、次のクエリ例を実行します。

#Retrieve the most recent CloudTrail events
fields @timestamp, @message
| sort @timestamp desc
| limit 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 などの列を返します。

Amazon EC2 RunInstances でフィルタリングする

RunInstances API コールに基づいて、AWS アカウントで起動された最新の Amazon Elastic Compute Cloud (Amazon 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

最新のコンソールログインでフィルタリングする

ConsoleLogin 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

出力例:

eventTimeeventNameResponseUserUser_Type
2022-02-18T17:35:44ZConsoleLoginSuccessarn:aws:iam::123456789012:user/test_userIAMUser

認証に失敗したコンソールログインでフィルタリングする

失敗したコンソールログインを取得し、より意味のあるラベルを使用するようにフィールド名を変更するには、次のクエリを実行してください。

#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

特定のバケットへの S3 オブジェクトアップロードでフィルタリングする

バケットへの PutObject API コールを取得し、より意味のあるラベルを使用するようにフィールド名を変更するには、次のクエリを実行してください。

#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

**注: ** target-s3-bucket をご利用のバケットの名前に置き換えてください。

出力例:

@timestampAPIBucket_NameKeyUserName
2022-02-12 17:16:07.415PutObjecttest_bucket1w4r9Hg4V7g.jpg

**注: ** S3 オブジェクトレベルのイベントでは、S3 の CloudTrail データイベントを有効にする必要があります。手順については、『データイベントをログ記録する』を参照してください。

S3 アクティビティを要約する

S3 サービスからのイベントをフィルタリングし、イベントをカウントで集約し、結果を API コール、バケット名、キーで分割するには、次のクエリを実行してください。

#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
GetObjectdevsupport-prodrdscr/individual/12345678901218

AWS KMS の復号アクティビティを要約する

AWS Key Management Service (AWS KMS) の Decrypt API からのイベントをフィルタリングし、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

エラーのある API コールを要約する

エラーコードを含むすべてのイベントをフィルタリングし、イベントをカウントで集約し、結果を AWS サービス、API コール、エラーコードで分割するには、次のクエリを実行してください。

#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

S3 API コールをエラーコードで要約する

エラーコードを含む S3 イベントをフィルタリングし、エラーコードとエラーメッセージでイベントを集約するには、次のクエリを実行してください。

#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

"AccessDenied" と "UnauthorizedOperation" の API コールを要約する

"AccessDenied" または "UnauthorizedOperation" エラーコードを含むイベントをフィルタリングし、AWS サービス、API コール、AWS Identity and Access Management (IAM) ID タイプでイベントを集約するには、次のクエリを実行してください。

#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

AWS KMS の時間別の復号呼び出しボリュームを要約する

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

関連情報

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

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

1コメント

This article was reviewed and updated on 2026-05-25.

エキスパート

応答済み 2ヶ月前