Kubernetes Pod Accessing Service Account Tokens
Query
let Lookback = 1d;
let AllowedNamespaces = dynamic([
"kube-system",
"gatekeeper-system",
"azure-arc",
"calico-system",
"cilium"
]);
CloudProcessEvents
| where Timestamp > ago(Lookback)
| where isnotempty(KubernetesPodName)
| extend Command = tolower(ProcessCommandLine)
| where Command has_any ("/var/run/secrets/kubernetes.io/serviceaccount/token", "serviceaccount/token", "serviceaccount/ca.crt", "serviceaccount/namespace")
| where KubernetesNamespace !in (AllowedNamespaces)
| project Timestamp, Detection="K8S service account token access", AzureResourceId, KubernetesNamespace, KubernetesPodName, ContainerName, ContainerImageName, AccountName, ParentProcessName, ProcessName, ProcessCommandLine, HostNameAbout this query
Kubernetes Pod Accessing Service Account Tokens
Query Information
MITRE ATT&CK Technique(s)
| Technique ID | Title | Link |
|---|---|---|
| T1552.001 | Credentials in Files | https://attack.mitre.org/techniques/T1552/001 |
Description
Detects instances where a process within a containerized application attempts to access Kubernetes service account tokens or related files (token, ca.crt, namespace). Accessing these files from non-authorized namespaces may indicate an attempt by a compromised container to gain unauthorized access to the Kubernetes API.
Author <Optional>
- Name: Benjamin Zulliger
- Github: https://github.com/benscha/KQLAdvancedHunting
- LinkedIn: https://www.linkedin.com/in/benjamin-zulliger/
Defender XDR
Explanation
This query is designed to detect suspicious activity within a Kubernetes environment. Specifically, it looks for instances where a process inside a container tries to access Kubernetes service account tokens or related files, such as token, ca.crt, or namespace. These files are typically used for authentication and authorization with the Kubernetes API.
Here's a simple breakdown of what the query does:
-
Time Frame: It examines events from the past day (
Lookback = 1d). -
Allowed Namespaces: It defines a list of namespaces (
AllowedNamespaces) where accessing these files is considered normal and not suspicious. These include system-related namespaces likekube-systemandgatekeeper-system. -
Event Filtering: The query filters events from
CloudProcessEventsto find processes that:- Have a non-empty Kubernetes pod name.
- Execute commands that include paths to the service account token files.
- Belong to namespaces not listed in the
AllowedNamespaces.
-
Output: For each suspicious event, it provides details such as the timestamp, resource ID, namespace, pod name, container name, image name, account name, and process details.
In summary, this query helps identify potentially unauthorized access attempts to Kubernetes service account tokens by processes running in unexpected namespaces, which could indicate a security breach or misconfiguration.