Containers Pentesting
Docker
Docker Architecture
Docker Daemon
The Docker Daemon, also known as the Docker server, is a critical part of the Docker platform that plays a pivotal role in container management and orchestration.
It has several essential responsibilities like:
running Docker containers
interacting with Docker containers
managing Docker containers on the host system.
Docker Clients
When we interact with Docker, we issue commands through the Docker Client, which communicates with the Docker Daemon (through a RESTful API or a Unix socket) and serves as our primary means of interacting with Docker.
Docker Compose
It is a tool that simplifies the orchestration of multiple Docker containers as a single application. It allows us to define our application's multi-container architecture using a YAML (.yaml/.yml) file. With it, we can specify the services comprising our application, their dependencies, and their configurations. We define container images, environment variables, networking, volume bindings, and other settings.
Docker Sockets
A Docker socket or Docker daemon socket is a special file that allows us and processes to communicate with the Docker daemon. This communication occurs either through a Unix socket or a network socket. By exposing the Docker socket over a network interface, we can remotely manage Docker hosts, issue commands, and control containers and other resources.
Basic commands:
Privilege escalation techniques
Docker socket is exposed
By default, it's writable by the root user and members of the docker group. Possessing write access to this socket can lead to privilege escalation. Here's a breakdown of how this can be done and alternative methods if the Docker CLI isn't available.
Download: https://master.dockerproject.org/linux/x86_64/docker
Docker Group privilege escalation
To gain root privileges through Docker, the user we are logged in with must be in the docker group. This allows him to use and control the Docker daemon. Usually, this socket is located in /var/run/docker.sock.
Docker breakout
docker exec privilege escalation
If a user possesses the permission to execute docker exec * as root (without a password, ), you can leverage it to escalate privileges and gain full control over the host system.
Docker breakout automatic tool
LXD/LXC
Linux Containers (LXC) is an operating system-level virtualization technique that allows multiple Linux systems to run in isolation from each other on a single host by owning their own processes but sharing the host system kernel for them.
To gain root privileges through LXD/LXC, the user we are logged in with must be in the lxd group.
Kubernetes
Awesome resource to get a complete understanding about kubernets:
Differences between K8 and Docker
Function
Docker
Kubernetes
Primary
Platform for containerizing Apps
An orchestration tool for managing containers
Scaling
Manual scaling with Docker swarm
Automatic scaling
Networking
Single network
Complex network with policies
Storage
Volumes
Wide range of storage options
Kubernetes architecture is primarily divided into two types of components:
The Control Plane(master node), which is responsible for controlling the Kubernetes clusterThe Worker Nodes(minions), where the containerized applications are runMaster node: The master node hosts the Kubernetes
Control Plane, which manages and coordinates all activities within the cluster and it also ensures that the cluster's desired state is maintained.Minions: execute the actual applications and they receive instructions from the Control Plane and ensure the desired state is achieved.
The
Scheduler, based on theAPI server, understands the state of the cluster and schedules new pods on the nodes accordingly. After deciding which node a pod should run on, the API server updates theetcd.
Control Plane
The Control Plane serves as the management layer. It consists of several crucial components, including:
Service
TCP Ports
etcd
2379, 2380
API server
6443
Scheduler
10251
Controller Manager
10252
Kubelet API
10250
Read-Only Kubelet API
10255
Kubernetes API
Request
Description
GET
Retrieves information about a resource or a list of resources.
POST
Creates a new resource.
PUT
Updates an existing resource.
PATCH
Applies partial updates to a resource.
DELETE
Removes a resource.
Authentication
Kubernetes supports various methods such as
client certificates,
bearer tokens
authenticating proxy
HTTP basic auth
Once the user has been authenticated, Kubernetes enforces authorization decisions using Role-Based Access Control (RBAC).
The Kubelet can be configured to permit anonymous access. By default, the Kubelet allows anonymous access. Anonymous requests are considered unauthenticated, which implies that any request made to the Kubelet without a valid client certificate will be treated as anonymous.
Understanding the container images and their versions used in the cluster can enable us to identify known vulnerabilities and exploit them to gain unauthorized access to the system.
Namespace information can provide insights into how the pods and resources are arranged within the cluster, which we can use to target specific namespaces with known vulnerabilities.
Metadata such as
uidandresourceVersionto perform reconnaissance and recognize potential targets for further attacks.
Privilege Escalation
To gain higher privileges and access the host system we have to obtain the Kubernetes service account's token and certificate (ca.crt) from the server.
We can check the access rights in the Kubernetes cluster.
We can get, create, and list pods and from here on, we can create a YAML file that we can use to create a new container and mount the entire root filesystem from the host system into this container's /root directory. From there on, we could access the host systems files and directories.
Last updated