0%

CTFd+CTFd-whale安装教程

动态靶机CTF靶场平台搭建

闲来无事,嫖了阿里云2个月试用机搭了一个训练靶场,在此记录详细过程。对应的是赵总博客的第一种模式:单机 Direct 模式

换源(虚拟机调配时使用)

Ubuntu系统中,软件源文件地址为:/etc/apt/sources.list

1.备份原来的源,将以前的源备份一下,以防以后可以用的。

1
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

2.打开/etc/apt/sources.list文件,在前面添加如下条目,并保存。

1
sudo vim /etc/apt/sources.list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#添加阿里源s
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
#添加清华源
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse multiverse

3.更新

更新源

1
sudo apt-get update

如出现依赖问题,解决方式如下:

1
sudo apt-get -f install

更新软件:

1
sudo apt-get upgrade

参考链接:Ubuntu20.04软件源更换

安装Docker和Docker Compose

本部分内容完全参考:CTFd平台搭建记录

安装Docker

Docker使用阿里云镜像源安装:

Ubuntu安装docker官网

卸载老版本Docker:

1
sudo apt-get remove docker docker-engine docker.io containerd runc

更新软件

1
sudo apt-get update

安装软件包

1
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

添加GPG密钥

1
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

查看密钥是否添加成功

1
sudo apt-key fingerprint 0EBFCD88

若添加成功,会显示如下信息:

img

添加阿里云的软件源

1
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

若添加成功,则在/etc/apt/sources.list中可看见类似如下的记录:

1
deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic stable

再次更新软件

1
sudo apt-get update

安装最新版的docker-ce

1
sudo apt-get install docker-ce docker-ce-cli containerd.io

安装完毕之后,通过docker --version来验证安装是否成功:

img

启动Docker服务并设置为开机启动

1
2
sudo systemctl start docker
sudo systemctl enable docker

然后为Docker添加镜像加速

修改/etc/docker/daemon.json文件(若不存在则自行创建),加入如下内容:

1
2
3
{
"registry-mirrors": ["https://hub-mirror.c.163.com/(地址)"]
}

里面更换为想要的镜像地址即可。下面有几个可选项:

修改文件后,执行命令重启docker服务:

1
2
sudo systemctl daemon-reload
sudo systemctl restart docker

安装Docker-compose

接下来是安装Docker Compose。官方给的是从GitHub下载安装,依然速度很慢,所以采用DaoCloud的镜像源来安装。

下载docker-compose

1
curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.5/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

为其添加可执行权限

1
sudo chmod +x /usr/local/bin/docker-compose

使用docker-compose --version验证安装:

img

启用Docker Swarm

1
2
3
docker swarm init  # 初始化
docker node ls # 查看节点ID
docker node update --label-add name=linux-1 <节点 ID> # 添加别名(一定要记住!)

安装CTFd&CTFd-Whale

下载赵总仓库的CTFd源码

(是否需要回滚忘了。参考博主的版本为9d8981a9808078a0634b13e61190cec4556782e4,如有报错,可以回滚)

1
2
git clone -b single https://github.com/glzjin/CTFd.git
cd CTFd

坑1:

清华源将python2和python3分开来了,所以Dockerfile里要把python和python-dev改掉。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
FROM python:3.7-alpine

WORKDIR /opt/CTFd
RUN mkdir -p /opt/CTFd /var/log/CTFd /var/uploads

RUN sed -i 's/dl-cdn.alpinelinux.org/mirror.tuna.tsinghua.edu.cn/g' /etc/apk/repositories && \
apk update && \
apk add \
python3 \
python3-dev \
linux-headers \
libffi-dev \
gcc \
make \
musl-dev \
py-pip \
mysql-client \
git \
openssl-dev

COPY . /opt/CTFd

RUN pip install -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com -r requirements.txt
RUN for d in CTFd/plugins/*; do \
if [ -f "$d/requirements.txt" ]; then \
pip install -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com -r $d/requirements.txt; \
fi; \
done;

RUN chmod +x /opt/CTFd/docker-entrypoint.sh
RUN adduser -D -u 1001 -s /bin/sh ctfd
RUN chown -R 1001:1001 /opt/CTFd /var/log/CTFd /var/uploads

USER 1001
EXPOSE 8000
ENTRYPOINT ["/opt/CTFd/docker-entrypoint.sh"]

坑2:

requirements.txt少装了一个库

1
greenlet==0.4.15

配置Frp Token

1
vim frp/frps.ini

frps.ini

1
2
3
[common]
bind_port = 6490
token = randomme
1
vim frp/frpc.ini

frpc.ini

1
2
3
4
5
6
7
8
9
[common]
token = randomme
server_addr = 172.1.0.1
server_port = 6490
pool_count = 200
tls_enable = true

admin_addr = 172.1.0.3
admin_port = 7400

下载CTFd-Whale插件

1
git submodule update --init  # 初始化CTFd-Whale插件

(可选)修改CTFd平台运行端口

打开docker-compose.yml,修改CTFd容器的端口信息:

1
2
3
4
5
6
ctfd:
build: .
user: root
restart: always
ports:
- "80:8000" # 意为将外部机器的80端口映射至CTFd容器的8000端口,只需修改前者即可

部署容器

1
docker-compose up -d

部署容器,部署完毕后就可以用http://ip:指定的端口访问CTFd进行设置。

参考链接:

CTFd平台搭建记录

CTFd-Whale 推荐部署实践

配置CTFd-Whale

(本次配置中踩的最大的坑)下面提到的所有端口,都需要在云服务器安全组+系统内防火墙中开放!!!否则Frp API将无法访问,Frp客户端也无法连接上服务端!!!

配置项 对应值
Docker API URL unix://var/run/docker.sock
Frp API IP 172.1.0.3
Frp API Port 7400
Frp Http Domain Suffix None
Frp Http Port 80
Frp Direct IP Address 填入服务器公网IP
Frp Direct Minimum Port 28000
Frp Direct Maximum Port 容器最大的端口值(28100)
Max Container Count 最大允许容器数量(100)
Max Renewal Times 最大允许更新次数(3)
Frp config template [common]
token = randomme
server_addr = 172.1.0.1
server_port = 6490
pool_count = 200
tls_enable = true

admin_addr = 172.1.0.3
admin_port = 7400
Docker Auto Connect Containers ctfd_frpc_1(即docker里面frpc容器的名称)
Docker Auto Connect Network ctfd_frp_containers(即承载开启的题目容器的网络名称)
Docker Dns Setting 可有可无
Docker Swarm Nodes 填入一开始修改的别名(linux-1)
Docker Multi-Container Network Subnet 173.0.0.0/16
Docker Multi-Container Network Subnet New Prefix 24
Docker Container Timeout 容器的存活时间(单位:秒)(3600)

配置题目

img

img

image-20210723110250464

参考链接:

CTFd-Whale 推荐部署实践

CTFd平台搭建记录

-------------本文结束感谢您的阅读-------------