其他分享
首页 > 其他分享> > CTFd动态靶机吐血总结

CTFd动态靶机吐血总结

作者:互联网

搭建环境

阿里云 Ubuntu-server 20.04

搭建步骤

更新一下软件源和软件列表

sudo apt-get update
sudo apt-get upgrade

安装docker

1. 更新系统包索引
sudo apt-get update
2. 添加HTTPS协议,允许apt从HTTPS安装软件包
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common

3. 添加Docker公共密钥 Docker 官方源
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add

4. 设置版本库类型,软件版本包括三种: stable、edge、test
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

5. 安装最新版docker-ce
sudo apt-get install docker-ce

6. 设置开机自启动并启动docker
sudo systemctl enable docker
sudo systemctl start docker

安装docker-compose

1.安装pip
sudo apt install python3-pip
2.更新库
sudo apt-get update
3.更新pip(一定要更新,不然后面建立容器项目会报错)
sudo pip3 install --upgrade pip
4.安装docker-compose
sudo pip3 install docker-compose

设置docker镜像加速

访问https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

找到镜像加速器,按照指示修改daemon配置文件

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://******.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

下载赵师傅的ctfd

git clone https://github.com.cnpmjs.org/glzjin/CTFd.git

下载ctf-whale(并且重命名文件名为小写)

在 CTFd/CTFd/plugins目录下

git clone https://github.com/glzjin/CTFd-Whale

docker集群设置

docker swarm init

docker node ls

docker node update --label-add name=linux-1 <节点 ID>

下载docker的frps(改文件夹名字为frp)

在root目录下

git clone https://github.com/glzjin/Frp-Docker-For-CTFd-Whale

 

进入目录后运行docker-compose up -d 即可

直接上ctfd的docker-compose.yml配置

version: '2.2'

services:
  ctfd-nginx:
    image: nginx:1.17
    volumes:
      - ./nginx/http.conf:/etc/nginx/nginx.conf   #这里注意
    user: root
    restart: always
    ports:
      #- "85:80"     #我将这里注释掉了,这里通过nginx转发感觉速度访问速度会变慢,多次尝试之后直接开8000端口访问不会对服务造成影响
      - "443:443"
    networks:
        default:
        internal:
    depends_on:
      - ctfd
    cpus: '1.00'  #可改
    mem_limit: 150M     #可改
  ctfd:
    build: .
    user: root
    restart: always
    ports:
      - "8000:8000"     #这里原本没开端口,直接打开访问网站速度会加快
    environment:
      - UPLOAD_FOLDER=/var/uploads
      - DATABASE_URL=mysql+pymysql://root:ctfd@db/ctfd
      - REDIS_URL=redis://cache:6379
      - WORKERS=1
      - LOG_FOLDER=/var/log/CTFd
      - ACCESS_LOG=-
      - ERROR_LOG=-
      - REVERSE_PROXY=true
    volumes:
      - .data/CTFd/logs:/var/log/CTFd
      - .data/CTFd/uploads:/var/uploads
      - .:/opt/CTFd:ro
      - /var/run/docker.sock:/var/run/docker.sock     #这里是添加的
    depends_on:
      - db
    networks:
        default:
        internal:
        frp:
            ipv4_address: 172.1.0.2
    cpus: '1.00'     #可改
    mem_limit: 450M     #可改

  db:
    image: mariadb:10.4
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=ctfd
      - MYSQL_USER=ctfd
      - MYSQL_PASSWORD=ctfd
    volumes:
      - .data/mysql:/var/lib/mysql
    networks:
        internal:
    # This command is required to set important mariadb defaults
    command: [mysqld, --character-set-server=utf8mb4, --collation-server=utf8mb4_unicode_ci, --wait_timeout=28800, --log-warnings=0]
    cpus: '1.00'     #可改
    mem_limit: 750M     #可改

  cache:
    image: redis:4
    restart: always
    volumes:
      - .data/redis:/data
    networks:
        internal:
    cpus: '1.00'     #可改
    mem_limit: 450M     #可改

  frpc:    
    image: glzjin/frp:latest     #赵师傅tql
    restart: always
    volumes:
      - ./frpc:/conf/     #这里注意
    entrypoint:
        - /usr/local/bin/frpc
        - -c
        - /conf/frpc.ini
    networks:
        frp:
            ipv4_address: 172.1.0.3  #记住此处
        frp-containers:
    cpus: '1.00'     #可改
    mem_limit: 250M     #可改

