Step Into Labs

Step Into C · Lesson 32 · Inside Windows

The PEB & the Loader

The process environment block, its fields, your command line, and the loader's list of modules.

This lesson is free. Open it in the app to write and run the code, with the real compiler and debugger. No sign-up needed.
Open the lesson

Every process has a PEB

The Process Environment Block is a structure in your own address space where Windows keeps facts about the process: where the EXE is loaded, the command line, the loaded modules, whether a debugger is attached. API functions read it for you: GetModuleHandleW(NULL) returns a field of it, GetCommandLineW() another, IsDebuggerPresent() a third. Debuggers, Process Explorer and the loader read it directly.

Each thread has a TEB (Thread Environment Block), and on x64 the gs segment register points at the current thread's TEB. The TEB holds a pointer to the PEB at offset 0x60:

c
PEB *peb = (PEB *)__readgsqword(0x60);       // compiler intrinsic: read qword at gs:[0x60]

NtQueryInformationProcess with ProcessBasicInformation reports the same address in PebBaseAddress.

The fields you will meet

Offset (x64)FieldMeans
0x02BeingDebugged1 while a debugger is attached (IsDebuggerPresent returns it)
0x10ImageBaseAddresswhere the EXE is mapped (GetModuleHandleW(NULL))
0x18Ldrthe loader's data: lists of loaded modules
0x20ProcessParametersimage path, command line, current directory, environment

winternl.h declares a partial PEB; most fields are named Reserved. Writing your own small struct with the documented offsets, as this lesson does, makes the layout explicit.

UNICODE_STRING

Kernel and ntdll structures store strings as a UNICODE_STRING: a pointer plus a length in bytes, not necessarily 0-terminated.

c
typedef struct { USHORT Length; USHORT MaximumLength; PWSTR Buffer; } UNICODE_STRING;
// characters = Length / sizeof(WCHAR)
printf("%.*ls", (int)(s.Length / sizeof(WCHAR)), s.Buffer);

The loader's module list

Ldr points at a PEB_LDR_DATA that holds three circular doubly linked lists of LDR_DATA_TABLE_ENTRY records, one per loaded module, in load order, memory order and initialization order. The list nodes are LIST_ENTRYs inside each record, exactly the LIST_ENTRY / CONTAINING_RECORD pattern from the C capstone:

c
LIST_ENTRY *head = &peb->Ldr->InMemoryOrderModuleList;
for (LIST_ENTRY *e = head->Flink; e != head; e = e->Flink) {
    LDR_DATA_TABLE_ENTRY *mod = CONTAINING_RECORD(e, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks);
    // mod->DllBase, mod->FullDllName ...
}

The order always starts the same way: the EXE, then ntdll.dll, kernel32.dll, kernelbase.dll. This lesson only reads these structures. Modifying them corrupts the process.

Watch out
Press Debug on this lesson's program: BeingDebugged becomes 1, and the debugger's stack and register views show you the very addresses you print.

Missions

  1. Find the PEB

    Implement find_peb with __readgsqword(0x60), and same_peb: look up NtQueryInformationProcess in ntdll, ask for ProcessBasicInformation, and compare its PebBaseAddress with yours. Also confirm the struct layout with offsetof:

    output
    same PEB: yes
    ImageBaseAddress offset: 0x10
  2. Read the fields

    In read_fields: compare ImageBaseAddress with GetModuleHandleW(NULL) and BeingDebugged with IsDebuggerPresent():

    output
    image base matches GetModuleHandleW(NULL): yes
    BeingDebugged matches IsDebuggerPresent: yes

    Both lines stay "yes" when you press Debug, even though BeingDebugged changes.

  3. Your command line, from the source

    In command_line: the ProcessParameters->CommandLine UNICODE_STRING holds your full command line. Print its last 9 characters (remember: Length is in bytes):

    output
    tail: step into

    when run with the arguments step into (the watchdog does this; type them in the Input tab to try).

  4. The loader's module list

    In modules: walk peb->Ldr->InMemoryOrderModuleList. For the first four modules print the file name from FullDllName (the part after the last \, in lower case), and check that the second one's DllBase is ntdll:

    output
    1: main.exe
    2: ntdll.dll
    3: kernel32.dll
    4: kernelbase.dll
    ntdll base matches GetModuleHandleW: yes
This lesson is free. Open it in the app to write and run the code, with the real compiler and debugger. No sign-up needed.
Open the lesson