You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: main/content/post/how-to-kernel-pwn.md
+90Lines changed: 90 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -47,6 +47,27 @@ This can be done with a custom `init` script. In order to pass a custom `init` s
47
47
48
48
I'd recommend placing most of your custom code in the same place. So, you'll want to copy your compiled `chall.ko` into something like `/challenge/chall.ko`.
49
49
50
+
### Compiling your kernel module
51
+
52
+
This is a more complex issue, but in short, you can use the Kernel's build system to build your challenge against the Kernel's specific headers. An example `Makefile` can be seen below:
53
+
54
+
```Makefile
55
+
obj-m += challenge.o
56
+
57
+
# Disables protections for easy challenge
58
+
ccflags-y += -fno-stack-protector
59
+
ccflags-y += -U_FORTIFY_SOURCE
60
+
ccflags-y += -fno-pie
61
+
ccflags-y += -no-pie
62
+
63
+
all:
64
+
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
65
+
66
+
clean:
67
+
$(MAKE) -C $(KERNELDIR) M=$(PWD) clean
68
+
```
69
+
70
+
50
71
### Writing an init script
51
72
52
73
Assuming you've kept the default settings, and you have a `rootfs.ext4` file, you can simply edit the file at `/sbin/init` to be your desired script. A template like the following is fairly standard to work with, and note that the script must switch to a long running process or the kernel will kill itself.
@@ -90,6 +111,75 @@ ENV JAIL_MEM=384M
90
111
```
91
112
At this point, depending on your build configurations, it may be necessary to tweak `start-qemu.sh` (for instance, to adjust filesystem mounting options or enabling / disabling runtime kernel protections).
92
113
114
+
## Running with docker (but unhinged)!
115
+
116
+
To take this a step further, we can completely automate the build process, so that the entire thing is done in Docker. An example of this can be seen below:
117
+
118
+
```Dockerfile
119
+
FROM debian:unstable@sha256:cc1675ddb1073d19ba9ef6fe9b9c625eceb02fccb9c0f7afbb4e60f16325c91d AS fs_builder
120
+
RUN apt-get update && apt-get install -y which sed make binutils \
RUN sed -i "s/console=tty1/rw nokaslr/" start-qemu.sh
163
+
RUN sed -i "s/-drive file=rootfs.ext2,if=virtio,format=raw/-drive file=rootfs.ext2,if=virtio,format=raw -snapshot/" start-qemu.sh
164
+
COPY entrypoint.sh ./
165
+
ENV FLAG=PWNED{FAKE_FLAG}
166
+
RUN echo "PWNED{FAKE_FLAG}" > /tmp/flag.txt
167
+
EXPOSE 5000
168
+
CMD ["/app/entrypoint.sh"]
169
+
```
170
+
171
+
You'll also need to populate `entrypoint.sh`, `init`, `.config`, `module/challenge.c`, and `module/Makefile`. In this case, my entrypoint looks something like the following:
0 commit comments