Guest User

Untitled

a guest
May 27th, 2015
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h>
  4.  
  5. #define NTSTATUS            LONG
  6. #define NT_SUCCESS(Status)  ((LONG)(Status) >= 0L)
  7. #define NTAPI               WINAPI
  8. #define NtCurrentProcess()  ((HANDLE) -1)
  9. typedef NTSTATUS (NTAPI *ALLOC_PAGE_PROTO)(
  10.     HANDLE ProcessHandle,
  11.     PVOID *BaseAddress,
  12.     ULONG_PTR ZeroBits,
  13.     PSIZE_T RegionSize,
  14.     ULONG AllocationType,
  15.     ULONG Protect
  16. );
  17. ALLOC_PAGE_PROTO            ZwAllocateVirtualMemory = NULL;
  18.  
  19. int main(int argc, char *argv[]) {
  20.     HMODULE hModNtDLL = GetModuleHandle(TEXT("ntdll.dll"));
  21.     if(!hModNtDLL) {
  22.         fprintf(stderr, "[!] This example must be run under Windows NT family!\n");
  23.         return EXIT_FAILURE;
  24.     }
  25.  
  26.     ZwAllocateVirtualMemory = (ALLOC_PAGE_PROTO) GetProcAddress(hModNtDLL, "ZwAllocateVirtualMemory");
  27.     if(!ZwAllocateVirtualMemory)
  28.         return EXIT_FAILURE;
  29.  
  30.     PVOID   lpAddrTrick = (PVOID) 1;    // 0x00000001
  31.     SIZE_T  sMemSize    = 0x1000;       // 4KB (1 page)
  32.     NTSTATUS status = ZwAllocateVirtualMemory(NtCurrentProcess(), &lpAddrTrick, 0UL, &sMemSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
  33.     if(NT_SUCCESS(status)) {
  34.         *(int *)NULL = 1234;
  35.         printf("[+] *(int *)NULL = %d\n", *(int *)NULL);
  36.     } else {
  37.         fprintf(stderr, "[!] ZwAllocateVirtualMemory failed! NTSTATUS code = 0x%08lX\n", status);
  38.         return status;
  39.     }
  40.  
  41.     return EXIT_SUCCESS;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment