Debugging on Windows: x64dbg and WinDbg
Most malware and most commercial software an analyst examines runs on Windows, so the dynamic-analysis skills of the last two lessons have to work there too. The concepts are identical (breakpoints, registers, memory, stepping, tracing API calls); the tools and some details differ: the Microsoft x64 calling convention, PE modules instead of ELF objects, the Windows API instead of libc, and a few Windows-only structures that debuggers and anti-debugging checks revolve around. The two tools to know are x64dbg (open-source, user-mode, the usual choice for malware and crackme work) and WinDbg (Microsoft's debugger, user and kernel mode, the tool for crash dumps and drivers). Neither runs here, so this lesson pairs a runnable look at the Windows build of a toy with labelled lab transcripts.
The same toy, compiled for Windows
The session compiles validate from the key-check toy of Decompilers for the Windows target with clang (a COFF object; it isn't linked or run, and since the sandbox has no Windows SDK, the two declarations it needs are written by hand) and disassembles it. This is the code an analyst steps through in x64dbg; compare it with the Linux build.
cat > lic.c <<'SRC'
typedef unsigned long long size_t; /* no Windows SDK in the sandbox: declare what we use */
size_t strlen(const char *);
int validate(const char *s) {
size_t n = strlen(s);
if (n != 8) return 0;
unsigned sum = 0;
for (size_t i = 0; i < n; i++) sum = sum * 33 + (unsigned char)s[i];
return (sum & 0xffff) == 0x1234;
}
SRC
clang --target=x86_64-pc-windows-msvc -O1 -c lic.c -o lic.obj
llvm-objdump -d -r --no-show-raw-insn -M intel lic.obj | grep -E '^\s+[0-9a-f]+:|IMAGE_REL' | sed 's/^ *//'0: push rsi
1: sub rsp, 0x20
5: mov rsi, rcx
8: call 0xd <validate+0xd>
0000000000000009: IMAGE_REL_AMD64_REL32 strlen
d: mov rcx, rax
10: xor eax, eax
12: cmp rcx, 0x8
16: jne 0x44 <validate+0x44>
18: xor eax, eax
1a: xor ecx, ecx
1c: nop dword ptr [rax]
20: mov edx, ecx
22: shl edx, 0x5
25: add edx, ecx
27: movzx ecx, byte ptr [rsi + rax]
2b: add ecx, edx
2d: inc rax
30: cmp rax, 0x8
34: jne 0x20 <validate+0x20>
36: movzx ecx, cx
39: xor eax, eax
3b: cmp ecx, 0x1234
41: sete al
44: add rsp, 0x20
48: pop rsi
49: ret
The argument arrives in rcx, not rdi (Calling Conventions); rsi and rdi, callee-saved on Windows, are pushed if the function uses them; the call to strlen reserves the 32-byte shadow space (sub rsp, 0x20 or more); and the relocation IMAGE_REL_AMD64_REL32 strlen is the COFF counterpart of ELF's R_X86_64_PLT32. In a linked .exe, that call goes through the IAT (PE Imports).
x64dbg: a lab session
Lab session: lic.exe, the Windows build of the same toy (compiled with MSVC cl /O1 lic.c in the course's isolated Windows VM, no network), opened in x64dbg. The analyst sets a breakpoint on the imported strlen, runs with a test key, and follows the return into validate. Abridged; addresses depend on the build.
Lab session: x64dbg, lic.exe TESTKEY1 (the course's own toy), Windows 11 VM 10.10.0.30, no network
Command: bp strlen ; breakpoint on the import (x64dbg resolves the module)
Command: g ; run (F9)
INT3 breakpoint at ucrtbase.strlen (00007FFB2A41D1F0)!
RCX : 000001D9A2B41E90 "TESTKEY1" ; first argument (Microsoft x64)
Command: rtr ; run to return (Ctrl+F9)
Command: sto ; step over the ret (F8): back in lic.exe
00007FF6A6E11013 | 48:83F8 08 | cmp rax,8 ; n == 8 ?
00007FF6A6E11017 | 75 2B | jne lic.7FF6A6E11044
Command: bp 7FF6A6E1103A ; the final comparison
Command: g
00007FF6A6E1103A | 81F9 34120000 | cmp ecx,1234
RCX : 000000000000C1FA ; low 16 bits of the hash of TESTKEY1: 0xC1FA, not 0x1234
Command: log "validate: low16={rcx}"The workflow is the same as with gdb: a breakpoint where the input enters, a look at the argument registers, then a breakpoint at the decision. x64dbg adds conveniences: symbols for system DLLs, string references (right-click → Search for → String references), a graph view, a memory map, and plugins (ScyllaHide against anti-debugging, Scylla for rebuilding the IAT of an unpacked process).
WinDbg: the same questions
Lab session: WinDbg, lic.exe TESTKEY1 (the course's own toy), same VM
0:000> bp ucrtbase!strlen ; g
Breakpoint 0 hit
0:000> da @rcx
000001d9`a2b41e90 "TESTKEY1"
0:000> gu ; go up: run until the function returns
0:000> r rax
rax=0000000000000008
0:000> k ; call stack
0:000> lm m lic ; module base and size
0:000> !peb ; Process Environment Block: BeingDebugged: YesCommon WinDbg commands: bp/bu/ba (breakpoint, deferred breakpoint, hardware breakpoint), g (go), p/t (step over/into), gu (go up), r (registers), db/dd/dq/da/du (memory as bytes, dwords, qwords, ASCII, UTF-16), k (stack), lm (modules), !peb, !analyze -v (crash-dump triage).
Windows-specific places analysts check
| Structure / feature | Why it matters |
|---|---|
PEB (Process Environment Block, gs:[0x60] on x64) |
BeingDebugged byte (what IsDebuggerPresent reads), NtGlobalFlag (heap debug flags set when a process starts under a debugger), the loaded-module lists that malware walks to find APIs without imports |
| TLS callbacks | functions in the PE's TLS directory run before the entry point; set the debugger to break on them (x64dbg: Options → Preferences → Events → TLS Callbacks, on by default; check it is still set) or code runs before your first breakpoint |
| SEH / vectored exception handlers | exceptions used as control flow (a deliberate int3 or division by zero whose handler continues elsewhere) |
The entry point vs main |
the CRT start-up code runs first; x64dbg's "EntryBreakpoint" stops at the PE entry, a "main" search finds the user code |
How debuggers are detected on Windows, and how analysts notice
Windows offers many ways for a process to ask "am I being debugged?": IsDebuggerPresent (the PEB byte), CheckRemoteDebuggerPresent, NtQueryInformationProcess with ProcessDebugPort, the PEB's NtGlobalFlag, timing with QueryPerformanceCounter or rdtsc, scanning its own code for 0xCC, and checking hardware debug registers via GetThreadContext. Analysts notice them through imports and strings (the API names), through behaviour that differs between a debugger run and a sandbox run, and by breaking on the APIs themselves. The Anti-Debugging lesson shows the Linux equivalents running.
How it is done safely
- Debug only in an isolated, snapshotted Windows VM (Lab Safety Rules): no shared folders, no network or a simulated one (INetSim, FakeNet-NG), and a snapshot to revert to after every run.
- Never debug an unknown sample on your workstation, even "just to look": the debugger runs it.
- On production servers, attaching a debugger to other users' processes and to services requires
SeDebugPrivilege(granted to administrators by default); restricting and auditing that privilege is a standard hardening step (Windows Accounts, Tokens and UAC).
Pitfalls
- x32dbg vs x64dbg: 32-bit programs need the 32-bit debugger (both ship together).
- ASLR: addresses change per boot and per run; x64dbg shows
module+offsetlabels, use them in notes. - WOW64 and .NET: 32-bit processes on 64-bit Windows and managed code need different tools (dnSpy for .NET).
- Breakpoints on APIs: break on the lowest common function (for file access,
kernelbase!CreateFileWorntdll!NtCreateFile), since programs may call any layer.
Practice
1. In the Windows build, which register holds validate's argument at entry, and where would a fifth argument be?
rcx; a fifth argument would be at [rsp+0x28] at entry (return address plus 32 bytes of shadow space).
2. Why should you enable "break on TLS callbacks" before running an unknown PE?
TLS callbacks execute before the entry point, so anti-debugging or unpacking code there would run before your first breakpoint.
3. Which WinDbg command shows a UTF-16 string at the address in rdx?
du @rdx.
Test yourself
Which x64dbg action runs until the current function returns?
- Step into (F7)
- Run to return (Ctrl+F9)
- Run (F9)
- Step over (F8)
What does IsDebuggerPresent read?
- The TracerPid file
- The BeingDebugged byte in the PEB
- The registry
- The IAT
What runs before a PE's entry point and can surprise a debugger?
- main
- TLS callbacks
- The import table
- The DOS stub
Which tool is the usual choice for kernel debugging and crash dumps on Windows?
- x64dbg
- WinDbg
- ltrace
- objdump