Practice library / Security operations basics
Lab · Hard · 3 hoursFree account
Dynamic Malware Analysis with WinDbg
Dynamic binary analysis with WinDBG, setting breakpoints, and inspecting memory.
On this page 12 sections
- ·Prerequisites
- ·Lab Setup
- 1Build the demo program
- 2Start the program under WinDbg and run to the crash
- Locked: Read the call stack
- Locked: Let WinDbg analyse the crash
- Locked: Set breakpoints on Windows API functions
- Locked: Trace API calls without stopping
- Locked: Write the behaviour report
- Locked: Defence and Detection
- Locked: Check Your Work
- Locked: Clean-up
WinDbg attaches to a running Windows process and reveals the behaviour that a static disassembly only hints at. Malware authors pack binaries, resolve APIs at run time and decrypt payloads in memory, so reversers watch each step live to find the true logic. One guided session on a demo binary teaches breakpoints, call-stack reading, register inspection and timed logging, then turns raw traces into a short behaviour report with detection hints.
What You'll Learn
- Build a small demo program with symbols and start it under WinDbg in a lab VM
- Stop on a crash and read the exception record, the faulting instruction and the registers
- Read a call stack with
kand map each frame back to a function - Set breakpoints on Windows API functions and read their arguments from registers
- Log what a program does without stopping at each call
- Turn a debugger session into a short behaviour report with detections
A debugger answers the question that static review cannot: what does this program really do when it runs? Analysts use WinDbg to catch a crash, to watch which files and keys a sample touches, and to read memory after the sample decrypts itself.
In this lab you learn the debugger on a harmless program that you build yourself. The program writes a log file and then crashes on a null pointer. You find the cause from the call stack and the registers. Then you use the same skills to trace API calls, the way an analyst traces a sample. You do not write or run any exploit.
Prerequisites
Read the Malware Analysis workbook first. It explains static and dynamic analysis, and why you run samples only in an isolated VM.
For the next step after this lab, see Process Injection Detection.
Lab Setup
Use a Windows 10 or 11 x64 virtual machine that you control. Take a snapshot before you start. Turn the network adapter to host-only or off.
You need these tools:
- WinDbg (the Microsoft Store app "WinDbg", or the Windows SDK Debugging Tools). The command
cdb.execomes with it. - A C compiler that makes PDB symbols: Visual Studio Build Tools with the "x64 Native Tools Command Prompt".
- A working folder
C:\Temp.
All code in this lab is your own demo. Do not debug software that you do not own, and never run real malware outside a sealed analysis VM with written permission.
Addresses, process IDs and offsets in the example output differ on your machine. Compare the shape of the output, not the numbers.
Step 1: Build the demo program
Write a program that crashes on purpose
A debugger is easier to learn on code that you can read. The program has three functions in one chain: main calls get_level, and get_level calls find_record. For id 7, find_record returns NULL. get_level then reads from that null pointer and the program crashes.
Create C:\Temp\demo.c with this content.
#include <windows.h>
#include <stdio.h>
#include <string.h>
typedef struct { char name[16]; int level; } Record;
static Record admin = { "admin", 1 };
void log_event(const char *path, const char *msg) {
DWORD n;
HANDLE h = CreateFileA(path, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (h == INVALID_HANDLE_VALUE) return;
WriteFile(h, msg, (DWORD)strlen(msg), &n, NULL);
CloseHandle(h);
}
Record *find_record(int id) {
if (id == 7) return NULL;
return &admin;
}
int get_level(int id) {
Record *r = find_record(id);
return r->level;
}
int main(void) {
log_event("C:\\Temp\\demo.log", "demo started");
printf("level of 1 = %d\n", get_level(1));
printf("level of 7 = %d\n", get_level(7));
return 0;
}Open the "x64 Native Tools Command Prompt for VS". Compile with debug symbols and no optimisation.
cd C:\Temp
cl /nologo /Zi /Od demo.c /link /debug
dir demo.*Example output:
demo.c
Microsoft (R) Incremental Linker Version 14.40.33811.0
...
Directory of C:\Temp
19/11/2026 10:02 49,152 demo.exe
19/11/2026 10:02 552,960 demo.pdbThe file demo.pdb holds the symbols. WinDbg uses it to show function names and source lines. Without it, you see only addresses.
Run the program once, outside the debugger.
demo.exeExample output:
level of 1 = 1Then Windows shows a crash message or the program exits with no second line. That is the fault you will analyse.
Step 2: Start the program under WinDbg and run to the crash
Launch, set up symbols, and catch the exception
When you launch a program in WinDbg, it stops at the first instruction of the loader. This is the initial break. You then run the program with g (go) until something stops it.
In WinDbg choose File, then Start debugging, then Launch executable. Select C:\Temp\demo.exe. In the command window, set the symbol path and run.
0:000> .symfix
0:000> .sympath+ C:\Temp
0:000> .reload
0:000> g.symfix points WinDbg at the Microsoft symbol server, so that system modules such as ntdll also have names. .sympath+ C:\Temp adds the folder with demo.pdb. WinDbg prints the first line, then stops at the fault.
level of 1 = 1
(1a4c.2b10): Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
demo!get_level+0x1a:
00007ff7`5e3a1049 8b4010 mov eax,dword ptr [rax+10h] ds:00000000`00000010=????????Read the output line by line:
(1a4c.2b10)is the process ID and the thread ID, in hexadecimal.c0000005is the Windows code for an access violation: the program used an address it must not use.first chancemeans the debugger sees the exception before the program can handle it.demo!get_level+0x1ais the module, the function and the offset in the function.- The instruction
mov eax,dword ptr [rax+10h]reads 4 bytes from the address inraxplus 16. ds:00000000`00000010=????????shows that WinDbg cannot read address0x10. Theraxregister is 0.
The offset 0x10 is 16. In the Record struct, name uses 16 bytes, so level starts at offset 16. The program reads r->level while r is NULL. You found the root cause from one line.
Show the exception record to confirm.
0:000> .exr -1Example output:
ExceptionAddress: 00007ff75e3a1049 (demo!get_level+0x000000000000001a)
ExceptionCode: c0000005 (Access violation)
ExceptionFlags: 00000000
NumberParameters: 2
Parameter[0]: 0000000000000000
Parameter[1]: 0000000000000010
Attempt to read from address 0000000000000010Parameter[0] is 0 for a read and 1 for a write. Parameter[1] is the address that the program tried to use.
Sign in to continue
8 more sections are for signed-in members. It is free: sign in with Google.
Related
This lab is extra practice for the course Security operations basics. Do the lessons first.Open the course