CTF 动态FLAG

因为要准备迎新啦,这个东西似乎也是刚需。
结果最后搭起来了,但是还是迷迷糊糊的,有空的时候再改一下下

构建一个动态docker镜像

docker 安装

sudo apt-get update
sudo apt-get remove docker docker-engine docker.io containerd runc
sudo apt-get update
sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

图形化管理

docker run -d -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock --restart=always --name portainer portainer/portainer

先写好flag.txt文件

echo flag{owo} > flag.txt

构建动态flag镜像

Dockfile 文件
#引用nginx镜像
FROM nginx
#环境目录
ENV MYPATH /opt/owo
RUN mkdir -p $MYPATH
WORKDIR $MYPATH
ADD flag.txt $MYPATH
RUN cat flag.txt >> /usr/share/nginx/html/index.html
EXPOSE 80

执行:

docker build -t owo:5 .
docker run -itd -P owo:5

可以看到已经构建成功
pic

文件写在这里了,镜像大小与nginx镜像大小基本一致,但是过程中生成了很多层镜像,先不管了。
pic2

搭建ctfd

安装frps

sudo apt-get install wget git
wget https://github.com/fatedier/frp/releases/download/v0.44.0/frp_0.44.0_linux_amd64.tar.gz
#解压
tar -zxvf frp_0.44.0_linux_amd64.tar.gz
mkdir /opt/frp
mv frp_0.44.0_linux_amd64/* /opt/frp/

#修改配置文件
vim /opt/frp/frps.ini

frps.ini

[common]
bind_port = 7000
bind_addr = 0.0.0.0
token = Oowow_Auth_key
vhost_http_port = 80

设置为系统服务

vim /usr/lib/systemd/system/frp.service
[Unit]
Description=Frp Server Service
After=network.target

[Service]
Type=simple
User=root
Restart=on-failure
RestartSec=5s
ExecStart=/opt/frp/frps -c /opt/frp/frps.ini

[Install]
WantedBy=multi-user.target

启动服务

systemctl start frp
systemctl restart frp
systemctl enable frp
systemctl status frp

安装frpc

sudo docker network create ctfd_frp-containers #创建frp网络

sudo docker run  -d -v /opt/frp/frpc.ini:/etc/frp/frpc.ini --name frpc_1 --network="ctfd_frp-containers" --restart=always "glzjin/frp" 

sudo docker network create frpcadmin #创建用于 ctfd <--> frpcadmin
sudo docker network connect frpcadmin frpc_1 #连接

owo

修改配置文件/opt/frp/frpc.ini 这个文件就是对应docker 容器的 frpc的配置文件

[common]
server_addr = 172.18.0.1
server_port = 7000
token = Oowow_Auth_key
admin_addr=0.0.0.0
admin_port=7400

部署ctfd

最新版应该是不是很好使,先用着老的教程里的吧

git clone https://github.com/CTFd/CTFd.git 
cd CTFd/ 
git reset 6c5c63d667a17aec159c8e26ea53dccfbc4d0fa3 --hard  #回滚到当前教程适合的版本 
cd CTFd/plugins  #打开ctfd插件目录 
git clone https://github.com/glzjin/CTFd-Whale.git ctfd-whale  #确保插件文件夹小写 
cd ctfd-whale 
git reset 5b32f457e9f56ee9b2b29495f4b3b118be3c57bd --hard  #回滚到当前教程适合的版本 
cd ../../..   #返回ctfd主目录 
vim docker-compose.yml

docker-compose.yml

version: '2'
services:
  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=-
    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:
  db:
    image: mariadb:10.4.12 #这里改成10.4.12,10.4.13会出错
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=ctfd
      - MYSQL_USER=ctfd
      - MYSQL_PASSWORD=ctfd
      - MYSQL_DATABASE=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]
  cache:
    image: redis:4
    restart: always
    volumes:
    - .data/redis:/data
    networks:
        internal:
networks:
    default:
         external:
          name: frpcadmin
    internal:
        internal: true

Dockerfile

FROM python:3.6-alpine
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/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 g++
RUN adduser -D -u 1001 -s /bin/bash ctfd
WORKDIR /opt/CTFd
RUN mkdir -p /opt/CTFd /var/log/CTFd /var/uploads
RUN pip3 config set global.index-url https://pypi.doubanio.com/simple
RUN pip3 config set install.trusted-host pypi.doubanio.com
COPY requirements.txt .
RUN pip install -r requirements.txt -i  https://pypi.doubanio.com/simple
COPY . /opt/CTFd
RUN for d in CTFd/plugins/*; do \
      if [ -f "$d/requirements.txt" ]; then \
        pip install -r $d/requirements.txt -i  https://pypi.doubanio.com/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"]

owo

Docker API URL

左对齐右对齐居中对齐
Docker API URL to connectunix://var/run/docker.sockdocker文件
Frp API IP172.19.0.2Frp的ip
Frp API Port7400frp的端口
Frp Http Domain Suffix 域名后缀
Docker API URL to connectNone
Frp Http Port80For http redirect
For direct redirect10.1.1.56
Frp Direct Minimum Port30000For direct redirect
Frp Direct Maximum Port39999
Docker Auto Connect Containersctfd_frpc_1
Docker Auto Connect Networkctfd_frp-containers
[common]
server_addr = 172.18.0.1
server_port = 7000
token = Oowow_Auth_key
admin_addr=0.0.0.0
admin_port=7400

配置文件如上

pic

喵的至少起来了
owo

看下容器的环境变量就很明显了
owo

截个图(题目配置)
owo

尝试重新构建

重新构建docker镜像

Dockfile 文件
#引用nginx镜像
FROM nginx
#环境目录
ENV MYPATH /opt/owo
RUN mkdir -p $MYPATH
WORKDIR $MYPATH
ADD flag.txt $MYPATH

#ENTRYPOINT ["/docker-entrypoint.sh"]
#在倒数第二行添加
RUN sed -i '$i\echo \$FLAG > /opt/owo/flag.txt && cat flag.txt >> /usr/share/nginx/html/index.html' /docker-entrypoint.sh \ 
   &&  cat flag.txt >> /usr/share/nginx/html/index.html
EXPOSE 80
docker build -t owo:8 .
docker run -itd -P owo:8

成果,就先这样,赶紧交作业了
owo

最后修改:2022 年 07 月 18 日
如果觉得我的文章对你有用,请随意赞赏