From 8daf93c744b681b1a8865e643a1130e8ecd4f184 Mon Sep 17 00:00:00 2001 From: Kev Date: Sun, 26 Oct 2025 18:43:07 +0100 Subject: [PATCH 1/2] add docker-compose dev stack, Dockerfile, scripts --- docker/.env.example | 8 ++++++ docker/Dockerfile | 23 ++++++++++++++++ docker/docker-compose.yml | 57 +++++++++++++++++++++++++++++++++++++++ docker/install.sh | 11 ++++++++ docker/pcap.sh | 4 +++ docker/run.sh | 9 +++++++ 6 files changed, 112 insertions(+) create mode 100644 docker/.env.example create mode 100644 docker/Dockerfile create mode 100644 docker/docker-compose.yml create mode 100644 docker/install.sh create mode 100644 docker/pcap.sh create mode 100644 docker/run.sh diff --git a/docker/.env.example b/docker/.env.example new file mode 100644 index 00000000000..ac45eb4a119 --- /dev/null +++ b/docker/.env.example @@ -0,0 +1,8 @@ +WEB_PORT=8000 +RESULT_PORT=2042 +PG_PORT=5432 +MONGO_PORT=27017 + +POSTGRES_USER=cape +POSTGRES_PASSWORD=cape +POSTGRES_DB=cape diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 00000000000..691ab1f0f8b --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,23 @@ +FROM mirror.gcr.io/library/python:3.11-bookworm + +RUN apt-get update \ + && apt-get upgrade -y \ + && apt-get install -y --no-install-recommends git libgraphviz-dev tcpdump libcap2-bin iproute2 + +RUN useradd -ms /bin/bash cape + +COPY pcap.sh pcap.sh +RUN bash pcap.sh + +COPY install.sh install.sh +RUN bash install.sh + +WORKDIR /cape + +RUN bash conf/copy_configs.sh +RUN chown -R cape:cape /cape + +COPY run.sh run.sh + +USER cape +ENTRYPOINT ["bash", "run.sh"] \ No newline at end of file diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml new file mode 100644 index 00000000000..770bf61407b --- /dev/null +++ b/docker/docker-compose.yml @@ -0,0 +1,57 @@ +services: + cape-db: + image: postgres:bookworm + hostname: cape-db + restart: always + ports: + - "127.0.0.1:${PG_PORT:-5432}:5432" + environment: + POSTGRES_USER: ${POSTGRES_USER:-cape} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-cape} + POSTGRES_DB: ${POSTGRES_DB:-cape} + PGDATA: /var/lib/postgresql/data/pgdata + volumes: + - ../pg-data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-cape} -d ${POSTGRES_DB:-cape}"] + interval: 5s + timeout: 5s + retries: 10 + start_period: 30s + + + mongodb: + image: mongo:6 + command: ["--bind_ip_all"] + volumes: + - ../mongodata:/data/db + ports: + - "127.0.0.1:${MONGO_PORT:-27017}:27017" + restart: unless-stopped + healthcheck: + test: ["CMD", "mongosh", "--eval", "db.runCommand({ ping: 1 })"] + interval: 10s + timeout: 5s + retries: 12 + start_period: 20s + + cape-server: + build: ./ + hostname: cape-server + restart: unless-stopped + depends_on: + cape-db: + condition: service_healthy + mongodb: + condition: service_healthy + environment: + - WEB_PORT=${WEB_PORT:-8000} + ports: + - "127.0.0.1:${RESULT_PORT:-2042}:2042" # result server + - "127.0.0.1:${WEB_PORT:-8000}:8000" # web ui + volumes: + - ../custom:/cape/custom + - ../custom/conf:/cape/custom/conf + cap_add: + - NET_ADMIN + - NET_RAW \ No newline at end of file diff --git a/docker/install.sh b/docker/install.sh new file mode 100644 index 00000000000..59f1559d24b --- /dev/null +++ b/docker/install.sh @@ -0,0 +1,11 @@ +git clone https://github.com/nbdy/CAPEv2 cape +cd cape + +bash extra/yara_installer.sh +bash extra/libvirt_installer.sh + +python -m venv venv +source venv/bin/activate +pip install -r requirements.txt +pip install -r extra/optional_dependencies.txt +pip install -U flare-floss \ No newline at end of file diff --git a/docker/pcap.sh b/docker/pcap.sh new file mode 100644 index 00000000000..a83b34c0315 --- /dev/null +++ b/docker/pcap.sh @@ -0,0 +1,4 @@ +groupadd pcap +usermod -a -G pcap cape +chgrp pcap /usr/bin/tcpdump +setcap cap_net_raw,cap_net_admin=eip /usr/bin/tcpdump \ No newline at end of file diff --git a/docker/run.sh b/docker/run.sh new file mode 100644 index 00000000000..739f9420cae --- /dev/null +++ b/docker/run.sh @@ -0,0 +1,9 @@ +source venv/bin/activate +python cuckoo.py & + +cd web + +: "${WEB_PORT:=8000}" + +python manage.py migrate +python manage.py runserver 0.0.0.0:${WEB_PORT} \ No newline at end of file From fc8b3fdfecbcb346ff494d2f513527d2a8882a1e Mon Sep 17 00:00:00 2001 From: Kev Date: Sun, 26 Oct 2025 20:10:59 +0100 Subject: [PATCH 2/2] feat: resolve comments --- .dockerignore | 51 +++++++++++++++++++++++++++++++++++++++ docker/Dockerfile | 33 +++++++++++++++++-------- docker/docker-compose.yml | 17 ++++++++----- docker/install.sh | 11 --------- docker/run.sh | 12 +++++++-- 5 files changed, 95 insertions(+), 29 deletions(-) create mode 100644 .dockerignore delete mode 100644 docker/install.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000000..41a281e7f49 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,51 @@ +# Git +.git +.gitignore +.github + +# Docker +docker-compose.yml +Dockerfile +.dockerignore + +# DB +mongodata +pgdata +*.db +*.sqlite3 + +# Python +__pycache__ +*.py[cod] +*$py.class +*.so +.Python +*.egg +*.egg-info +dist +build +.eggs +.venv +venv +env + +# IDE +.vscode +.idea +*.swp +*.swo +*~ + +# Logs +*.log +logs + +# OS +.DS_Store +Thumbs.db + +# Others +.env.local +.cache +tmp +temp \ No newline at end of file diff --git a/docker/Dockerfile b/docker/Dockerfile index 691ab1f0f8b..5e8975ddc89 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,23 +1,36 @@ -FROM mirror.gcr.io/library/python:3.11-bookworm +FROM python:3.11-bookworm RUN apt-get update \ - && apt-get upgrade -y \ - && apt-get install -y --no-install-recommends git libgraphviz-dev tcpdump libcap2-bin iproute2 + && apt-get install -y --no-install-recommends git libgraphviz-dev tcpdump libcap2-bin iproute2 libjansson-dev libmagic-dev \ + && rm -rf /var/lib/apt/lists/* RUN useradd -ms /bin/bash cape -COPY pcap.sh pcap.sh -RUN bash pcap.sh +RUN pip install --no-cache-dir poetry -COPY install.sh install.sh -RUN bash install.sh +RUN poetry config virtualenvs.create false + +RUN mkdir -p /etc/poetry/bin && ln -s $(which poetry) /etc/poetry/bin/poetry +RUN mkdir -p /opt && ln -s /cape /opt/CAPEv2 WORKDIR /cape +COPY pyproject.toml poetry.lock* ./ + +RUN poetry install --no-interaction --no-ansi --no-root + +COPY . . + +RUN poetry install --no-interaction --no-ansi + +RUN pip install --no-cache-dir -U flare-floss +RUN bash extra/yara_installer.sh + +RUN bash docker/pcap.sh + RUN bash conf/copy_configs.sh RUN chown -R cape:cape /cape -COPY run.sh run.sh - USER cape -ENTRYPOINT ["bash", "run.sh"] \ No newline at end of file + +CMD ["bash", "docker/run.sh"] \ No newline at end of file diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 770bf61407b..8bbb4880305 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -2,7 +2,7 @@ services: cape-db: image: postgres:bookworm hostname: cape-db - restart: always + restart: unless-stopped ports: - "127.0.0.1:${PG_PORT:-5432}:5432" environment: @@ -11,20 +11,19 @@ services: POSTGRES_DB: ${POSTGRES_DB:-cape} PGDATA: /var/lib/postgresql/data/pgdata volumes: - - ../pg-data:/var/lib/postgresql/data + - cape-db-data:/var/lib/postgresql/data healthcheck: test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-cape} -d ${POSTGRES_DB:-cape}"] interval: 5s timeout: 5s retries: 10 start_period: 30s - mongodb: image: mongo:6 command: ["--bind_ip_all"] volumes: - - ../mongodata:/data/db + - cape-mongo-data:/data/db ports: - "127.0.0.1:${MONGO_PORT:-27017}:27017" restart: unless-stopped @@ -36,7 +35,9 @@ services: start_period: 20s cape-server: - build: ./ + build: + context: ../ + dockerfile: docker/Dockerfile hostname: cape-server restart: unless-stopped depends_on: @@ -54,4 +55,8 @@ services: - ../custom/conf:/cape/custom/conf cap_add: - NET_ADMIN - - NET_RAW \ No newline at end of file + - NET_RAW + +volumes: + cape-db-data: + cape-mongo-data: \ No newline at end of file diff --git a/docker/install.sh b/docker/install.sh deleted file mode 100644 index 59f1559d24b..00000000000 --- a/docker/install.sh +++ /dev/null @@ -1,11 +0,0 @@ -git clone https://github.com/nbdy/CAPEv2 cape -cd cape - -bash extra/yara_installer.sh -bash extra/libvirt_installer.sh - -python -m venv venv -source venv/bin/activate -pip install -r requirements.txt -pip install -r extra/optional_dependencies.txt -pip install -U flare-floss \ No newline at end of file diff --git a/docker/run.sh b/docker/run.sh index 739f9420cae..58ac6303763 100644 --- a/docker/run.sh +++ b/docker/run.sh @@ -1,9 +1,17 @@ -source venv/bin/activate +#!/bin/bash +set -e + +cd /cape + +cd web +python manage.py migrate +cd .. + python cuckoo.py & +CUCKOO_PID=$! cd web : "${WEB_PORT:=8000}" -python manage.py migrate python manage.py runserver 0.0.0.0:${WEB_PORT} \ No newline at end of file