1327 words
7 minutes
FLARE Learning Hub - Chapter 9: Reversing Windows Binaries

Chapter 9 — the last chapter I’m writing up from Mandiant/FLARE’s “Malware Analysis Crash Course”. This is the chapter I find most practical for malware analysis, since almost every notable piece of malware behavior on Windows (creating files, modifying the registry, network communication…) goes through the Windows API.

mandiant
/
flare-learning-hub
Waiting for api.github.com...
00K
0K
0K
Waiting...

Windows API Functions#

The Windows API (Win32 API) is the core set of functions applications use to interact with the operating system — since user-space applications can’t touch hardware directly, almost every operation ultimately comes down to an API call. An interesting detail the material mentions: CreateWindow sample code from the 1985 book Windows Programming Guide (Charles Petzold) still compiles and runs correctly today — a testament to the Windows API’s rare 40-year stability (the one exception: the PASCAL calling convention once used for API imports has since been replaced by STDCALL).

Windows Data Types and Data Structures#

The basic type table (same as Table 1.1 back in Chapter 1, revisited from the Windows API perspective): BYTE/WORD/DWORD/QWORD. DWORD is the most common type for unsigned integer values; an explicit QWORD only shows up when the source code deliberately uses a 64-bit number — most 64-bit values in 64-bit code are actually pointers, not numbers.

Windows defines its own types via typedef rather than using standard C types directly, to stay language-neutral. For example, the SYSTEMTIME struct:

