๐ณdocker pentesting
Docker Pentesting
Theory
Linux Containers
Containers have been popular within many of platform as a service (PaaS) o๏ฌerings since the late 2000s. Docker is a software package for building cross-platform container solutions. Linux, however, has some unique properties that allow both proprietary container software and various other container technologies to exist.
Some of the more commonly used technologies include the following:
chroot, which is a technology that changes the root directory for a process and its children
Union mount ๏ฌle systems, such as Overlay2, Overlay, 4 and Aufs
Container Internals
Containers were not based on any standard when they were ๏ฌrst conceived of; in fact, the Open Container Initiative (OCI5) was established in 2015 by the Docker company. Prior to 2015, many container frameworks created their own standards for how to interact with the kernel. This has led to many di๏ฌerent types of container runtimes in one form or another over the last several years. Regardless of the di๏ฌerences in Linux containers, many of the initial constructs remain the same.
Cgroups
Starting in version 2.6.24 of the Linux Kernel, a functionality known as control groups, or cgroups for short, was released. The latest release of cgroups (cgroups v2) was introduced in Kernel 4.5 and brings security enhancements to the system. Control groups are a series of kernel-level resource controls for processes that can include the ability to limit resources, such as CPU, network, and disk, and isolate those resources from one another.
Namespaces
Namespaces, similar to how programming like C++ use them, allow for a process or collection of kernel control objects to be grouped together. This grouping limits or controls what that process or object can see.
To leverage the namespace, we can use a set of APIs that are exposed by the kernel itself:
clone() This will clone a process and then create the appropriate namespace for it.
setns() This allows an existing process to move into a namespace that we may be able to use.
unshare() This moves the process out of a namespace.
Storage
The mechanism that Docker and several other container runtimes use is known as a union ๏ฌle system (UnionFS).
There are several union ๏ฌle systems in existence today, such as Aufs and OverlayFS. Overlay2 is the current ๏ฌlesystem which uses a technology that merges di๏ฌerent directories together to form a consolidated ๏ฌlesystem.
Container Security
Containers are designed to be a โpoint-in-timeโ style of system that doesnโt change. This is a bene๏ฌt for software developers to continue to support applications that had been compiled on much older versions of software, but itโs also an advantage for attackers who want to leverage older vulnerabilities in software.
Practical
Create a docker container
Edit the Dockerfile
Build and Run container
Most of the containers you encounter will have strange hostnames attached them. The standard Docker container will have the last part of the SHA-256 hash that is used as the cgroup marker on the host. This may become important later when you want to locate the running processes.
View the cgroup location
Docker API commands
Container Storage
letโs begin by creating a very simple container that when run will give us a shell:
The Docker๏ฌle we create will also be of Debian:bullseye- slim. Using the OverlayFS, this layer should match the existing container, and only changes should be appended:
Build and Run the container
Look for Docker Daemons
To explore this further, letโs ๏ฌrst look at the API without the client:
Interact with docker API
View the remote host's processes
Execute Commands Remotely
env Command
The HOSTNAME=: string that follows is in 8-digit hex format, indicative of a Docker container naming convention
The PYTHON_VERSION= string indicates that the container is speci๏ฌcally only for Python or a Python- based service, like ๏ฌask.
ps command
mount command
netstat command
Pivoting
We can move laterally in an environment several ways, including setting up port forwards and proxies. We can also just bring down binaries to help us move further in an environment until we need to perform an additional direct pivot.
OS details
Install redis for alpine linux
Run redis-cli
Breaking Out of Containers
Privileged Pods
Letโs execute a Docker container using the privileged command and passing in devices from the host.
Abusing Cgroups
Why is this possible? How can we accomplish these commands? First of all, the cgroups v1 system has to be in place, which will be in place for quite some time, as the Linux Kernel did not introduce cgroups v2 until Kernel version 4.5 and was not in major distribution until Fedora 31. Ubuntu 18.04 LTS and 20.04 LTS still use cgroups v1, as do many RedHat distributions still in use today. The next thing we need is either the --privilege ๏ฌag or the kernel capabilities that enable mount. This is just one example of many in which a kernel attack can lead to system compromise. Have you ever seen any kernel exploits in the wild that may also be a vehicle for this?
REFERENCES
www.kernel.org/doc/html/latest/admin-guide/cgroup-v1/cgroups.html
www.netdevconf.info/1.1/proceedings/slides/rosen-namespaces-cgroups-lxc.pdf
www.kernel.org/doc/Documentation/cgroup-v1/cgroups.txt
www.github.com/opencontainers/image-spec/blob/master/spec.md
www.kernel.org/doc/Documentation/cgroup-v2.txt
www.docs.docker.com/engine/api/v1.41/
www.twitter.com/_fel1x/status/1151487051986087936