CTF出题人指南:如何用Docker-Compose快速打包部署一个Web题目(含源码泄露题实战)
CTF出题人实战指南Docker-Compose高效封装Web赛题全流程在CTF竞赛生态中Web类题目始终占据重要地位。对于出题人而言如何将精心设计的题目快速封装为标准化环境并确保在不同平台间无缝迁移是提升赛事运维效率的关键环节。本文将聚焦Docker-Compose技术栈通过一个典型的源码泄露题型案例详解从题目设计到容器化部署的完整工作流。1. 环境准备与基础架构设计1.1 题目原型构建我们先创建一个基础Web题目原型——通过查看网页源码获取flag的经典题型。在项目根目录建立以下结构f12_challenge/ ├── files/ │ └── index.php ├── Dockerfile └── docker-compose.yml其中index.php内容如下?php $flag getenv(FLAG) ?: flag{default_flag_here}; ? !DOCTYPE html html head titleView Source Challenge/title style body { font-family: Arial, sans-serif; margin: 40px; } .hint { color: #999; font-size: 0.8em; } /style /head body h1Welcome to F12 Challenge/h1 pNothing suspicious here.../p !-- Flag is hidden in the page source -- div styledisplay:none;FLAG: ?php echo htmlspecialchars($flag); ?/div p classhintHint: Sometimes developers leave interesting things in comments/p /body /html1.2 Docker化核心组件Dockerfile采用轻量级基础镜像FROM php:8.2-apache WORKDIR /var/www/html COPY ./files/ . RUN echo ServerName localhost /etc/apache2/apache2.conf EXPOSE 80 ENV FLAGflag{this_is_sample_flag} CMD [apache2-foreground]关键配置说明使用官方PHP-Apache组合镜像设置工作目录并拷贝题目文件配置Apache服务监听端口通过环境变量注入flag值2. 容器编排与平台集成2.1 docker-compose.yml 深度配置version: 3.8 services: web: build: . ports: - 8001:80 environment: - FLAGflag{real_flag_for_competition} restart: unless-stopped networks: - ctf_network networks: ctf_network: driver: bridge配置要点解析参数作用典型值ports端口映射主机端口:容器端口environment环境变量FLAG值可动态修改restart容器策略unless-stopped保持运行networks网络隔离自定义bridge网络2.2 多题目编排实践对于需要数据库支持的题目如SQL注入扩展配置如下services: db: image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: ctfdemo MYSQL_DATABASE: ctfdb volumes: - mysql_data:/var/lib/mysql networks: - ctf_network volumes: mysql_data:注意数据库类题目需特别注意数据持久化配置避免容器重启导致数据丢失3. 高级运维技巧3.1 容器更新策略当需要修改已部署题目时执行标准流程# 停止并移除旧容器 docker-compose down # 清理残留网络 docker network prune -f # 重建并启动新容器 docker-compose up -d --build常见问题处理对照表现象解决方案原理修改未生效添加--build参数强制重建镜像端口冲突修改compose文件端口映射避免主机端口占用环境变量不更新删除旧容器后重建环境变量在构建时确定3.2 平台集成方案针对不同CTF平台的集成方式CTFd平台通过容器挑战插件自动部署需配置正确的API端点H1ve平台使用动态题目插件配置docker-compose路径# H1ve平台部署示例 cd /opt/h1ve/challenges git clone https://github.com/your_repo/web_challenges.git docker-compose -f single-nginx.yml up -d4. 安全加固与性能优化4.1 容器安全配置在Dockerfile中添加安全措施RUN apt-get update \ apt-get install -y --no-install-recommends \ libapache2-mod-security2 \ a2enmod security2 \ rm -rf /var/lib/apt/lists/* COPY security.conf /etc/apache2/conf-enabled/安全配置文件示例Directory /var/www/html Options -Indexes AllowOverride None Require all granted SecRuleEngine On SecRule REQUEST_URI contains /etc/passwd deny,status:403 /Directory4.2 资源限制方案在docker-compose.yml中设置资源约束services: web: deploy: resources: limits: cpus: 0.5 memory: 256M reservations: memory: 128M监控容器资源使用docker stats --format table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}5. 实战构建完整题目仓库建议的题目仓库结构ctf_challenges/ ├── web/ │ ├── f12/ │ │ ├── README.md │ │ └── solution/ │ ├── sql_injection/ │ │ ├── db_init.sql │ │ └── web/ │ └── xss/ │ ├── admin_bot.js │ └── web/ ├── pwn/ └── crypto/自动化部署脚本示例deploy.sh#!/bin/bash CHALLENGE_NAME$1 PORT$2 FLAG$3 cd $CHALLENGE_NAME sed -i s/FLAG.*/FLAG$FLAG/ docker-compose.yml sed -i s/- [0-9]*:80/- $PORT:80/ docker-compose.yml docker-compose down docker-compose up -d --build