Query Details

P1 04b Eks Audit Guardduty Cloudtrail Correlated

Query

// Standalone KQL for Analytic Rule: AWS SecOps P1-04b - EKS Audit Risk Correlated with GuardDuty Container Findings or CloudTrail EKS Role Changes
// Rule File: analytics/P1-04b-eks-audit-guardduty-cloudtrail-correlated.yaml | Rule ID: REPLACE_WITH_RULE_GUID | Severity: High
// Schedule: 15m / Lookback: 2h | REVIEW ONLY - Do not deploy/schedule without approval.
// 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 KQL query is designed to identify potential security risks in AWS EKS (Elastic Kubernetes Service) environments by correlating data from three different sources: AWS CloudWatch, AWS GuardDuty, and AWS CloudTrail. Here's a simplified breakdown of what the query does:

  1. EKS Audit Logs (EksRisk):

    • It retrieves audit logs from AWS CloudWatch for the past hour, focusing on Kubernetes events.
    • It filters for specific actions that might indicate risky behavior, such as executing commands in pods, creating or updating role bindings, or running privileged pods.
    • It summarizes these actions by cluster name, username, and source IP address.
  2. GuardDuty Findings (GdEks):

    • It collects findings from AWS GuardDuty for the past two hours.
    • It filters out archived findings and summarizes the types of activities detected and their severity by cluster name.
  3. CloudTrail EKS Role Changes (EksControlPlane):

    • It examines AWS CloudTrail logs for the past two hours for specific EKS-related API calls that could indicate changes to the control plane configuration.
    • It summarizes these API changes by cluster name.
  4. Correlation and Output:

    • The query joins the results from the three data sources based on the cluster name.
    • It filters to include only those clusters that have either GuardDuty findings or CloudTrail API changes.
    • It projects a summary of the findings, including the AWS account ID, cluster name, username, source IP, and summaries of Kubernetes actions, GuardDuty findings, and CloudTrail API changes.

Overall, this query helps security teams identify clusters with potentially risky activities by correlating audit logs, security findings, and control plane changes.