2. Win32 API and Windows Native API

Windows NTAPI (Native API)

NTAPI (Windows Native API) is a set of low-level system functions provided by ntdll.dll that allow user-mode applications to interact directly with the Windows NT kernel (ntoskrnl.exe).

Unlike Win32 API (which is used by most applications), NTAPI functions are primarily used by Windows itself and low-level system utilities for:


1. NTAPI vs. Win32 API

Feature NTAPI (Native API) Win32 API (Windows API)
Access Level Lower-level (closer to kernel) Higher-level (user-friendly)
Availability Implemented in ntdll.dll Implemented in kernel32.dll, user32.dll
Usage Used internally by Windows Used by most applications
Stability Unstable (can change between Windows versions) Stable and backward-compatible
Examples NtCreateFile, NtAllocateVirtualMemory CreateFile, VirtualAlloc

2. How NTAPI Works

When a user-mode application makes a Win32 API call, it often internally calls an NTAPI function in ntdll.dll, which then transitions into kernel mode via a system call.

graph TD
  A[User_Application] -->|Calls Win32 API _e.g., CreateFile_| B[WinAPI_DLLs _kernel32.dll_]
  B -->|Calls NTAPI _e.g., NtCreateFile_| C[ntdll.dll]
  C -->|Performs Syscall _SSDT_| D[Windows_Kernel _ntoskrnl.exe_]
  D -->|Executes System Operation| E[Hardware_&_OS_Components]
  E -->|Returns Data| D
  D -->|Returns to User Mode| C
  C -->|Passes Data to Win32 API| B
  B -->|Returns Result to Application| A

3. Examples of NTAPI Functions

Here's a table with NTAPI functions categorized by their use, along with brief explanations:

Category Function Brief Explanation
Process & Thread Management NtCreateProcess Creates a new process in Windows. Used internally for spawning processes.
NtCreateThreadEx Creates a new thread within a process. Often used in process injection techniques.
NtTerminateProcess Terminates a process by its handle, forcibly stopping execution.
Memory Management NtAllocateVirtualMemory Allocates memory in a process's virtual address space. Used in malware injection.
NtFreeVirtualMemory Frees memory that was allocated with NtAllocateVirtualMemory.
NtProtectVirtualMemory Changes memory protection (e.g., making a page executable). Used in shellcode execution.
File & Registry Operations NtCreateFile Opens or creates a file, bypassing standard Windows APIs.
NtReadFile Reads data from a file handle. Used for direct file access.
NtOpenKey Opens a registry key. Used in Windows configuration modifications.
NtQueryValueKey Retrieves the value of a registry key. Often used in malware persistence.
Security & Access Control NtOpenProcessToken Retrieves the security token of a process. Used in privilege escalation.
NtAdjustPrivilegesToken Modifies the privileges of a process (e.g., enabling SeDebugPrivilege for admin access).

Key Takeaways

Would you like specific syscall examples in C or assembly for exploitation or debugging? 🚀


4. Why NTAPI Matters in Security

Legitimate Uses

Exploit and Malware Uses


5. Key Takeaways

Would you like examples of NTAPI-based process injection or how to analyze NTAPI calls in memory for security research? 🚀