Query Details

01 Executive Kpi Tiles

Query

// REVIEW ONLY workbook query: 📊 1.0 Executive Cross-Cloud Security Metrics Summary
let Start = {TimeRange:start};
let End = {TimeRange:end};
let AccountFilter = trim(" ", base64_decode_tostring("{AwsAccount:base64}"));
let UseAllAccounts = AccountFilter in ("", "*");
let SafeCloudTrail = union isfuzzy=true
  (datatable(TimeGenerated:datetime, AwsEventId:string, EventSource:string, EventName:string, AWSRegion:string,
    RecipientAccountId:string, UserIdentityAccountId:string, UserIdentityType:string, UserIdentityArn:string,
    UserIdentityAccessKeyId:string, SourceIpAddress:string, UserAgent:string,
    ErrorCode:string, ErrorMessage:string, RequestParameters:string, ResponseElements:string, Resources:string,
    ManagementEvent:bool, ReadOnly:bool, SessionMfaAuthenticated:string)[]),
  (AWSCloudTrail
   | where TimeGenerated between (Start .. End)
   | project TimeGenerated, AwsEventId, EventSource, EventName, AWSRegion, RecipientAccountId,
       UserIdentityAccountId, UserIdentityType, UserIdentityArn,
       UserIdentityAccessKeyId, SourceIpAddress, UserAgent, ErrorCode, ErrorMessage,
       RequestParameters, ResponseElements, Resources, ManagementEvent, ReadOnly, SessionMfaAuthenticated);
let IdentityMap = union isfuzzy=true
  (datatable(SearchKey:string)[]),
  (_GetWatchlist('AWS_EntraIdentityMap'))
| where isnotempty(tostring(column_ifexists("SearchKey", "")))
| extend VerifiedVal = tolower(tostring(column_ifexists("Verified", ""))),
    MappingEvidenceVal = tostring(column_ifexists("MappingEvidence", ""))
| where VerifiedVal == "true" and isnotempty(MappingEvidenceVal)
| project AwsAccessKeyId=tostring(column_ifexists("AwsAccessKeyId", "")),
    AwsPrincipalArn=tostring(column_ifexists("AwsPrincipalArn", "")),
    AWSAccountId=tostring(column_ifexists("AWSAccountId", "")),
    EntraTenantId=tolower(tostring(column_ifexists("EntraTenantId", ""))),
    EntraObjectId=tolower(tostring(column_ifexists("EntraObjectId", ""))),
    IdentityType=tostring(column_ifexists("IdentityType", "")),
    AppClientId=tolower(tostring(column_ifexists("AppClientId", ""))),
    ValidFrom=todatetime(column_ifexists("ValidFromUtc", "")),
    ValidTo=todatetime(column_ifexists("ValidToUtc", ""))
| summarize MappingRows=count(), take_any(*) by AwsAccessKeyId, AwsPrincipalArn, AWSAccountId
| where MappingRows == 1 and isnotempty(EntraTenantId) and isnotempty(EntraObjectId);
let ScopedTrail = SafeCloudTrail
| where UseAllAccounts or RecipientAccountId == AccountFilter or UserIdentityAccountId == AccountFilter;
let DeniedCalls = toscalar(ScopedTrail | where isnotempty(ErrorCode) | count);
let CrossAcctSecrets = toscalar(ScopedTrail
| where EventSource == "secretsmanager.amazonaws.com" and EventName == "GetSecretValue" and isempty(ErrorCode)
    and isnotempty(UserIdentityAccountId) and UserIdentityAccountId != RecipientAccountId | count);
