In reverse engineering and vulnerability research, we often want to inspect and alter the runtime behavior of a compiled application. What makes in-memory patching uniquely interesting is that it is strictly ephemeral: the modifications exist solely inside the process's allocated virtual address space. Once the process terminates, the original executable file on disk remains completely intact and untouched.
The Target C Program
We will build a simple authentication utility in C. The program dynamically collects user credentials from standard input,
retrieves a secret passphrase stored in an environment variable, and uses strcmp() to decide whether access is granted or denied.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *read_char() {
char *str = NULL;
int length = 0;
char c;
while ((c = getchar()) != '\n' && c != EOF) {
char *tmp = realloc(str, length + 2);
if (!tmp) {
free(str);
return NULL;
}
str = tmp;
str[length++] = c;
}
if (str)
str[length] = '\0';
return str;
}
int main() {
printf("Username : ");
char *nom = read_char();
printf("Password : ");
char *mdp = read_char();
if (!nom || !mdp) {
printf("Allocation error\n");
free(nom);
free(mdp);
return 1;
}
const char *pass = getenv("PASSWORD");
if (pass == NULL) {
printf("Error: PASSWORD not set.\n");
return 1;
}
if (strcmp(mdp, pass) == 0) {
printf("Access granted.\n");
} else {
printf("You are banned!\n");
}
free(nom);
free(mdp);
return 0;
}
This program is intended for educational reverse-engineering analysis. Production authentication systems must always employ salted cryptographic hashing (e.g., Argon2, bcrypt) and constant-time comparison routines.
Starting GDB & Navigating ASLR
Before launching the program, we must set the secret passphrase into the expected environment variable.
Then, we compile the source with debug symbols (-g) and load the binary into GDB:
$export PASSWORD="gdb_learn"
# 2. Compile with debug symbols
$gcc -g main.c -o main
# 3. Launch GDB
$gdb ./main
Once inside GDB, issue the start command:
start instead of setting breakpoints immediately?
Modern operating systems enable ASLR (Address Space Layout Randomization) by default. The binary's text and stack segments are relocated to random base addresses every time execution begins. Running start pauses the binary right at the prologue of main(), letting us inspect the actual, relocated runtime memory addresses.
Disassembly & Pinpointing the Check
To locate the exact assembly instruction where our password verification occurs, we disassemble the main function:
Scanning through the AArch64 instructions reveals two critical operations:
-
bl strcmp@plt: The branch-with-link call to the Procedure Linkage Table (PLT) stub forstrcmp(). -
cmp w0, #0: The immediate instruction following the call. On ARM64 (AArch64), function return values are returned in registerw0(the 32-bit lower half ofx0).
Setting the Breakpoint After strcmp
From our disassembly, we identify the exact relocated address directly after the strcmp call:
0x0000aaaaaaaa0a70.
If we set the breakpoint inside strcmp, the function is still actively comparing character bytes; modifying registers mid-execution could cause unpredictable behavior or segfaults. Immediately after strcmp returns, its return value is already stored in w0, waiting to be evaluated by cmp w0, #0.
Executing & Register Hijacking
Now, resume execution with the run command. The program prompts for user input. We can supply any arbitrary password:
Username : attacker Password : wrong_password_123
As soon as the input is submitted, execution hits our breakpoint right before the conditional branch:
Because the entered password was incorrect, strcmp returned a non-zero value in w0.
We now overwrite w0 with 0 and let execution resume:
(gdb)set $w0 = 0
# Resume program execution
(gdb)continue
Conclusion & Key Takeaways
In just a few debugger operations, we altered the logical decision flow of an AArch64 executable at runtime without knowing the actual passphrase or altering the compiled binary on disk.
Summary of Key Concepts
-
Zero Disk Trace: The binary file on disk remained unmodified throughout the entire process. Once GDB quits, running
./mainbehaves with original security constraints. -
AArch64 Calling Convention: On ARM64, 32-bit integer return values reside in register
w0. Knowing the target architecture's ABI allows pinpoint register intervention. -
ASLR Awareness: Always use
startbefore referencing absolute memory addresses so the dynamic loader finishes initial address space relocation. -
Defensive Reality: Client-side or unauthenticated memory checks cannot be trusted in untrusted environments. Authorization checks must be backed by cryptographic trust or server-side guarantees.