11 Geo Aws Activity Map
Query
// REVIEW ONLY workbook query: 🗺️ 5.1 AWS CloudTrail Activity World Map (Bubble Size = Events · Color = Denied/Mutating Risk %)
let Start = {TimeRange:start};
let End = {TimeRange:end};
let AccountFilter = trim(" ", base64_decode_tostring("{AwsAccount:base64}"));
let UseAllAccounts = AccountFilter in ("", "*");
union isfuzzy=true
(datatable(TimeGenerated:datetime, RecipientAccountId:string, UserIdentityAccountId:string, SourceIpAddress:string, ErrorCode:string, ReadOnly:bool, UserIdentityArn:string)[]),
(AWSCloudTrail | where TimeGenerated between (Start .. End)
| project TimeGenerated, RecipientAccountId, UserIdentityAccountId, SourceIpAddress, ErrorCode, ReadOnly, UserIdentityArn)
| where (UseAllAccounts or RecipientAccountId == AccountFilter or UserIdentityAccountId == AccountFilter)
| where isnotempty(SourceIpAddress) and isnotnull(parse_ipv4(SourceIpAddress)) and not(ipv4_is_private(SourceIpAddress))
| extend Geo = geo_info_from_ip_address(SourceIpAddress)
| 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 TotalEvents = count(),
DeniedCalls = countif(isnotempty(ErrorCode)),
MutatingCalls = countif(ReadOnly == false),
UniqueIps = dcount(SourceIpAddress),
UniquePrincipals = dcount(UserIdentityArn)
by Country, City, Latitude, Longitude
| extend RiskRate = round(todouble(DeniedCalls + MutatingCalls) / todouble(max_of(TotalEvents, 1)) * 100.0, 2)
| order by TotalEvents desc
| take 1000Explanation
This query is designed to analyze AWS CloudTrail activity and visualize it on a world map. Here's a simplified breakdown of what it does:
-
Time Range and Account Filtering: It sets a time range (
StartandEnd) and filters for a specific AWS account if provided. If no specific account is given, it considers all accounts. -
Data Collection: It gathers data from AWS CloudTrail logs within the specified time range. The data includes details like the time of the event, account IDs, source IP addresses, error codes, whether the action was read-only, and user identity information.
-
IP Address Filtering: It filters out events with empty or private IP addresses, focusing only on public IP addresses.
-
Geolocation: It uses the source IP addresses to determine the geographical location (country, city, latitude, and longitude) of the events.
-
Data Aggregation: It summarizes the data by location (country and city) to calculate:
- Total number of events.
- Number of denied calls (events with errors).
- Number of mutating calls (events that are not read-only).
- Number of unique IP addresses.
- Number of unique user identities (principals).
-
Risk Calculation: It calculates a risk rate based on the proportion of denied and mutating calls relative to the total number of events.
-
Sorting and Limiting Results: It orders the results by the total number of events in descending order and limits the output to the top 1,000 locations.
The result is a dataset that can be used to create a world map visualization, where the size of the bubbles represents the number of events, and the color indicates the risk percentage (based on denied and mutating events).