Kubernetes Privileged Pod Created


Description

Detects creation of privileged pods across Kubernetes clusters. Privileged pods have full access to the host's namespace and devices, have the ability to exploit the kernel, have dangerous linux capabilities, and can be a powerful launching point for further attacks. In the event of a successful container escape where a user is operating with root privileges, the attacker retains this role on the node.

Query · python

from panther_kubernetes_helpers import (
    get_pod_context_fields,
    get_pod_name,
    is_failed_request,
    is_privileged_container,
    is_system_namespace,
    is_system_principal,
    k8s_alert_context,
)


def rule(event):
    verb = event.udm("verb")
    resource = event.udm("resource")
    namespace = event.udm("namespace")
    username = event.udm("username")
    response_status = event.udm("responseStatus")

    # Only check pod creation events
    if verb != "create" or resource != "pods":
        return False

    # Skip failed requests
    if is_failed_request(response_status):
        return False

    # Exclude system principals creating pods in system namespaces (legitimate)
    # but alert on system principals in user namespaces (malicious Deployments)
    # and alert on user-created pods in system namespaces (suspicious)
    if is_system_principal(username) and is_system_namespace(namespace):
        return False

    # Check request object for privileged containers
    containers = event.udm("containers") or []
    if is_privileged_container(containers):
        return True

    return False


def title(event):
    username = event.udm("username") or "<UNKNOWN_USER>"
    namespace = event.udm("namespace") or "<UNKNOWN_NAMESPACE>"
    name = get_pod_name(event)

    return f"[{username}] created a privileged pod [{namespace}/{name}]"


def dedup(event):
    username = event.udm("username") or "<UNKNOWN_USER>"
    return f"privileged_pod_{username}"


def alert_context(event):
    return k8s_alert_context(event, extra_fields=get_pod_context_fields(event))

Analyst notes

  1. Check if the username who created the privileged pod has a history of deploying system infrastructure in the past 90 days
  2. Review all API calls by this username in the 6 hours before the alert to understand context and related activity
  3. Query for other privileged pods created by the same user in the past 30 days to identify if this is an established pattern
Raw source Kubernetes Privileged Pod Created · Panther Python
Esc
Published by panther-labs/panther-analysis ↗, licensed under Apache 2.0 ↗. Reproduced here unmodified.
AnalysisType: rule
RuleID: "Kubernetes.Pod.Privileged.Created"
DisplayName: "Kubernetes Privileged Pod Created"
Enabled: true
Filename: k8s_privileged_pod_created.py
LogTypes:
  - Amazon.EKS.Audit
  - Azure.MonitorActivity
  - GCP.AuditLog
Severity: High
Description: >
  Detects creation of privileged pods across Kubernetes clusters. Privileged pods have full access
  to the host's namespace and devices, have the ability to exploit the kernel, have dangerous linux
  capabilities, and can be a powerful launching point for further attacks. In the event of a
  successful container escape where a user is operating with root privileges, the attacker retains
  this role on the node.
Runbook: |
  1. Check if the username who created the privileged pod has a history of deploying system infrastructure in the past 90 days
  2. Review all API calls by this username in the 6 hours before the alert to understand context and related activity
  3. Query for other privileged pods created by the same user in the past 30 days to identify if this is an established pattern
Reference: https://kubernetes.io/docs/concepts/security/pod-security-standards/#restricted
Reports:
  Stratus Red Team:
    - k8s.privilege-escalation.privileged-pod
  MITRE ATT&CK:
    - TA0004:T1548.003 # Abuse Elevation Control Mechanism: Sudo and Sudo Caching
DedupPeriodMinutes: 360
Tags:
  - Kubernetes
  - Security Control
  - Privilege Escalation
  - Unified Detection
