2 h 15 min · 11 lessons

C Programming

C is the language underneath nearly everything we study. Operating systems, interpreters, and most of the binaries we exploit are written in it. Learning to write C teaches you how a program actually handles memory: what a pointer holds, where a buffer lives, and what happens when you read past its end. Every vulnerability in Binary Exploitation is a mistake someone made writing C.

This course is built around the programming section of CTF competitions, or the Screener. Each challenge gives us a data file and a set of instructions. Our goal is to read the file, parse it into the right data structure, and process it to produce a flag in flag{} braces. The challenge is rarely the algorithm itself; it's doing all of this in a language with no classes, no built-in data structures, and no garbage collector.

Course Layout

The challenges are split into two parts based on how the data is stored:

  1. ASCII File I/O. The data is plaintext. We read it line by line, split it into pieces, and convert the pieces into numbers and strings. These challenges vary the most, and they get harder as you go. Most of them are adapted from Advent of Code 2022, and each credits the puzzle it's based on.
  2. Serialized File I/O. The data is stored in binary. Each challenge tells us the exact layout of every element, and we read it byte for byte into a struct. Once the data is read, these are typically the easier of the two.

Each part starts with an introduction to the functions and data structures it relies on. Every challenge then follows the same template: the problem description, a set of hints, a full walk-through, and the complete solution.

Try the challenges yourself first!
Each challenge has hints between the problem description and the walk-through. Open them one at a time, and only read the walk-through once you've solved it or you're truly stuck. Solving these on your own is the only way to get fast at them.

Toolchain

All we need is a C compiler and a debugger. I recommend gcc and gdb, which come installed on most Linux distributions. Each solution in this course lives in a single file, solve.c, in the same directory as the challenge's data file:

gcc -o solve solve.c && ./solve

When a solution misbehaves, compile it with -g to include debugging symbols and step through it in gdb. We have a GDB Guide if you've never used it before. Adding -Wall makes gcc warn you about common mistakes, like comparing strings with ==, before they cost you time.

Warning
You should be comfortable with C's syntax, pointers, and malloc/free before starting the challenges. They lean heavily on data structures you build yourself, especially linked lists. If you've never built a linked list in C, try writing one before you begin.