Query Details

H09 Cloudtrail Rare Mutating Api And Error Spikes

Query

// REVIEW ONLY - Hunting Query H09: AWSCloudTrail Rare Mutating API Calls & AccessDenied Spikes
// Complements Identity & Control Plane workbook tab without overlapping native IAM enumeration templates.
let Lookback = 14d;
let RecentWindow = 24h;
let HistoricalApis = AWSCloudTrail
| where TimeGenerated between (ago(Lookback) .. ago(RecentWindow))
| where ReadOnly == false
| distinct RecipientAccountId, EventSource, EventName;
let RareMutations = AWSCloudTrail
| where TimeGenerated > ago(RecentWindow) and ReadOnly == false
| join kind=leftanti HistoricalApis on RecipientAccountId, EventSource, EventName
| summarize FirstSeen=min(TimeGenerated), Calls=count(), Principals=make_set(UserIdentityArn, 10),
    SourceIps=make_set(SourceIpAddress, 10), Errors=make_set(ErrorCode, 5)
    by RecipientAccountId, AWSRegion, EventSource, EventName
| extend HuntSignal = "FirstSeenMutatingApiIn14d";
let ErrorSpikes = AWSCloudTrail
| where TimeGenerated > ago(RecentWindow)
| where ErrorCode in ("AccessDenied", "AccessDeniedException", "UnauthorizedOperation", "Client.UnauthorizedOperation")
| summarize FirstSeen=min(TimeGenerated), Calls=count(), DistinctApis=dcount(EventName),
    Principals=make_set(UserIdentityArn, 5), SourceIps=make_set(SourceIpAddress, 10),
    Errors=make_set(ErrorCode, 5)
    by RecipientAccountId, AWSRegion, EventSource=UserIdentityArn, EventName="MultiServiceAccessDeniedBurst"
| where Calls >= 25 and DistinctApis >= 8
| extend HuntSignal = strcat("AccessDeniedReconBurst(DistinctApis=", DistinctApis, ")");
union RareMutations, ErrorSpikes
| order by Calls desc
| take 1000

Explanation

This query is designed to identify unusual or potentially suspicious activities in AWS CloudTrail logs over a specific period. Here's a breakdown of what it does:

  1. Lookback Period: It examines data from the last 14 days, focusing on the most recent 24 hours.

  2. Historical API Calls: It first identifies all unique non-read-only API calls (mutating actions) made in the 14-day period, excluding the last 24 hours. This serves as a baseline of normal activity.

  3. Rare Mutating API Calls: It then looks at the last 24 hours to find any non-read-only API calls that were not seen in the previous 13 days. These are considered rare and potentially suspicious. For each of these rare calls, it records the first time it was seen, the number of times it was called, the users involved, the source IPs, and any errors encountered.

  4. Access Denied Spikes: The query also checks for spikes in "Access Denied" errors in the last 24 hours. It identifies cases where there are at least 25 such errors across 8 or more different APIs, which might indicate unauthorized access attempts. It records similar details as above.

  5. Combining Results: The results from both rare mutating API calls and access denied spikes are combined, sorted by the number of calls, and the top 1000 entries are returned.

Overall, this query helps in hunting for unusual API activities and potential security incidents in AWS environments by highlighting rare actions and access denial spikes.