In the last lesson, you got your hands dirty by directly modifying the raw bytes of an executable with a hex editor. This static approach is powerful for making targeted patches. Today, we transition from analyzing a program at rest to watching it in motion. This lesson brings you into the world of dynamic analysis, where you'll use a debugger to observe how the operating system brings an executable to life.
Our goal is to understand what happens when you double-click an .exe file. You will use a debugger to see how the PE file is mapped from disk into a virtual address space and, critically, how it resolves and calls functions from external libraries like kernel32.dll and user32.dll. This process of tracing imported functions is a fundamental skill in reverse engineering, whether you're analyzing malware or understanding complex software.
From Disk to Memory: The Loader's Role
When you execute a program, the Windows loader performs a series of critical tasks. It creates a new process, allocates a private virtual address space for it, and then parses the PE file from disk. It doesn't just copy the file byte-for-byte; instead, it reads the section headers and maps each section (.text, .data, etc.) into memory at its specified virtual address.
A key concept here is Address Space Layout Randomization (ASLR). While a PE file has a preferred ImageBase address (often 0x400000 for 32-bit executables), ASLR means the loader will often place the executable at a random base address in memory. This is a security measure to make it harder for exploits to predict memory locations. As a reverse engineer, you must be aware of this and always work relative to the actual base address you observe in the debugger.
The following video provides an excellent visual demonstration of this process. The presenter uses Process Hacker and WinDbg to compare the PE file on disk with its representation in memory, clearly showing the effects of ASLR and section mapping.
🔴 Portable Executable Files: Analyzing In-Memory versus On Disk
This video from Dr. Josh Stroschein's "The Cyber Yeti" channel demonstrates the differences between a PE file's structure on disk and in memory.
Please watch these two segments: Loading into Memory: Observe how a program is loaded and how its base address in memory can be found using Process Hacker. Notice that the base address (0x900000) is different from the preferred default. On Disk vs. In Memory: This section compares the on-disk view in a hex editor with the in-memory view. Pay close attention to the discussion of ASLR, and the difference between the PointerToRawData (used for the on-disk file) and the VirtualAddress (used for the in-memory mapping).
Once loaded, the process's memory map contains not only the main executable's sections but also all the Dynamic-Link Libraries (DLLs) it depends on, such as kernel32.dll and ntdll.dll. Each of these is also mapped into the process's address space at its own base address.

Tracing the Path of an API Call
When your C code calls a function like MessageBoxA, the compiler doesn't hardcode the address of that function into your executable. Why? Because the address of MessageBoxA can change depending on the Windows version, service packs, or ASLR applied to user32.dll.
Instead, the compiler uses a level of indirection through the Import Address Table (IAT). A CALL instruction in your code will point to an entry in the IAT. When the program is loaded, the Windows loader reads the executable's import directory to see which functions are needed from which DLLs. It then finds the actual addresses of these functions in the loaded DLLs and "patches" the IAT, filling it with the correct runtime addresses.
Let's dig into the low-level structures that make this possible. The following resource provides a deep dive into the Windows internals that govern this process. It uses the powerful kernel debugger, WinDbg, to inspect these structures directly in memory. Given your CSE background and comfort with low-level details, you will find this exploration illuminating.
[PDF] Deep Dive into OS Internals with Windbg - Exploit DB
This document offers a detailed, hands-on walkthrough of PE structures in memory using WinDbg. It explains precisely how the import/export mechanism works.
Please read the sections on pages 8-13. Start at the heading "Import Directory" on page 8. This introduces the IMAGE_IMPORT_DESCRIPTOR structure, which describes which DLL a program imports from. Continue reading to understand the OriginalFirstThunk (which points to the Import Name Table) and the FirstThunk (which points to the IAT). The key insight is on page 10: before loading, both tables are similar, but after loading, the IAT (FirstThunk) is populated with the actual virtual addresses of the functions. Read the section "Import Address Table" on page 12. This shows how to use a debugger to dump the IAT and see the resolved function pointers directly. Finally, read the section "Export Directory" on page 13. This explains the other side of the handshake: how the loader finds the function addresses in the first place by looking at the DLL's Export Address Table (EAT).
Imports in Practice: Debugging with x64dbg
While WinDbg is extremely powerful, for user-mode application reversing, a debugger like x64dbg is often more approachable. Let's see how these concepts apply in a practical malware analysis scenario, where import resolution is often deliberately obfuscated.

