Query Details

AWS SecOps P1-04b - EKS Audit Risk Correlated with GuardDuty Container Findings or CloudTrail EKS Role Changes

P1 04b Eks Audit Guardduty Cloudtrail Correlated

Query

// REVIEW ONLY. Validate EKS audit destination first via discovery/D07-eks-audit-discovery.kql.
let EksRisk = AWSCloudWatch
| where TimeGenerated > ago(1h) and ingestion_time() > ago(15m)
| where Message has "\"kind\":\"Event\"" and Message has "audit.k8s.io/"
| extend Audit = parse_json(Message)
| where tostring(Audit.stage) == "ResponseComplete" and toint(Audit.responseStatus.code) < 300
| extend Username = tostring(Audit.user.username),
    Verb = tostring(Audit.verb),
    Resource = tostring(Audit.objectRef.resource),
    Subresource = tostring(Audit.objectRef.subresource),
    ClusterName = coalesce(tostring(Audit.annotations["arn.amazonaws.com/cluster-name"]), extract(@"/aws/eks/([^/]+)/cluster", 1, Message), ""),
    SourceIp = tostring(Audit.sourceIPs[0]),
    ReqText = tostring(Audit.requestObject)
| where isnotempty(ClusterName)
| where (Resource == "pods" and Subresource in ("exec", "attach", "portforward"))
    or (Resource in ("clusterrolebindings", "rolebindings") and Verb in ("create", "update", "patch"))
    or (Resource == "pods" and ReqText has "\"privileged\":true")
| summarize FirstAuditUtc=min(TimeGenerated), K8sActions=make_set(strcat(Verb, ":", Resource, "/", Subresource), 10)
    by ClusterName, Username, SourceIp;
let GdEks = AWSGuardDuty
| where TimeGenerated > ago(2h)
| summarize arg_max(TimeGenerated, *) by Id
| where tobool(ServiceDetails.archived) != true
| extend ClusterName = tostring(ResourceDetails.eksClusterDetails.name)
| where isnotempty(ClusterName)
| summarize GdFindings=make_set(ActivityType, 10), MaxGdSeverity=max(Severity), AWSAccountId=take_any(AccountId)
    by ClusterName;
let EksControlPlane = AWSCloudTrail
| where TimeGenerated > ago(2h) and EventSource == "eks.amazonaws.com" and isempty(ErrorCode)
| where EventName in ("CreateAccessEntry", "AssociateAccessPolicy", "UpdateClusterConfig", "CreateAddon")
| extend ClusterName = tostring(parse_json(RequestParameters).name)
| where isnotempty(ClusterName)
| summarize EksApiChanges=make_set(EventName, 10), CtAccountId=take_any(RecipientAccountId)
    by ClusterName;
EksRisk
| join kind=leftouter GdEks on ClusterName
| join kind=leftouter EksControlPlane on ClusterName
| where array_length(GdFindings) > 0 or array_length(EksApiChanges) > 0
| extend EffectiveAccountId = coalesce(AWSAccountId, CtAccountId, "UnknownAccount"),
    K8sActionSummary = tostring(K8sActions),
    GdSummary = tostring(coalesce(GdFindings, dynamic([]))),
    EksApiSummary = tostring(coalesce(EksApiChanges, dynamic([])))
| project TimeGenerated=FirstAuditUtc, AWSAccountId=EffectiveAccountId, ClusterName,
    AccountName=Username, SourceIpAddress=SourceIp, K8sActionSummary, GdSummary, EksApiSummary

Explanation

This query is designed to monitor and correlate suspicious activities in Amazon EKS (Elastic Kubernetes Service) environments. Here's a simplified breakdown:

  1. Purpose: The query aims to identify and correlate potentially risky actions in Kubernetes audit logs with findings from AWS GuardDuty and changes in AWS CloudTrail related to EKS roles.

  2. Data Sources: It uses data from AWS CloudWatch, AWS GuardDuty, and AWS CloudTrail, all accessed via an AWS S3 connector.

  3. Frequency and Period: The query runs every 15 minutes and looks back over the past 2 hours.

  4. Severity: The severity of the findings is classified as "High."

  5. Detection Logic:

    • EKS Audit Logs: It examines Kubernetes audit logs for specific actions like executing commands in pods, creating or updating role bindings, and creating privileged pods.
    • GuardDuty Findings: It checks for any GuardDuty findings related to the EKS clusters.
    • CloudTrail Changes: It looks for specific EKS-related changes in CloudTrail, such as access policy associations or cluster configuration updates.
  6. Correlation: The query correlates these findings by matching them based on the EKS cluster name. It identifies clusters with either GuardDuty findings or CloudTrail changes.

  7. Output: The results include details like the time of the first audit event, AWS account ID, cluster name, username, source IP address, and summaries of Kubernetes actions, GuardDuty findings, and CloudTrail changes.

  8. Incident Creation: If any suspicious activity is detected, an incident is created. The incidents are grouped by AWS account ID and cluster name, and closed incidents can be reopened if new related events are found.

  9. Version and Configuration: This is version 1.0.0 of a scheduled query, configured to create incidents based on the findings.

In summary, this query is a security measure to detect and respond to potentially malicious activities in AWS EKS environments by correlating audit logs, GuardDuty findings, and CloudTrail changes.