Creating a Kernel Pwn Challenge : Setup and Deployment

For this article I will be heavily referencing these 2 blogs for their scripts and setup

Building your own Linux Kernel

Installing Buildroot

Head over to Buildroot and install a copy of it and extract it.

Configuration

Inside the directory, run

To change the Buildroot configurations, run

  • Go to Kernel > Kernel Version and select the kernel version that you want. If the version that you want is not specified in here, you will need to select Custom Version . Then, go back to Kernel Version and set your custom version. Then, back to main menu and go to Toolchain > Custom kernel header series and set to your desired version.

  • Go to Filesystem images to select the type of filesystem image you want (cpio or ext2/3/4)

Next, we will configure the Linux kernel, run

  • Enable kernel-level debugging features by selecting Kernel hacking > Kernel debugging

  • Enable kernel debug symbols at Compile-time checks and compiler options -> Debug information -> Rely on the toolchain's implicit default DWARF version

  • Sometimes, you might want to patch the kernel to make it easier to exploit. You should have the headers at /output/build/linux-headers-X where you can patch the code. I have not tested this yet so I will not cover this.

Then, configure some BusyBox user utilities

  • Select Runtime utilities -> setuidgid which we will use later

  • Select Shells -> cttyhack

Finally, to finish building your linux kernel, run

If everything ran successfully, you should see your Linux Kernel (bzImage) and filesystem image (ext or cpio) in <BUILDROOT>/output/images and vmlinux with debugging symbols at `<BUILDROOT>/output/build/linux-<VERSION>/ .

Running your Kernel with Qemu

The scripts from https://r1ru.github.io/posts/0/#how-to-run-the-linux-kernel-with-qemu works fine. Mount the filesystem and create a /init file in mounted fs and add the contents

init

Remember to make this file executable. Next, create a script to run it

run.sh

Writing your own vulnerable Kernel module

This is the part where you write the code for your kernel module. I wont cover how to write this as there are plenty of resouces online. Once you are done writing and ready to compile, create a Makefile

Makefile

This is important to ensure you are compiling against the headers of the linux kernel that is being used. If all is successful, you should have your vuln.ko.

Deployment to Server

Before deploying to server, mount your file system again and modify /etc/shadow . Look for the line root:::::::: and change it to root:*:::::::. This will disable password login to root so players cant do a su root.

Next, add your kernel module into the file system. Then, edit your init file to load the module at startup. Add the lines

That /dev/vuln is if you registered a new device in ur module.

Dockerfile
deploy.sh

This method expects the players to be able to host their binary up to download. This can be easily done using ngrok.

Final words

Thanks for reading! If you notice any misconfigurations or want to suggest improvements, you can drop me a message

Last updated