NKBuild is NKLegacy’s built-in minimal PE32 binary builder. It runs interactively inside the shell — typeDocumentation 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.
build to enter NKBuild mode, provide build directives line by line, then type done to finalize the binary in memory. The resulting PE32 image can immediately be executed with the run command via the PE Loader.
Shell Workflow
Enter NKBuild mode
Type
build at the shell prompt. The shell calls nkbuild_start(), which resets the internal buffer and prints a status message. The prompt changes to nkbuild> in light red (0x0C).Enter directives
Type one directive per line. Each line is passed to
nkbuild_process_line(). Supported directives are listed in the Directives section below. Unknown directives print an error and are skipped.Finalize with done
Type The shell exits NKBuild mode and returns to the normal prompt.
done to trigger the PE header assembly. NKBuild writes the IMAGE_DOS_HEADER, IMAGE_NT_HEADERS32, and IMAGE_SECTION_HEADER into the front of the buffer, then reports the final binary size.API Reference
All functions are declared inntoskrnl/exe/nkbuild.h.
nkbuild_start
g_exe_buffer, resets g_exe_size and g_code_size to 0, and positions the internal code write pointer at offset 512 (one FileAlignment unit past the start of the buffer, reserving space for PE headers). Prints:
build.
nkbuild_process_line
0 when line is "done" and the PE binary has been finalized. Returns 1 for all other inputs (assembled instruction or error).
Special value — "done": Finalizes the PE binary by filling in all header structures:
IMAGE_DOS_HEADERwithe_magic = IMAGE_DOS_SIGNATURE(0x5A4D) ande_lfanewpointing immediately after the DOS header.IMAGE_NT_HEADERS32withSignature = IMAGE_NT_SIGNATURE(0x00004550), machine type0x014C(i386), one section, andIMAGE_OPTIONAL_HEADER32fields set for a native PE32 image loaded atImageBase = 0x01000000withAddressOfEntryPoint = 0x1000.- One
IMAGE_SECTION_HEADERnamed.textwithVirtualAddress = 0x1000, characteristics0x60000020(READ | EXECUTE | CODE), andSizeOfRawDatarounded up to the next 512-byte FileAlignment boundary.
"done" is processed, g_exe_size is set to g_code_offset + sec->SizeOfRawData and the following is printed:
nkbuild_get_exe_size
0 if done has not yet been processed (i.e., the build is still in progress or has never been started).
nkbuild_get_exe_buffer
g_exe_buffer holding the built PE image, or NULL if g_exe_size == 0 (no image has been finalized yet). The run shell command passes this pointer directly to pe_load_and_run.
Directives
NKBuild supports a minimal subset of x86 assembly directives. Each directive emits raw machine code bytes directly into the.text section of the PE image.
| Directive | Encoding | Bytes | Description |
|---|---|---|---|
mov eax, <imm32> | 0xB8 <imm32-le> | 5 | Load a 32-bit immediate into EAX |
ret | 0xC3 | 1 | Near return |
int 3 | 0xCC | 1 | Breakpoint / software interrupt 3 |
done | (special) | — | Finalize headers and exit build mode |
PE Image Layout
The finalized binary is laid out inside the 8 KB (MAX_EXE_SIZE = 8192) static buffer as follows:
The built PE is kept in a single static 8 KB buffer (
g_exe_buffer). Each call to nkbuild_start() zeroes the entire buffer, overwriting any previously built binary. Only one binary can exist at a time.