0%

Kernel PWN——内核编译以及模拟

编译

内核版本下载,这里随便选一个LTS版本

1
curl -O -L https://mirrors.tuna.tsinghua.edu.cn/kernel/v5.x/linux-5.4.98.tar.xz

下载完解压进入对应的目录来配置一些选项

1
2
3
cd linux-5.4.98/
make menuconfig # Kernel hacking -> Compile-time checks and compiler options -> [*]Compile the kernel with debug info
make -j3 bzImage

编译好后会显示如下信息

1
Kernel: arch/x86/boot/bzImage is ready  (#1)

编译便到此结束

QEMU模拟

这里首先需要安装busybox来模拟文件系统

1
2
wget https://busybox.net/downloads/busybox-1.32.1.tar.bz2
tar -jxf busybox-1.32.1.tar.bz2

然后进行配置

1
2
3
4
make menuconfig
# Setttings -> [*] Build static binary (no shared libs)
# Linux System Utilities -> [] Support mounting NFS file systems on Linux < 2.6.23 (NEW)
# Networking Utilities -> [] inetd

编译

1
make -j3

创建_install目录

1
make install

_install 目录下创建以下文件夹

1
mkdir -p  proc sys dev etc/init.d

创建init启动脚本

1
2
3
4
5
6
7
8
9
10
#!/bin/sh
echo "INIT SCRIPT"
mkdir /tmp
mount -t proc none /proc
mount -t sysfs none /sys
mount -t devtmpfs none /dev
mount -t debugfs none /sys/kernel/debug
mount -t tmpfs none /tmp
echo -e "Boot took $(cut -d' ' -f1 /proc/uptime) seconds"
setsid /bin/cttyhack setuidgid 1000 /bin/sh

随后在该目录下打包文件系统

1
2
find . | cpio -o --format=newc > ../rootfs.img
# 打包的文件系统在上一级目录下

启动!

启动脚本

1
2
3
4
5
6
7
8
9
#!/bin/sh
qemu-system-x86_64 \
-m 64M \
-nographic \
-kernel ./bzImage \
-initrd ./rootfs.img \
-append "root=/dev/ram rw console=ttyS0 oops=panic panic=1 kaslr" \
-smp cores=2,threads=1 \
-cpu kvm64

upload successful