H14 Eks Interactive Vs Controller Audit Hunt
Query
// REVIEW ONLY - Hunting Query H14: EKS Audit Interactive Human/AssumedRole vs Controller Baseline
// Distinguishes human/federated IAM-mapped Kubernetes users from cluster controllers across namespaces.
AWSCloudWatch
| where TimeGenerated > ago(24h)
| where Message has "\"kind\":\"Event\"" and Message has "audit.k8s.io/"
| extend Audit = parse_json(Message)
| where tostring(Audit.stage) == "ResponseComplete"
| extend Username = tostring(Audit.user.username),
Groups = tostring(Audit.user.groups),
Verb = tostring(Audit.verb),
Resource = tostring(Audit.objectRef.resource),
Subresource = tostring(Audit.objectRef.subresource),
Namespace = coalesce(tostring(Audit.objectRef.namespace), "cluster-scoped"),
SourceIp = tostring(Audit.sourceIPs[0]),
UserAgent = tostring(Audit.userAgent),
StatusCode = toint(Audit.responseStatus.code),
ClusterName = coalesce(tostring(Audit.annotations["arn.amazonaws.com/cluster-name"]), extract(@"/aws/eks/([^/]+)/cluster", 1, Message), "UnknownCluster")
| extend ActorClass = case(
Username startswith "system:serviceaccount:", "KubernetesServiceAccount",
Username startswith "system:", "ControlPlaneOrNodeSystem",
Username startswith "eks:", "EksManagedService",
"HumanOrFederatedIamPrincipal")
| where ActorClass in ("HumanOrFederatedIamPrincipal", "KubernetesServiceAccount")
| where Verb !in ("get", "list", "watch") or Resource in ("secrets", "pods")
| summarize Events=count(), DeniedCount=countif(StatusCode >= 400),
Actions=make_set(strcat(Verb, ":", Resource, iff(isnotempty(Subresource), strcat("/", Subresource), "")), 20),
SourceIps=make_set(SourceIp, 10), UserAgents=make_set(UserAgent, 5)
by ClusterName, ActorClass, Username, Namespace
| order by DeniedCount desc, Events desc
| take 1000Explanation
This query is designed to analyze AWS EKS (Elastic Kubernetes Service) audit logs to distinguish between actions performed by human or federated IAM-mapped Kubernetes users and those performed by cluster controllers. Here's a simplified breakdown:
-
Data Source: It pulls data from AWS CloudWatch logs for the past 24 hours.
-
Filtering: It focuses on log messages that contain Kubernetes audit events, specifically those that have completed their response stage.
-
Data Extraction: The query extracts various details from the logs, such as the username, user groups, action performed (verb), resource involved, namespace, source IP, user agent, status code, and cluster name.
-
Classification: Users are classified into categories like "KubernetesServiceAccount," "ControlPlaneOrNodeSystem," "EksManagedService," or "HumanOrFederatedIamPrincipal" based on their username patterns.
-
Focus: It narrows down to actions performed by human or federated IAM principals and Kubernetes service accounts, excluding common read-only actions unless they involve sensitive resources like secrets or pods.
-
Aggregation: The query summarizes the data by counting the number of events and denied actions, and it compiles lists of unique actions, source IPs, and user agents for each combination of cluster, user type, username, and namespace.
-
Sorting and Limiting: Results are sorted by the number of denied actions and total events, and the top 1000 entries are returned.
In essence, this query helps identify and analyze potentially suspicious or noteworthy activities by human users or service accounts in an EKS environment, focusing on actions that might indicate security concerns.