Query Details

16 Network Geo Attack Origins World Map

Query

// REVIEW ONLY - AWS Security Operations Workbook Visual Query
// Tab: Network | Panel: geo-attack-origins-world-map | Visualization: map | Mode: All
// Security Question: Which cities and countries are originating suspicious or denied external AWS activity?
// Drill-Through: Review Panel 5.0b below and click any SourceIp row to populate SelectedIp across all tabs.
// Empty/No-Data Contract: No high-risk, AccessDenied, or GuardDuty external source IPs observed in the selected window.
let Start = {TimeRange:start};
let End = {TimeRange:end};
let OrgFilter = base64_decode_tostring("{Organization:base64}");
let AccountFilter = base64_decode_tostring("{AwsAccount:base64}");
let RegionFilter = base64_decode_tostring("{AwsRegion:base64}");
let EnvFilter = base64_decode_tostring("{Environment:base64}");
let WorkloadFilter = base64_decode_tostring("{Workload:base64}");
let SeverityFilter = base64_decode_tostring("{Severity:base64}");
let SourceFilter = base64_decode_tostring("{SourceTable:base64}");
let SummaryPref = base64_decode_tostring("{SummaryMode:base64}");
let SelectedPrincipalFilter = base64_decode_tostring("{SelectedPrincipal:base64}");
let SelectedIpFilter = base64_decode_tostring("{SelectedIp:base64}");
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 (AccountFilter == "*" 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 5000

Explanation

This query is designed to identify and visualize the geographic origins of suspicious or denied external activities targeting AWS resources. Here's a simplified breakdown of what the query does:

  1. Time and Filters Setup: It starts by setting up time range filters and various other filters (like organization, account, region, environment, workload, severity, source table, selected principal, and selected IP) using base64 encoded strings.

  2. Data Collection:

    • SuspiciousTrailIps: It collects data from AWS CloudTrail logs within the specified time range, focusing on events with errors, root user activities, or specific sensitive actions (like accessing secrets or modifying IAM policies). It filters out private IPs and assigns a weight to each event based on its nature.
    • GdRemoteIps: It gathers data from AWS GuardDuty findings, focusing on remote IP details and filtering out private IPs. Each finding is assigned a weight.
  3. Data Union: It combines the data from CloudTrail and GuardDuty into a single dataset.

  4. Geolocation: For each IP address, it retrieves geolocation information (country, city, latitude, and longitude).

  5. Data Aggregation: It summarizes the data by country and city, counting the number of suspicious events, distinct attacking IPs, and targets. It also calculates a weighted sum of events to assess the severity.

  6. Threat Scoring: It calculates a threat score for each location based on the weighted sum and the number of targets.

  7. Ordering and Limiting: It orders the results by threat score in descending order and limits the output to the top 5000 entries.

The result is a list of cities and countries, along with their geolocation coordinates, that are the origins of suspicious or denied AWS activities, ranked by their threat score. This information is visualized on a map to help security teams identify potential threats geographically.