CTF Pwn 静态/动态靶机模版互联网上已经有较为完善的教程文章,单个异构程序的 IoT 题目只需要在互联网已有模版基础上嵌套 qemu-user 即可解决,需要完整异架构系统的 IoT 题目环境部署方案尚存在空缺。本文针对这个问题,给出一套解决方案。
宿主机运行 docker Ubuntu 等基础镜像,docker 内安装 qemu-system 运行完整异构系统,qemu 内运行题目相关服务程序。通过配置 qemu-system 实现动态靶机。
所需要文件下载地址:https://people.debian.org/~aurel32/qemu/。
部署不同环境所需要文件数量不一致,具体看后文,或下载站点 README 。
vmlinuz :系统内核
initrd :启动镜像,启动相关的驱动模块
hda :磁盘镜像
三者固定搭配,挑选一种下载即可:
-kernel vmlinuz-2.6.32-5-versatile -initrd initrd.img-2.6.32-5-versatile -hda debian_squeeze_armel_standard.qcow2
-kernel vmlinuz-2.6.32-5-versatile -initrd initrd.img-2.6.32-5-versatile -hda debian_squeeze_armel_desktop.qcow2
-kernel vmlinuz-3.2.0-4-versatile -initrd initrd.img-3.2.0-4-versatile -hda debian_wheezy_armel_standard.qcow2
-kernel vmlinuz-3.2.0-4-versatile -initrd initrd.img-3.2.0-4-versatile -hda debian_wheezy_armel_desktop.qcow2
vmlinuz :系统内核
hda :磁盘镜像
-kernel vmlinux-2.6.32-5-4kc-malta -hda debian_squeeze_mips_standard.qcow2
-kernel vmlinux-3.2.0-4-4kc-malta -hda debian_wheezy_mips_standard.qcow2
-hda debian_squeeze_powerpc_standard.qcow2
-hda debian_squeeze_powerpc_desktop.qcow2
-hda debian_wheezy_powerpc_standard.qcow2
-hda debian_wheezy_powerpc_desktop.qcow2
以下为参考出题环境:
本地 Ubuntu 虚拟机
Qemu-system
docker/docker-compose :用于提供异构环境
scp : 用于向 qemu 虚拟机传输文件
arm-linux-gnueabi-gcc :用于异架构程序编译(qemu 内默认没有编译环境)
远程 VPS (可选):用于部署验证
以下为参考顺序,自行根据实际调整:
最重要一步:在 docker 将 qemu 虚拟机磁盘文件(qcow2)拷贝出来并保存,以后靶机启动的就是这个磁盘文件,就能恢复好搭建完毕的环境
使用 qemu-system 的 user 网络模式(类似 vmware nat 模式),qemu 启动命令:
#!/bin/bash
qemu-system-arm -M versatilepb \
-kernel vmlinuz-3.2.0-4-versatile \
-initrd initrd.img-3.2.0-4-versatile \
-hda debian_wheezy_armel_standard.qcow2 \
-append "root=/dev/sda1 console=tty0" \
-net user,hostfwd=tcp::8888-:22,hostfwd=tcp::8080-:8080,hostfwd=tcp::1234-:1234,restrict=no \
-net nic \
-nographic
-net nic:让 qemu 自动配置一张满足最基础需求的虚拟网卡 eth0 ,ip 位置默认为 10.0.2.15
-net user,hostfwd=tcp::8888-:22,hostfwd=tcp::8080-:8080,hostfwd=tcp::1234-:1234:
user 指定使用 user 模式
hostfwd=tcp::8888-:22,hostfwd=tcp::8080-:8080,hostfwd=tcp::1234-:1234 映射宿 docker 端口到 qemu 虚拟机中,多端口则按需添加
然后在 Dockerfile 开放对应端口:
# challenge
EXPOSE 8080
# ssh
EXPOSE 8888
# gdbserver
EXPOSE 1234
docker-compose.yml 配置宿主机到 docker 的映射端口:
version: "2"
services:
game:
build: .
restart: unless-stopped
ports:
- "12000:8080"
- "8877:8888"
- "1234:1234"
至此,启动服务后,可以通过 <宿主机IP:12000> 等端口访问 qemu 内对应服务。
qemu-system 挂载目标需要是文件夹。各个竞赛平台实现动态 flag 传入方法不尽相同,但通常出题模版会给出 run.sh 文件(供作者在靶机开启后进行自定义操作),解决思路是:
在 Dockerfile 创建固定 flag 文件夹并做好权限管理:/home/ctf/flag
COPY ./flag /home/ctf/flag/flag在 run.sh 将竞赛平台 flag 写入 /home/ctf/flag/flag
Qemu 启动命令:
#!/bin/bash
qemu-system-arm -M versatilepb \
-kernel vmlinuz-3.2.0-4-versatile \
-initrd initrd.img-3.2.0-4-versatile \
-hda debian_wheezy_armel_standard.qcow2 \
-hdb fat:/home/ctf/flag \
-append "root=/dev/sda1 console=tty0" \
-net user,hostfwd=tcp::8888-:22,hostfwd=tcp::8080-:8080,hostfwd=tcp::1234-:1234,restrict=no \
-net nic \
-nographic
-hdb fat:/home/ctf/flag :将 /home/ctf/flag 文件挂载,默认权限是只读
Qemu 开机后需要再虚拟机内挂载文件夹:
# 创建对应的挂载点
mkdir -p /home/ctf/flag
# 挂载
mount /dev/sdb1 /home/ctf/flag
因为 qemu 虚拟机会随靶机开关而开关,需要配置开机自动挂载 flag 。
在 /etc/init.d 创建文件 mount-flag.sh 写入以下内容:
#! /bin/sh
### BEGIN INIT INFO
# Provides: SkYe231_mount-flag
# Required-Start: $local_fs $network
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Desc ription: mount service
# Desc ription: mount docker flag
### END INIT INFO
mkdir -p /home/ctf/flag
mount /dev/sdb1 /home/ctf/flag
加上权限:
chmod +x mount-flag.sh
添加启动事件:
insserv -d mount-flag.sh
将初步配置的 qemu 虚拟机磁盘镜像提取出来,替换下载的,后续启动的都是这个已配置的镜像
docker cp xxx:/home/ctf/xxx.vmdk . run.sh 写入 qemu 启动命令:
#!/bin/bash
qemu-system-arm -M versatilepb \
-kernel vmlinuz-3.2.0-4-versatile \
-initrd initrd.img-3.2.0-4-versatile \
-hda debian_wheezy_armel_standard.qcow2 \
-hdb fat:/home/ctf/flag \
-append "root=/dev/sda1 console=tty0" \
-net user,hostfwd=tcp::8888-:22,hostfwd=tcp::8080-:8080,hostfwd=tcp::1234-:1234,restrict=no \
-net nic \
-nographic
Dockerfile 配置启动、暴露端口等:
COPY ./run.sh /home/ctf
COPY debian_wheezy_armel_standard.qcow2 /home/ctf
COPY initrd.img-3.2.0-4-versatile /home/ctf
COPY vmlinuz-3.2.0-4-versatile /home/ctf
COPY ./flag /home/ctf/flag/flag
# 禁用QEMU音频配置
ENV QEMU_AUDIO_DRV=none
CMD exec /bin/bash -c "./run.sh; trap : TERM INT; sleep infinity & wait"
# challenge
EXPOSE 8080
# ssh
EXPOSE 8888
# gdbserver
EXPOSE 8080
docker-compose.yml 配置宿主机映射端口:
version: "2"
services:
game:
build: .
restart: unless-stopped
ports:
- "12000:8080"
- "8888:8888"
- "1234:1234"
每次访问启动全新环境:docker xintd 守护 qemu 虚拟机
进程崩溃后恢复沿用此前环境:qemu 虚拟机内守护进程
#!/bin/bash
#ulimit -t 55 #max cpu using
#ulimit -m 524288 #max memory
#ulimit -u 1500 #max process
qemu-system-arm -M versatilepb \
-kernel vmlinuz-3.2.0-4-versatile \
-initrd initrd.img-3.2.0-4-versatile \
-hda debian_wheezy_armel_standard.qcow2 \
-hdb fat:/home/ctf/flag \
-append "root=/dev/sda1 console=tty0" \
-net user,hostfwd=tcp::8888-:22,hostfwd=tcp::8080-:8080,hostfwd=tcp::1234-:1234,restrict=no \
-net nic \
-nographic
service ctf
{
disable = no
socket_type = stream
protocol = tcp
wait = no
user = root
type = UNLISTED
port = 8080
bind = 0.0.0.0
server = /usr/sbin/chroot
# replace helloworld to your program
server_args = --userspec=1000:1000 /home/ctf timeout 50 ./run.sh
banner_fail = /etc/banner_fail
# safety options
per_source= 10 # the maximum instances of this service per source IP address
rlimit_cpu= 60 # the maximum number of CPU seconds that the service may use
rlimit_as = 1024M # the Address Space resource limit for the service
#access_times = 2:00-9:00 12:00-24:00
#Instances=20 #process limit
#per_source=5 #link ip limit
#log warning die
log_on_success = PID HOST EXIT DURATION
log_on_failure =HOST ATTEMPT
log_type =FILE /var/log/myservice.log 8388608 15728640
}
server_args :修改调整为 run.sh
其他配置可以参考,根据需要客制化
使用 shell 脚本实现守护进程,配置为开机自启动项
/etc/init.d 创建守护进程 shell 脚本 challenage-start-agent.sh,写入内容:
#!/bin/bash
PROGRAM=httpd
while true; do
RESULT=`ps aux | grep -w ${PROGRAM} | grep -v grep | wc -l`
if [ ${RESULT} = 0 ];then
# echo "${PROGRAM} was killed"
cd /var/www/html
./httpd 2>/dev/null 1>&2 &
fi
sleep 5s
done
每 5s 检查 pid 是否存在指定进程,只要进程数等于 0 则重新启动
守护进程脚本名与 ${PROGRAM} 不应该存在相同,否则误判进程数
加上权限:
chmod +x challenage-start-agent.sh /etc/init.d 创建开机启动配置文件 challenage-start-agent,写入内容:
#!/bin/bash
### BEGIN INIT INFO
# Provides: SkYe231_challenage-start-agent
# Required-Start: $local_fs $syslog
# Required-Stop: $local_fs $syslog
# Should-Start: $portmap
# Should-Stop: $portmap
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: challenage start agent
### END INIT INFO
cd /etc/init.d
nohup ./challenage-start-agent.sh &
Provides :提供者,需要唯一不重复
剩余配置项是启动时间等,有需要自己客制化
加上权限:
chmod +x challenage-start-agent 添加启动事件:
insserv -d challenage-start-agent