A Thrilling Ride: Building a Self-Modifying C Program

2025-05-25

This article details the creation of a C program capable of modifying its own code at runtime. The author first explains how program code is stored in memory and how to use the `mprotect()` function to change the permissions of the code segment to writable. Then, by modifying the immediate value of an `addl` instruction, the author demonstrates altering the program's output. Finally, the author goes further, using `memcpy()` to copy shellcode into the program's code segment, successfully executing a shell command at runtime. The entire process is technically challenging, requiring a strong understanding of x86_64 assembly language.