Query Details

12 Geo Attack Origins Map

Query

// REVIEW ONLY workbook query: 🎯 5.2 High-Risk, Denied & GuardDuty Attack Origins World Map
let Start = {TimeRange:start};
let End = {TimeRange:end};
let AccountFilter = trim(" ", base64_decode_tostring("{AwsAccount:base64}"));
let UseAllAccounts = AccountFilter in ("", "*");
let SuspiciousTrailIps = union isfuzzy=true
  (datatable(TimeGenerated:datetime, RecipientAccountId:string, UserIdentityAccountId:string, SourceIpAddress:string, ErrorCode:string, EventSource:string, EventName:string, UserIdentityType:string, UserIdentityArn:string)[]),
  (AWSCloudTrail | where TimeGenerated between (Start .. End)
   | project TimeGenerated, RecipientAccountId, UserIdentityAccountId, SourceIpAddress, ErrorCode, EventSource, EventName, UserIdentityType, UserIdentityArn)
| where (UseAllAccounts or RecipientAccountId == AccountFilter or UserIdentityAccountId == AccountFilter)
| where isnotempty(SourceIpAddress) and isnotnull(parse_ipv4(SourceIpAddress)) and not(ipv4_is_private(SourceIpAddress))
| where isnotempty(ErrorCode)
    or UserIdentityType =~ "Root"
    or (EventSource == "secretsmanager.amazonaws.com" and EventName == "GetSecretValue")
    or (EventSource == "iam.amazonaws.com" and EventName in ("CreateAccessKey", "CreateLoginProfile", "AttachUserPolicy", "AttachRolePolicy", "PutUserPolicy", "PutRolePolicy", "UpdateAssumeRolePolicy"))
| project SourceIp = SourceIpAddress, TargetKey = coalesce(UserIdentityArn, RecipientAccountId), Weight = iff(UserIdentityType =~ "Root" or EventName == "GetSecretValue", 5, 1);
let GdRemoteIps = union isfuzzy=true
  (datatable(TimeGenerated:datetime, Id:string, AccountId:string, ServiceDetails:dynamic)[]),
  (AWSGuardDuty | where TimeGenerated between (Start .. End) | project TimeGenerated, Id, AccountId, ServiceDetails=todynamic(ServiceDetails))
| summarize arg_max(TimeGenerated, *) by Id
| extend SourceIp = coalesce(
    iff(isnotempty(tostring(ServiceDetails.action.awsApiCallAction.remoteIpDetails.ipAddressV4)), tostring(ServiceDetails.action.awsApiCallAction.remoteIpDetails.ipAddressV4), ""),
    tostring(ServiceDetails.action.networkConnectionAction.remoteIpDetails.ipAddressV4))
| where isnotempty(SourceIp) and isnotnull(parse_ipv4(SourceIp)) and not(ipv4_is_private(SourceIp))
| project SourceIp, TargetKey = AccountId, Weight = 10;
union SuspiciousTrailIps, GdRemoteIps
| extend Geo = geo_info_from_ip_address(SourceIp)
| extend Country = tostring(Geo.country),
    City = coalesce(iff(isnotempty(tostring(Geo.city)), tostring(Geo.city), ""), tostring(Geo.country)),
    Latitude = todouble(Geo.latitude),
    Longitude = todouble(Geo.longitude)
| where isnotnull(Latitude) and isnotnull(Longitude) and isnotempty(City)
| summarize SuspiciousEvents = count(), AttackingIps = dcount(SourceIp), Targets = dcount(TargetKey), WeightedSum = sum(Weight)
    by Country, City, Latitude, Longitude
| extend ThreatScore = WeightedSum * max_of(Targets, 1)
| order by ThreatScore desc
| take 1000

Explanation

This query is designed to analyze and visualize high-risk activities and potential attack origins on a world map using AWS CloudTrail and GuardDuty data. Here's a simplified breakdown of what the query does:

  1. Time Range and Account Filtering:

    • It sets a time range (Start and End) and filters for a specific AWS account (AccountFilter). If no specific account is provided, it considers all accounts.
  2. Suspicious CloudTrail Events:

    • It gathers data from AWS CloudTrail logs within the specified time range.
    • It filters for events with non-private IP addresses that have error codes, involve root user actions, or specific sensitive operations (like accessing secrets or modifying IAM policies).
    • It assigns a weight to each event, with higher weights for more sensitive actions.
  3. GuardDuty Findings:

    • It collects data from AWS GuardDuty findings within the same time range.
    • It extracts remote IP addresses involved in these findings and assigns a high weight to these events.
  4. Combining and Geolocating Data:

    • It combines the suspicious IP addresses from both CloudTrail and GuardDuty data.
    • It uses geolocation to determine the country, city, latitude, and longitude of these IP addresses.
  5. Summarizing and Scoring:

    • It summarizes the data by country and city, counting the number of suspicious events, distinct attacking IPs, and targets.
    • It calculates a "ThreatScore" based on the weighted sum of events and the number of targets, ordering the results by this score.
  6. Output:

    • The query outputs the top 1000 locations with the highest threat scores, which can be visualized on a world map to identify potential attack origins.

In essence, this query helps identify and prioritize geographic locations that are potential sources of high-risk activities against AWS accounts.