raesene / kube_security_lab

228 stars 41 forks source link

Kubernetes Local Security Testing Lab

The goal of this project is to make use of Docker and specifically kind to create a lab environment for testing Kubernetes exploits and security tools entirely locally on a single machine without any requirement for remote resources or Virtual Machines being spun up.

To get the flexibility to set-up the various vulnerable clusters we're using Ansible playbooks.

If you want to get an idea of how this works and where to start, there's an episode of rawkode live where we go through it all. If you run into trouble while installing the Pre-Requisites, check out this installation guide which walks you though installing all the dependencies.

Pre-requisites

Before starting you'll need to install

If you're running Ubuntu 18.04, you can use the install_ansible_ubuntu.sh file to do the ansible setup. If you're running Ubuntu 20.04 then you can just get ansible from apt.

Getting Started

  1. Start up the vulnerable cluster you want to use, from the list below. At the end of the playbook you'll get an IP address for the cluster.
  2. Start the client machine container, and exec into a shell on it
  3. For the SSH clusters (the playbooks start ssh-to-*) SSH into a pod on the cluster with ssh -p 32001 sshuser@[Kubernetes Cluster IP] and a password of sshuser
  4. Attack away :)

More detailed explanations below .

Client Machine

There's a client machine with tools for Kubernetes security testing which can be brought up with the client-machine.yml playbook. It's best to use this client machine for all CLI tasks when running the scenarios, so you don't accidentally pick up creds from the host, but remember to start the kind cluster before the client machine, or the Docker network to connect to, may not be available.

Once you've run the playbook, you can connect to the client machine with:-

docker exec -it client /bin/bash

The machine should be on the 172.18.0.0/24 network with the kind clusters (as well as being on the Docker default bridge)

Vulnerable Clusters

There's a number of playbooks which will bring up cluster's with a specific mis-configuration that can be exploited.

If you would like to choose a random scenario to test your skills, run the get-random-scenario.sh script from your project folder!

Using the clusters

Each of these can be used to try out various techniques for attacking Kubernetes clusters. In general the goal of each exercise should be to get access to the /etc/kubernetes/pki/ca.key file as that's a "golden key" to persistent cluster access.

For each cluster the place to start is in the Scenario Setups which has details of how to get started.

If you want some information on one possible solution look in the Scenario Walkthroughs folder

Cleanup

When you're finished with your cluster(s) just use:

kind get clusters

To get a list of running clusters, then:

kind delete cluster --name=[CLUSTERNAME]

to remove the kind clusters, and:

docker stop client

to remove the client container

Demo Setup

There's a specific pair of playbooks which can be useful for demonstrating Kubernetes vulnerabilities. the demo-cluster.yml brings up a kind cluster with multiple vulnerabilities and the demo-client-machine.yml brings up a client container with the Kubernetes Kubeconfig for the demo cluster already installed. For this pair, it's important to bring up the cluster before the client machine, so that the kubeconfig file is available to be installed.