typedef struct _SYSTEMTIME {
  WORD wYear;
  WORD wMonth;
  WORD wDayOfWeek;
  WORD wDay;
  WORD wHour;
  WORD wMinute;
  WORD wSecond;
  WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME;

A Windows struct typically produces 3 names: struct _SYSTEMTIME (the original C name, rarely used directly), SYSTEMTIME (the typedef, the standard commonly-used name), and PSYSTEMTIME (a pointer to that struct — the P/LP prefix always means “pointer to this type”).

Hungarian Notation#

Windows API field/variable names are prefixed according to their data type — for example, the year field in SYSTEMTIME is named wYear (prefix w = WORD):

Windows TypePrefixExampleMeaning
WORDwwYeara 16-bit unsigned integer representing a year
DWORDdwdwSizea 32-bit unsigned integer representing a size
LPCSTRlplpFileNamea pointer to a constant, null-terminated ASCII string

LPCSTR in particular is an abbreviation combining 3 pieces of meaning: STR (an 8-bit ASCII string), C (constant — the function doesn’t modify this string), LP (pointer). Being able to break this down lets you guess a parameter’s meaning right away without having to look up the docs.

Reading a Windows API Function Prototype#

Microsoft’s prototypes often look different from a standard C function declaration — for example, GetSystemTime:

VOID WINAPI GetSystemTime(
  _Out_ LPSYSTEMTIME lpSystemTime
);
  • VOID — the return type, meaning no return value
  • WINAPI — a macro specifying the calling convention (usually __stdcall on 32-bit)
  • _Out_ — a Microsoft SAL (Source-code Annotation Language) annotation, indicating this parameter receives data from the function (the function writes to it). Other annotations: _In_ (the caller supplies the data, the function only reads it), _Inout_ (both directions), _In_opt_ (an optional parameter, allowed to be NULL). SAL is purely documentation/a hint for static analysis tools, and gets stripped out by the preprocessor before real compilation.

The CreateFile example illustrates this reading approach clearly:

HANDLE WINAPI CreateFile(
  _In_     LPCTSTR               lpFileName,
  _In_     DWORD                 dwDesiredAccess,
  _In_     DWORD                 dwShareMode,
  _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  _In_     DWORD                 dwCreationDisposition,
  _In_     DWORD                 dwFlagsAndAttributes,
  _In_opt_ HANDLE                hTemplateFile
);

lpSecurityAttributes and hTemplateFile are marked _In_opt_ — it’s safe to pass NULL if you don’t need them.

Handles#

A handle is Windows’ unified mechanism for managing system resources as objects — user-land applications can’t touch an object directly, only operate on it through a handle. Fundamentally, a handle is an index into a handle table private to each process — since each process has its own table, the same handle value in Process A might point to a completely different object in Process B.

A typical handle lifecycle (illustrated through a file-read operation):

  1. CreateFile opens the file, returning a handle on success
  2. ReadFile uses that handle to read data
  3. CloseHandle closes it when done — this function is shared across nearly every type of handle, not just files

Windows API Naming Conventions#

Most string-handling APIs come in 2 versions: ANSI and Unicode. CreateFile in the documentation is just the generic name — the system library actually exports both CreateFileA (suffix A = ANSI) and CreateFileW (suffix W = Wide/Unicode UTF-16LE). Source code calls the CreateFile macro, which resolves to A or W at compile time depending on the project’s configuration. Since the Windows kernel internally uses Unicode, the ANSI version (CreateFileA) actually just converts the string to wide and then calls the CreateFileW version.

File API Workflows#

The material groups file APIs into 2 separate categories:

I/O group — reading/writing file contents:

CreateFile  →  ReadFile hoặc WriteFile  →  CloseHandle

Enumeration group — listing files matching a pattern:

FindFirstFile (với search pattern)  →  FindNextFile (lặp lại cho tới hết)  →  FindClose

Windows Registry#

A tree-structured configuration database made of keys/subkeys — every key contains at least 1 value (the “Default” value), and each value has its own data type (REG_SZ for strings, REG_DWORD for 32-bit numbers…). Malware frequently modifies the registry to establish persistence or hide a payload.

The 5 main root keys:

Root KeyAbbreviationDescription
HKEY_CURRENT_USERHKCUData for the currently logged-in user
HKEY_USERSHKUInformation for every account on the machine
HKEY_CLASSES_ROOTHKCRFile type associations, COM object registration
HKEY_LOCAL_MACHINEHKLMSystem-wide configuration, applies to all users
HKEY_CURRENT_CONFIGHKCCInformation about the current hardware profile

The two hives malware targets most often: HKLM (affecting the whole system) and HKCU (specific to the current user).

Registry API Workflow#

RegCreateKeyEx (tạo mới) hoặc RegOpenKeyEx (mở key có sẵn)
  → RegQueryValueEx (đọc) hoặc RegSetValueEx (ghi)
  → RegCloseKey

Process Creation Overview#

When a .exe file is launched, the first thing that happens isn’t the program’s own code — it’s the Windows loader initializing the process itself.

Windows uses a virtual memory model — each process has its own private address space, fully isolated by default. When an executable is run, ntdll.dll and kernel32.dll are automatically mapped into the process first. The first code that runs is not the program’s entry point — it’s the Windows loader’s own code (living inside ntdll.dll), which proceeds in this order:

  1. Parses the PE header to validate the file
  2. Builds the virtual memory space, mapping the file’s sections into memory exactly as described in the header
  3. Recursively resolves the import table — loading all the listed required DLLs, then resolving the imports of those very DLLs in turn
  4. Hands off execution to the AddressOfEntryPoint declared in the file — this is when the program’s “real” code finally starts running

Besides the static import table mechanism, LoadLibrary + GetProcAddress allow manually loading and resolving functions at runtime — this is called Runtime Dynamic Linking. Malware very often uses this pair to dodge the static import table: hiding the real DLL/function names entirely (usually encrypted), making it hard for static/automated analysis tools to figure out what the program is actually calling.

DLL Function Usage#

A concrete example of resolving printf at runtime from msvcrt.dll:

  1. LoadLibraryA("msvcrt.dll") — maps the DLL into the process (if not already present), returning an HMODULE (which is simply that DLL’s base address)
  2. GetProcAddress(hModule, "printf") — returns the actual address of the printf function inside the just-loaded DLL
  3. Calls printf indirectly through the function pointer just obtained (there’s no explicit CALL printf in the disassembly at all — only a CALL [pointer])

Windows Networking#

Windows provides 2 layers of networking APIs:

  • Winsock — closely follows the Berkeley Sockets standard, for low-level TCP/UDP, leaving higher-level protocol handling up to you. Both client and server call socket first; the client fills in the destination address in a sockaddr struct and calls connect; the server calls bind (attaching the socket to a local port) → listen (entering a waiting state) → accept (receiving an incoming connection).
  • WinInet — a higher-level abstraction, with built-in support for HTTP/HTTPS/FTP. The typical flow for sending a single HTTP request:
InternetOpen        -> khởi tạo thư viện WinInet
InternetConnect      -> chỉ định server đích + port
HttpOpenRequest       -> tạo request, khai báo verb (GET/POST) + path
HttpSendRequest        -> gửi header + dữ liệu (nếu có)
InternetReadFile        -> đọc response trả về
InternetCloseHandle      -> giải phóng mọi handle đã mở

Malware using HTTP-based C2 usually leaves a very clear trail through this InternetOpen → InternetConnect → HttpOpenRequest → HttpSendRequest call chain — tracing imports + xrefs is the fastest way to track down the C2 server’s domain/IP.


And that wraps up all 9 theory chapters of “Malware Analysis Crash Course”.

FLARE Learning Hub - Chapter 9: Reversing Windows Binaries
https://stewmalwarehunter.id.vn/posts/flare-macc-ch9---reversing-windows-binaries-en/
Author
Stew
Published at
2026-10-02
License
CC BY-NC-SA 4.0