Platform:Windows/Loader module lists
How user-mode code enumerates loaded modules without API calls: PEB.Ldr -> PEB_LDR_DATA -> three doubly linked lists of LDR_DATA_TABLE_ENTRY, and what the common walks look like.
The three lists
PEB.Ldr points to a PEB_LDR_DATA holding three LIST_ENTRY heads: InLoadOrderModuleList, InMemoryOrderModuleList, InInitializationOrderModuleList. Each list links the matching embedded LIST_ENTRY inside every LDR_DATA_TABLE_ENTRY (InLoadOrderLinks at entry+0, InMemoryOrderLinks at +0x10 on x64 / +8 on x86, InInitializationOrderLinks at +0x20 / +0x10). So code that walks the memory-order list reads the entry from link - 0x10 (x64) or link - 8 (x86). That -8/-0x10 correction is the telltale sign in disassembly.
Typical walks
- Memory-order list: first entry is the main executable, second is ntdll.dll; the third is kernel32.dll on Windows 7 and earlier, kernelbase.dll or kernel32.dll later. Hand-written code that finds kernel32 by position must handle this (Unverified for every build; check the BaseDllName).
- Initialization-order list: ntdll is not listed first in all versions; do not rely on it.
- Matching: compare
BaseDllName(UNICODE_STRING, case-insensitive, UTF-16) or a hash of it; the module base isDllBase, sizeSizeOfImage, entry pointEntryPoint. - With the base, the code parses the export directory of the PE (Format:PE) to resolve functions by name or hash.
Notes for reversers
- Walking the lists is used by loaders, hook engines, and packers alike; it is not evidence of anything by itself.
- Modules mapped with
LoadLibraryEx(LOAD_LIBRARY_AS_DATAFILE)are not in the lists; their handle has low bits set (LDR_IS_DATAFILE: bit 0, LDR_IS_IMAGEMAPPING: bit 1). - The lists are only valid while the loader lock is held or the process is single-threaded.
Related
Platform:Windows/PEB and TEB, Platform:Windows.
Evidence & references
phnt ntldr.h and ntpsapi.h (MIT).