Man in the middle leading to root privilege escalation using hostNetwork=true (CAP_NET_RAW considered harmful)
A container with hostNetwork=true and CAP_NET_RAW can intercept and modify unencrypted traffic, including the host's requests to the cloud metadata service. By tampering with the metadata response, an attacker can inject an SSH key and gain root access on the host.
- Triaged in 39 hours
- Disclosed after 16 months
Summary by the researcher
Write up with both EKS and GKE exploits and more details available at https://blog.champtar.fr/Metadata_MITM_root_EKS_GKE/
The report
Summary:
CAP_NET_RAW capability is still included by default in K8S, leading to yet another attack.
An attacker gaining access to a hostNetwork=true container with CAP_NET_RAW capability can listen to all the traffic going through the host and inject arbitrary traffic, allowing to tamper with most unencrypted traffic (HTTP, DNS, DHCP, ...), and disrupt encrypted traffic. In many cloud deployments the host queries the metadata service at http://169.254.169.254 to get many information including the authorized ssh keys. This report contains a POC running on GKE, manipulating the metadata service responses to gain root privilege on the host. The same attack should work on all clouds using similar metadata services to provision ssh keys (Amazon / Azure / OpenStack / ...)
The goal of this report is to ask the K8S team to make a breaking change by removing CAP_NET_RAW from the default capabilities,
as it allows way too many attacks.
K8S could enable net.ipv4.ping_group_range to still let users use ping (maybe 99% of CAP_NET_RAW usage)
Kubernetes Version:
This was tested on a default GKE cluster (1.14.10-gke.36)
Steps To Reproduce:
- Create a GKE cluster
gcloud beta container --project "copper-frame-263204" clusters create "hostmitm" --zone "us-central1-c" --no-enable-basic-auth --cluster-version "1.14.10-gke.36" --machine-type "n1-standard-1" --image-type "COS" --disk-type "pd-standard" --disk-size "100" --metadata disable-legacy-endpoints=true --scopes "https://www.googleapis.com/auth/devstorage.read_only","https://www.googleapis.com/auth/logging.write","https://www.googleapis.com/auth/monitoring","https://www.googleapis.com/auth/servicecontrol","https://www.googleapis.com/auth/service.management.readonly","https://www.googleapis.com/auth/trace.append" --num-nodes "3" --enable-stackdriver-kubernetes --enable-ip-alias --network "projects/copper-frame-263204/global/networks/default" --subnetwork "projects/copper-frame-263204/regions/us-central1/subnetworks/default" --default-max-pods-per-node "110" --no-enable-master-authorized-networks --addons HorizontalPodAutoscaling,HttpLoadBalancing --enable-autoupgrade --enable-autorepair --max-surge-upgrade 1 --max-unavailable-upgrade 0
- Create a hostNetwork=true pod
kubectl apply -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata:
name: ubuntu-node
spec:
hostNetwork: true
containers:
- name: ubuntu
image: ubuntu:latest
command: [ "/bin/sleep", "inf" ]
EOF
- Copy our script
kubectl cp metadatascapy.py ubuntu-node:/metadatascapy.py
(download F869463)
- Connect to the container
kubectl exec -ti ubuntu-node -- /bin/bash
(the next commands are in the container shell)
- Install the needed packages
apt update && apt install -y python3-scapy openssh-client
- Generate an ssh key (this is the key that we are going to inject and use to ssh into the host)
ssh-keygen -t ed25519 -f /root/.ssh/id_ed25519 -N ""
- Launch the script, wait up to 2min, enjoy
python3 /metadatascapy.py
(If you see a kubeconfig and some certificates printed, it worked)
Impact
An attacker able to execute code in a hostNetwork=true container with CAP_NET_RAW capability can, in cloud deployments, easily gain root privileges on the host.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the default inclusion of CAP_NET_RAW in Kubernetes allows containers with hostNetwork=true to perform man-in-the-middle attacks on host traffic, including unencrypted metadata service requests.
Impact
An attacker with code execution in a hostNetwork=true container with CAP_NET_RAW can gain root privileges on the host in cloud deployments.
What the attacker needed
Access to a hostNetwork=true container; CAP_NET_RAW capability; cloud metadata service used for SSH keys
The fix
The Kubernetes team should remove CAP_NET_RAW from the default capabilities and enable net.ipv4.ping_group_range to allow ping without it.
How it was found
- Create a GKE cluster
- Create a hostNetwork=true pod
- Copy the attack script into the container
- Connect to the container
- Install needed packages
- Generate an SSH key
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Containers usually have limited network access, but some are given special permissions to see all network traffic on the machine. If an attacker can run code in such a container, they can intercept and change unencrypted messages, like the ones the machine sends to a cloud service to get setup information. By changing that information, they can add their own login key and take full control of the machine.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.