Fn AWS Sensitive Target Eval
Query
// REVIEW ONLY - Reusable Workspace Function Spec: fn_AWS_SensitiveTargetEval
// Purpose: Classifies IAM policy/role/trust/PassRole targets and matches against AWS_SensitiveRolesAndPolicies watchlist.
let Lookback = 24h;
let SensitiveCatalog = _GetWatchlist('AWS_SensitiveRolesAndPolicies')
| where tolower(tostring(column_ifexists("Verified", ""))) == "true"
| project CatalogAccountId=tostring(column_ifexists("AWSAccountId", "")), ResourceArnPattern=tolower(tostring(column_ifexists("ResourceArnPattern", ""))),
Criticality=tostring(column_ifexists("Criticality", "")), AdminEquivalent=tolower(tostring(column_ifexists("AdminEquivalent", "")));
AWSCloudTrail
| where TimeGenerated > ago(Lookback)
| where EventSource in ("iam.amazonaws.com", "sts.amazonaws.com", "ec2.amazonaws.com", "lambda.amazonaws.com", "ecs.amazonaws.com", "cloudformation.amazonaws.com")
| extend Req = parse_json(RequestParameters)
| extend TargetRoleOrUser = coalesce(tostring(Req.roleName), tostring(Req.userName), tostring(Req.groupName), tostring(Req.roleArn), tostring(Req.iamInstanceProfile.arn), tostring(Req.role), ""),
PolicyArn = tostring(Req.policyArn),
PolicyDoc = coalesce(tostring(Req.policyDocument), tostring(Req.assumeRolePolicyDocument), ""),
SetAsDefault = tobool(Req.setAsDefault)
| extend HasAdminWildcardDoc = PolicyDoc matches regex @"(?i)""Effect""\s*:\s*""Allow""[\s\S]{0,400}""Action""\s*:\s*(\""\*\""|\[\s*\""\*\""\s*\])",
HasExternalTrustPrincipal = (EventName == "UpdateAssumeRolePolicy" and PolicyDoc matches regex @"arn:aws[a-z-]*:iam::[0-9]{12}:root|""AWS""\s*:\s*""\*"""),
IsAdminManagedPolicy = PolicyArn matches regex @"(?i):(policy|aws:policy)/(AdministratorAccess|PowerUserAccess|IAMFullAccess)$"
| extend SensitiveKey = tolower(coalesce(iff(isnotempty(PolicyArn), PolicyArn, ""), TargetRoleOrUser))
| join kind=leftouter SensitiveCatalog on $left.RecipientAccountId == $right.CatalogAccountId and $left.SensitiveKey == $right.ResourceArnPattern
| extend IsSensitiveTarget = (HasAdminWildcardDoc or HasExternalTrustPrincipal or IsAdminManagedPolicy or AdminEquivalent == "true" or Criticality in~ ("Critical", "High"))
| project TimeGenerated, AwsEventId, RecipientAccountId, AWSRegion, EventSource, EventName, UserIdentityArn,
SourceIpAddress, TargetRoleOrUser, PolicyArn, SetAsDefault, HasAdminWildcardDoc, HasExternalTrustPrincipal,
IsAdminManagedPolicy, IsSensitiveTarget, Criticality=coalesce(Criticality, iff(IsSensitiveTarget, "High", "Standard"))
| take 1000Explanation
This query is designed to identify and evaluate potentially sensitive actions related to AWS Identity and Access Management (IAM) policies, roles, and trust relationships. Here's a simplified breakdown of what the query does:
-
Define a Lookback Period: It looks back over the last 24 hours to analyze recent activities.
-
Load a Sensitive Catalog: It retrieves a list of sensitive roles and policies from a watchlist named
AWS_SensitiveRolesAndPolicies. This list includes details like account IDs, resource patterns, criticality, and whether they are equivalent to admin roles. -
Filter AWS CloudTrail Logs: It filters logs from AWS CloudTrail for specific services like IAM, STS, EC2, Lambda, ECS, and CloudFormation within the defined lookback period.
-
Extract and Parse Request Parameters: It extracts details from the request parameters, such as role names, user names, policy ARNs, and policy documents.
-
Identify Potentially Sensitive Actions:
- Checks if the policy document allows all actions (
"Effect": "Allow"with"Action": "*") which could indicate admin-like permissions. - Identifies if there are external trust relationships, such as allowing any AWS account to assume a role.
- Checks if the policy is a known admin-managed policy like
AdministratorAccess.
- Checks if the policy document allows all actions (
-
Match Against Sensitive Catalog: It joins the extracted data with the sensitive catalog to see if any actions match known sensitive roles or policies.
-
Determine Sensitivity: It flags actions as sensitive if they meet certain criteria, such as having admin-like permissions, external trust principals, or being listed in the sensitive catalog.
-
Output Results: It projects relevant information such as event time, account ID, region, event source, and whether the action is considered sensitive. It also assigns a criticality level, defaulting to "High" if the action is sensitive.
-
Limit Results: Finally, it limits the output to the first 1000 records.
In summary, this query helps identify and flag potentially risky IAM configurations and activities in AWS by comparing them against a predefined watchlist of sensitive roles and policies.