The following article demonstrates how to trace imports in x64dbg. It shows how malware can hide its imports by hashing API names and resolving them dynamically, a technique you will frequently encounter.
How to Analyze Malware with x64dbg
This Varonis blog post provides a step-by-step guide to analyzing a malware sample in x64dbg, focusing on how to handle and trace imported functions.
Read the section titled "Analyzing an Unpacked Sample". Pay close attention to how the initial search for intermodular calls yields almost nothing. The author then steps over some deobfuscation code, after which the IAT becomes populated with legitimate Windows API names. This is dynamic import resolution in action. Read the section "Analyzing Imported Functions". This is a fantastic walkthrough of setting a breakpoint on an API call (CreateMutexW), observing the parameters being pushed to the stack, and following a pointer in the memory dump to see the data being used by the function. This entire process is fundamental to dynamic analysis.
Dynamically Finding Functions: The PEB method
What if a program wants to call a function without having a corresponding entry in its IAT? This is the standard method for position-independent shellcode and is also used by sophisticated malware to hide its capabilities from static analysis tools.
The solution lies in the Process Environment Block (PEB). The PEB is a user-mode data structure that contains a wealth of information about the current process. Crucially, it contains a linked list of all modules (DLLs) loaded into the process.
By getting the address of the PEB (which can be reliably found at fs:[0x30] in 32-bit processes), a program can walk this list, find the base address of a desired module like kernel32.dll, parse its Export Address Table (EAT) in memory, and find the address of any function it exports, such as LoadLibraryA. Once it has the address of LoadLibraryA, it can load any other DLL and resolve any other function it needs.
The "Deep Dive into OS Internals with Windbg" document you reviewed earlier contains a superb walkthrough of exactly this technique.
[PDF] Deep Dive into OS Internals with Windbg - Exploit DB
This resource provides an expert-level walkthrough of locating and using the PEB to dynamically find function addresses, a cornerstone of advanced malware and exploit development.
Please read pages 17-25, starting from the heading "Process Environment Block". First, review the multiple methods for finding the PEB's address. The most important one for shellcode is Method 5: fs:[30]. Follow the "Understanding an Example Shellcode" section. This traces, instruction by instruction, how shellcode can get the PEB, find the _PEB_LDR_DATA, and walk the InLoadOrderModuleList to find the base address of kernel32.dll. Finally, review the "Practical Example with Rustock.B Rootkit" section. This shows the second part of the process: once kernel32.dll's base address is found, the code parses its Export Directory in memory to find the address of a specific function (GetSystemDirectoryA). This is a real-world application of the theory.
Practical Exercise
Let's put this together with a simple hands-on exercise.
- Create a file named
apitest.cwith the following code:#include <windows.h> int main() { MessageBoxA(NULL, "Hello from the debugger!", "API Test", MB_OK); return 0; } - Compile it as a 32-bit executable using a C compiler on Windows (like MinGW-w64 or the compiler from Visual Studio). For example, with GCC:
gcc -o apitest.exe apitest.c -luser32 - Load
apitest.exeinto x64dbg (use the 32-bit version, x32dbg). - Let the program run to the main entry point (it will pause there by default). Go to the "Symbols" tab. Find
user32.dllin the list of modules on the left and select it. In the right pane, find the address of theMessageBoxAfunction. Note this address. - Return to the "CPU" tab. In the disassembly, find the
callinstruction that corresponds to yourMessageBoxAcall in the C code. - You will notice that the
callinstruction jumps to an address inside the.idatasection of yourapitest.exemodule, not the address you found inuser32.dll. This address is an entry in your program's IAT. - Right-click on this
callinstruction's operand (the IAT address) and select "Follow in Dump". The dump window will navigate to that address. - Observe the 4 bytes at that location in the dump. They should form the exact address of
MessageBoxAwithinuser32.dllthat you noted in step 4. You have just manually traced a resolved API call through the IAT!
Conclusion
In this lesson, you've bridged the gap between a static file on disk and a running process in memory. You have seen how the OS loader maps an executable, how ASLR randomizes its location, and how the all-important import mechanism allows a program to connect with the vast functionality provided by system DLLs.
Here are the key takeaways:
- The Windows loader parses a PE file's sections and maps them into a virtual address space.
- ASLR randomizes the base address of executables and DLLs as a security measure.
- Function calls to external DLLs are resolved via the Import Address Table (IAT). The loader populates the IAT with the correct runtime function addresses.
- Advanced code, like shellcode or malware, can bypass the IAT entirely by traversing the Process Environment Block (PEB) to find loaded modules and parse their Export Address Tables (EATs) manually.
You've now completed your foundational tour of executable formats and their lifecycle. In the next module, we will build upon this dynamic analysis foundation and explore higher-level reverse engineering techniques using industry-standard tools. Our first topic will be using Ghidra's powerful decompiler to translate raw assembly back into much more readable pseudo-C code.