NKLegacy includes a minimal PE32 loader that can validate and execute PE32 executables directly inside the kernel address space. It is used by theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/pastaboy12345/NKLegacy/llms.txt
Use this file to discover all available pages before exploring further.
run shell command to execute binaries produced by NKBuild. The loader is implemented in ntoskrnl/exe/pe.c and its types and public API are declared in ntoskrnl/exe/pe.h.
PE32 Format Overview
NKLegacy’s loader understands the standard PE32 on-disk layout:Key Type Definitions
All types are defined inntoskrnl/exe/pe.h.
Constants
IMAGE_DOS_HEADER
IMAGE_FILE_HEADER
IMAGE_OPTIONAL_HEADER32
IMAGE_NT_HEADERS32
IMAGE_SECTION_HEADER
API Reference
pe_load_and_run
0 on success (after the entry point function returns). Returns -1 on any validation failure, printing a diagnostic message via kprintf.
Parameters
| Parameter | Description |
|---|---|
file_buffer | Pointer to the in-memory PE image (e.g. from NKBuild). |
file_size | Size of the image in bytes. |
Validation Steps
pe_load_and_run performs the following checks in order before executing any code:
Buffer size check
Verifies that
file_buffer is non-NULL and that file_size >= sizeof(IMAGE_DOS_HEADER). Prints PE: Invalid buffer size and returns -1 on failure.MZ signature check
Reads
dos_header->e_magic and verifies it equals IMAGE_DOS_SIGNATURE (0x5A4D). Prints PE: Invalid DOS signature (0x<val>) on failure.NT headers offset bounds check
Verifies that
e_lfanew is within the buffer and that e_lfanew + sizeof(IMAGE_NT_HEADERS32) does not exceed file_size. Prints PE: Invalid NT headers offset on failure.PE signature check
Reads
nt_headers->Signature and verifies it equals IMAGE_NT_SIGNATURE (0x00004550). Prints PE: Invalid NT signature (0x<val>) on failure.Machine type check
Reads
FileHeader.Machine and verifies it equals 0x014C (i386). Prints PE: Invalid Machine type (0x<val>). Only i386 supported. on failure.Section mapping
Iterates over the If
NumberOfSections entries in IMAGE_SECTION_HEADER[]. For each section, computes:size > 0 and dest != src, copies the raw data to the virtual address with memcpy. This requires that the target physical address is identity-mapped.Prints: PE: Loading <N> sections to ImageBase 0x<addr>...run Command Flow
The run shell command in cmd.c wires NKBuild and the PE loader together:
Get buffer
nkbuild_get_exe_buffer() returns the pointer to the static g_exe_buffer, or NULL if no binary has been finalized.Guard check
If either value indicates no built binary, prints
No executable built yet. Run 'build' first. and returns without calling the loader.