networks:
    default:
    internal:
        internal: true
    frp:
        driver: bridge
        ipam:
            config:
                - subnet: 172.1.0.0/16
    frp-containers:
        driver: overlay
        internal: true
        ipam:
            config:
                - subnet: 172.2.0.0/16

在docker-compose.yml同目录下建nginx文件夹

建立 http.conf 文件写入以下内容

worker_processes 4;
events {
  worker_connections 1024;
}
http {
  # Configuration containing list of application servers
  upstream app_servers {
    server ctfd:8000;
  }
  server {
    listen 80;
    client_max_body_size 4G;
    # Handle Server Sent Events for Notifications
    location /events {
      proxy_pass http://app_servers;
      proxy_set_header Connection '';
      proxy_http_version 1.1;
      chunked_transfer_encoding off;
      proxy_buffering off;
      proxy_cache off;
      proxy_redirect off;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Host $server_name;
    }
    # Proxy connections to the application servers
    location / {
      proxy_pass http://app_servers;
      proxy_redirect off;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Host $server_name;
    }
  }
}

下载frp

wget https://github.com/fatedier/frp/releases/download/v0.29.0/frp_0.29.0_linux_amd64.tar.gz

tar -zxvf frp_0.29.0_linux_amd64.tar.gz

在docker-compose.yml同目录下建frpc文件夹,然后进入解压的/frp_0.29.0_linux_amd64文件夹,或直接上传,将

frpc
frpc.ini
frpc_full.ini
LICENSE

放入frpc文件夹
接着配置frpc.ini,直接上配置

[common]
token = randomme
server_addr = 172.1.0.4
server_port = 6490     #此处必须与frpc.ini配置一致
pool_count = 200
tls_enable = true

admin_addr = 172.1.0.3 #一定要加!!与后面相应
admin_port = 7400

配置Dockerfile

FROM python:3.7-alpine  
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories && \
    apk update && \
    apk add linux-headers libffi-dev gcc g++ make musl-dev py-pip mysql-client git openssl-dev python3-dev python3
RUN adduser -D -u 1001 -s /bin/bash ctfd

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

COPY requirements.txt .

RUN pip install -r requirements.txt -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/  --no-cache-dir

COPY . /opt/CTFd

RUN for d in CTFd/plugins/*; do \
      if [ -f "$d/requirements.txt" ]; then \
        pip install -r $d/requirements.txt -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/ ; \
      fi; \
    done;

RUN chmod +x /opt/CTFd/docker-entrypoint.sh
RUN chown -R 1001:1001 /opt/CTFd
RUN chown -R 1001:1001 /var/log/CTFd /var/uploads

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

修改requirements.txt

只需要删除 requirements.txt 中的 gevent 版本号

/requirements.txt
...
redis==3.3.11
datafreeze==0.1.0
gevent
python-dotenv==0.10.3
flask-restplus==0.13.0
...

开始运行了

docker-compose build
docker-compose up -d

访问 http://ip:8000 即可访问ctfd

配置CTFD

进入后随便设置,然后进Admin Panel进行设置

 

 

 

 

设置docker网络

这个时候运行docker ps
发现frpc在无限重启,因为我们还没有配置网络

运行docker network inspect ctfd_frp

这个是docker-compose里compose后创建的网络,我们将frps加入此网络

例如我的frp容器id为12345,则运行docker network connect ctfd_frp 12345<即frp容器id>

 

 

这里可以看到ip与我们之前设置的相应。

此时运行docker restart ctfd_frpc_1 frp_frps_1

然后docker ps

 

设置题目

 

 

 

 

 

 成功

 

 

参考文章

https://blog.csdn.net/weixin_43802844/article/details/108572271

https://err0r.top/article/CTFD/

标签:frp,ctfd,吐血,CTFd,proxy,https,docker,靶机
来源: https://www.cnblogs.com/deluzhao/p/14341252.html