Kubernetes Pod with Dangerous Linux Capabilities


Description

This detection monitors for pods created with dangerous Linux capabilities such as SYS_ADMIN, NET_ADMIN, or BPF. These capabilities can enable privilege escalation, container escape, or unauthorized access to host resources. Attackers often add these capabilities to containers to bypass security restrictions and gain elevated privileges on the underlying host.

Query · python

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

# Dangerous Linux capabilities that enable privilege escalation or container escape
DANGEROUS_CAPABILITIES = {
    "SYS_ADMIN",  # Most powerful
    "NET_ADMIN",  # Network manipulation
    "BPF",  # eBPF programs
    "SYS_PTRACE",  # Process tracing
    "SYS_MODULE",  # Load kernel modules
    "DAC_READ_SEARCH",  # Bypass file read permission checks
    "DAC_OVERRIDE",  # Bypass file permission checks
}


def has_dangerous_capabilities(containers):
    """Check if any container has dangerous Linux capabilities."""
    if not containers:
        return []

    dangerous_caps_found = []

    for container in containers:
        added_caps = deep_get(container, "securityContext", "capabilities", "add", default=[])

        if added_caps:
            # Check for intersection with dangerous capabilities
            dangerous = set(added_caps) & DANGEROUS_CAPABILITIES
            if dangerous:
                dangerous_caps_found.extend(list(dangerous))

    return dangerous_caps_found


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 namespaces and system principals to reduce false positives
    if is_system_namespace(namespace) or is_system_principal(username):
        return False

    # Check for dangerous capabilities
    containers = event.udm("containers") or []
    dangerous_caps = has_dangerous_capabilities(containers)

    if dangerous_caps:
        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)

    containers = event.udm("containers") or []
    dangerous_caps = has_dangerous_capabilities(containers)
    caps_str = ", ".join(sorted(set(dangerous_caps)))

    return (
        f"[{username}] created pod [{namespace}/{name}] with dangerous capabilities "
        f"[{caps_str}]"
    )


def dedup(event):
    username = event.udm("username") or "<UNKNOWN_USER>"
    namespace = event.udm("namespace") or "<UNKNOWN_NAMESPACE>"
    return f"k8s_dangerous_caps_{username}_{namespace}"


def alert_context(event):
    containers = event.udm("containers") or []
    dangerous_caps = has_dangerous_capabilities(containers)

    context_fields = get_pod_context_fields(event)
    context_fields["dangerous_capabilities"] = sorted(set(dangerous_caps))
    return k8s_alert_context(event, extra_fields=context_fields)

Analyst notes

  1. Review all pod creation events by the username in the 24 hours before the alert to determine if this is routine deployment activity
  2. Analyze the specific capabilities granted and their legitimate business justification for the workload
  3. Search for other pods with dangerous capabilities deployed by this user across all clusters in the past 30 days
Raw source Kubernetes Pod with Dangerous Linux Capabilities · Panther Python
Esc
Published by panther-labs/panther-analysis ↗, licensed under Apache 2.0 ↗. Reproduced here unmodified.
AnalysisType: rule
RuleID: "Kubernetes.Pod.Dangerous.Capabilities"
DisplayName: "Kubernetes Pod with Dangerous Linux Capabilities"
Enabled: true
Filename: k8s_pod_dangerous_capabilities.py
LogTypes:
  - Amazon.EKS.Audit
  - Azure.MonitorActivity
  - GCP.AuditLog
Tags:
  - Kubernetes
  - Security Control
  - Privilege Escalation
  - Container Escape
  - Unified Detection
Severity: High
Description: >
  This detection monitors for pods created with dangerous Linux capabilities such as SYS_ADMIN,
  NET_ADMIN, or BPF. These capabilities can enable privilege escalation, container escape, or
  unauthorized access to host resources. Attackers often add these capabilities to containers
  to bypass security restrictions and gain elevated privileges on the underlying host.
Runbook: |
  1. Review all pod creation events by the username in the 24 hours before the alert to determine if this is routine deployment activity
  2. Analyze the specific capabilities granted and their legitimate business justification for the workload
  3. Search for other pods with dangerous capabilities deployed by this user across all clusters in the past 30 days
Reports:
  MITRE ATT&CK:
    - TA0004:T1611 # Privilege Escalation: Escape to Host
    - TA0005:T1068 # Defense Evasion: Exploitation for Privilege Escalation
Reference: >
  - https://kubernetes.io/docs/tasks/configure-pod-container/security-context/
  - https://www.dynatrace.com/news/blog/kubernetes-security-essentials-container-misconfigurations-from-theory-to-exploitation/
DedupPeriodMinutes: 60
SummaryAttributes:
  - username
  - namespace
  - p_source_label
