I have been using NixOS on all my devices, so the following blog is about dealing with user-level and kernel-level pwn challenges on NixOS through fish scripts and nix configurations.
I often get asked what ‘lianpwn’ in my WP is. It’s actually just some simple wrappers based on pwncli or pwntools. The specific code can be found in the nix-config repository. Update: You can use pip install lianpwn
User-level Pwn
I have a python module named lianpwn, which is based on pwncli and pwntools. There’re a few lambdas and helper classes defined in it:
After that, here comes the simple template:
Other Dynamic Libraries
It often suffers when coming to missing libs in NixOS, but I’ve got several solutions.
The easiest way is to use nix-shell:
However, the above way can only help compile c code with certain libs, such as libseccomp. The ultimate solution is to get such so from docker and copy to local environment. After that, patch it to any glibc version you want with patchelf. I also have a light tool called patch4pwn.
Kernel-level Pwn
Here’s my kernel pwn template:
Upload
It’s always suffering to upload our compiled elfs to the remote server. The following script is still improving:
Compilation
It’s simple to start a compilation environment:
When compiling loadable kernel modules (LKM, with .ko as filename extension), the first step is to fetch linux source code from https://github.com/gregkh/linux/tags (easier to locate certain linux version).
After that, write c code with following template:
Makefile is also required for LKMs:
New Methods for Packaging Kernel Pwn
Actually we have got good pwn templates for a long period of time, but when reproducing a kernel challenge from bi0sCTF-2024 [1]. I found a brand new configuration for accelerating kernel pwn debugging. If you are challenge setters, you can also refer to this blog to improve challengers’ pwning experience.
Using .img Instead of .cpio
.img files (.ext3 in the mentioned case from bi0sCTF-2024) are much easier to deal with, though their size might be larger than .cpio files.
Provided with rootfs.img, challengers can perform real-time operations on it through following commands:
The challengers can use following commands to create such image files:
Attaching Exploit to Qemu
Instead of copying files to rootfs directory, the palindromatic challenge from bi0sCTF-2024 provides a new way:
run.sh
init
Through the above configuration, we can enter pwn in qemu and run the exploit locally.