Detection rules › Panther

Kubernetes Long-Lived Service Account Token Created

Status
Experimental
Severity
informational
Group by
name, namespace, username
Compliance
Stratus Red Team k8s.persistence.create-token
Log types
Amazon.EKS.Audit, Azure.MonitorActivity
Tags
Kubernetes, Persistence, Credential Access
Reference
https://stratus-red-team.cloud/attack-techniques/kubernetes/k8s.persistence.create-token/
Source
github.com/panther-labs/panther-analysis

Detects the creation of long-lived service account tokens via the serviceaccounts/token subresource. Kubernetes 1.24+ deprecated automatic token creation, but users with appropriate permissions can still manually create non-expiring tokens for service accounts. Attackers can abuse this to establish persistent access credentials that don't expire automatically. This technique is documented by Stratus Red Team as a persistence mechanism. Note: GCP GKE does not log TokenRequest API operations in Kubernetes audit logs.

MITRE ATT&CK coverage

TacticTechniques
PersistenceT1098 Account Manipulation
Credential AccessT1552 Unsecured Credentials

Rule body yaml

AnalysisType: rule
RuleID: "Kubernetes.ServiceAccount.TokenCreated"
DisplayName: "Kubernetes Long-Lived Service Account Token Created"
Enabled: true
Status: Experimental
Filename: k8s_serviceaccount_token_created.py
LogTypes:
  - Amazon.EKS.Audit
  - Azure.MonitorActivity
Tags:
  - Kubernetes
  - Persistence
  - Credential Access
Severity: Info
Description: >
  Detects the creation of long-lived service account tokens via the serviceaccounts/token subresource. Kubernetes 1.24+
  deprecated automatic token creation, but users with appropriate permissions can still manually create non-expiring tokens
  for service accounts. Attackers can abuse this to establish persistent access credentials that don't expire automatically.
  This technique is documented by Stratus Red Team as a persistence mechanism.
  Note: GCP GKE does not log TokenRequest API operations in Kubernetes audit logs.
Runbook: |
  1. Immediately investigate the user creating the token and determine if they have a legitimate need for long-lived credentials
  2. Review the service account permissions to assess what access the token provides by checking RoleBindings and ClusterRoleBindings
  3. Search for API activity using this service account in the past 24 hours to identify any unauthorized operations
Reports:
  Stratus Red Team:
    - k8s.persistence.create-token
  MITRE ATT&CK:
    - TA0003:T1098 # Persistence: Account Manipulation
    - TA0006:T1552 # Credential Access: Unsecured Credentials
Reference: https://stratus-red-team.cloud/attack-techniques/kubernetes/k8s.persistence.create-token/
DedupPeriodMinutes: 60
SummaryAttributes:
  - username
  - namespace
  - name
  - p_source_label
