Skip to content

Instantly share code, notes, and snippets.

@BoeJaker
Last active April 19, 2023 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BoeJaker/a0cea80c42f2facc0750f92a073bc078 to your computer and use it in GitHub Desktop.
Save BoeJaker/a0cea80c42f2facc0750f92a073bc078 to your computer and use it in GitHub Desktop.
Docker Red Playground

Simple Docker-compose Playground

Docker-compose is a powerful tool that allows you to define and run multi-container Docker applications. With it, you can create and manage your own cybersecurity lab that simulates a variety of attacks and defenses in a controlled environment.

To get started, you’ll need to have Docker and docker-compose installed on your system. Once you have these installed, create a new directory and create a docker-compose.yml file inside it. In this file, you can define the containers you want to run in your lab. For example, you might want to run a container that simulates a vulnerable web application, a container that runs a vulnerable operating system, and a container that runs a network analysis tool.

Step 1 – Install Docker and docker-compose

Before you can create a cybersecurity lab using docker-compose, you need to have Docker and docker-compose installed on your system. If you haven’t already, you can download and install Docker from the official website. Once Docker is installed, you can install docker-compose by following the instructions in the official documentation.

Step 2 – Create a directory for your lab

Create a new directory on your system to store your cybersecurity lab files. You can name this directory anything you like, for example “cyberlab”.

Step 3 – Define your labs target

What kinds of attacks would you like to practice? what kind of targets would you like to simulate? the tcp/ip stack, a web app, databases? Once you have made a decision find an analogous test target-framework like owasp’s insecure web app, bWapp. you will then need to implement it as a docker container, as per our previous guide Building a Cybersecurity Lab with Docker: a Beginners Guide.

Step 4 – Choose appropriate tools

Now you have a target container you can combine an assortment of tools to test your containerized targets. You could either use tools on the containers host or create a docker image with the tools built into that and include it as part of the same compose stack as the targets.

Think about things like, do you need a VNC? Vim or nano? If your unsure of the entire tool-set you will need, that is fine, you can always start with a generic set such as that provided by kali and add more later.

Step 5 – Write a docker-compose.yml File

Inside your “cyberlab” directory, create a new file called “docker-compose.yml”. This file will contain the definitions for the containers that make up your cybersecurity lab. Here’s an example of what your docker-compose.yml file might look like:

version: '3'
services:
webapp:
    image: vulnerables/web-dvwa
    ports:
    - "80:80"
    volumes:
    - ./data:/var/www/html
    environment:
    DB_HOST: db
    MYSQL_USER: dvwa
    MYSQL_PASSWORD: p@ssw0rd
db:
    image: mysql:5.7
    volumes:
    - ./mysql:/var/lib/mysql
    environment:
    MYSQL_DATABASE: dvwa
    MYSQL_USER: dvwa
    MYSQL_PASSWORD: p@ssw0rd
    MYSQL_ROOT_PASSWORD: r00tp@ssw0rd

In this example, we’re defining two containers: “webapp” and “db”. The “webapp” container runs a vulnerable web application called DVWA, which is available on port 80. The “db” container runs a MySQL database for the web application. We’re also specifying volumes to persist data and environment variables to configure the containers.

Here are some key points to keep in mind when writing your “docker-compose.yml” file:

The “version” field specifies the version of the Docker Compose file format to use. The current version is “3”. Each service is defined as a separate block under the “services” field. You can name your services anything you like. The “image” field specifies the Docker image to use for the container. You can use images from Docker Hub, or you can build your own images using the “docker build” command. The “ports” field specifies the ports to expose on the host machine. You can map container ports to any available ports on the host machine. The “volumes” field specifies any data volumes that need to be mounted in the container. This is useful for persisting data across container restarts. The “environment” field specifies any environment variables that need to be set in the container. This is useful for configuring the container at runtime.

Step 6 – Run the lab

To run the lab, in the terminal, inside your “cyberlab” directory, file run the command

docker-compose up -d Once running you will be able to start a terminal session with

docker exec -it [Your container name here] /bin/bash Once you’ve defined your containers in the “docker-compose.yml” file, you can run your cybersecurity lab using the “docker-compose up” command. This command will start all of the containers defined in the “docker-compose.yml” file, and it will stream the logs from all of the containers to your console.

When you run the “docker-compose up” command, Docker Compose will create a new network for your containers. By default, this network will be named after the directory containing the “docker-compose.yml” file. This network allows your containers to communicate with each other using container names as hostnames.

Step 7 – Test your skills

Once your cybersecurity lab is up and running, you can start testing it to see if it behaves as expected. You can use tools like Nmap, Metasploit, and Wireshark to test your lab and simulate attacks.

You can also access your lab by opening a web browser and navigating to the IP address of your Docker host on the port that your lab’s services are running on. In the example above, the web application is running on port 80, so you would navigate to `http://localhost:80

Here are some tips for testing your cybersecurity lab:

Start with simple tests to make sure everything is working correctly. For example, you can try accessing the web application from your host machine to see if it’s up and running. Use different tools and techniques to test your lab. For example, you can use Nmap to scan your lab for open ports, or you can use Metasploit to try to exploit vulnerabilities in your lab. Keep track of your progress and any issues you encounter. This will help you refine your lab and make it more effective over time. Next Steps Sharing your cybersecurity lab can be easily accomplished by using docker-compose to create a lab. By exporting your docker-compose.yml file, you can share it with others, who can then run the same lab on their own system. One of the advantages of using docker-compose is the ability to create snapshots of your lab at different stages of your testing. These snapshots can be used to restore your lab to a previous state if something goes wrong.

To create an all-in-one playground for testing, you can incorporate the custom image from the guide “Building a Cybersecurity Lab with Docker: a Beginners Guide.” This image can serve as a starting point for expanding and customizing the lab to meet specific testing needs.

Rather than using pre-built insecure web applications, it is recommended to design and deploy your own secure application into the compose stack and attempt to break into it. This provides an opportunity to practice penetration testing techniques and gain a deeper understanding of secure application development.

Conclusion

In conclusion, Docker and Docker Compose can be powerful tools for creating and managing cybersecurity labs. By utilizing containerization technology, one can easily create a flexible, scalable, and customizable environment for testing and experimentation. Sharing your cybersecurity lab with others is also made easy with Docker Compose’s ability to export and import configurations. It is also recommended to incorporate custom images and applications into the lab for a more comprehensive and realistic testing environment. By leveraging the benefits of Docker Compose, individuals and organizations can improve their cybersecurity skills and develop better defenses against cyber threats.

# Build and Run commands - execute from the root of the project folder
docker build -t cybersecurity-lab .
docker run -it --rm --net=host --privileged cybersecurity-lab
version: '3'
services:
webapp:
image: vulnerables/web-dvwa
ports:
- "80:80"
volumes:
- ./data:/var/www/html
environment:
DB_HOST: db
MYSQL_USER: dvwa
MYSQL_PASSWORD: p@ssw0rd
db:
image: mysql:5.7
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: dvwa
MYSQL_USER: dvwa
MYSQL_PASSWORD: p@ssw0rd
MYSQL_ROOT_PASSWORD: r00tp@ssw0rd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment