CTF比赛平台搭建

克隆CTFd

GitHub:https://github.com/CTFd/CTFd

1
2
3
4
5
6
7
8
9
┌──(kali㉿kali)-[/var/www/html]
└─$ sudo git clone https://github.com/CTFd/CTFd.git 128 ⨯
Cloning into 'CTFd'...
remote: Enumerating objects: 14012, done.
remote: Counting objects: 100% (869/869), done.
remote: Compressing objects: 100% (555/555), done.
remote: Total 14012 (delta 480), reused 578 (delta 291), pack-reused 13143
Receiving objects: 100% (14012/14012), 25.84 MiB | 1.69 MiB/s, done.
Resolving deltas: 100% (8691/8691), done.

安装docker

1
2
┌──(kali㉿kali)-[/var/www/html/CTFd]
└─$ sudo apt install docker docker-compose
1
2
3
4
5
6
7
┌──(kali㉿kali)-[/var/www/html/CTFd]
└─$ docker --version
Docker version 20.10.8+dfsg1, build 3967b7d

┌──(kali㉿kali)-[/var/www/html/CTFd]
└─$ docker-compose --version
docker-compose version 1.27.4, build unknown

添加执行权限

1
2
┌──(kali㉿kali)-[/var/www/html/CTFd]
└─$ sudo chmod +x /usr/bin/docker-compose

在CTFd根目录下启动docker

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
┌──(kali㉿kali)-[/var/www/html/CTFd]
└─$ sudo docker-compose up -d 130 ⨯
Creating network "ctfd_internal" with the default driver
Creating network "ctfd_default" with the default driver
Pulling db (mariadb:10.4.12)...
10.4.12: Pulling from library/mariadb
23884877105a: Pull complete
bc38caa0f5b9: Pull complete
2910811b6c42: Pull complete
36505266dcc6: Pull complete
e69dcc78e96e: Pull complete
222f44c5392d: Pull complete
efc64ea97b9c: Pull complete
9912a149de6b: Pull complete
7ef6cf5b5697: Pull complete
8a05be3688e0: Pull complete
c09ffdc1b660: Pull complete
2eb7fe288fc8: Pull complete
b41d1cc4d40f: Pull complete
a92376500910: Pull complete
......

注:首次启动由于下载依赖等会比较慢

平台配置

打开浏览器,输入127.0.0.1:8000配置赛事相关信息

GFgMuY.png

GFgTJ9.png

GFgWcX.png

添加题目

在GitHub上找题目,clone到本地

1
2
3
4
5
6
7
┌──(kali㉿kali)-[/var/www/html/CTFd/ctf_training]
└─$ sudo git clone https://github.com/CTFTraining/CTFTraining.git
Cloning into 'CTFTraining'...
remote: Enumerating objects: 86, done.
remote: Total 86 (delta 0), reused 0 (delta 0), pack-reused 86
Receiving objects: 100% (86/86), 31.72 KiB | 331.00 KiB/s, done.
Resolving deltas: 100% (48/48), done.

实际测试发现不能把所有的题目都clone下来,就先试一道题

1
2
3
4
5
6
7
┌──(kali㉿kali)-[/var/www/html/CTFd/ctf_training]
└─$ sudo git clone https://github.com/CTFTraining/0ctf_2016_unserialize.git 128 ⨯
Cloning into '0ctf_2016_unserialize'...
remote: Enumerating objects: 32, done.
remote: Total 32 (delta 0), reused 0 (delta 0), pack-reused 32
Receiving objects: 100% (32/32), 779.15 KiB | 2.07 MiB/s, done.
Resolving deltas: 100% (3/3), done.

docker images 命令查看已有镜像

