pastebin - collaborative debugging tool
nrubsig.kpaste.net RSS


ntcreatepagefile.c - create a Windows NT paging file
Posted by Anonymous on Sat 25th Oct 2025 14:35
raw | new post

  1.  
  2. /*
  3.  * ntcreatepagefile.c - create a Windows NT paging file
  4.  *
  5.  * - Compile with
  6.  * $ clang -target x86_64-pc-windows-gnu -Wall -Wextra -DUNICODE=1 -D_UNICODE=1 -I/usr/include -g ntcreatepagefile.c -lntdll -o ntcreatepagefile.exe
  7.  *
  8.  * - Useful commands:
  9.  * # list active pagefiles
  10.  * powershell -Command 'Get-CimInstance Win32_PageFile | Select-Object Name,AllocatedSize'
  11.  */
  12. #include <windows.h>
  13. #include <stdio.h>
  14. #include <stdbool.h>
  15.  
  16. typedef LONG NTSTATUS;
  17.  
  18. #ifndef NT_SUCCESS
  19. #define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
  20. #endif
  21.  
  22. /* |UNICODE_STRING| compatible with Native API */
  23. typedef struct _UNICODE_STRING {
  24.     USHORT Length;
  25.     USHORT MaximumLength;
  26.     PWSTR  Buffer;
  27. } UNICODE_STRING, *PUNICODE_STRING;
  28.  
  29. /* NtCreatePagingFile from ntdll.dll (user-mode export) */
  30. __declspec(dllimport)
  31. NTSTATUS NTAPI NtCreatePagingFile(
  32.     const UNICODE_STRING* PageFileName, /* OM path, e.g. \??\N:\pagefile.sys */
  33.     const LARGE_INTEGER*  MinimumSize,  /* bytes, multiple of page size */
  34.     const LARGE_INTEGER*  MaximumSize,  /* bytes, multiple of page size */
  35.     ULONG                 FlagsOrPriority /* historically ignored; 0 is fine */
  36. );
  37.  
  38. static
  39. bool EnablePrivilege(const char *privName, BOOL enable)
  40. {
  41.     HANDLE hTok = NULL;
  42.     if (!OpenProcessToken(GetCurrentProcess(),
  43.         TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hTok)) {
  44.         (void)fprintf(stderr, "OpenProcessToken failed , lasterr=%d\n",
  45.             (int)GetLastError());
  46.         return false;
  47.     }
  48.  
  49.     LUID luid;
  50.     if (!LookupPrivilegeValueA(NULL, privName, &luid)) {
  51.         (void)fprintf(stderr,
  52.             "LookupPrivilegeValueA('%s') failed, lasterr=%d\n",
  53.             privName, (int)GetLastError());
  54.         (void)CloseHandle(hTok);
  55.         return false;
  56.     }
  57.  
  58.     TOKEN_PRIVILEGES tp = {
  59.         .PrivilegeCount = 1,
  60.         .Privileges[0].Luid = luid,
  61.         .Privileges[0].Attributes = (enable ? SE_PRIVILEGE_ENABLED : 0)
  62.     };
  63.  
  64.     if (!AdjustTokenPrivileges(hTok, FALSE, &tp, sizeof(tp), NULL, NULL)) {
  65.         (void)fprintf(stderr, "AdjustTokenPrivileges failed, lasterr=%d\n",
  66.             (int)GetLastError());
  67.         (void)CloseHandle(hTok);
  68.         return false;
  69.     }
  70.  
  71.     if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) {
  72.         (void)fprintf(stderr, "Privilege '%s' not held by this process.\n",
  73.             privName);
  74.         (void)CloseHandle(hTok);
  75.         return false;
  76.     }
  77.  
  78.     (void)CloseHandle(hTok);
  79.     return true;
  80. }
  81.  
  82. static void InitUnicodeString(PUNICODE_STRING u, PCWSTR s)
  83. {
  84.     u->Buffer = (PWSTR)s;
  85.     u->Length = (USHORT)(wcslen(s) * sizeof(WCHAR));
  86.     u->MaximumLength = u->Length;
  87. }
  88.  
  89. const ULONGLONG MiB = 1024ULL * 1024ULL;
  90.  
  91. int main(int ac, char *av[])
  92. {
  93.     (void)ac; /* unused */
  94.  
  95.     /* Native path: \??\N:\pagefile.sys (Object Manager alias for DosDevices) */
  96.     const wchar_t *ntPath = L"\\??\\N:\\pagefile.sys";
  97.  
  98.     HANDLE h = CreateFileW(
  99.         ntPath,
  100.         GENERIC_READ | GENERIC_WRITE,
  101.         FILE_SHARE_READ | FILE_SHARE_WRITE,
  102.         NULL,
  103.         CREATE_ALWAYS,
  104.         FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_WRITE_THROUGH,
  105.         NULL
  106.     );
  107.     if (h == INVALID_HANDLE_VALUE) {
  108.         (void)fprintf(stderr, "%s: CreateFileA() failed, lasterr=%d\n",
  109.             av[0], (int)GetLastError());
  110.         return EXIT_FAILURE;
  111.     }
  112.  
  113.     LARGE_INTEGER off = {
  114.         .QuadPart = 512ULL * MiB
  115.     };
  116.  
  117.     if (!SetFilePointerEx(h, off, NULL, FILE_BEGIN)) {
  118.         (void)fprintf(stderr, "%s: SetFilePointerEx() failed, lasterr=%d\n",
  119.             av[0], (int)GetLastError());
  120.         (void)CloseHandle(h);
  121.         return EXIT_FAILURE;
  122.     }
  123.  
  124.     if (!SetEndOfFile(h)) {
  125.         (void)fprintf(stderr, "%s: SetEndOfFile() failed, lasterr=%d\n",
  126.             av[0], (int)GetLastError());
  127.         (void)CloseHandle(h);
  128.         return EXIT_FAILURE;
  129.     }
  130.  
  131.     if (!FlushFileBuffers(h)) {
  132.         (void)fprintf(stderr, "%s: FlushFileBuffers() failed, lasterr=%d\n",
  133.             av[0], (int)GetLastError());
  134.         (void)CloseHandle(h);
  135.         return EXIT_FAILURE;
  136.     }
  137.     (void)CloseHandle(h);
  138.  
  139.     /* Set min/max sizes (bytes). Must be multiples of page size. */
  140.     LARGE_INTEGER minSize = { .QuadPart = 512ULL * MiB };
  141.     LARGE_INTEGER maxSize = { .QuadPart = 512ULL * MiB };
  142.  
  143.     /* Enable SeCreatePagefilePrivilege */
  144.     if (!EnablePrivilege("SeCreatePagefilePrivilege", TRUE)) {
  145.         (void)fprintf(stderr,
  146.             "%s: Failed to enable SeCreatePagefilePrivilege\n", av[0]);
  147.         return EXIT_FAILURE;
  148.     }
  149.  
  150.     UNICODE_STRING u;
  151.     InitUnicodeString(&u, ntPath);
  152.  
  153.     NTSTATUS st = NtCreatePagingFile(&u, &minSize, &maxSize, 0UL);
  154.  
  155.     /* Revert the privilege */
  156.     EnablePrivilege("SeCreatePagefilePrivilege", FALSE);
  157.  
  158.     if (!NT_SUCCESS(st)) {
  159.         (void)fprintf(stderr,
  160.             "%s: NtCreatePagingFile() failed: ntstatus=0x%lx\n",
  161.             av[0], (long)st);
  162.  
  163.         return EXIT_FAILURE;
  164.     }
  165.  
  166.     (void)printf("SUCCESS: paging file activated at '%ls' (min=%lld, max=%lld bytes)\n",
  167.         ntPath, minSize.QuadPart, maxSize.QuadPart);
  168.     return EXIT_SUCCESS;
  169. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at