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.

Step 01

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.

main.c
C
#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;
}
Demonstration Purpose

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.

Step 02

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:

# 1. Define the secret environment variable
$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:

(gdb)start
Why 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.

GDB Start Command Output
Figure 1 — Initial execution paused at main() with relocated virtual memory addresses.
Step 03

Disassembly & Pinpointing the Check

To locate the exact assembly instruction where our password verification occurs, we disassemble the main function:

(gdb)disassemble main

Scanning through the AArch64 instructions reveals two critical operations:

  • bl strcmp@plt: The branch-with-link call to the Procedure Linkage Table (PLT) stub for strcmp().
  • cmp w0, #0: The immediate instruction following the call. On ARM64 (AArch64), function return values are returned in register w0 (the 32-bit lower half of x0).
GDB Disassembly View of main
Figure 2 — Disassembly of main(): the highlighted red box reveals the strcmp call and the subsequent cmp instruction.
Step 04

Setting the Breakpoint After strcmp

From our disassembly, we identify the exact relocated address directly after the strcmp call: 0x0000aaaaaaaa0a70.

(gdb)break *0x0000aaaaaaaa0a70
Why set the breakpoint AFTER strcmp?

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.

w0 == 0 → Passwords match ("Access granted.")
w0 != 0 → Passwords differ ("You are banned!")
Setting Breakpoint at relocated address
Figure 3 — Hardware/Software breakpoint established at instruction 0x0000aaaaaaaa0a70.
Step 05

Executing & Register Hijacking

Now, resume execution with the run command. The program prompts for user input. We can supply any arbitrary password:

(gdb)run
Username : attacker Password : wrong_password_123

As soon as the input is submitted, execution hits our breakpoint right before the conditional branch:

Hitting breakpoint with arbitrary credentials
Figure 4 — Execution halted at the breakpoint with the comparison result in w0.

Because the entered password was incorrect, strcmp returned a non-zero value in w0. We now overwrite w0 with 0 and let execution resume:

# Force register w0 to 0 (indicating strcmp matched)
(gdb)set $w0 = 0

# Resume program execution
(gdb)continue
Bypass successful: Access granted
Figure 5 — w0 register forced to zero: the branch evaluates as a match and prints "Access granted."
Step 06

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 ./main behaves 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 start before 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.