ctf简单出题流程

ctf简单出题流程

文件介绍

复制代码
index
  src  写一些题目的文件
   index.php
  dockerfile   

首先肯定是要安装好Docker的

Docker的流程网上有,这里就不再追述了

首先可以查看镜像

shell 复制代码
sudo docker images

文件创建

我们可以在我们的**/app目录下创建一个文件夹用于存储我们的文件index**

shell 复制代码
sudo mkdir index
god@localhost:/app$ sudo mkdir index

我们创建好了文件夹之后,进入文件夹。

在新的文件夹中创建新的目录文件src

shell 复制代码
sudo makir src
god@localhost:/app/index$ sudo mkdir src

然后创建一个配置文件dockerfile

shell 复制代码
god@localhost:/app$ cd index
god@localhost:/app/index$ sudo mkdir src
god@localhost:/app/index$ sudo touch dockerfile
god@localhost:/app/index$ ls
dockerfile  src
god@localhost:/app/index$ 

然后在src下创建index.php

php 复制代码
<?php phpinfo();

然后在dockerfile里面写入

shell 复制代码
FROM ctftraining/base_image_nginx_mysql_php_56

COPY src /var/www/html



基于一个已有的镜像 ctftraining/base_image_nginx_mysql_php_56 来构建新的镜像。

把当前目录下的 src 文件夹中的所有文件,复制到容器内的 /var/www/html 目录下。
这个目录是 Nginx 默认的 Web 根目录,也就是说,你的网站代码(PHP、HTML、JS 等)会被放到这里,供浏览器访问。
复制代码
god@localhost:/app/index$ cat dockerfile 
FROM ctftraining/base_image_nginx_mysql_php_56

COPY src /var/www/html
god@localhost:/app/index$ 

构建文件

先到文件夹/app/index中

shell 复制代码
sudo docker build -t index:v1 .

镜像已经准备好了

然后用

shell 复制代码
sudo docker images

启动docker

shell 复制代码
sudo docker run -d -p 2555:80 index:v1

2555:80端口映射
段落 含义
sudo 以管理员身份运行,避免权限不足。
docker run 启动一个容器。
-d 后台运行(detach),容器不会霸占当前终端。
-p 2555:80 端口映射 :把宿主机2555 端口映射到容器内部的 80 端口。
index:v1 使用刚才构建的镜像。

修改完必须把容器删了才能重新开启

把容器停掉和删除容器

shell 复制代码
# 1. 停容器
sudo docker stop 300c2a6cc488
300c2a6cc488 id号
# 2. 删容器
sudo docker rm 300c2a6cc488

# 3. 再删镜像
sudo docker rmi index:v1

修改网页文件构建真正题目

index.php

php 复制代码
<?php
error_reporting(0);
highlight_file(__FILE__);
include 'flag.php';
if (isset($_GET['flag'])) {
    echo $flag;
} else {
    echo '666';
}
?>

flag.php

php 复制代码
<?php 
$flag="flag{123456}"

直接运行

复制代码
sudo docker build -t index:v1 .
sudo docker run -d -p 2555:80 index:v1
 
god@localhost:/app/indexsudo docker build -t index:v1 .
[+] Building 3.7s (7/7) FINISHED                 docker:default
 => [internal] load build definition from dockerfile       0.0s
 => => transferring dockerfile: 165B                       0.0s
 => [internal] load metadata for docker.io/ctftraining/ba  3.1s
 => [internal] load .dockerignore                          0.0s
 => => transferring context: 2B                            0.0s
 => [internal] load build context                          0.1s
 => => transferring context: 456B                          0.1s
 => CACHED [1/2] FROM docker.io/ctftraining/base_image_ng  0.0s
 => [2/2] COPY src /var/www/html                           0.2s
 => exporting to image                                     0.0s
 => => exporting layers                                    0.0s
 => => writing image sha256:62ef83cb7df28e6cac2bd047958b7  0.0s
 => => naming to docker.io/library/index:v1                0.0s
god@localhost:/app/index$ sudo docker run -d -p 2555:80 index:v1
da1df97450eed98d71a2061114e930d50fc36a11ef27a7665c4fd7c86a2366d5

改进

由于上面的问题总是要重新启动容器才能对修改的内容起作用,所以进行方案调整

把一堆 Docker 参数从命令行搬进文件,让"基础设施"可复制、可版本化、一键启停。

我们可以运用一个东西进行挂载

启动docker-compose.yml
yml 复制代码
version: "2"
services:
  web:
    image: ctftraining/base_image_nginx_mysql_php_56
    container_name: index
    ports:
      - "2556:80"
    volumes:
      - ./src/index.php:/var/www/html/index.php
      - ./src/flag.php:/var/www/html/flag.php

启动

shell 复制代码
sudo docker-compose up -d

 sudo docker compose up -d

如果没安装

shell 复制代码
# Ubuntu / Debian 示例
sudo apt update
sudo apt install docker-compose -y

然后我们更改内容的时候就不需要重新启动容器了

想停止

shell 复制代码
cd /app/index                  # 确保和 docker-compose.yml 同目录
sudo docker compose down       # 停+删容器+网