Tests:
  - Name: EKS Privileged Pod Created
    ExpectedResult: true
    Log:
      {
        "kind": "Event",
        "apiVersion": "audit.k8s.io/v1",
        "level": "RequestResponse",
        "auditID": "abc-123",
        "stage": "ResponseComplete",
        "requestURI": "/api/v1/namespaces/default/pods",
        "verb": "create",
        "user": {
          "username": "john.doe@company.com",
          "groups": ["system:authenticated"]
        },
        "sourceIPs": ["1.2.3.4"],
        "userAgent": "kubectl/v1.28.0",
        "objectRef": {
          "resource": "pods",
          "namespace": "default",
          "name": "test-privileged-pod",
          "apiVersion": "v1"
        },
        "responseStatus": {"code": 201},
        "requestObject": {
          "kind": "Pod",
          "apiVersion": "v1",
          "metadata": {"name": "test-privileged-pod", "namespace": "default"},
          "spec": {
            "containers": [{
              "name": "nginx",
              "image": "nginx",
              "securityContext": {"privileged": true}
            }]
          }
        },
        "p_log_type": "Amazon.EKS.Audit",
        "p_source_label": "eks-cluster",
        "p_event_time": "2024-02-13 12:45:06.073"
      }
  - Name: AKS Privileged Pod Created
    ExpectedResult: true
    Log:
      {
        "p_log_type": "Azure.MonitorActivity",
        "category": "kube-audit",
        "operationName": "Microsoft.ContainerService/managedClusters/diagnosticLogs/Read",
        "resourceId": "/subscriptions/xxx/resourceGroups/rg/providers/Microsoft.ContainerService/managedClusters/cluster",
        "properties": {
          "log": "{\"kind\":\"Event\",\"apiVersion\":\"audit.k8s.io/v1\",\"level\":\"RequestResponse\",\"auditID\":\"abc-123\",\"stage\":\"ResponseComplete\",\"requestURI\":\"/api/v1/namespaces/default/pods\",\"verb\":\"create\",\"user\":{\"username\":\"admin@example.com\",\"groups\":[\"system:authenticated\"]},\"sourceIPs\":[\"10.0.0.1\"],\"userAgent\":\"kubectl/v1.28.0\",\"objectRef\":{\"resource\":\"pods\",\"namespace\":\"default\",\"name\":\"test-pod\",\"apiVersion\":\"v1\"},\"responseStatus\":{\"code\":201},\"requestObject\":{\"kind\":\"Pod\",\"apiVersion\":\"v1\",\"metadata\":{\"name\":\"test-pod\"},\"spec\":{\"containers\":[{\"name\":\"nginx\",\"image\":\"nginx\",\"securityContext\":{\"privileged\":true}}]}}}"
        },
        "p_source_label": "aks-cluster",
        "p_event_time": "2024-03-20 10:00:00.000"
      }
  - Name: GCP GKE Privileged Pod Created
    ExpectedResult: true
    Log:
      {
        "logName": "projects/test-project/logs/cloudaudit.googleapis.com%2Factivity",
        "protoPayload": {
          "at_sign_type": "type.googleapis.com/google.cloud.audit.AuditLog",
          "authenticationInfo": {"principalEmail": "john.doe@company.com"},
          "authorizationInfo": [{
            "granted": true,
            "permission": "io.k8s.core.v1.pods.create",
            "resource": "core/v1/namespaces/default/pods/test-privileged-pod"
          }],
          "methodName": "io.k8s.core.v1.pods.create",
          "request": {
            "@type": "core.k8s.io/v1.Pod",
            "apiVersion": "v1",
            "kind": "Pod",
            "metadata": {"name": "test-privileged-pod", "namespace": "default"},
            "spec": {
              "containers": [{
                "image": "nginx",
                "name": "nginx",
                "securityContext": {"privileged": true}
              }]
            }
          },
          "requestMetadata": {"callerIP": "1.2.3.4"},
          "resourceName": "core/v1/namespaces/default/pods/test-privileged-pod",
          "response": {
            "@type": "core.k8s.io/v1.Pod",
            "spec": {
              "containers": [{
                "image": "nginx",
                "name": "nginx",
                "securityContext": {"privileged": true}
              }]
            }
          },
          "serviceName": "k8s.io",
          "status": {}
        },
        "resource": {
          "labels": {
            "cluster_name": "gke-cluster",
            "project_id": "test-project"
          },
          "type": "k8s_cluster"
        },
        "p_log_type": "GCP.AuditLog",
        "p_source_label": "gke-cluster",
        "timestamp": "2024-02-13 12:45:06.073"
      }
  - Name: Run-As-Root Pod Created
    ExpectedResult: true
    Log:
      {
        "kind": "Event",
        "apiVersion": "audit.k8s.io/v1",
        "level": "RequestResponse",
        "auditID": "abc-123",
        "stage": "ResponseComplete",
        "requestURI": "/api/v1/namespaces/default/pods",
        "verb": "create",
        "user": {"username": "john.doe@company.com"},
        "sourceIPs": ["1.2.3.4"],
        "objectRef": {
          "resource": "pods",
          "namespace": "default",
          "name": "test-pod",
          "apiVersion": "v1"
        },
        "responseStatus": {"code": 201},
        "requestObject": {
          "kind": "Pod",
          "spec": {
            "containers": [{
              "name": "nginx",
              "image": "nginx",
              "securityContext": {"runAsNonRoot": false}
            }]
          }
        },
        "p_log_type": "Amazon.EKS.Audit",
        "p_source_label": "eks-cluster"
      }
  - Name: Pod with runAsUser=0 (root)
    ExpectedResult: true
    Log:
      {
        "kind": "Event",
        "apiVersion": "audit.k8s.io/v1",
        "verb": "create",
        "user": {"username": "developer@company.com"},
        "sourceIPs": ["1.2.3.4"],
        "objectRef": {
          "resource": "pods",
          "namespace": "default",
          "name": "root-user-pod",
          "apiVersion": "v1"
        },
        "responseStatus": {"code": 201},
        "requestObject": {
          "kind": "Pod",
          "spec": {
            "containers": [{
              "name": "nginx",
              "image": "nginx",
              "securityContext": {"runAsUser": 0}
            }]
          }
        },
        "p_log_type": "Amazon.EKS.Audit",
        "p_source_label": "eks-cluster"
      }
  - Name: Non-Privileged Pod Created
    ExpectedResult: false
    Log:
      {
        "kind": "Event",
        "apiVersion": "audit.k8s.io/v1",
        "verb": "create",
        "user": {"username": "john.doe@company.com"},
        "sourceIPs": ["1.2.3.4"],
        "objectRef": {
          "resource": "pods",
          "namespace": "default",
          "name": "test-pod"
        },
        "responseStatus": {"code": 201},
        "requestObject": {
          "kind": "Pod",
          "spec": {
            "containers": [{
              "name": "nginx",
              "image": "nginx",
              "securityContext": {}
            }]
          }
        },
        "p_log_type": "Amazon.EKS.Audit"
      }
  - Name: Error Creating Pod
    ExpectedResult: false
    Log:
      {
        "kind": "Event",
        "verb": "create",
        "objectRef": {"resource": "pods", "namespace": "default"},
        "responseStatus": {"code": 409, "status": "Failure"},
        "requestObject": {
          "kind": "Pod",
          "spec": {
            "containers": [{
              "name": "nginx",
              "securityContext": {"privileged": true}
            }]
          }
        },
        "p_log_type": "Amazon.EKS.Audit"
      }
  - Name: Privileged Pod in System Namespace (Excluded)
    ExpectedResult: false
    Log:
      {
        "kind": "Event",
        "verb": "create",
        "user": {"username": "system:serviceaccount:kube-system:deployment-controller"},
        "objectRef": {
          "resource": "pods",
          "namespace": "kube-system",
          "name": "network-agent"
        },
        "responseStatus": {"code": 201},
        "requestObject": {
          "kind": "Pod",
          "spec": {
            "containers": [{
              "name": "agent",
              "securityContext": {"privileged": true}
            }]
          }
        },
        "p_log_type": "Amazon.EKS.Audit"
      }


