This guide outlines the steps to build and debug a simple C password-checking program using GCC in MSYS2 and x64dbg on Windows. The goal is to compile the program with specific flags, load it into x64dbg, set a breakpoint on strcmp, analyze function parameters, identify the correct password, and verify it. Mermaid diagrams are included to visualize the compilation and debugging processes.
The C program (password.c) to debug is:
#include <stdio.h>
#include <string.h>
int main() {
char input[50];
printf("Enter password: ");
scanf("%s", input);
if (strcmp(input, "debug123") == 0) {
printf("Access granted!\n");
} else {
printf("Access denied!\n");
}
return 0;
}The following flowchart illustrates the compilation process:
graph TD
A[Save password.c] --> B[Open MSYS2 MinGW 64-bit Terminal]
B --> C[Run: gcc -g -fno-stack-protect -no-pie -o password password.c]
C --> D{Compilation Successful?}
D -->|Yes| E[Verify: password.exe exists]
D -->|No| F[Check GCC installation and code]
E --> G[Proceed to Debugging]
F --> C
-
Save the Code:
- Save the C code in a file named
password.cin your MSYS2 working directory (e.g.,C:\msys64\home\user\password.c).
- Save the C code in a file named
-
Open MSYS2 Terminal:
- Launch the MSYS2 MinGW 64-bit terminal (
mingw64.exe) to use the 64-bit toolchain.
- Launch the MSYS2 MinGW 64-bit terminal (
-
Compile the Program:
- Run the following command to compile with debugging symbols, no stack canary, and no PIE:
gcc -g -fno-stack-protector -no-pie -o password password.c
-g: Includes debugging symbols for x64dbg.-fno-stack-protector: Disables stack-smashing protection (stack canary).-no-pie: Disables Position-Independent Executable for fixed addresses.-o password: Names the outputpassword.exe.
- Run the following command to compile with debugging symbols, no stack canary, and no PIE:
-
Verify Compilation:
- Check for
password.exein the directory:ls
- If compilation fails, ensure
gccis installed (pacman -S mingw-w64-x86_64-gcc) and the code is correct.
- Check for
-
Install x64dbg:
- Download and extract x64dbg from its official site or GitHub releases to a directory (e.g.,
C:\x64dbg).
- Download and extract x64dbg from its official site or GitHub releases to a directory (e.g.,
-
Launch x64dbg:
- Open
x64dbg.exe(use the 64-bit version for the 64-bit executable).
- Open
-
Load the Executable:
- Go to
File > Openand selectpassword.exe. - The program loads, showing the entry point in the CPU window.
- Go to
-
Verify Symbols:
- Check the “Symbols” tab to confirm debugging symbols are loaded, making
strcmpand variables likeinputvisible.
- Check the “Symbols” tab to confirm debugging symbols are loaded, making
-
Locate
strcmp:- In the “Symbols” tab, find
strcmp(likely inmsvcrt.dll). - Right-click and select “Set Breakpoint” or press
F2.
- In the “Symbols” tab, find
-
Alternative Method:
- If
strcmpisn’t in Symbols:- Navigate to the
mainfunction in the CPU window (double-clickmainin Symbols). - Step through the code to find the
calltostrcmp(e.g.,call msvcrt.strcmp). - Set a breakpoint (
F2) on this instruction.
- Navigate to the
- If
The debugging process is visualized in the following flowchart:
graph TD
A[Load password.exe in x64dbg] --> B[Set breakpoint on strcmp]
B --> C[Run program F9]
C --> D[Enter wrong password e.g. wrongpass]
D --> E[Breakpoint hit at strcmp]
E --> F[Examine RCX: Input string]
E --> G[Examine RDX: Hardcoded password debug123]
F --> H[Compare strings: Mismatch]
G --> H
H --> I[Continue F9: Access denied]
I --> J[Restart Ctrl+F2]
J --> K[Run and enter debug123]
K --> L[Breakpoint hit at strcmp]
L --> M[Examine RCX and RDX: Both debug123]
M --> N[Continue F9: Access granted]
-
Run the Program:
- Press
F9to run until the breakpoint orF8to step to the password prompt. - A console window appears with “Enter password:”.
- Press
-
Enter a Wrong Password:
- Type a wrong password (e.g.,
wrongpass) and press Enter. - The program pauses at the
strcmpbreakpoint.
- Type a wrong password (e.g.,
-
Inspect Registers:
- At the
strcmpbreakpoint, view the Registers pane. RCX: Points to your input string (e.g.,wrongpass).RDX: Points to the hardcoded password (debug123).
- At the
-
Follow in Dump:
- Right-click
RCXin Registers and select “Follow in Dump” to see your input string. - Repeat for
RDXto seedebug123.
- Right-click
-
Verify Strings:
- In the Dump window,
RCXshowswrongpass\0, andRDXshowsdebug123\0. strcmpcompares these, returning non-zero (mismatch), leading to “Access denied!”.
- In the Dump window,
- From
RDXin the Dump window, the correct password isdebug123, hardcoded in the program.
-
Restart the Program:
- Press
Ctrl+F2to restart in x64dbg. - Run (
F9) to the password prompt.
- Press
-
Enter the Correct Password:
- Type
debug123and press Enter. - The program hits the
strcmpbreakpoint.
- Type
-
Re-examine Parameters:
- Check
RCXandRDX:RCX: Points todebug123\0(your input).RDX: Points todebug123\0(hardcoded).
strcmpreturns 0 (match).
- Check
-
Continue Execution:
- Press
F9to continue. - The console outputs “Access granted!”.
- Press
-
Optional: Step Through Logic:
- Step over (
F8) thestrcmpcall to seeRAX = 0(indicating equality). - Observe the conditional jump to the “Access granted!” code.
- Step over (
- Debugging Symbols: The
-gflag ensures variable names are visible in x64dbg’s Symbols or Memory Map. - No Stack Canary/PIE: These flags simplify debugging with static addresses and no stack checks.
- Potential Issues:
- If
strcmpisn’t found, verifymsvcrt.dllis loaded in the Modules tab. - If the program crashes, confirm the executable is 64-bit (
file password.exe). scanf("%s", input)doesn’t handle spaces; considerfgetsfor robustness if modifying the code.
- If
This process demonstrates compiling a program with specific flags, debugging it in x64dbg, analyzing strcmp parameters, and identifying/verifying the password.