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

相关推荐
Starshime10 分钟前
PHP、Apache环境中部署sqli-labs
网络安全
conkl16 分钟前
Apache网页优化实战指南 - 让网站加载速度提升
linux·运维·服务器·开发语言·阿里云·apache
thinkMoreAndDoMore41 分钟前
linux驱动开发(9)- 信号量
linux·运维·驱动开发
程序员阿超的博客1 小时前
云原生核心技术 (9/12): K8s 实战:如何管理应用的配置 (ConfigMap/Secret) 与数据 (Volume)?
云原生·容器·kubernetes
海天胜景1 小时前
nginx 配置返回 文件大小
运维·nginx
五阿哥爱跳舞1 小时前
【环境配置】解决linux每次打开终端都需要source .bashrc文件的问题
linux·运维·服务器
时央1234562 小时前
C#使用Tuple方法实现OpreateResultModel功能
运维·开发语言·c#
爱学习的白杨树2 小时前
Spring Cloud Gateway 介绍
java·运维·开发语言
网易独家音乐人Mike Zhou2 小时前
【Linux应用】Linux系统日志上报服务,以及thttpd的配置、发送函数
linux·运维·服务器·mcu·物联网·嵌入式·iot
bingbingyihao3 小时前
服务自动添加实例工具
linux·运维·bash