Query Details

04 Overview Top Risky Identities And Resources

Query

// REVIEW ONLY - AWS Security Operations Workbook Visual Query
// Tab: Overview | Panel: top-risky-identities-and-resources | Visualization: table | Mode: All
// Security Question: Which IAM principals or AWS resources combine high-severity findings, privilege/persistence mutations, and critical asset tags?
// Drill-Through: Clicking a row exports EntityKey into SelectedPrincipal across Identity, GuardDuty, S3, and Coverage cross-cloud tabs.
// Empty/No-Data Contract: No high-risk identity or resource signals matched current filters; verify AWS_CriticalAssets watchlist and source health.
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 AssetContext = union isfuzzy=true
    (datatable(SearchKey:string)[]),
    (_GetWatchlist('AWS_CriticalAssets'))
| where isnotempty(tostring(column_ifexists("SearchKey", "")))
| where tolower(tostring(column_ifexists("Verified", ""))) == "true"
| extend AssetAccount=tostring(column_ifexists("AWSAccountId", "")),
    AssetKey=tolower(tostring(column_ifexists("ResourceIdOrName", ""))),
    Environment=tostring(column_ifexists("Environment", "")),
    Workload=tostring(column_ifexists("Workload", "")),
    Criticality=tostring(column_ifexists("Criticality", "")),
    Organization=tostring(column_ifexists("Organization", ""))
| where (EnvFilter == "*" or Environment =~ EnvFilter) and (WorkloadFilter == "*" or Workload =~ WorkloadFilter) and (OrgFilter == "*" or Organization =~ OrgFilter)
| project AssetAccount, AssetKey, Environment, Workload, Criticality;
let IamSignals = union isfuzzy=true
    (datatable(TimeGenerated:datetime, RecipientAccountId:string, AWSRegion:string, ErrorCode:string, UserIdentityType:string, EventName:string, UserIdentityArn:string)[]),
    (AWSCloudTrail | where TimeGenerated between (Start .. End) | project TimeGenerated, RecipientAccountId, AWSRegion, ErrorCode, UserIdentityType, EventName, UserIdentityArn)
| where AccountFilter == "*" or RecipientAccountId == AccountFilter
| where RegionFilter == "*" or AWSRegion == RegionFilter
| where isempty(ErrorCode) and (UserIdentityType =~ "Root" or EventName in ("UpdateAssumeRolePolicy", "PutRolePolicy", "PutUserPolicy", "CreateAccessKey", "CreateLoginProfile", "PutEventSelectors", "StopLogging"))
| project AWSAccountId=RecipientAccountId, AWSRegion, EntityKey=coalesce(iff(isnotempty(UserIdentityArn), UserIdentityArn, ""), "root"), SignalCategory="ControlPlaneHighRisk", Weight=todouble(iff(UserIdentityType =~ "Root", 30, 15));
let GdSignals = union isfuzzy=true
    (datatable(TimeGenerated:datetime, Id:string, AccountId:string, Region:string, ActivityType:string, Severity:real, ServiceDetails:dynamic, ResourceDetails:dynamic)[]),
    (AWSGuardDuty | where TimeGenerated between (Start .. End) | project TimeGenerated, Id, AccountId, Region, ActivityType, Severity=todouble(Severity), ServiceDetails=todynamic(ServiceDetails), ResourceDetails=todynamic(ResourceDetails))
| where AccountFilter == "*" or AccountId == AccountFilter
| where RegionFilter == "*" or Region == RegionFilter
| summarize arg_max(TimeGenerated, *) by Id
| where tobool(ServiceDetails.archived) != true and Severity >= 4.0
| extend EntityKey = coalesce(
    iff(isnotempty(tostring(ResourceDetails.accessKeyDetails.userName)), tostring(ResourceDetails.accessKeyDetails.userName), ""),
    iff(isnotempty(tostring(ResourceDetails.instanceDetails.instanceId)), tostring(ResourceDetails.instanceDetails.instanceId), ""),
    iff(isnotempty(tostring(ResourceDetails.s3BucketDetails[0].name)), tostring(ResourceDetails.s3BucketDetails[0].name), ""),
    Id)
| project AWSAccountId=AccountId, AWSRegion=Region, EntityKey, SignalCategory=strcat("GuardDuty:", ActivityType), Weight=Severity * 5.0;
union IamSignals, GdSignals
| extend LookupKey = tolower(EntityKey)
| join kind=leftouter AssetContext on $left.AWSAccountId == $right.AssetAccount and $left.LookupKey == $right.AssetKey
| where (EnvFilter == "*" or isnotempty(Environment)) and (WorkloadFilter == "*" or isnotempty(Workload))
| summarize RiskScore=round(sum(Weight), 1), SignalCount=count(), DistinctSignals=make_set(SignalCategory, 10)
    by EntityKey, AWSAccountId, AWSRegion, Criticality=coalesce(iff(isnotempty(Criticality), Criticality, ""), "Unclassified"),
       Environment=coalesce(iff(isnotempty(Environment), Environment, ""), "Unclassified"),
       Workload=coalesce(iff(isnotempty(Workload), Workload, ""), "Unclassified")
| order by RiskScore desc
| take 5000

Explanation

This query is designed to identify high-risk AWS IAM principals or resources by combining data from various sources like AWS CloudTrail and AWS GuardDuty. Here's a simplified breakdown:

  1. Purpose: The query aims to find IAM principals or AWS resources that have high-severity security findings, changes in privileges or persistence, and are tagged as critical assets.

  2. Filters: It uses several filters such as organization, account, region, environment, workload, severity, and source to narrow down the search.

  3. Asset Context: It checks against a watchlist of critical assets to ensure that only verified critical resources are considered.

  4. IAM Signals: It gathers data from AWS CloudTrail to identify high-risk activities, such as changes to role policies or creation of access keys, especially focusing on root or privileged actions.

  5. GuardDuty Signals: It collects findings from AWS GuardDuty with a severity of 4.0 or higher, excluding archived findings, and identifies the associated entities.

  6. Data Integration: The query combines IAM and GuardDuty signals, linking them with critical assets based on account and resource identifiers.

  7. Risk Scoring: It calculates a risk score for each entity based on the severity and type of signals, and summarizes the findings by entity, account, region, and other attributes.

  8. Output: The results are sorted by risk score in descending order, and the top 5000 entries are displayed in a table format.

  9. Drill-Through: Users can click on a row to export specific data for further analysis across different AWS services.

  10. Empty/No-Data Handling: If no high-risk entities are found, it suggests verifying the critical assets watchlist and the health of data sources.

In essence, this query helps security teams quickly identify and prioritize high-risk entities within their AWS environment for further investigation or action.