Query Details

28 Containers Eks Audit High Risk Events

Query

// REVIEW ONLY - AWS Security Operations Workbook Visual Query
// Tab: Containers | Panel: eks-audit-high-risk-events | Visualization: table | Mode: All
// Security Question: Did any non-controller user or service account exec into pods, launch privileged/hostPath containers, bind cluster-admin, or read secrets?
// Drill-Through: Click Username to set SelectedPrincipal and correlate with IAM AssumeRole / GuardDuty below.
// Empty/No-Data Contract: Zero EKS audit events found in AWSCloudWatch (confirm EKS control-plane audit logging to CloudWatch/S3 via discovery/D07).
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}");
union isfuzzy=true
    (datatable(TimeGenerated:datetime, Message:string)[]),
    (AWSCloudWatch | where TimeGenerated between (Start .. End) | project TimeGenerated, Message)
| 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),
    Verb = tostring(Audit.verb),
    Resource = tostring(Audit.objectRef.resource),
    Subresource = tostring(Audit.objectRef.subresource),
    Namespace = coalesce(iff(isnotempty(tostring(Audit.objectRef.namespace)), tostring(Audit.objectRef.namespace), ""), "cluster-scoped"),
    ObjectName = tostring(Audit.objectRef.name),
    SourceIp = tostring(Audit.sourceIPs[0]),
    StatusCode = toint(Audit.responseStatus.code),
    ClusterName = coalesce(
        iff(isnotempty(tostring(Audit.annotations["arn.amazonaws.com/cluster-name"])), tostring(Audit.annotations["arn.amazonaws.com/cluster-name"]), ""),
        iff(isnotempty(extract(@"/aws/eks/([^/]+)/cluster", 1, Message)), extract(@"/aws/eks/([^/]+)/cluster", 1, Message), ""),
        "UnknownCluster"),
    ReqText = tostring(Audit.requestObject)
| where not(Username startswith "system:node:" or Username startswith "system:kube-" or Username in ("system:apiserver", "eks:addon-manager"))
| where (Resource == "pods" and Subresource in ("exec", "attach", "portforward"))
    or (Resource == "pods" and ReqText matches regex @"(?i)(""privileged""\s*:\s*true|""hostNetwork""\s*:\s*true|""hostPID""\s*:\s*true|""hostPath""\s*:)")
    or (Resource in ("clusterrolebindings", "rolebindings") and Verb in ("create", "update", "patch"))
    or (Resource == "secrets" and Verb in ("get", "list"))
    or Username == "system:anonymous"
| summarize Events=count(), FirstSeen=min(TimeGenerated), LastSeen=max(TimeGenerated),
    Objects=make_set(ObjectName, 10), SourceIps=make_set(SourceIp, 5), StatusCodes=make_set(StatusCode, 5)
    by ClusterName, Namespace, Username, Verb, Resource, Subresource
| order by LastSeen desc
| take 5000

Explanation

This query is designed to monitor and identify potentially risky activities in an Amazon EKS (Elastic Kubernetes Service) environment by analyzing audit logs. Here's a simplified breakdown of what the query does:

  1. Time and Filters Setup: It sets up the time range and various filters (like organization, account, region, environment, workload, severity, source, selected principal, and IP) to narrow down the data being analyzed.

  2. Data Source: It pulls data from AWS CloudWatch logs within the specified time range.

  3. Event Filtering: The query looks for specific Kubernetes audit events that are marked as "ResponseComplete" and extracts relevant details from these events.

  4. User and Action Filtering: It filters out events performed by system users (like nodes and kube-system components) and focuses on actions by non-controller users or service accounts. Specifically, it looks for:

    • Executing commands inside pods.
    • Launching containers with elevated privileges or using host resources.
    • Creating or modifying cluster role bindings.
    • Accessing secrets.
    • Actions by anonymous users.
  5. Data Aggregation: It summarizes the findings by counting the number of events, noting the first and last occurrence, and listing affected objects, source IPs, and status codes for each combination of cluster, namespace, user, action, and resource.

  6. Output: The results are ordered by the most recent activity and limited to the top 5000 entries.

This query helps security teams identify unauthorized or risky actions in their Kubernetes clusters, allowing them to take appropriate measures to secure their environment.