Tests:
  - Name: EKS long-lived token created
    ExpectedResult: true
    Log:
      {
        "kind": "Event",
        "apiVersion": "audit.k8s.io/v1",
        "verb": "create",
        "user": {"username": "attacker@example.com"},
        "sourceIPs": ["203.0.113.42"],
        "userAgent": "kubectl/v1.28.0",
        "objectRef": {
          "resource": "serviceaccounts",
          "subresource": "token",
          "namespace": "production",
          "name": "persistence-sa",
          "apiVersion": "v1"
        },
        "responseStatus": {"code": 201},
        "p_log_type": "Amazon.EKS.Audit",
        "p_source_label": "eks-cluster"
      }
  - Name: AKS long-lived token created
    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\":\"persistence-user@example.com\"},\"sourceIPs\":[\"1.2.3.4\"],\"objectRef\":{\"resource\":\"serviceaccounts\",\"subresource\":\"token\",\"namespace\":\"default\",\"name\":\"backdoor-sa\",\"apiVersion\":\"v1\"},\"responseStatus\":{\"code\":201}}"
        },
        "p_source_label": "aks-cluster"
      }
  - Name: Token created in default namespace
    ExpectedResult: true
    Log:
      {
        "kind": "Event",
        "verb": "create",
        "user": {"username": "attacker@example.com"},
        "objectRef": {
          "resource": "serviceaccounts",
          "subresource": "token",
          "namespace": "default",
          "name": "attacker-sa"
        },
        "responseStatus": {"code": 201},
        "p_log_type": "Amazon.EKS.Audit",
        "p_source_label": "eks-cluster"
      }
  - Name: User creating token in system namespace (alert)
    ExpectedResult: true
    Log:
      {
        "kind": "Event",
        "verb": "create",
        "user": {"username": "admin@example.com"},
        "objectRef": {
          "resource": "serviceaccounts",
          "subresource": "token",
          "namespace": "kube-system",
          "name": "escalation-sa"
        },
        "responseStatus": {"code": 201},
        "p_log_type": "Amazon.EKS.Audit"
      }
  - Name: System principal in user namespace (alert)
    ExpectedResult: true
    Log:
      {
        "kind": "Event",
        "verb": "create",
        "user": {"username": "system:kube-controller-manager"},
        "objectRef": {
          "resource": "serviceaccounts",
          "subresource": "token",
          "namespace": "production",
          "name": "app-sa"
        },
        "responseStatus": {"code": 201},
        "p_log_type": "Amazon.EKS.Audit"
      }
  - Name: System principal in system namespace (excluded)
    ExpectedResult: false
    Log:
      {
        "kind": "Event",
        "verb": "create",
        "user": {"username": "system:kube-controller-manager"},
        "objectRef": {
          "resource": "serviceaccounts",
          "subresource": "token",
          "namespace": "kube-system",
          "name": "legitimate-sa"
        },
        "responseStatus": {"code": 201},
        "p_log_type": "Amazon.EKS.Audit"
      }
  - Name: Failed token creation (excluded)
    ExpectedResult: false
    Log:
      {
        "kind": "Event",
        "verb": "create",
        "user": {"username": "attacker@example.com"},
        "objectRef": {
          "resource": "serviceaccounts",
          "subresource": "token",
          "namespace": "production",
          "name": "denied-sa"
        },
        "responseStatus": {"code": 403},
        "p_log_type": "Amazon.EKS.Audit"
      }
  - Name: Service account creation (not token)
    ExpectedResult: false
    Log:
      {
        "kind": "Event",
        "verb": "create",
        "user": {"username": "admin@example.com"},
        "objectRef": {
          "resource": "serviceaccounts",
          "namespace": "production",
          "name": "new-sa"
        },
        "responseStatus": {"code": 201},
        "p_log_type": "Amazon.EKS.Audit"
      }
  - Name: Get service account (not create)
    ExpectedResult: false
    Log:
      {
        "kind": "Event",
        "verb": "get",
        "user": {"username": "admin@example.com"},
        "objectRef": {
          "resource": "serviceaccounts",
          "subresource": "token",
          "namespace": "production",
          "name": "existing-sa"
        },
        "responseStatus": {"code": 200},
        "p_log_type": "Amazon.EKS.Audit"
      }

Detection logic

Condition

not (verb ne "create" or resource ne "serviceaccounts" or subresource ne "token")
not (responseStatus is_not_null and (responseStatus.code ge "400" or (responseStatus.code ge "1" and responseStatus.code le "16")))
not (username is_not_null and (username in ["masterclient", "aksService"] or (username starts_with "system:" and username not contains "serviceaccount")) and namespace is_not_null and namespace in ["kube-system", "gke-system", "kube-node-lease", "kube-public"])

This rule also runs imperative logic the parser cannot express as a filter; the conditions above are the structured part it could extract.

Exclusions

Top-level NOT(...) conjuncts: predicates this rule actively suppresses.

FieldKindExcluded values
usernamecontainsserviceaccount
usernamestarts_withsystem:
usernameinaksService, masterclient
namespaceingke-system, kube-node-lease, kube-public, kube-system
namespaceis_not_null(no value, null check)
usernameis_not_null(no value, null check)
responseStatus.codege1
responseStatus.codele16
responseStatus.codege400
responseStatusis_not_null(no value, null check)
resourceneserviceaccounts
subresourcenetoken
verbnecreate

Indicators

Each row is a field, operator, and value that the rule matches. The corpus column counts how many other rules in the catalog look for the same combination: high numbers point to widely-used, community-vetted indicators. Blank or 1 shows that the indicator is specific to this rule.

FieldKindValues
usernamecontains
  • serviceaccount

Output fields

Fields the rule emits when it matches. Chronicle authors list these in the outcome block; they appear on the detection and $risk_score drives alerting. Sentinel / Defender XDR rules build them up through project / summarize / extend stages. Sentinel maps these into alert fields via entityMappings and customDetails; Defender XDR custom detections surface them as alert fields directly.

FieldSource
username
sourceIPs
userAgent
namespace
verb
resource
requestURI
responseStatus
clusterp_source_label
name