1.下载kernel源码
$ wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.11.tar.xz
$ tar -xvf linux-5.11.tar.xz
$ make menuconfig
# 安装依赖库
$ sudo apt-get install git fakeroot build-essential ncurses-dev xz-utils libssl-dev bc flex libelf-dev bison
$ sudo apt-get install kernel-package build-essential libncurses5-dev fakeroot zstd
要选中如下选项(一般是默认选中,直接保存就行):
- Kernel hacking —-> Kernel debugging
- Kernel hacking —-> Compile-time checks and compiler options —-> Compile the kernel with debug info
- Kernel hacking —-> Generic Kernel Debugging Instruments —> KGDB: kernel debugger
- kernel hacking —-> Compile the kernel with frame pointers
Kernel hacking --->
[*] Kernel debugging
Compile-time checks and compiler options --->
[*] Compile the kernel with debug info
[*] Provide GDB scripts for kernel debuggin
Processor type and features ---->
[] Randomize the address of the kernel image (KASLR)
2.编译命令
$ cd linux-4.14.191
$ export ARCH=x86
$ make x86_64_defconfig
$ make menuconfig
$ make bzImage
# 可以使用make bzImage -j4加速编译
编译错误解决:
报错出现如下内容时:
cc1: error: code model kernel does not support PIC mode
make[1]: *** No rule to make target 'debian/canonical-certs.pem', needed by 'certs/x509_certificate_list'. Stop
修改Makefile文件中:
KBUILD_CFLAGS 的尾部添加选项 -fno-pie
CC_USING_FENTRY 项添加 -fno-pic
以及在 .config 文件中找到这一项,等于号后面的值改为 ""
CONFIG_SYSTEM_TRUSTED_KEYS="debian/canonical-certs.pem"
CONFIG_SYSTEM_TRUSTED_KEYS="" <- 修改后
修改
CONFIG_DEBUG_INFO_BTF="y"
修改后:
CONFIG_DEBUG_INFO_BTF="n"
报错
warning: the frame size of 1040 bytes is larger than 1024 bytes
使用 make menuconfig
---> hack kernel
---> Compile-time checks and compiler options
---> Warn for stack frames larger than 修改它为 4096
编译完成后输出如下信息为成功:
Kernel: arch/x86/boot/bzImage is ready (#1)
- mlinux: 带符号的内核文件
当前目录下生成vmlinux
$ file vmlinux
vmlinux: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, BuildID[sha1]=f1fc85f87a5e6f3b5714dad93a8ac55fa7450e06, with debug_info, not stripped
- bzImage: 压缩内核镜像
在arch/x86/boot目录下提取bzImage
$ file arch/x86/boot/bzImage
arch/x86/boot/bzImage: Linux kernel x86 boot executable bzImage, version 5.11.0 (root@iZf3ye3at4zthpZ)
3.获取busybox
busybox是一个集成了三百多个常用Linux命令和工具的软件
官网选择版本:https://busybox.net/downloads/
当前使用如下版本:
$ wget https://busybox.net/downloads/busybox-1.33.0.tar.bz2
解压:
$ tar -jxvf busybox-1.33.0.tar.bz2
4.编译busybox源码
进入配置界面
$ make menuconfig
勾选Settings —-> Build static binary file (no shared lib)
编译
$ make install
编译完成后会生成一个_install目录,这个目录有用。
5.构建磁盘镜像
# 初始化文件系统
$ cd _install
$ mkdir -pv {bin,sbin,etc,proc,sys,home,lib64,lib/x86_64-linux-gnu,usr/{bin,sbin}}
$ touch etc/inittab
$ mkdir etc/init.d
$ touch etc/init.d/rcS
$ chmod +x ./etc/init.d/rcS
# 配置初始化脚本
# 首先配置etc/inttab
::sysinit:/etc/init.d/rcS
::askfirst:/bin/ash
::ctrlaltdel:/sbin/reboot
::shutdown:/sbin/swapoff -a
::shutdown:/bin/umount -a -r
::restart:/sbin/init
# 在上面的文件中指定了系统初始化脚本,因此接下来配置etc/init.d/rcS,写入如下内容:
#!/bin/sh
mount -t proc none /proc
mount -t sys none /sys
/bin/mount -n -t sysfs none /sys
/bin/mount -t ramfs none /dev
/sbin/mdev -s
主要是配置各种目录的挂载
也可以在根目录下创建init文件,写入如下内容:
```
#!/bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
mount -t devtmpfs devtmpfs /dev
exec 0</dev/console
exec 1>/dev/console
exec 2>/dev/console
echo -e "\nBoot took $(cut -d' ' -f1 /proc/uptime) seconds\n"
setsid cttyhack setuidgid 1000 sh
umount /proc
umount /sys
poweroff -d 0 -f
```
# 给文件添加执行权限:
$ chmod +x ./init
# 配置用户组
$ echo "root:x:0:0:root:/root:/bin/sh" > etc/passwd
$ echo "ctf:x:1000:1000:ctf:/home/ctf:/bin/sh" >> etc/passwd
$ echo "root:x:0:" > etc/group
$ echo "ctf:x:1000:" >> etc/group
$ echo "none /dev/pts devpts gid=5,mode=620 0 0" > etc/fstab
# 在这里建立了两个用户组root和ctf,以及两个用户root和ctf
6.打包文件系统为镜像文件
$ find . | cpio -o --format=newc > ../../rootfs.cpio
…/…/rootfs.cpio 这个位置是自己选的。
如果要修改已经打包好的磁盘镜像可以这么做:
# 解压磁盘镜像
$ cpio -idv < ./rootfs.cpio # 会将镜像中的所有文件解压到当前目录下
# 重打包磁盘镜像
$ find . | cpio -o --format=newc > new_rootfs.cpio
7.使用qemu运行内核
配置启动脚本,首先将bzImage和rootfs.cpio放到同一目录下
编写启动脚本
$ touch boot.sh
# 内容如下
#!/bin/sh
/home/hx/Desktop/qemu-6.2.0/build/qemu-system-x86_64 \
-m 2G \
-kernel bzImage \
-initrd rootfs.cpio \
-monitor /dev/null \
-append "root=/dev/ram ordinit=/init ops=panic panic=1 loglevel=3 quiet nokaslr console=ttyS0" \
-cpu kvm64,+smep \
-smp cores=2,threads=1 \
-netdev user,id=t0, -device e1000,netdev=t0,id=nic0 \
-nographic \
-s
参数说明:
-m : "虚拟机内存大小"
-kernel: "内存镜像路径"
-initrd: "磁盘镜像路径"
-append: "附加参数选项"
nokalsr : "关闭内核地址随机化"
rdinit: "指定初始化进程,/sbin/init进程会默认以/etc/init.d/rcS作为启动脚本"
loglevel=3&quiet:"不输出启动log"
console=ttyS0:"指定终端为/dev/ttyS0,这样一启动就能进入终端界面"
-monitor: "将监视器重定向到主机设备/dev/null,这里重定向至null主要是防止CTF中被人给偷了qemu拿flag"
-cpu: "设置CPU安全选项,这里开启了smep保护"
-s: "相当于-gdb tcp::1234的简写"
-S 就是挂起gdbserver,让gdb remote connect it
运行boot.sh 成功启动
gdb调试
$ gdb /usr/src/linux-5.1/vmlinux
$ target remote:1234
$ b start_kernel
$ c
参考链接: