🐋docker container escape

Theory

Docker is an open-source containerization platform used for developing, deploying, and managing applications in lightweight virtualized environments called containers.

Docker Architecture

Practical (Exploitation)

Checking if we're in Container

  1. List running processes

circle-info

If there are few no. of process is running then you might be in docker.

  1. Look for docker.env file

circle-info

If you see .dockerenv in base dir, then you’re in a container.

  1. Those pesky cgroups

Navigating to “/proc/1” and then catting the “cgroups” file (cat cgroup).

Use following code to Verify you are in Docker

Docker Escaping Techniques

1. Escape via Exposed Docker Daemon

Run the following cmd

If we’re in bash

If we’re in alpine

You can see the images repo

circle-info

NOTE: ubuntu:18.04 is the image repo

2. Shared Namespaces

By using ps aux you can view the process with processID see pid 1 is running root it is the first one that executed when the system is booted.

Exploiting it with nsenter

3. Escape By Mounting File System

circle-info

NOTE: In this case sda2 is the dir we mount. Might be different in your case

4. Misconfigured Privileges

list out all the capabilities

circle-info

If we get sys_admin capability, means the system is vuln.

On attacker VM:

First make a shell.sh and set python server and set listner.

On target machine:

5. Exploitation of docker.sock in /var/run or /run if you're ROOT

Check /var/run dir for docker.sock file, if it’s there and you’re root then you can exploit it. First see that you can use curl cmd, if not then wget static curl from your system for static curl see the arch of target machine and get the static curl from Resourcearrow-up-right

STEP1: Listing the images of the container of the host

STEP2: Now generate id_rsa in your machine

STEP3: Creating a new docker container with image ID

circle-info

NOTE: replace “c3:latest” with the docker image name that you’ll get from step1. eg: “RepoTags”:[“c3:latest”]

Now you’ll see you created a docker and get the id. eg: {“Id”:”c19a25c6cc7245030bf9741d300f632cc7f1e5f12adad238edce23d387ba00c2”,”Warnings”:[]}

STEP4: Now we gonna use the id and start the docker

STEP5: Login SSH via your private key as user root and now you’re root

6. Debugfs

3. Writeable Backup Scripts

If we can find any writable backup scripts in a docker instance then we can add our own reverse shell into it.

Run a netcat listener

circle-info

After the script executes you should get a root shell on the real machine.

REFERENCES

Last updated