Windows Native API


The Native API is a lightweight application programming interface used by Windows NT's kernel and user mode applications. This API is used in the early stages of Windows NT startup process, when other components and APIs are still unavailable. Therefore, a few Windows components, such as the Client/Server Runtime Subsystem, are implemented using the Native API. The Native API is also used by subroutines such as those in kernel32.dll that implement the Windows API, the API based on which most of the Windows components are created.
Most of the Native API calls are implemented in ntoskrnl.exe and are exposed to user mode by ntdll.dll. The entry point of ntdll.dll is LdrInitializeThunk. Native API calls are handled by the kernel via the System Service Descriptor Table.

Function groups

The Native API comprises many functions. They include C runtime functions that are needed for a very basic C runtime execution, such as strlen, sprintf, memcpy and floor. Other common procedures like malloc, printf, scanf are missing. The vast majority of other Native API routines, by convention, have a 2 or 3 letter prefix, which is:
  • Nt or Zw are system calls declared in ntdll.dll and ntoskrnl.exe. When called from ntdll.dll in user mode, these groups are almost exactly the same; they execute an interrupt into kernel mode and call the equivalent function in ntoskrnl.exe via the SSDT. When calling the functions directly in ntoskrnl.exe, the Zw variants ensure kernel mode, whereas the Nt variants do not. The Zw prefix does not stand for anything.
  • Rtl is the second largest group of ntdll calls. These comprise the C Run-Time Library, which includes many utility functions that can be used by native applications, yet don't directly involve kernel support.
  • Csr are client-server functions that are used to communicate with the Win32 subsystem process, csrss.exe.
  • Dbg are debugging functions such as a software breakpoint.
  • Ki are upcalls from kernel mode for events like APC dispatching.
  • Ldr are loader functions for PE file handling and starting of new processes.
  • Nls for National Language Support.
  • Pfx for prefix handling.
  • Tp for threadpool handling.
user32.dll and gdi32.dll include several other calls that execute an interrupt into kernel mode. These were not part of the original Windows NT design, as can be seen in Windows NT 3.5. However, due to performance issues of hardware of that age, it was decided to move the graphics subsystem into kernel mode. As such, system call in the range of 0x1000-0x1FFF are satisfied by win32k.sys, and are declared in user32.dll and gdi32.dll. These functions have the NtUser and NtGdi prefix.

Uses

Uses of Native API functions includes but not limited to:
  • Enabling and disabling privileges
  • Creating remote threads within processes that are running in different session
  • Running native applications
  • Performing a forced shutdown
  • Causing a BSOD in User mode
  • Displaying a string in Native Mode