Tests:
  - Name: EKS Pod with SYS_ADMIN Capability
    ExpectedResult: true
    Log:
      {
        "kind": "Event",
        "apiVersion": "audit.k8s.io/v1",
        "verb": "create",
        "user": {"username": "admin@example.com"},
        "sourceIPs": ["1.2.3.4"],
        "userAgent": "kubectl/v1.28.0",
        "objectRef": {
          "resource": "pods",
          "namespace": "default",
          "name": "test-pod",
          "apiVersion": "v1"
        },
        "responseStatus": {"code": 201},
        "requestObject": {
          "kind": "Pod",
          "spec": {
            "containers": [{
              "name": "test",
              "image": "nginx",
              "securityContext": {
                "capabilities": {
                  "add": ["SYS_ADMIN", "NET_RAW"]
                }
              }
            }]
          }
        },
        "p_log_type": "Amazon.EKS.Audit",
        "p_source_label": "eks-cluster"
      }
  - Name: AKS Pod with NET_ADMIN Capability
    ExpectedResult: true
    Log:
      {
        "p_log_type": "Azure.MonitorActivity",
        "category": "kube-audit",
        "operationName": "Microsoft.ContainerService/managedClusters/diagnosticLogs/Read",
        "properties": {
          "log": "{\"kind\":\"Event\",\"apiVersion\":\"audit.k8s.io/v1\",\"verb\":\"create\",\"user\":{\"username\":\"user@example.com\"},\"sourceIPs\":[\"10.0.0.1\"],\"objectRef\":{\"resource\":\"pods\",\"namespace\":\"default\",\"name\":\"network-tool\"},\"responseStatus\":{\"code\":201},\"requestObject\":{\"spec\":{\"containers\":[{\"name\":\"net\",\"securityContext\":{\"capabilities\":{\"add\":[\"NET_ADMIN\"]}}}]}}}"
        },
        "p_source_label": "aks-cluster"
      }
  - Name: GKE Pod with BPF Capability
    ExpectedResult: true
    Log:
      {
        "protoPayload": {
          "authenticationInfo": {"principalEmail": "user@company.com"},
          "authorizationInfo": [{
            "granted": true,
            "permission": "io.k8s.core.v1.pods.create",
            "resource": "core/v1/namespaces/default/pods/monitoring"
          }],
          "methodName": "io.k8s.core.v1.pods.create",
          "requestMetadata": {"callerIP": "1.2.3.4"},
          "resourceName": "core/v1/namespaces/default/pods/monitoring",
          "serviceName": "k8s.io",
          "request": {
            "kind": "Pod",
            "spec": {
              "containers": [{
                "name": "monitor",
                "image": "bpftrace:latest",
                "securityContext": {
                  "capabilities": {
                    "add": ["BPF", "SYS_PTRACE"]
                  }
                }
              }]
            }
          }
        },
        "resource": {
          "type": "k8s_cluster",
          "labels": {"project_id": "test-project"}
        },
        "p_log_type": "GCP.AuditLog",
        "p_source_label": "gke-cluster"
      }
  - Name: Pod with Safe Capabilities Only
    ExpectedResult: false
    Log:
      {
        "kind": "Event",
        "verb": "create",
        "objectRef": {"resource": "pods", "namespace": "default"},
        "responseStatus": {"code": 201},
        "requestObject": {
          "spec": {
            "containers": [{
              "securityContext": {
                "capabilities": {
                  "add": ["NET_BIND_SERVICE", "CHOWN"]
                }
              }
            }]
          }
        },
        "p_log_type": "Amazon.EKS.Audit"
      }
  - Name: Pod without Capabilities
    ExpectedResult: false
    Log:
      {
        "kind": "Event",
        "verb": "create",
        "objectRef": {"resource": "pods", "namespace": "default"},
        "responseStatus": {"code": 201},
        "requestObject": {
          "spec": {
            "containers": [{
              "name": "app",
              "image": "nginx"
            }]
          }
        },
        "p_log_type": "Amazon.EKS.Audit"
      }
  - Name: Pod Creation Failed
    ExpectedResult: false
    Log:
      {
        "kind": "Event",
        "verb": "create",
        "objectRef": {"resource": "pods", "namespace": "default"},
        "responseStatus": {"code": 403},
        "requestObject": {
          "spec": {
            "containers": [{
              "securityContext": {
                "capabilities": {"add": ["SYS_ADMIN"]}
              }
            }]
          }
        },
        "p_log_type": "Amazon.EKS.Audit"
      }


# ------ paired body: k8s_pod_dangerous_capabilities.py ------

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

# Dangerous Linux capabilities that enable privilege escalation or container escape
DANGEROUS_CAPABILITIES = {
    "SYS_ADMIN",  # Most powerful
    "NET_ADMIN",  # Network manipulation
    "BPF",  # eBPF programs
    "SYS_PTRACE",  # Process tracing
    "SYS_MODULE",  # Load kernel modules
    "DAC_READ_SEARCH",  # Bypass file read permission checks
    "DAC_OVERRIDE",  # Bypass file permission checks
}


def has_dangerous_capabilities(containers):
    """Check if any container has dangerous Linux capabilities."""
    if not containers:
        return []

    dangerous_caps_found = []

    for container in containers:
        added_caps = deep_get(container, "securityContext", "capabilities", "add", default=[])

        if added_caps:
            # Check for intersection with dangerous capabilities
            dangerous = set(added_caps) & DANGEROUS_CAPABILITIES
            if dangerous:
                dangerous_caps_found.extend(list(dangerous))

    return dangerous_caps_found


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 namespaces and system principals to reduce false positives
    if is_system_namespace(namespace) or is_system_principal(username):
        return False

    # Check for dangerous capabilities
    containers = event.udm("containers") or []
    dangerous_caps = has_dangerous_capabilities(containers)

    if dangerous_caps:
        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)

    containers = event.udm("containers") or []
    dangerous_caps = has_dangerous_capabilities(containers)
    caps_str = ", ".join(sorted(set(dangerous_caps)))

    return (
        f"[{username}] created pod [{namespace}/{name}] with dangerous capabilities "
        f"[{caps_str}]"
    )


def dedup(event):
    username = event.udm("username") or "<UNKNOWN_USER>"
    namespace = event.udm("namespace") or "<UNKNOWN_NAMESPACE>"
    return f"k8s_dangerous_caps_{username}_{namespace}"


def alert_context(event):
    containers = event.udm("containers") or []
    dangerous_caps = has_dangerous_capabilities(containers)

    context_fields = get_pod_context_fields(event)
    context_fields["dangerous_capabilities"] = sorted(set(dangerous_caps))
    return k8s_alert_context(event, extra_fields=context_fields)

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.