docker build ./ -t [镜像名称] 启动Dockerfile文件,创建镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
┌──(kali㉿kali)-[/var/…/html/CTFd/ctf_training/0ctf_2016_unserialize]
└─$ sudo docker build ./ -t 0ctf_2016_unserialize
Sending build context to Docker daemon 1.841MB
Step 1/8 : FROM php:5.6-fpm-alpine
5.6-fpm-alpine: Pulling from library/php
169185f82c45: Pull complete
8ba0d98519aa: Pull complete
75f762bbc102: Pull complete
04f939cd9fe5: Pull complete
5feedbe48aa1: Pull complete
47e0f87348c0: Pull complete
68b7fb9d3c14: Pull complete
7e4b086d6b5d: Pull complete
a15e624700d4: Pull complete
f714654cc532: Pull complete
Digest: sha256:e3845c650c700234be3fb5b94865753d1a4534f8820d4dea1d0ee6d875efe02b
1
2
3
4
5
6
7
8
9
10
11
12
┌──(kali㉿kali)-[/var/…/html/CTFd/ctf_training/0ctf_2016_unserialize]
└─$ sudo docker images 1 ⨯
REPOSITORY TAG IMAGE ID CREATED SIZE
0ctf_2016_unserialize latest 931233916db0 2 minutes ago 333MB
ctfd_ctfd latest b36ee26f2439 49 minutes ago 791MB
python 3.7-slim-buster cd7d26aa86ac 10 days ago 113MB
nginx 1.17 9beeba249f3e 18 months ago 127MB
mariadb 10.4.12 b6184b68d1fd 19 months ago 357MB
redis 4 191c4017dcdd 19 months ago 89.3MB
ubuntu 18.10 9dc19675e327 2 years ago 67.3MB
php 5.6-fpm-alpine 6feac8f2ef42 2 years ago 54.5MB
php 5-apache-jessie d34f09f63596 2 years ago 374MB

再通过镜像创建容器:

1
2
┌──(kali㉿kali)-[/var/…/html/CTFd/ctf_training/0ctf_2016_unserialize]
└─$ sudo docker run --name 0ctf_2016_unserialize -p 20000:80 -d 0ctf_2016_unserialize

查看现有容器:

1
2
3
4
5
6
7
8
┌──(kali㉿kali)-[/var/…/html/CTFd/ctf_training/0ctf_2016_unserialize]
└─$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9cae78cf79bc 0ctf_2016_unserialize "docker-php-entrypoi…" 5 seconds ago Up 4 seconds 9000/tcp, 0.0.0.0:20000->80/tcp, :::20000->80/tcp 0ctf_2016_unserialize
3b3889db12c7 nginx:1.17 "nginx -g 'daemon of…" 57 minutes ago Up 57 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp ctfd_nginx_1
7b58b9661715 ctfd_ctfd "/opt/CTFd/docker-en…" 57 minutes ago Up 57 minutes 0.0.0.0:8000->8000/tcp, :::8000->8000/tcp ctfd_ctfd_1
93e49564bff7 mariadb:10.4.12 "docker-entrypoint.s…" 57 minutes ago Up 57 minutes ctfd_db_1
9f4380eb4639 redis:4 "docker-entrypoint.s…" 57 minutes ago Up 57 minutes

访问127.0.0.1:20000即可访问到刚刚添加的题目,20000是刚刚指定的端口

GFrlx9.png

打开题目容器:

1
docker exec -i -t <container> /bin/sh

为题目添加flag:

1
2
3
4
5
6
7
┌──(kali㉿kali)-[/var/…/html/CTFd/ctf_training/0ctf_2016_unserialize]
└─$ sudo docker exec -i -t 0ctf_2016_unserialize /bin/sh 1 ⨯
/var/www/html # ls
class.php config.php index.php profile.php register.php static update.php upload www.zip
/var/www/html # echo flag{an_e@sy_un5eri@li2e_chall3nge} > flag.txt
/var/www/html # ls
class.php config.php flag.txt index.php profile.php register.php static update.php upload www.zip

最后在后台添加题目地址等信息
GFVPrX.png
GFVFO9.png

GFVxLn.png

同理可添加其他题目

搭建中遇到的问题

  • git clone 命令克隆失败:
1
2
3
4
┌──(kali㉿kali)-[/var/www/html/CTFd/ctf_training]
└─$ sudo git clone https://github.com/CTFTraining/0ctf_2016_unserialize.git 128 ⨯
Cloning into '0ctf_2016_unserialize'...
fatal: unable to access 'https://github.com/CTFTraining/0ctf_2016_unserialize.git/': Failed to connect to github.com port 443: Connection refused

