CTFd构建和动态flag生成

1 篇文章 0 订阅
1 篇文章 0 订阅

环境:Ubuntu22.04
镜像参考来源:
阿里巴巴开源镜像站:
https://developer.aliyun.com/mirror/

1、构建CTFd

先换源:

 vim /etc/apt/sources.list
#阿里源地址

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

然后保存退出:wq!

apt-get update  
apt-get upgrade

安装docker和docker-compose

#安装依赖包
sudo apt install apt-transport-https ca-certificates curl gnupg lsb-release

#- 添加阿里云Docker镜像源GPG秘钥
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

#- 添加阿里云镜像源
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

#- 更新apt
sudo apt update

#- 安装Docker
sudo apt install docker-ce docker-ce-cli containerd.io

#- 查看Docker是否启动成功
docker --version

#安装 Docker Compose
apt-get install docker-compose

#启动docker和开机启动docker
sudo systemctl start docker
sudo systemctl enable docker

# Docker集群设置
docker swarm init		# 初始化
docker node ls			# 查看节点ID
docker node update --label-add name=linux-1 <节点 ID>  # 添加别名

下载和安装CTFd

git clone -b frp https://gitee.com/vicosna/CTFd.git

#进入CTFd目录
cd CTFd

#修改子模块Url 
sed -i 's/github.com/gitee.com/g' .gitmodules	

#更新CTFd-Whale子模块
git submodule update --init 								

# 构建镜像
docker-compose build

# 部署容器
docker-compose up -d

然后访问ip地址就可以访问CTFd

2、部署题目

拉取镜像

我们可以先拉取别人弄好的题目测试是否有动态flag

#下载CTF题目,只要替换0ctf_2016_unserialize为其他目录名称即可下载所有题目
sudo git clone htt/opt/CTFps://github.com/CTFTraining/0ctf_2016_unserialize.git 
#进入题目目录中
cd 0ctf_2016_unserialize/ 

sudo docker-compose up -d

docker ps -a

CTFd配置

然后就能看到生成的环境
然后我们进入网站
管理员点击进入Admin Panel 管理界面:
点击whale:
然后去配置frp
在direct ip address
配置网站的IP地址

点击Challenges
加号

Challenge Types选择dynamic_docker

Docker Image 填 镜像的名称

Frp Redirect Type 选择 Direct

Frp Redirect Port 选择镜像的服务,默认一般是80

其他的自己选择然后创建

State 这里选择Visible设置为可以见

Docker Image 名字 千万不能弄错

可以用docker ps 查看 IMAGE 这个下面的就是镜像的名称

然后就好了启动环境一般都比较慢需要等一下

怎么自己写一个ctf题目

首先我们需要一个dockerfile文件

先创建一个文件夹

在里面设置放一个dockerfile文件和docker-compose.yml文件然后一个名字为file的文件夹,文件夹用来放环境

dockerfile

FROM php:5.6-apache

# 在 Dockerfile 中生成动态的 flag
RUN echo "flag_here" > /flag.txt

# 复制本地目录 ./cx 到容器中的 /tmp 目录
COPY ./file /tmp/file/

# 将 /tmp/cx 目录中的内容复制到 Apache 默认根目录
RUN cp -rf /tmp/file/* /var/www/html/ && \       
    chown -R root:root /var/www/html && \
    chmod -R 755 /var/www/html

# 暴露容器的80端口
EXPOSE 80

# 设置容器启动时执行的命令
CMD sh -c 'export FLAG=not_flag && \
           sed -i "s/flag_here/$FLAG/" /flag.txt && \
           apache2ctl -D FOREGROUND'

docker-compose.yml

version: "2"

services:
  web:
    build: .
    image: web/php_rce4
    environment:
      - FLAG=${FLAG}
    restart: always
    ports:
      - "0.0.0.0:8004:80"

file文件夹下面放了一个index.php


<?php
error_reporting(0);
$c = $_GET['c'];
eval($c);
highlight_file(__FILE__);

然后在dockerfile同一个文件夹下面执行命令

docker-compose up -d

然后就可以去Docker Image 填 镜像的名称就可以生成环境,然后在/目录下面生成一个flag.txt文件,并且是动态的flag

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值