Skip to content

Instantly share code, notes, and snippets.

@TheFlash2k
Created April 7, 2025 10:30
Show Gist options
  • Save TheFlash2k/d0527dbb6c1b6e19f3886b230e592f85 to your computer and use it in GitHub Desktop.
Save TheFlash2k/d0527dbb6c1b6e19f3886b230e592f85 to your computer and use it in GitHub Desktop.
Makefile for pwn-kernel chal building
# Kernel Pwn-Chal Build @TheFlash2k
.ONESHELL:
KERN_VERSION := 6.6.85
KERN_BASE_VER := 6
BUSYBOX_VER := 1.32.1
KERNEL_NAME := bzImage
ROOTFS_NAME := rootfs.cpio
ROOTFS_DIR := root
KERNEL_DRIVER := kernel101
KERNEL_DIR := $(PWD)/linux-$(KERN_VERSION)
CHAL_DIR := chal
# required when building the ko
obj-m += $(KERNEL_DRIVER).o
all: kernel rootfs driver
kernel:
ifneq ("$(wildcard $(KERNEL_NAME))", "")
@echo "[*] Kernel $(KERNEL_NAME) already exists!"
exit
endif
ifneq ("$(wildcard $(KERNEL_DIR))", "")
@echo "[*] Kernel folder linux-$(KERN_VERSION) already found."
else
wget https://cdn.kernel.org/pub/linux/kernel/v$(KERN_BASE_VER).x/linux-$(KERN_VERSION).tar.xz
tar -xvf "linux-$(KERN_VERSION).tar.xz"
rm -f "linux-$(KERN_VERSION).tar.xz"
cd linux-$(KERN_VERSION)
@echo "Building the kernel v$(KERN_VERSION)"
make defconfig && make -j `nproc`
cp ./arch/x86_64/boot/bzImage "$(CURR_DIR)"
endif
rootfs:
ifneq ("$(wildcard $(ROOTFS_NAME))", "")
@echo "[*] Rootfs $(ROOTFS_NAME) already exists!"
else
mkdir $(ROOTFS_DIR) && cd $(ROOTFS_DIR)
wget https://busybox.net/downloads/busybox-$(BUSYBOX_VER).tar.bz2
tar xvf ./busybox-$(BUSYBOX_VER).tar.bz2
rm -f ./busybox-$(BUSYBOX_VER).tar.bz2
cd busybox-$(BUSYBOX_VER)
@read -p "Settings-> Build Options-> Build static binary (no shared libs) [For static binaries]. Press enter to continue." cont;
make menuconfig
make -j`nproc`
make install
cd _install
mkdir -p proc sys dev etc
touch init
chmod +x init
find . | cpio -o --format=newc > "../../../$(ROOTFS_NAME)"
endif
driver:
ifneq ("$(wildcard $(KERNEL_DRIVER))", "")
@echo "[*] Kernel Driver already exists!"
else
make -C $(KERNEL_DIR) M=$(PWD) modules
endif
chal:
mkdir -p "$(CHAL_DIR)"
cp "$(KERNEL_NAME)" "$(ROOTFS_NAME)" "$(CHAL_DIR)"
cd "$(CHAL_DIR)"
wget https://raw.githubusercontent.com/TheFlash2k/pwn-chal/refs/heads/master/samples/kernel/Makefile
wget https://raw.githubusercontent.com/TheFlash2k/pwn-chal/refs/heads/master/samples/kernel/Dockerfile
sed -i 's/initramfs.cpio.gz/$(ROOTFS_NAME)/g' Dockerfile
sed -i 's/bzImage/$(KERNEL_NAME)/g' Dockerfile
clean_all: clean
# It will just clean the directories and not the actual bzImage/rootfs
@read -p "Do you want to clean the Kernel Directory? (y/n) " clean_kern; \
if [ "$$clean_kern" != "y" ]; then \
echo "Skipping kernel cleaning"; \
else \
echo "Cleaning Kernel Directory..."; \
rm -f "linux-$(KERN_VERSION).tar.xz"
rm -rf "linux-$(KERN_VERSION)"
fi
@read -p "Do you want to clean the Root Filesystem? (y/n) " clean_rootfs; \
if [ "$$clean_rootfs" != "y" ]; then \
echo "Skipping rootfs cleaning"; \
else \
echo "Cleaning Rootfs Directory..."; \
rm -rf "$(ROOTFS_DIR)"
fi
clean:
# We don't clean the FS and everything else. Just challenge.
make -C $(KERNEL_DIR) M=$(PWD) clean
rm -rf *.o *.mod.* *.symvers *.order
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment