hacktricks/forensics/basic-forensic-methodology/docker-forensics.md

144 lines
6.2 KiB
Markdown
Raw Normal View History

2022-05-01 13:25:53 +00:00
# Docker Forensics
2022-04-28 16:01:33 +00:00
<details>
2024-01-13 22:04:19 +00:00
<summary><strong>Learn AWS hacking from zero to hero with</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2024-01-13 22:04:19 +00:00
Other ways to support HackTricks:
2022-04-28 16:01:33 +00:00
2024-01-13 22:04:19 +00:00
* If you want to see your **company advertised in HackTricks** or **download HackTricks in PDF** Check the [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Get the [**official PEASS & HackTricks swag**](https://peass.creator-spring.com)
* Discover [**The PEASS Family**](https://opensea.io/collection/the-peass-family), our collection of exclusive [**NFTs**](https://opensea.io/collection/the-peass-family)
2024-02-05 02:29:11 +00:00
* **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)**.**
2024-01-13 22:04:19 +00:00
* **Share your hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-04-28 16:01:33 +00:00
</details>
2022-05-01 13:25:53 +00:00
## Container modification
2021-01-03 11:59:52 +00:00
There are suspicions that some docker container was compromised:
```bash
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cc03e43a052a lamp-wordpress "./run.sh" 2 minutes ago Up 2 minutes 80/tcp wordpress
```
2022-09-09 16:42:39 +00:00
You can easily **find the modifications done to this container with regards to the image** with:
2021-01-03 11:59:52 +00:00
```bash
docker diff wordpress
C /var
C /var/lib
C /var/lib/mysql
A /var/lib/mysql/ib_logfile0
A /var/lib/mysql/ib_logfile1
A /var/lib/mysql/ibdata1
A /var/lib/mysql/mysql
A /var/lib/mysql/mysql/time_zone_leap_second.MYI
A /var/lib/mysql/mysql/general_log.CSV
...
```
2021-11-30 16:46:07 +00:00
In the previous command **C** means **Changed** and **A,** **Added**.\
2021-01-03 11:59:52 +00:00
If you find that some interesting file like `/etc/shadow` was modified you can download it from the container to check for malicious activity with:
```bash
2022-09-09 16:42:39 +00:00
docker cp wordpress:/etc/shadow.
2021-01-03 11:59:52 +00:00
```
You can also **compare it with the original one** running a new container and extracting the file from it:
```bash
docker run -d lamp-wordpress
docker cp b5d53e8b468e:/etc/shadow original_shadow #Get the file from the newly created container
diff original_shadow shadow
```
If you find that **some suspicious file was added** you can access the container and check it:
```bash
docker exec -it wordpress bash
```
2022-05-01 13:25:53 +00:00
## Images modifications
2021-01-03 11:59:52 +00:00
When you are given an exported docker image (probably in `.tar` format) you can use [**container-diff**](https://github.com/GoogleContainerTools/container-diff/releases) to **extract a summary of the modifications**:
2021-01-03 11:59:52 +00:00
```bash
2021-01-03 12:04:12 +00:00
docker save <image> > image.tar #Export the image to a .tar file
2021-09-26 23:02:14 +00:00
container-diff analyze -t sizelayer image.tar
2021-01-03 11:59:52 +00:00
container-diff analyze -t history image.tar
2021-09-26 23:02:14 +00:00
container-diff analyze -t metadata image.tar
2021-01-03 11:59:52 +00:00
```
2021-11-30 16:46:07 +00:00
Then, you can **decompress** the image and **access the blobs** to search for suspicious files you may have found in the changes history:
2021-01-03 11:59:52 +00:00
```bash
tar -xf image.tar
```
2022-05-01 13:25:53 +00:00
### Basic Analysis
2021-12-29 12:26:06 +00:00
You can get **basic information** from the image running:
```bash
docker inspect <image>
```
You can also get a summary **history of changes** with:
```bash
docker history --no-trunc <image>
```
You can also generate a **dockerfile from an image** with:
```bash
alias dfimage="docker run -v /var/run/docker.sock:/var/run/docker.sock --rm alpine/dfimage"
dfimage -sV=1.36 madhuakula/k8s-goat-hidden-in-layers>
```
2022-05-01 13:25:53 +00:00
### Dive
2021-12-29 12:26:06 +00:00
2022-01-31 14:51:03 +00:00
In order to find added/modified files in docker images you can also use the [**dive**](https://github.com/wagoodman/dive) (download it from [**releases**](https://github.com/wagoodman/dive/releases/tag/v0.10.0)) utility:
2021-09-26 23:02:14 +00:00
```bash
#First you need to load the image in your docker repo
sudo docker load < image.tar 1
Loaded image: flask:latest
#And then open it with dive:
sudo dive flask:latest
```
2022-09-09 16:42:39 +00:00
This allows you to **navigate through the different blobs of docker images** and check which files were modified/added. **Red** means added and **yellow** means modified. Use **tab** to move to the other view and **space** to collapse/open folders.
With die you won't be able to access the content of the different stages of the image. To do so you will need to **decompress each layer and access it**.\
2021-09-26 23:02:14 +00:00
You can decompress all the layers from an image from the directory where the image was decompressed executing:
```bash
tar -xf image.tar
for d in `find * -maxdepth 0 -type d`; do cd $d; tar -xf ./layer.tar; cd ..; done
```
2022-05-01 13:25:53 +00:00
## Credentials from memory
2021-01-03 11:59:52 +00:00
Note that when you run a docker container inside a host **you can see the processes running on the container from the host** just running `ps -ef`
2022-05-01 13:25:53 +00:00
Therefore (as root) you can **dump the memory of the processes** from the host and search for **credentials** just [**like in the following example**](../../linux-hardening/privilege-escalation/#process-memory).
2022-04-28 16:01:33 +00:00
<details>
2024-01-13 22:04:19 +00:00
<summary><strong>Learn AWS hacking from zero to hero with</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2024-01-13 22:04:19 +00:00
Other ways to support HackTricks:
2022-04-28 16:01:33 +00:00
2024-01-13 22:04:19 +00:00
* If you want to see your **company advertised in HackTricks** or **download HackTricks in PDF** Check the [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Get the [**official PEASS & HackTricks swag**](https://peass.creator-spring.com)
* Discover [**The PEASS Family**](https://opensea.io/collection/the-peass-family), our collection of exclusive [**NFTs**](https://opensea.io/collection/the-peass-family)
2024-02-05 02:29:11 +00:00
* **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)**.**
2024-01-13 22:04:19 +00:00
* **Share your hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-04-28 16:01:33 +00:00
</details>