CTF: 在本地虚拟机内部署CTF题目docker

step 1 安装基本依赖

复制代码
sudo apt-get update
sudo apt-get install -y \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

step 2 安装docker

复制代码
sudo apt-get remove docker docker.io containerd runc
sudo apt-get update
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release \
    software-properties-common
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=$(dpkg --print-architecture) 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
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin

step 3 换源

复制代码
sudo nano /etc/docker/daemon.json

sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": [ "https://docker.unsee.tech" ] } EOF

sudo systemctl daemon-reload && sudo systemctl restart docker

step 4 测试

复制代码
sudo docker run hello-world

a5rz@a5rz-virtual-machine ~/桌面
 % sudo docker run hello-world                                  
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete 
Digest: sha256:305243c734571da2d100c8c8b3c3167a098cab6049c9a5b066b6021a60fcb966
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

step 5 拉取/构建题目镜像

方法一:从 Docker Hub 或其他镜像仓库拉取

如果您要试验的 CTF 题目已经有现成的 Docker 镜像,可以直接拉取:

bash 复制代码
sudo docker pull your_image_name

your_image_name 替换为实际的镜像名称,例如 ctfchallenge/web_challenge

方法二:使用 Dockerfile 构建镜像

如果您有题目的源码和 Dockerfile,需要自行构建镜像:

bash 复制代码
cd /path/to/your/ctf_challenge
sudo docker build -t your_image_name .
  • cd 命令切换到包含 Dockerfile 的目录。
  • -t your_image_name 为构建的镜像指定一个名称。

step 6 运行

使用以下命令运行容器:

bash 复制代码
sudo docker run -d -p HostPort:ContainerPort --name ContainerName your_image_name
  • -d:后台运行容器(守护态)。
  • -p HostPort:ContainerPort:将宿主机端口映射到容器端口。
  • --name ContainerName:为容器指定一个名称,方便管理。

示例:

如果您的题目在容器的 80 端口提供服务,您希望通过宿主机的 1337 端口访问:

bash 复制代码
sudo docker run -d -p 1337:80 --name ctf_challenge your_image_name

查看正在运行的容器:

bash 复制代码
sudo docker ps

查看所有容器(包括已停止的):

bash 复制代码
sudo docker ps -a

停止容器:

bash 复制代码
sudo docker stop ContainerName

启动已停止的容器:

bash 复制代码
sudo docker start ContainerName

删除容器:

bash 复制代码
sudo docker rm ContainerName

查看镜像列表:

bash 复制代码
sudo docker images

删除镜像:

bash 复制代码
sudo docker rmi your_image_name

step 7 进入容器获得shell

docker exec 命令允许在正在运行的容器中执行命令或启动交互式 Shell,从而直接对容器内部进行操作。

获取容器 ID 或名称

首先,您需要知道容器的名称或 ID,可以使用以下命令查看正在运行的容器:

bash 复制代码
sudo docker ps

您将看到类似以下的输出:

复制代码
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                  NAMES
abc123def456   your_image_name   "/bin/sh -c 'service..."   15 minutes ago   Up 15 minutes   0.0.0.0:1337->80/tcp   ctf_challenge

进入容器的交互式 Shell

使用 docker exec 命令进入容器的交互式终端:

bash 复制代码
sudo docker exec -it ContainerName /bin/bash

ContainerName 替换为您的容器名称或 ID,例如:

bash 复制代码
sudo docker exec -it ctf_challenge /bin/bash

在容器内操作文件

现在,您已经进入了容器内部,可以像操作普通 Linux 系统一样操作文件系统。例如:

bash 复制代码
cd /path/to/your/directory
ls -la

当您完成操作后,输入 exit 退出容器终端:

bash 复制代码
exit

使用 docker cp 复制文件

docker cp 命令允许在宿主机和容器之间复制文件或目录。

2.1 从宿主机复制文件到容器

语法:

bash 复制代码
sudo docker cp /宿主机/的文件/或目录 容器名称:/容器内的目标路径

示例:

bash 复制代码
sudo docker cp /home/user/flag.txt ctf_challenge:/app/flag.txt

将宿主机的 /home/user/flag.txt 复制到容器内的 /app/flag.txt

2.2 从容器复制文件到宿主机

语法:

bash 复制代码
sudo docker cp 容器名称:/容器内的文件/或目录 /宿主机/的目标路径

示例:

bash 复制代码
sudo docker cp ctf_challenge:/app/logs /home/user/container_logs

将容器内的 /app/logs 目录复制到宿主机的 /home/user/container_logs

相关推荐
芷栀夏2 分钟前
CANN开源实战:基于DrissionPage构建企业级网页自动化与数据采集系统
运维·人工智能·开源·自动化·cann
寄存器漫游者25 分钟前
Linux 软件编程 - IO 编程
linux·运维·spring
charlotte1024102428 分钟前
高并发:关于在等待学校教务系统选课时的碎碎念
java·运维·网络
gaize121344 分钟前
Moltbot(Clawdbot) 专属轻量服务器
运维·服务器
鸽芷咕2 小时前
DrissionPage 成 CANN 仓库爆款自动化工具:背后原因何在?
运维·python·自动化·cann
枷锁—sha2 小时前
【SRC】SQL注入快速判定与应对策略(一)
网络·数据库·sql·安全·网络安全·系统安全
池央2 小时前
CANN 算子生态的深度演进:稀疏计算支持与 PyPTO 范式的抽象层级
运维·人工智能·信号处理
OJAC1112 小时前
当所有人都在说“运维稳了”,近屿智能看到了另一种可能
运维
人鱼传说2 小时前
docker desktop是一个好东西
运维·docker·容器
阿梦Anmory3 小时前
Ubuntu配置代理最详细教程
linux·运维·ubuntu