Kernel Pwn In CTF
简单分析一下 CTF Kernel Pwn 题目的形式,以 2017 CISCN babydrive 为例。
先对文件包解压➜ example ls
babydriver.tar
➜ example file babydriver.tar
babydriver.tar: POSIX tar archive
➜ example tar -xvf babydriver.tar
boot.sh
bzImage
rootfs.cpio
➜ example ls
babydriver.tar boot.sh bzImage rootfs.cpio
得到 boot.sh,bzImage,rootfs.cpio 三个文件
boot.sh➜ example cat -n boot.sh
1 #!/bin/bash
2 qemu-system-x86_64 \
3 -initrd rootfs.cpio \
4 -kernel bzImage \
5 -append 'console=ttyS0 root=/dev/ram oops=panic panic=1' \
6 -enable-kvm \
7 -monitor /dev/null \
8 -m 64M \
9 --nographic \
10 -smp cores=1,threads=1 \
11 -cpu kvm64,+smep
boot.sh 文件是用来启动这个程序的,调用 qemu 来加载 rootfs.cpio 与 bzImage 运行起来
上面的参数都是 qemu 的参数-initrd rootfs.cpio,使用 rootfs.cpio 作为内核启动的文件系统
-kernel bzImage,使用 bzImage 作为 kernel 映像
-cpu kvm64,+smep,设置 CPU 的安全选项,这里开启了 smep
-m 64M,设置虚拟 RAM 为 64M,默认为 128M
bzImage➜ example file bzImage
bzImage: Linux kernel x86 boot executable bzImage, version 4.4.72 (atum@ubuntu) #1 SMP Thu Jun 15 19:52:50 PDT 2017, RO-rootFS, swap_dev 0x6, Normal VGA
bzImage 是经压缩过的 linux 内核文件
rootfs.cpio➜ example file rootfs.cpio
rootfs.cpio: gzip compressed data, last modified: Tue Jul 4 08:39:15 2017, max compression, from Unix
这是一个 linux 内核文件系统压缩包,我们可以对其解压并重新压缩,从而修改这个系统的文件
新建一个文件夹来解压➜ example mkdir fs && cd fs
➜ fs cp ../rootfs.cpio ./rootfs.cpio.gz
➜ fs gunzip ./rootfs.cpio.gz
➜ fs cpio -idmv < rootfs.cpio
.
etc
etc/init.d
etc/passwd
etc/group
bin
......
linuxrc
home
home/ctf
5556 blocks
➜ fs ll
total 2.8M
drwxrwxr-x 2 mask mask 4.0K 1 月 20 12:16 bin
drwxrwxr-x 3 mask mask 4.0K 1 月 20 12:16 etc
drwxrwxr-x 3 mask mask 4.0K 1 月 20 12:16 home
-rwxrwxr-x 1 mask mask 396 6 月 16 2017 init
drwxr-xr-x 3 mask mask 4.0K 1 月 20 12:16 lib
lrwxrwxrwx 1 mask mask 11 1 月 20 12:16 linuxrc -> bin/busybox
drwxrwxr-x 2 mask mask 4.0K 6 月 15 2017 proc
-rwxrwxr-x 1 mask mask 2.8M 1 月 20 12:15 rootfs.cpio
drwxrwxr-x 2 mask mask 4.0K 1 月 20 12:16 sbin
drwxrwxr-x 2 mask mask 4.0K 6 月 15 2017 sys
drwxrwxr-x 2 mask mask 4.0K 6 月 15 2017 tmp
drwxrwxr-x 4 mask mask 4.0K 1 月 20 12:16 usr
这些就是运行起来后这个系统拥有的文件,查看这个 init 文件➜ fs cat -n ./init
1 #!/bin/sh
2
3 mount -t proc none /proc
4 mount -t sysfs none /sys
5 mount -t devtmpfs devtmpfs /dev
6 chown root:root flag
7 chmod 400 flag
8 exec 0
9 exec 1>/dev/console
10 exec 2>/dev/console
11
12 insmod /lib/modules/4.4.72/babydriver.ko
13 chmod 777 /dev/babydev
14 echo -e "\nBoot took $(cut -d' ' -f1 /proc/uptime) seconds\n"
15 setsid cttyhack setuidgid 1000 sh
16
17 umount /proc
18 umount /sys
19 poweroff -d 0 -f
看到第 12 行的 insmod /lib/modules/4.4.72/babydriver.ko,意味着要调试这个 ko 文件,使用 IDA 对其进行分析,利用漏洞
对此文件系统进行打包也是要在这个目录下进行➜ fs find . | cpio -o --format=newc > rootfs.cpio
cpio: File ./rootfs.cpio grew, 43008 new bytes not copied
5640 blocks
vmlinux
有些题目会给 vmlinux 这个文件,这是编译出来的最原始的内核文件,未压缩的,是个 ELF 形式,方便找 gadget
可以使用一个工具来从 bzImage 中导出 vmlinux,extract-vmlinux➜ example ./extarct-vmlinux ./bzImage > vmlinux
➜ example file bzImage
bzImage: Linux kernel x86 boot executable bzImage, version 4.4.72 (atum@ubuntu) #1 SMP Thu Jun 15 19:52:50 PD