Collecting logs is half the job. The other half is being able to ask them questions under pressure, with the incident still unfolding. CloudWatch Logs Insights queries log groups with its own query language, which helps a lot when CloudTrail is sent to CloudWatch Logs. In an investigation, filtering quickly by event, identity and source is the difference between understanding the incident and staying in the dark.
The idea here is to use Logs Insights for investigations in AWS. The walkthrough starts with the CloudTrail fields that answer the most questions, moves on to queries by activity pattern, which come up in almost every case, and ends at the point where a recurring query becomes an alert, always with an explanation of how to read the result. The intended use is authorized analysis of your own environment, focused on detection and response.
CloudTrail fields that matter
Every CloudTrail event carries fields that answer the basic questions of an investigation. The eventName field tells you which action happened, sourceIPAddress shows where it came from, userIdentity identifies who acted and awsRegion tells you in which Region. The errorCode field, in turn, appears on denied attempts. Cross-referencing these fields reveals patterns such as an identity acting from an unusual IP or a sequence of denied actions, which suggests permission enumeration.
fields @timestamp, eventName, userIdentity.arn, sourceIPAddress| filter eventSource = "iam.amazonaws.com"| sort @timestamp desc| limit 50Extracting and grouping with parse and stats
Besides filtering, Logs Insights aggregates. The stats command summarizes activity, and bin groups events by time window, which makes bursts visible. A sudden spike from a single IP or a single identity in a short window (the 5 minutes in the query below, for example) usually tells you more than a long list of scattered events.
fields @timestamp, userIdentity.arn| stats count(*) as events by bin(5m), userIdentity.arn| sort events descQueries by activity pattern
Some queries show up in almost every investigation. A list of denied attempts helps detect enumeration and permission abuse. Grouping actions by source IP shows where the activity came from. And a filter on sensitive events, such as the creation of users and keys or policy changes, reveals persistence attempts.
fields @timestamp, eventName, userIdentity.arn, errorCode| filter ispresent(errorCode)| stats count(*) as attempts by userIdentity.arn, errorCode| sort attempts descSigns of credential abuse and persistence
Some events deserve a query of their own, because they point to session abuse or to someone setting up persistent access. Examples are AssumeRole and GetSessionToken calls at unusual volume, access key creation and changes to a role's trust policy. Filtering by these event names and cross-referencing the result with the identity and source IP helps separate normal operations from suspicious activity.
fields @timestamp, eventName, userIdentity.arn, sourceIPAddress| filter eventName in ["AssumeRole", "GetSessionToken", "CreateAccessKey"]| stats count(*) as calls by userIdentity.arn, eventName| sort calls desc- Filter sensitive IAM events (user, key and policy creation)
- Group activity by sourceIPAddress to see where it comes from
- List errorCode values to catch enumeration and abuse
- Watch for AssumeRole and GetSessionToken at unusual volume
- Look for root account usage and services being turned off
- Narrow the time window to the incident period
From query to alert
Investigating after the fact is useful, but ideally some patterns should raise the alarm on their own. With CloudTrail in CloudWatch Logs, you create a metric filter that counts the occurrences of a pattern and attach that filter to an alarm. From then on, events such as root account usage or CloudTrail being stopped send a notification by themselves, without waiting for someone to run a query.
{ $.userIdentity.type = "Root" && $.userIdentity.invokedBy NOT EXISTS && $.eventType != "AwsServiceEvent" }Send the alarm to a channel that has an owner and a response deadline. An alarm that only lights up on a dashboard depends on someone looking at it. Keep Logs Insights for fast investigation, when you need to explore the data and rebuild the timeline, and hand known, recurring patterns over to metric filters.
Limitations and good practices
Logs Insights only sees what is in the log group. If CloudTrail is not being sent to CloudWatch, or if retention is short, the query will not find the event. Billing is based on the volume of data scanned, so narrowing the time window lowers the cost and speeds up the result. For very large volumes or long windows, a data lake with queries can come out cheaper. And keep reusable queries ready, so you do not have to improvise in the middle of an incident.
Practical checklist
- Send CloudTrail to CloudWatch Logs if investigations need fast queries
- Set log group retention to what investigations need
- Keep a standard set of investigation queries
- Use stats and bin to see bursts by identity and by IP
- Filter sensitive IAM events
- Track AssumeRole, GetSessionToken and key creation
- List denied attempts to detect enumeration
- Limit the time window to the incident, which makes the query faster and cheaper
- Create metric filters with alarms for critical patterns
- Look for root account usage and services being disabled
- Consider a data lake for large volumes and long windows
Best practices
- Have your queries ready before the incident
- Cross-reference event, identity and source to find patterns
- Group with stats and bin so bursts of activity stand out
- Treat denied attempts as a possible sign of enumeration
- Turn known patterns into metric filters with alarms
- Keep retention at least as long as it takes you to discover an incident
- Narrow the time window to keep query costs under control
Common mistakes
- CloudTrail not in CloudWatch when you need to query
- The quick query cannot find the events the investigation needs.
- Short log group retention
- By the time the incident is discovered, the relevant events have already expired.
- Improvising queries during the incident
- Time is lost and, under pressure, what matters can slip by.
- Ignoring errorCode
- Enumeration and permission abuse go by without anyone noticing.
- Querying huge windows without focus
- The query gets expensive and slow, and the analysis is no clearer for it.
- Relying only on manual queries, with no metric filter
- Critical patterns sit waiting for someone to look, when they could be raising alerts.
Authorship and review
Cesar Gargiulo
CEO and founder of GUARDIASEC. Technical and editorial lead for the content published in this library.
Published on . Found a technical error? Write to [email protected] and we will correct it and keep a record of the change.