Linux kernel Exploit 内核漏洞学习(0)-环境安装
字数 1284 2025-08-04 08:17:33
Linux Kernel Exploit 环境搭建与调试指南
前言
本指南详细介绍了Linux内核漏洞学习环境的搭建过程,包括内核编译、文件系统构建、QEMU虚拟机配置以及调试方法。这是学习Linux内核漏洞利用的基础准备工作。
1. 内核编译
1.1 准备工作
- 从Linux内核官网下载内核源代码
- 安装必要的依赖项:
sudo apt-get install git fakeroot build-essential ncurses-dev xz-utils libssl-dev bc
1.2 配置内核
- 进入解压后的内核源代码目录
- 运行配置菜单:
make menuconfig - 关键配置选项:
- Kernel Hacking:
- 选中
Compile the kernel with debug info - 选中
Compile the kernel with frame pointers
- 选中
- Processor type and features:
- 去掉
Paravirtualized guest support
- 去掉
- Kernel Hacking:
- 保存配置为
.config
1.3 编译内核
-
开始编译(使用多核加速):
make -j4注:根据CPU核心数调整-j参数
-
完成后续编译步骤:
make all make modules -
获取编译结果:
- 内核镜像:
./arch/x86/boot/bzImage - 调试符号文件:源码根目录下的
vmlinux
- 内核镜像:
2. 构建文件系统
2.1 编译BusyBox
-
下载并解压BusyBox:
wget https://busybox.net/downloads/busybox-1.31.0.tar.bz2 tar -jxvf busybox-1.31.0.tar.bz2 -
配置BusyBox:
make menuconfig关键配置:
- Busybox Settings > Build Options:
- 选中
Build Busybox as a static binary
- 选中
- 关闭以下选项:
Linux System Utilities > Support mounting NFS file systemNetworking Utilities > inetd (Internet超级服务器)
- Busybox Settings > Build Options:
-
编译并安装:
make install
2.2 创建根文件系统
-
在BusyBox的
_install目录中创建必要目录:cd _install mkdir proc sys dev etc etc/init.d -
创建启动脚本
etc/init.d/rcS:#!/bin/sh mount -t proc none /proc mount -t sysfs none /sys /sbin/mdev -s并设置可执行权限:
chmod +x etc/init.d/rcS -
生成文件系统镜像:
find . | cpio -o --format=newc > ../rootfs.img
3. 使用QEMU运行内核
3.1 基本启动命令
qemu-system-x86_64 \
-kernel ~/tools/linux-5.2.1/arch/x86_64/boot/bzImage \
-initrd ~/tools/busybox-1.31.0/rootfs.img \
-append "console=ttyS0 root=/dev/ram rdinit=/sbin/init" \
-cpu kvm64,+smep,+smap \
-nographic \
-gdb tcp::1234
3.2 参数说明
-kernel: 指定编译好的内核镜像路径-initrd: 指定构建的文件系统镜像-append: 内核启动参数-cpu kvm64,+smep,+smap: 启用SMEP和SMAP保护机制-nographic: 不使用图形界面-gdb tcp::1234: 开启GDB调试服务器,监听1234端口
4. 内核模块操作
- 加载驱动:
insmod module.ko - 卸载驱动:
rmmod module - 查看已加载模块:
lsmod
5. GDB调试配置
-
连接QEMU的GDB服务器:
gdb vmlinux (gdb) target remote :1234 -
加载内核模块符号(需知道模块加载地址):
(gdb) add-symbol-file module.ko 0xffffffffc0000000
注意事项
- 编译过程可能会遇到各种错误,需要根据具体报错信息解决
- 依赖项可能不完整,编译过程中可能需要安装额外的包
- 内核编译耗时较长,请耐心等待
- 建议使用较新版本的内核和BusyBox
通过以上步骤,您已经建立了一个可用于Linux内核漏洞学习和调试的环境。后续可以进行内核模块开发、漏洞利用技术研究等工作。