diff --git a/ops/tulip/.env.example b/ops/tulip/.env.example new file mode 100644 index 0000000..d216984 --- /dev/null +++ b/ops/tulip/.env.example @@ -0,0 +1,23 @@ +# The UI is loopback-only by default. Use the VPS VPN address for remote team access. +TULIP_BIND_ADDRESS=127.0.0.1 +TULIP_PORT=3000 + +# Persistent host directory for completed captures. Relative paths resolve here. +TRAFFIC_DIR_HOST=./data/traffic + +# Game-specific values. TICK_LENGTH is milliseconds. +TICK_START=2026-01-01T00:00:00Z +TICK_LENGTH=60000 +FLAG_LIFETIME=5 +FLAG_REGEX=[A-Z0-9]{31}= +VM_IP=10.0.0.1 + +# JSON array consumed by the API. Keep it on one line. +TULIP_SERVICES=[{"ip":"10.0.0.1","port":8080,"name":"service"}] + +# Optional capture profile. The interface must exist in the host network namespace. +CAPTURE_INTERFACE=game +CAPTURE_ROTATE_SECONDS=60 + +# PostgreSQL shared memory ceiling; it is not preallocated. +TIMESCALE_SHM_SIZE=1gb diff --git a/ops/tulip/.gitignore b/ops/tulip/.gitignore new file mode 100644 index 0000000..4226092 --- /dev/null +++ b/ops/tulip/.gitignore @@ -0,0 +1,5 @@ +.env +upstream/ +data/ +*.pcap +*.pcapng diff --git a/ops/tulip/Dockerfile.capture b/ops/tulip/Dockerfile.capture new file mode 100644 index 0000000..d6ca79d --- /dev/null +++ b/ops/tulip/Dockerfile.capture @@ -0,0 +1,9 @@ +FROM debian:12-slim@sha256:311f7a192fd9a92b165a86b32fa3aae9005b11849bfb1db2d4fd784559ee464f + +RUN apt-get update \ + && apt-get install --no-install-recommends -y tcpdump=4.99.3-1 \ + && rm -rf /var/lib/apt/lists/* + +COPY --chmod=0555 finalize-capture.sh /usr/local/bin/finalize-capture + +ENTRYPOINT ["tcpdump"] diff --git a/ops/tulip/Dockerfile.timescale b/ops/tulip/Dockerfile.timescale new file mode 100644 index 0000000..7ca617a --- /dev/null +++ b/ops/tulip/Dockerfile.timescale @@ -0,0 +1,10 @@ +FROM timescale/timescaledb:latest-pg15 + +RUN apk add --no-cache build-base make clang19 llvm19 git +COPY upstream/services/timescale/tulip /tulip +RUN cd /tulip \ + && make USE_PGXS=1 install \ + && cd / \ + && git clone --depth 1 --branch PG15 https://github.com/ossc-db/pg_hint_plan \ + && cd pg_hint_plan \ + && make USE_PGXS=1 install diff --git a/ops/tulip/README.md b/ops/tulip/README.md new file mode 100644 index 0000000..ed54492 --- /dev/null +++ b/ops/tulip/README.md @@ -0,0 +1,85 @@ +# Tulip deployment + +This is a small Docker Compose deployment of [OpenAttackDefenseTools/tulip](https://github.com/OpenAttackDefenseTools/tulip) for Debian 12 on x86_64. It follows upstream `master` at commit `523e5179043a5b6ae1bbcc1990d774f23166319b` (2025-10-06, GPL-3.0). Upstream publishes Compose build definitions rather than release images: TimescaleDB, API, assembler, and the Vite frontend are the minimum useful flow-analysis stack. Optional flag-ID scraping, Suricata enrichment, and PCAP-over-IP are deliberately omitted. + +The checkout is pinned, local images use non-`latest` names, and the capture base image is pinned to the current Debian 12 amd64 digest. Upstream Dockerfiles still use mutable language/Timescale base tags and one mutable `pg_hint_plan` branch, so builds are operationally repeatable from a fixed Tulip tree but not bit-for-bit reproducible. Preserve built images for an event and review upstream before changing the commit. + +## Required inputs + +Copy `.env.example` to `.env`, then set: + +- `TICK_START`, `TICK_LENGTH`, `FLAG_REGEX`, and `FLAG_LIFETIME` for the game. +- `VM_IP` and `TULIP_SERVICES`, a one-line JSON array of `{ip, port, name}` objects. +- `TULIP_BIND_ADDRESS`. Keep `127.0.0.1` and use an SSH tunnel, or bind the VPS VPN address. Tulip may display captured flags, so do not bind `0.0.0.0` without a separate authenticated reverse proxy and firewall policy. +- `CAPTURE_INTERFACE=game` when the OpenVPN interface exists on the same VPS. + +An empty `capture.bpf` captures all packets on `game`. Put a normal tcpdump/libpcap filter in that file to restrict capture, for example `tcp port 8080 or tcp port 8081`. + +## Prepare and start + +Run from this directory: + +```sh +cp .env.example .env +# Edit .env and capture.bpf before continuing. +git clone --filter=blob:none https://github.com/OpenAttackDefenseTools/tulip.git upstream +git -C upstream checkout --detach 523e5179043a5b6ae1bbcc1990d774f23166319b +test "$(git -C upstream rev-parse HEAD)" = 523e5179043a5b6ae1bbcc1990d774f23166319b +install -d -m 0750 data/traffic data/traffic/.spool +docker compose config --quiet +docker compose build +docker compose --profile capture up -d +``` + +If captures arrive by `rsync`, NFS, or another sniffer instead, omit the capture profile and place only completed `.pcap` files in `data/traffic`: + +```sh +docker compose up -d +``` + +The first database initialization runs the three SQL files from the pinned upstream checkout. Changing them later does not migrate an existing volume. + +## Capture privilege boundary + +Tulip itself only receives a read-only capture-directory mount. The optional `capture` service alone uses host networking to see the OpenVPN interface. It is not privileged, mounts no Docker socket or host filesystem, drops every capability, then adds `CAP_NET_RAW` for packet sockets and `CAP_NET_ADMIN` for TUN capture. `tcpdump -p` keeps promiscuous mode disabled. The process remains constrained by `no-new-privileges`. + +Captures are written under `data/traffic/.spool` and atomically moved into the watched directory only after tcpdump closes each rotation. This prevents the assembler from ingesting a partial file. + +## Verify + +```sh +docker compose --profile capture ps +docker compose --profile capture logs --tail=50 timescale api assembler frontend capture +docker compose exec -T frontend node -e "require('http').get('http://127.0.0.1:3000/api/',r=>{process.stdout.write(String(r.statusCode)+'\\n');process.exit(r.statusCode===200?0:1)}).on('error',e=>{console.error(e);process.exit(1)})" +docker compose --profile capture exec -T capture sh -c 'test -e "/sys/class/net/$CAPTURE_INTERFACE"' +find data/traffic -maxdepth 1 -type f -name '*.pcap' -printf '%TY-%Tm-%Td %TH:%TM:%TS %s %p\n' | tail +``` + +With the default bind, open `http://127.0.0.1:3000` on the VPS or tunnel it: + +```sh +ssh -N -L 3000:127.0.0.1:3000 user@vps +``` + +Health checks cover PostgreSQL readiness, the API, the frontend-to-API proxy, and capture-interface presence. The assembler has no upstream readiness endpoint; Docker restarts it if its process exits, and successful PCAP ingestion is the practical readiness check. + +## Stop and rollback + +Stop containers while preserving captures, the database volume, checkout, and images: + +```sh +docker compose --profile capture down --remove-orphans +``` + +Remove the database volume as an explicit destructive rollback, while preserving PCAPs: + +```sh +docker compose --profile capture down --volumes --remove-orphans +``` + +After confirming no event data is needed, remove local runtime inputs separately: + +```sh +rm -rf -- data/traffic upstream +rm -f -- .env +``` diff --git a/ops/tulip/capture.bpf b/ops/tulip/capture.bpf new file mode 100644 index 0000000..e69de29 diff --git a/ops/tulip/compose.yaml b/ops/tulip/compose.yaml new file mode 100644 index 0000000..949b3b0 --- /dev/null +++ b/ops/tulip/compose.yaml @@ -0,0 +1,182 @@ +name: mctf-tulip + +services: + timescale: + build: + context: . + dockerfile: Dockerfile.timescale + image: mctf-tulip-timescale:523e517 + restart: unless-stopped + shm_size: ${TIMESCALE_SHM_SIZE:-1gb} + environment: + POSTGRES_HOST_AUTH_METHOD: trust + POSTGRES_USER: tulip + POSTGRES_DB: tulip + volumes: + - timescale-data:/var/lib/postgresql/data + - ./upstream/services/schema/system.sql:/docker-entrypoint-initdb.d/100_system.sql:ro + - ./upstream/services/schema/functions.sql:/docker-entrypoint-initdb.d/101_functions.sql:ro + - ./upstream/services/schema/schema.sql:/docker-entrypoint-initdb.d/102_schema.sql:ro + networks: + - tulip-internal + healthcheck: + test: ["CMD-SHELL", "pg_isready -U tulip -d tulip"] + interval: 10s + timeout: 5s + retries: 12 + start_period: 30s + + api: + build: + context: ./upstream/services/api + dockerfile: Dockerfile-api + image: mctf-tulip-api:523e517 + restart: unless-stopped + depends_on: + timescale: + condition: service_healthy + environment: + TIMESCALE: postgres://tulip@timescale:5432/tulip + TULIP_TRAFFIC_DIR: /traffic + TULIP_SERVICES: ${TULIP_SERVICES:?set TULIP_SERVICES in .env} + FLAG_REGEX: ${FLAG_REGEX:?set FLAG_REGEX in .env} + TICK_START: ${TICK_START:?set TICK_START in .env} + TICK_LENGTH: ${TICK_LENGTH:?set TICK_LENGTH in .env} + VM_IP: ${VM_IP:-127.0.0.1} + volumes: + - ${TRAFFIC_DIR_HOST:-./data/traffic}:/traffic:ro + - ./configurations.py:/app/configurations.py:ro + networks: + - tulip-internal + security_opt: + - no-new-privileges:true + healthcheck: + test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:5000/', timeout=3).read()"] + interval: 10s + timeout: 5s + retries: 12 + start_period: 20s + + assembler: + build: + context: ./upstream/services/go-importer + dockerfile: Dockerfile-assembler + image: mctf-tulip-assembler:523e517 + restart: unless-stopped + depends_on: + timescale: + condition: service_healthy + command: + - ./assembler + - -http-session-tracking + - -skipchecksum + - -disable-converters + - -dir + - /traffic + environment: + TIMESCALE: postgres://tulip@timescale:5432/tulip + FLAG_REGEX: ${FLAG_REGEX:?set FLAG_REGEX in .env} + TICK_START: ${TICK_START:?set TICK_START in .env} + TICK_LENGTH: ${TICK_LENGTH:?set TICK_LENGTH in .env} + FLAGID_SCAN: "" + FLAG_LIFETIME: ${FLAG_LIFETIME:-5} + FLAG_VALIDATOR_TYPE: "" + FLAG_VALIDATOR_TEAM: "" + PCAP_OVER_IP: "" + DUMP_PCAPS: "" + DUMP_PCAPS_INTERVAL: 1m + DUMP_PCAPS_FILENAME: 2006-01-02_15-04-05.pcap + BPF: "" + volumes: + - ${TRAFFIC_DIR_HOST:-./data/traffic}:/traffic:ro + networks: + - tulip-internal + security_opt: + - no-new-privileges:true + + frontend: + build: + context: ./upstream/frontend + dockerfile: Dockerfile-frontend + image: mctf-tulip-frontend:523e517 + restart: unless-stopped + depends_on: + api: + condition: service_healthy + environment: + API_SERVER_ENDPOINT: http://api:5000/ + ports: + - "${TULIP_BIND_ADDRESS:-127.0.0.1}:${TULIP_PORT:-3000}:3000" + networks: + - tulip-internal + - tulip-edge + security_opt: + - no-new-privileges:true + healthcheck: + test: + - CMD + - node + - -e + - "const r=require('http').get('http://127.0.0.1:3000/api/',x=>process.exit(x.statusCode===200?0:1));r.on('error',()=>process.exit(1));r.setTimeout(3000,()=>{r.destroy();process.exit(1)})" + interval: 10s + timeout: 5s + retries: 12 + start_period: 20s + + capture: + profiles: + - capture + platform: linux/amd64 + build: + context: . + dockerfile: Dockerfile.capture + image: mctf-tulip-capture:bookworm + restart: unless-stopped + # Host networking exposes the OpenVPN interface. tcpdump needs NET_RAW for + # packet sockets and NET_ADMIN for TUN capture on this Docker host. + network_mode: host + cap_drop: + - ALL + cap_add: + - NET_ADMIN + - NET_RAW + security_opt: + - no-new-privileges:true + read_only: true + tmpfs: + - /tmp:size=16m,mode=1777 + environment: + CAPTURE_INTERFACE: ${CAPTURE_INTERFACE:-game} + CAPTURE_ROTATE_SECONDS: ${CAPTURE_ROTATE_SECONDS:-60} + volumes: + - ${TRAFFIC_DIR_HOST:-./data/traffic}:/traffic + - ./capture.bpf:/etc/tcpdump/filter:ro + entrypoint: + - /bin/sh + - -ec + command: + - | + umask 027 + mkdir -p /traffic/.spool + set -- tcpdump -i "$${CAPTURE_INTERFACE}" -p -n -U -s 0 -Z root \ + -G "$${CAPTURE_ROTATE_SECONDS}" \ + -w "/traffic/.spool/$${CAPTURE_INTERFACE}-%Y%m%d-%H%M%S.pcap" \ + -z /usr/local/bin/finalize-capture + if [ -s /etc/tcpdump/filter ]; then + set -- "$$@" -F /etc/tcpdump/filter + fi + exec "$$@" + healthcheck: + test: ["CMD-SHELL", "test -e /sys/class/net/$${CAPTURE_INTERFACE}"] + interval: 10s + timeout: 3s + retries: 3 + start_period: 5s + +volumes: + timescale-data: + +networks: + tulip-internal: + internal: true + tulip-edge: diff --git a/ops/tulip/configurations.py b/ops/tulip/configurations.py new file mode 100644 index 0000000..1e9f30e --- /dev/null +++ b/ops/tulip/configurations.py @@ -0,0 +1,16 @@ +import json +import os +from pathlib import Path + +traffic_dir = Path(os.getenv("TULIP_TRAFFIC_DIR", "/traffic")).resolve() +dump_pcaps_dir = Path(os.getenv("DUMP_PCAPS") or traffic_dir).resolve() +tick_length = os.getenv("TICK_LENGTH", "120000") +flag_lifetime = os.getenv("FLAG_LIFETIME", "5") +start_date = os.getenv("TICK_START", "2018-06-27T13:00:00Z") +flag_regex = os.getenv("FLAG_REGEX", "[A-Z0-9]{31}=") +vm_ip = os.getenv("VM_IP", "127.0.0.1") +visualizer_url = os.getenv("VISUALIZER_URL", "http://127.0.0.1:1337") + +services = json.loads(os.environ["TULIP_SERVICES"]) +if not isinstance(services, list): + raise ValueError("TULIP_SERVICES must be a JSON array") diff --git a/ops/tulip/finalize-capture.sh b/ops/tulip/finalize-capture.sh new file mode 100755 index 0000000..6bbcb09 --- /dev/null +++ b/ops/tulip/finalize-capture.sh @@ -0,0 +1,13 @@ +#!/bin/sh +set -eu + +capture=$1 +case "$capture" in + /traffic/.spool/*.pcap) + mv -- "$capture" "/traffic/${capture##*/}" + ;; + *) + echo "refusing unexpected capture path: $capture" >&2 + exit 64 + ;; +esac diff --git a/scripts/redeploy-service b/scripts/redeploy-service new file mode 100755 index 0000000..58c2020 --- /dev/null +++ b/scripts/redeploy-service @@ -0,0 +1,107 @@ +#!/bin/sh +set -eu + +usage() { + printf 'Usage: %s \n' "$0" >&2 + printf 'Services: braindance chrome-ledger dark-tariff delamain pamsave sondib towercore\n' >&2 +} + +if [ "$#" -ne 1 ]; then + usage + exit 2 +fi + +service=$1 +env_file= + +case "$service" in + braindance) + compose_file=braindance/compose.yml + ;; + chrome-ledger) + compose_file=chrome-ledger/compose.yml + ;; + dark-tariff) + compose_file=dark-tariff/compose.yml + env_file=dark-tariff/env + ;; + delamain) + compose_file=delamain/docker-compose.yml + ;; + pamsave) + compose_file=pamsave/compose.yml + env_file=pamsave/.env + ;; + sondib) + compose_file=sondib/compose.yml + ;; + towercore) + compose_file=towercore/docker-compose.yml + ;; + *) + usage + exit 2 + ;; +esac + +repo_root=$(git rev-parse --show-toplevel) +current_dir=$(pwd -P) +repo_root=$(CDPATH= cd -- "$repo_root" && pwd -P) + +if [ "$current_dir" != "$repo_root" ]; then + printf 'Run this helper from the repository root: %s\n' "$repo_root" >&2 + exit 1 +fi + +worktree_status=$(git status --porcelain) +if [ -n "$worktree_status" ]; then + printf 'Refusing to deploy: the VPS worktree is not clean.\n' >&2 + exit 1 +fi + +old_head=$(git rev-parse HEAD) + +print_rollback() { + status=$? + trap - 0 + + if [ "$status" -ne 0 ]; then + printf 'Redeploy failed. No automatic rollback was performed.\n' >&2 + printf 'Rollback: git restore --source=%s --worktree -- %s && ' "$old_head" "$service" >&2 + if [ -n "$env_file" ]; then + printf 'docker compose --env-file %s -f %s up -d --build\n' "$env_file" "$compose_file" >&2 + else + printf 'docker compose -f %s up -d --build\n' "$compose_file" >&2 + fi + fi + + exit "$status" +} +trap print_rollback 0 + +printf 'Pulling origin/main for %s...\n' "$service" +git pull --ff-only origin main +new_head=$(git rev-parse HEAD) + +if [ ! -f "$compose_file" ]; then + printf 'Compose file not found: %s\n' "$compose_file" >&2 + exit 1 +fi +if [ -n "$env_file" ] && [ ! -f "$env_file" ]; then + printf 'Environment file not found: %s\n' "$env_file" >&2 + exit 1 +fi + +printf 'Service: %s\n' "$service" +printf 'Compose file: %s\n' "$compose_file" +printf 'Old commit: %s\n' "$old_head" +printf 'New commit: %s\n' "$new_head" + +set -- docker compose +if [ -n "$env_file" ]; then + set -- "$@" --env-file "$env_file" +fi +set -- "$@" -f "$compose_file" + +"$@" up -d --build +"$@" ps diff --git a/sploits/.gitkeep b/sploits/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/towercore/Dockerfile.towercore b/towercore/Dockerfile.towercore index 73ff7bc..9f119b0 100644 --- a/towercore/Dockerfile.towercore +++ b/towercore/Dockerfile.towercore @@ -11,7 +11,6 @@ patches = ( (0x292E8, bytes.fromhex("a8 01 b7 93"), bytes.fromhex("9c 01 b7 93")), (0x292F0, bytes.fromhex("b0 01 a5 8f"), bytes.fromhex("a4 01 a5 8f")), (0x292FC, bytes.fromhex("ac 01 a4 8f"), bytes.fromhex("a0 01 a4 8f")), - (0x29324, bytes.fromhex("03 00 55 10"), bytes.fromhex("03 00 00 10")), ) for offset, original, replacement in patches: actual = bytes(data[offset : offset + len(original)])