由于网络问题,可能需要多克隆几次,实在不行可以试试把压缩包下载下来

  • 有些题目克隆下来可能有问题,运行过程中可能会报错(目前仍不了解是什么原因)

    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
    ┌──(kali㉿kali)-[/var/…/html/CTFd/ctf_training/34c3ctf_2017_urlstorage]
    └─$ sudo docker build ./ -t urlstorage 1 ⨯
    Sending build context to Docker daemon 282.6kB
    Step 1/7 : FROM ubuntu:18.10
    18.10: Pulling from library/ubuntu
    8a532469799e: Pull complete
    32f4dcec3531: Pull complete
    230f0701585e: Pull complete
    e01f70622967: Pull complete
    Digest: sha256:7d657275047118bb77b052c4c0ae43e8a289ca2879ebfa78a703c93aa8fd686c
    Status: Downloaded newer image for ubuntu:18.10
    ---> 9dc19675e327
    Step 2/7 : ENV DOCKER=1 PHANTOMJS_VERSION="2.1.1"
    ---> Running in 98e854ac92f3
    Removing intermediate container 98e854ac92f3
    ---> 154a521cb653
    Step 3/7 : RUN sed -i 's/http:\/\/archive.ubuntu.com\//http:\/\/mirrors.tuna.tsinghua.edu.cn\//g' /etc/apt/sources.list && sed -i '/security/d' /etc/apt/sources.list && apt-get -y update && apt-get -yqq install curl python3 python3-pip mariadb-server libmysqlclient-dev nginx wget bzip2 libfreetype6 libfontconfig && mkdir -p /srv/var && wget --local-encoding=UTF-8 --no-check-certificate -O /tmp/phantomjs-$PHANTOMJS_VERSION-linux-x86_64.tar.bz2 https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-$PHANTOMJS_VERSION-linux-x86_64.tar.bz2 && tar -xjf /tmp/phantomjs-$PHANTOMJS_VERSION-linux-x86_64.tar.bz2 -C /tmp && rm -f /tmp/phantomjs-$PHANTOMJS_VERSION-linux-x86_64.tar.bz2 && mv /tmp/phantomjs-$PHANTOMJS_VERSION-linux-x86_64/ /srv/var/phantomjs && ln -s /srv/var/phantomjs/bin/phantomjs /usr/bin/phantomjs && pip3 install django gunicorn mysqlclient requests lxml pyyaml django-simple-captcha
    ---> Running in 8d702a730242
    Ign:1 http://mirrors.tuna.tsinghua.edu.cn/ubuntu cosmic InRelease
    Ign:2 http://mirrors.tuna.tsinghua.edu.cn/ubuntu cosmic-updates InRelease
    Ign:3 http://mirrors.tuna.tsinghua.edu.cn/ubuntu cosmic-backports InRelease
    Err:4 http://mirrors.tuna.tsinghua.edu.cn/ubuntu cosmic Release
    404 Not Found [IP: 101.6.15.130 80]
    Err:5 http://mirrors.tuna.tsinghua.edu.cn/ubuntu cosmic-updates Release
    404 Not Found [IP: 101.6.15.130 80]
    Err:6 http://mirrors.tuna.tsinghua.edu.cn/ubuntu cosmic-backports Release
    404 Not Found [IP: 101.6.15.130 80]
    Reading package lists...
    E: The repository 'http://mirrors.tuna.tsinghua.edu.cn/ubuntu cosmic Release' does not have a Release file.
    E: The repository 'http://mirrors.tuna.tsinghua.edu.cn/ubuntu cosmic-updates Release' does not have a Release file.
    E: The repository 'http://mirrors.tuna.tsinghua.edu.cn/ubuntu cosmic-backports Release' does not have a Release file.
    The command '/bin/sh -c sed -i 's/http:\/\/archive.ubuntu.com\//http:\/\/mirrors.tuna.tsinghua.edu.cn\//g' /etc/apt/sources.list && sed -i '/security/d' /etc/apt/sources.list && apt-get -y update && apt-get -yqq install curl python3 python3-pip mariadb-server libmysqlclient-dev nginx wget bzip2 libfreetype6 libfontconfig && mkdir -p /srv/var && wget --local-encoding=UTF-8 --no-check-certificate -O /tmp/phantomjs-$PHANTOMJS_VERSION-linux-x86_64.tar.bz2 https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-$PHANTOMJS_VERSION-linux-x86_64.tar.bz2 && tar -xjf /tmp/phantomjs-$PHANTOMJS_VERSION-linux-x86_64.tar.bz2 -C /tmp && rm -f /tmp/phantomjs-$PHANTOMJS_VERSION-linux-x86_64.tar.bz2 && mv /tmp/phantomjs-$PHANTOMJS_VERSION-linux-x86_64/ /srv/var/phantomjs && ln -s /srv/var/phantomjs/bin/phantomjs /usr/bin/phantomjs && pip3 install django gunicorn mysqlclient requests lxml pyyaml django-simple-captcha' returned a non-zero code: 100
  • 许多命令都需要root权限,如果在哪一步运行报错了可以检查以下是否是权限问题

评论