Advertisement
Guest User

Untitled

a guest
Jun 11th, 2023
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 KB | Source Code | 0 0
  1. #include <windows.h>
  2. #include <winternl.h>
  3. #include <array>
  4. #include <iostream>
  5.  
  6. static void* NtOpenProcessAddress{ GetProcAddress(GetModuleHandleA("ntdll.dll"), "ZwOpenProcess") };
  7.  
  8. LONG WINAPI ExceptionHandler(EXCEPTION_POINTERS* const exceptionInfo)
  9. {
  10.     if (exceptionInfo->ExceptionRecord->ExceptionCode == STATUS_GUARD_PAGE_VIOLATION)
  11.     {
  12.         if (exceptionInfo->ExceptionRecord->ExceptionAddress == NtOpenProcessAddress)
  13.         {
  14.             printf("NtOpenProcess called \n");
  15.         }
  16.  
  17.         // Set single step flag so that memory breakpoints are re-enabled
  18.         // on the next instruction execution.
  19.         exceptionInfo->ContextRecord->EFlags |= 0x100;
  20.  
  21.         return EXCEPTION_CONTINUE_EXECUTION;
  22.     }
  23.  
  24.     if (exceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_SINGLE_STEP) {
  25.  
  26.         // Re-enable memory breakpoint since a different address might
  27.         // have caused the guard page violation.
  28.         std::array<unsigned char, 11> NtProtectVirtualMemoryBytes =
  29.         {
  30.             0x4C, 0x8B, 0xD1,               /*mov r10, rcx*/
  31.             0xB8, 0x50, 0x00, 0x00, 0x00,   /*mov eax, 0x50*/
  32.             0x0F, 0x05,                     /*syscall*/
  33.             0xC3                            /*ret*/
  34.         };
  35.  
  36.         auto* NtProtectVirtualMemoryStub{ VirtualAlloc(nullptr,
  37.             NtProtectVirtualMemoryBytes.size(),
  38.             MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE) };
  39.  
  40.         memcpy(NtProtectVirtualMemoryStub, NtProtectVirtualMemoryBytes.data(), NtProtectVirtualMemoryBytes.size());
  41.  
  42.         using NtProtectVirtualMemoryFnc = NTSTATUS(NTAPI*)(HANDLE, PVOID*, SIZE_T*, ULONG, PULONG);
  43.         auto NtProtectVirtualMemory{
  44.             reinterpret_cast<NtProtectVirtualMemoryFnc>(NtProtectVirtualMemoryStub) };
  45.  
  46.         DWORD oldPermissions{};
  47.         SIZE_T allocSize = 4096;
  48.         auto result{ NtProtectVirtualMemory(GetCurrentProcess(), &NtOpenProcessAddress, &allocSize, PAGE_WRITECOPY | PAGE_GUARD, &oldPermissions) };
  49.  
  50.         return EXCEPTION_CONTINUE_EXECUTION;
  51.     }
  52.  
  53.     return EXCEPTION_CONTINUE_SEARCH;
  54. }
  55.  
  56. BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  57. {
  58.     switch (ul_reason_for_call)
  59.     {
  60.     case DLL_PROCESS_ATTACH:
  61.         AllocConsole();
  62.  
  63.         // Add a custom exception handler
  64.         AddVectoredExceptionHandler(true, ExceptionHandler);
  65.  
  66.         // Set Breakpoint
  67.         std::array<unsigned char, 11> NtProtectVirtualMemoryBytes =
  68.         {
  69.             0x4C, 0x8B, 0xD1,               /*mov r10, rcx*/
  70.             0xB8, 0x50, 0x00, 0x00, 0x00,   /*mov eax, 0x50*/
  71.             0x0F, 0x05,                     /*syscall*/
  72.             0xC3                            /*ret*/
  73.         };
  74.  
  75.         auto* NtProtectVirtualMemoryStub{ VirtualAlloc(nullptr,
  76.             NtProtectVirtualMemoryBytes.size(),
  77.             MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE) };
  78.  
  79.         memcpy(NtProtectVirtualMemoryStub, NtProtectVirtualMemoryBytes.data(),
  80.             NtProtectVirtualMemoryBytes.size());
  81.  
  82.         using NtProtectVirtualMemoryFnc = NTSTATUS(NTAPI*)(HANDLE, PVOID*, SIZE_T*, ULONG, PULONG);
  83.         auto NtProtectVirtualMemory{
  84.             reinterpret_cast<NtProtectVirtualMemoryFnc>(NtProtectVirtualMemoryStub) };
  85.  
  86.         DWORD oldPermissions{};
  87.         SIZE_T allocSize = 4096;
  88.         auto result{ NtProtectVirtualMemory(GetCurrentProcess(), &NtOpenProcessAddress, &allocSize,
  89.             PAGE_WRITECOPY | PAGE_GUARD, &oldPermissions) };
  90.  
  91.         printf("NtProtectVirtualMemory NTSTATUS: %016I64x\n", result);
  92.  
  93.         return TRUE;
  94.     }
  95.  
  96.     return TRUE;
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement