Command and KubeCTL
Real-World Kubernetes Security for Pentesters
© NCC Group 2020. All rights reserved
Hello,
Thanks you for your interest in these slides. Please feel free to share this deck internally with your teams or to friends. I built this deck in my free time so if you do, please consider tagging me on Twitter (@antitree) or dropping me an email (me@antitree.com) -- I’m also interested in having a conversation about securing Kubernetes and so feel free to provide your thoughts.
~Mark
© NCC Group 2020. All rights reserved
My goal today:
Discuss Kubernetes case studies
Walk through tools and attack chain
Discuss Components
© NCC Group 2020. All rights reserved
Technical Director and consultant at NCC Group
Created the Container Practice within the company
Perform assessments of
#ROC Hacker Community:
Mark Manning // @antitree
me
© NCC Group 2020. All rights reserved
In the beginning there was isolation and it was good!
IDEALLY: a container is:
Containers are a “security opportunity”
© NCC Group 2020. All rights reserved
And then came Kubernetes...
If you want to run containers in production you (at least) need:
�Kubernetes provides:
© NCC Group 2020. All rights reserved
© NCC Group 2020. All rights reserved
10.10.10.10/24
10.10.10.20/24
10.10.10.30/24
10.10.20.0/16
.1
.2
.3
.4
© NCC Group 2020. All rights reserved
Pods can run multiple containers
A Node is a host in the cluster
Some services will use a “sidecar” to perform inter-pod networking
A k8s “Namespace” logically separates workloads
The API is the kernel of the cluster and handles all operations within it
© NCC Group 2020. All rights reserved
The Threat Model is up for interpretation
Misconfigurations and flaws are subtle and require expertise
Humans are still the best at doing security reviews of complex systems
Challenges of Pentesting K8s
© NCC Group 2020. All rights reserved
Randomly Generated Case Studies
Ticketing is our Jam
We commit ourselves to cloud fineness!
The power maniacs!
How does their kubernetes infrastructure respond to the same exploit scenario
© NCC Group 2020. All rights reserved
Power Maverick Threat Model
Mission Statement: A power company that sells a product to do power analysis on-prem inside your own infrastructure. Provide their customers with a Kubernetes cluster.
Security expectations:
© NCC Group 2020. All rights reserved
Power Maverick Threat Model
Security controls:
./helm2 \� --host=tiller-deploy.kube-system.svc.cluster.local \� install antitree/brick
Results:
© NCC Group 2020. All rights reserved
CyberShmoo Threat Model
Mission Statement: A business designed solely to sell Shmoocon tickets. Multi-cloud architecture with teams of SREs and a need for geographically diverse availability.
Security expectations:
© NCC Group 2020. All rights reserved
Security controls:
Results:
© NCC Group 2020. All rights reserved
eKlowd Threat Model
Mission Statement: eKlowd is a geographically diverse startup. It has many developer groups that are self-managed. Each group deploys their application to centralized infrastructure.
Security expectations:
© NCC Group 2020. All rights reserved
eKlowd Threat Model
Security controls:
Results:
© NCC Group 2020. All rights reserved
Attack Chain
Find a service to exploit
Exploit it
Steal its tokens
Curl the API to find endpoints
Kubectl with compromised token
Try to deploy pod blocked
Net scan for other services
Socat tunnels to access pods
Exploit Pod across namespaces
Takeover the host
Deploy malicious privileged Pod
Krew with access_matrix
Bypass PSP
Port Forward to deployed pod
...
© NCC Group 2020. All rights reserved
© NCC Group 2020. All rights reserved
Service Account Tokens
Authentication is performed by a “service account token” and always exists under:� /var/run/secrets/kubernetes.io/serviceaccount/token
Most Pods have permission to access the KubeAPI because:
© NCC Group 2020. All rights reserved
© NCC Group 2020. All rights reserved
Attack Chain
Find a service to exploit
Exploit it
Steal its tokens
Curl the API to find endpoints
Kubectl with compromised token
Try to deploy pod blocked
Net scan for other services
Socat tunnels to access pods
Exploit Pod across namespaces
Takeover the host
Deploy malicious privileged Pod
Krew with access_matrix
Bypass PSP
Port Forward to deployed pod
...
© NCC Group 2020. All rights reserved
© NCC Group 2020. All rights reserved
Kubectl For Attacks
Kubectl can:
First tool you want to get working:
© NCC Group 2020. All rights reserved
Now that you have a token, what do you do?
What can I do in the container:
conmachi
amicontained
What can I do in the cluster:
kubectl auth can-i --list --token=XXXX
Using Compromised Tokens
© NCC Group 2020. All rights reserved
Attack Chain
Find a service to exploit
Exploit it
Steal its tokens
Curl the API to find endpoints
Kubectl with compromised token
Try to deploy pod blocked
Net scan for other services
Socat tunnels to access pods
Exploit Pod across namespaces
Takeover the host
Deploy malicious privileged Pod
Krew with access_matrix
Bypass PSP
Port Forward to deployed pod
...
© NCC Group 2020. All rights reserved
Roles and Rolebindings
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: namespace-admin
rules:
- apiGroups:
- '*'
resources:
- '*'
verbs:
- '*'
- nonResourceURLs:
- '*'
verbs:
- '*'
More about RBAC auditing http://bit.ly/nccrbac
© NCC Group 2020. All rights reserved
Attack Pods
Deploying your own Pod into a cluster could be:
If the cluster allows to use third party registries, there’s a lot of opportunity:
antitree/brick
© NCC Group 2020. All rights reserved
Attack Chain
Find a service to exploit
Exploit it
Steal its tokens
Curl the API to find endpoints
Kubectl with compromised token
Try to deploy pod blocked
Net scan for other services
Socat tunnels to access pods
Exploit Pod across namespaces
Takeover the host
Deploy malicious privileged Pod
Krew with access_matrix
Bypass PSP
Port Forward to deployed pod
...
© NCC Group 2020. All rights reserved
Pod Security Policies
Pod Security Policies (PSPs) defines the minimum level of security a Pod must follow
PSPs applied on top of Roles
We have 2 namespaces with different PSPs:
© NCC Group 2020. All rights reserved
The Kubernetes API receives a request to run a Pod
It sends the request to an admission controller which decides YES or NO based on PSP
© NCC Group 2020. All rights reserved
Privilege Escalation
A PSP cannot monitor containers to prevent something from becoming UID 0
Why do we even need to be root?
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
name: restricted
spec:
privileged: false
seLinux:
rule: RunAsAny
supplementalGroups:
rule: RunAsAny
runAsUser:
# Require the container to run without root privileges.
rule: 'MustRunAsNonRoot'
fsGroup:
rule: RunAsAny
volumes:
- '*'
© NCC Group 2020. All rights reserved
Attack Chain
Find a service to exploit
Exploit it
Steal its tokens
Curl the API to find endpoints
Kubectl with compromised token
Try to deploy pod blocked
Net scan for other services
Socat tunnels to access pods
Exploit Pod across namespaces
Takeover the host
Deploy malicious privileged Pod
Krew with access_matrix
Bypass PSP
Port Forward to deployed pod
...
© NCC Group 2020. All rights reserved
© NCC Group 2020. All rights reserved
Kubernetes Namespaces
Our Pod exists within a namespace:
Simply a logical group of Kubernetes objects within the cluster.
Creates the opportunity for inter-pod communications
10.10.10.10/24
.1
.2
.3
.4
© NCC Group 2020. All rights reserved
Attack Chain
Find a service to exploit
Exploit it
Steal its tokens
Curl the API to find endpoints
Kubectl with compromised token
Try to deploy pod blocked
Net scan for other services
Socat tunnels to access pods
Exploit Pod across namespaces
Takeover the host
Deploy malicious privileged Pod
Krew with access_matrix
Bypass PSP
Port Forward to deployed pod
...
© NCC Group 2020. All rights reserved
© NCC Group 2020. All rights reserved
Attack Chain
Find a service to exploit
Exploit it
Steal its tokens
Curl the API to find endpoints
Kubectl with compromised token
Try to deploy pod blocked
Net scan for other services
Socat tunnels to access pods
Exploit Pod across namespaces
Takeover the host
Deploy malicious privileged Pod
Krew with access_matrix
Bypass PSP
Port Forward to deployed pod
...
© NCC Group 2020. All rights reserved
Socat to Internal Services
We’ve deployed a malicious Pod into the Secure namespace our options are:
Socat: Can be used to create a proxy to other services
kubectl run -n secure --restart=Never \
--image=alpine/socat socat1 \
-- -d -d tcp-listen:9999,fork,reuseaddr \
tcp-connect:10.24.2.3:5000
© NCC Group 2020. All rights reserved
Krew
Kubectl is the base tool to interact with a cluster
Krew is a repository of plugins to extend it
Net-forward is a kubectl plugin listed on Krew that lets you make arbitrary TCP connections to any IP the cluster can access
More details about net-forward: http://bit.ly/nccfwd
© NCC Group 2020. All rights reserved
© NCC Group 2020. All rights reserved
Privilege Escalation From Pod To Host
apiVersion: v1
kind: Pod
metadata:
name: brick-privpod
spec:
containers:
- name: brick
image: gcr.io/shmoocon-talk-hacking/brick
volumeMounts:
- mountPath: /chroot
name: host
securityContext:
runAsUser: 999
privileged: true
volumes:
- name: host
hostPath:
path: /
type: Directory
© NCC Group 2020. All rights reserved
Attack Chain
Find a service to exploit
Exploit it
Steal its tokens
Curl the API to find endpoints
Kubectl with compromised token
Try to deploy pod blocked
Net scan for other services
Socat tunnels to access pods
Exploit Pod across namespaces
Takeover the cluster
Deploy malicious privileged Pod
Krew with access_matrix
Bypass PSP
Port Forward to deployed pod
...
© NCC Group 2020. All rights reserved
© NCC Group 2020. All rights reserved
eKlowd Security Issues
© NCC Group 2020. All rights reserved
Future Topics To Dive Into
Service Mesh Restrictions:
Exfiltration and Evasion
Practical Compromising:
eBPF, a Defense Feature or Attacker Tool:
© NCC Group 2020. All rights reserved
Case Study Threat Models
Ticketing is our Jam
We commit ourselves to cloud fineness!
The power maniacs!
Do any of these organizations have the same security assumptions?
© NCC Group 2020. All rights reserved
Where is the security boundary?
© NCC Group 2020. All rights reserved
Conclusions
Kubernetes offers features that hopefully can fit with your threat model (not always true)
We’re not talking about 0days and CVE’s, we’re talking about design decisions
�
© NCC Group 2020. All rights reserved
NCC Group Tools and Research
Keyctl-unmask: exploit lack of seccomp policies to steal the Linux kernel keyrings
go-pillage-registries: Extract secrets from the metadata of Docker registries at scale
Net-forward: Create tunnels through Kubernetes clusters to arbitrary hosts
RBAC tools: Discussion on using different tools for analyzing RBAC policies:
© NCC Group 2020. All rights reserved
Thank you!
@antitree
Email: mark.manning@nccgroup.com
https://www.antitree.com
Jack Leadford @randohacker
Rory McCune @raesene
Ian Coldwater @IanColdwater
Brad Geesaman @BradGeesaman
Chris Le Roy @brompwnie
Greg Castle @mrgcastle
Tim Allclair @tallclair
Maya @MayaKaczorowski
Jay Beale @jaybeale
© NCC Group 2020. All rights reserved