let UnmappedKeys = toscalar(ScopedTrail
| where isnotempty(UserIdentityAccessKeyId) and isnotempty(UserIdentityArn)
| distinct AWSAccountId=UserIdentityAccountId, AwsAccessKeyId=UserIdentityAccessKeyId, AwsPrincipalArn=UserIdentityArn
| join kind=leftanti IdentityMap on AWSAccountId, AwsAccessKeyId, AwsPrincipalArn | count);
let GeoCountries = toscalar(ScopedTrail
| where isnotempty(SourceIpAddress) and isnotnull(parse_ipv4(SourceIpAddress)) and not(ipv4_is_private(SourceIpAddress))
| extend Country = tostring(geo_info_from_ip_address(SourceIpAddress).country)
| where isnotempty(Country)
| summarize dcount(Country));
let HighAlerts = toscalar(union isfuzzy=true
  (datatable(TimeGenerated:datetime, SystemAlertId:string, AlertSeverity:string, AlertName:string, ProductName:string)[]),
  (SecurityAlert | where TimeGenerated between (Start .. End) | project TimeGenerated, SystemAlertId, AlertSeverity, AlertName, ProductName)
| summarize arg_max(TimeGenerated, *) by SystemAlertId
| where (ProductName has_any ("AWS", "GuardDuty") or AlertName has "AWS") and AlertSeverity in ("High", "Critical")
| count);
let UebaSignals = toscalar(union isfuzzy=true
  (datatable(TimeGenerated:datetime)[]),
  (Anomalies | where TimeGenerated between (Start .. End) and (AnomalyTemplateName has_any ("AwsCloudTrail", "AWS", "GuardDuty") or RuleName has_any ("AwsCloudTrail", "AWS", "GuardDuty")) | project TimeGenerated),
  (SentinelBehaviorInfo | where TimeGenerated between (Start .. End) and (DataSources has_any ("AWS", "CloudTrail") or AdditionalFields has "AWSCloudTrail") | project TimeGenerated)
| count);
print
  M1 = "🔴 High/Critical AWS Alerts", V1 = HighAlerts,
  M2 = "❌ AccessDenied API Calls", V2 = DeniedCalls,
  M3 = "🟠 Unmapped Active AWS Keys", V3 = UnmappedKeys,
  M4 = "🔑 Cross-Account Secret Reads", V4 = CrossAcctSecrets,
  M5 = "🌍 External Source Countries", V5 = GeoCountries,
  M6 = "🧠 UEBA & Anomaly Signals", V6 = UebaSignals
| project Metric = pack_array(M1, M2, M3, M4, M5, M6),
    Value = pack_array(V1, V2, V3, V4, V5, V6)
| mv-expand Metric to typeof(string), Value to typeof(long)
| take 10

Explanation

This query is designed to provide a summary of security metrics across AWS cloud environments. Here's a simplified breakdown of what it does:

  1. Time Range and Account Filtering: It sets a time range (Start and End) and filters for specific AWS accounts based on a decoded base64 string (AccountFilter). If no specific account is provided, it considers all accounts.

  2. CloudTrail Data: It gathers AWS CloudTrail logs within the specified time range, focusing on various event details like event source, event name, user identity, and error codes.

  3. Identity Mapping: It checks a watchlist for verified mappings between AWS access keys and Entra identities, ensuring these mappings are valid and unique.

  4. Scoped Trail: Filters CloudTrail data to include only events relevant to the specified account(s).

  5. Metrics Calculation:

    • Denied API Calls: Counts the number of API calls that resulted in an "AccessDenied" error.
    • Cross-Account Secret Reads: Counts instances where secrets were accessed across different accounts without errors.
    • Unmapped AWS Keys: Identifies AWS access keys that are active but not mapped to any known identity.
    • External Source Countries: Counts distinct countries from which external IP addresses accessed the AWS environment.
    • High/Critical Alerts: Counts security alerts with high or critical severity related to AWS services.
    • UEBA & Anomaly Signals: Counts anomaly signals related to AWS, indicating unusual behavior.
  6. Output: The query prints a list of metrics with their respective values, providing a quick overview of the security posture across AWS environments. Each metric is labeled with an emoji for easy identification, such as "🔴 High/Critical AWS Alerts" and "❌ AccessDenied API Calls".