# ------ paired body: k8s_privileged_pod_created.py ------

from panther_kubernetes_helpers import (
    get_pod_context_fields,
    get_pod_name,
    is_failed_request,
    is_privileged_container,
    is_system_namespace,
    is_system_principal,
    k8s_alert_context,
)


def rule(event):
    verb = event.udm("verb")
    resource = event.udm("resource")
    namespace = event.udm("namespace")
    username = event.udm("username")
    response_status = event.udm("responseStatus")

    # Only check pod creation events
    if verb != "create" or resource != "pods":
        return False

    # Skip failed requests
    if is_failed_request(response_status):
        return False

    # Exclude system principals creating pods in system namespaces (legitimate)
    # but alert on system principals in user namespaces (malicious Deployments)
    # and alert on user-created pods in system namespaces (suspicious)
    if is_system_principal(username) and is_system_namespace(namespace):
        return False

    # Check request object for privileged containers
    containers = event.udm("containers") or []
    if is_privileged_container(containers):
        return True

    return False


def title(event):
    username = event.udm("username") or "<UNKNOWN_USER>"
    namespace = event.udm("namespace") or "<UNKNOWN_NAMESPACE>"
    name = get_pod_name(event)

    return f"[{username}] created a privileged pod [{namespace}/{name}]"


def dedup(event):
    username = event.udm("username") or "<UNKNOWN_USER>"
    return f"privileged_pod_{username}"


def alert_context(event):
    return k8s_alert_context(event, extra_fields=get_pod_context_fields(event))

Detection rules belong to the projects that publish them and remain under their own licenses. This site indexes and links to them; it claims no rights in them.