Your first kernel bugs, on kernels with SMEP, SMAP and usually KASLR turned off. When the kernel jumps to an address you control, it can simply run code that lives in your own process, with full kernel privileges. That is ret2usr: the easiest way to see what "running in ring 0" means.

What you will learn

  • How a loadable kernel module exposes a device (open/read/write/ioctl) and how to talk to it from C
  • How to debug a guest kernel (-s, gdb, /proc/kallsyms, vmlinux-to-elf/extract-vmlinux)
  • Getting root from kernel context: commit_creds(prepare_kernel_cred(0)) (or &init_cred on newer kernels)
  • Getting back to userland cleanly (swapgs; iretq) with a saved user context
  • Why SMEP/SMAP and KASLR exist, by exploiting kernels that lack them

Prerequisites

Userland pwn basics (stack overflows, calling conventions, writing small static C programs). No kernel background needed.

References



Challenges

CakeCTF 2022 · difficulty ★☆☆☆☆ · module: Ring 0 Basics: ret2usr

A driver that is a bit too trusting of the pointers you hand it, on a kernel with almost no hardening: a first look at running code in ring 0.

Mitigations: none (nopti, nokaslr, qemu64 without SMEP/SMAP)

Running it

Run /challenge/run. It boots the original kernel and root filesystem in QEMU with the serial console on your terminal. Quit with Ctrl-A then X. Put a statically linked exploit at /home/hacker/exploit first; it appears in the VM as /exploit. exploit.c and Makefile are the organizers' starter template, not a solution.

The flag is put where the original challenge kept it, readable only by root in the VM (unless a note above says otherwise). Files in /challenge (kernel, initramfs, sources and the original launcher, where the CTF shipped them): Makefile, README.ja.md, README.md, bzImage, exploit.c, rootfs.cpio, run.sh, src/driver.c.

Original challenge

Description

Introduction to Linux Kernel Exploit :)

* There is a limit to the number of connections. Wait 5 minutes if you cannot connect.

Points

113

Solves

75

Credits

  • Challenge author(s): ptr-yudai, for CakeCTF 2022
  • Compiled by: mito753 in Kernel-Exploit-Dojo, which also has the original handout and writeups

Midnight Sun CTF 2023 Quals · difficulty ★☆☆☆☆ · module: Ring 0 Basics: ret2usr

A speed challenge: a driver trusts the file position as a stack buffer offset, and the mitigations are mostly off.

Mitigations: no SMEP, no SMAP, no KASLR (kernel not relocatable)

Running it

Run /challenge/run. It boots the original kernel and root filesystem in QEMU with the serial console on your terminal. Quit with Ctrl-A then X. Put a statically linked exploit at /home/hacker/exploit first; it appears in the VM as /exploit.

The flag is put where the original challenge kept it, readable only by root in the VM (unless a note above says otherwise). Files in /challenge (kernel, initramfs, sources and the original launcher, where the CTF shipped them): System.map, bzImage, rootfs.gz, start.sh, vuln.ko.

Original challenge

Description

One speed pwn will be released every 6 hours. Fastest pwners get 200, 150 and 100 pts respectively, everyone else gets 50 pts per solve.

Points

50

Solves

Unknown

Credits

  • Challenge author(s): hfs, for Midnight Sun CTF 2023 Quals
  • Compiled by: mito753 in Kernel-Exploit-Dojo, which also has the original handout and writeups

m0leCon CTF 2020 Teaser · difficulty ★★☆☆☆ · module: Ring 0 Basics: ret2usr

A classic kernel stack overflow with KASLR and SMEP turned off: take over a saved return address and return straight into your own user-space code.

Mitigations: nokaslr, no SMEP/SMAP (default CPU)

Running it

Run /challenge/run. It boots the original kernel and root filesystem in QEMU with the serial console on your terminal. Quit with Ctrl-A then X. Put a statically linked exploit at /home/hacker/exploit first; it appears in the VM as /exploit. Baby initialized! may print right after the first / $ prompt; press Enter.

The flag is put where the original challenge kept it, readable only by root in the VM (unless a note above says otherwise). Files in /challenge (kernel, initramfs, sources and the original launcher, where the CTF shipped them): babyk.c, bzImage, initramfs.cpio, start.sh.

Original challenge

Description

I wanted to play a bit with, you know, modules and stuff...let me know if you liked it!

Points

217

Solves

20

Credits

  • Challenge author(s): @madt1m, for m0leCon CTF 2020 Teaser
  • Compiled by: mito753 in Kernel-Exploit-Dojo, which also has the original handout and writeups

DownUnderCTF 2022 · difficulty ★★★☆☆ · module: Ring 0 Basics: ret2usr

A home-made pointer authentication scheme on x86; find where its checks fall short.

Mitigations: KASLR, no SMEP/SMAP (default CPU), custom PAC

Running it

Run /challenge/run. It boots the original kernel and root filesystem in QEMU with the serial console on your terminal. Quit with Ctrl-A then X. Put a statically linked exploit at /home/hacker/exploit first; it appears in the VM as /exploit.

The flag is put where the original challenge kept it, readable only by root in the VM (unless a note above says otherwise). Files in /challenge (kernel, initramfs, sources and the original launcher, where the CTF shipped them): bzImage, encryption.c, initramfs.cpio.gz, launch.sh, pac.c.

Original challenge

Description

We've rolled our own pointer authentication for our kernel module, so we don't need other protections like SMEP and SMAP...

Points

500

Solves

7

Credits

  • Challenge author(s): joseph#8210, for DownUnderCTF 2022
  • Compiled by: mito753 in Kernel-Exploit-Dojo, which also has the original handout and writeups

30-Day Scoreboard:

This scoreboard reflects solves for challenges in this module after the module launched in this dojo.

Rank Hacker Badges Score