Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.16 KB | None | 0 0
  1.  BOOL
  2.  NTAPI
  3.  WriteProcessMemory(IN HANDLE hProcess,
  4.                     IN LPVOID lpBaseAddress,
  5.                     IN LPCVOID lpBuffer,
  6.                     IN SIZE_T nSize,
  7.                     OUT SIZE_T *lpNumberOfBytesWritten)
  8.  {
  9.      NTSTATUS Status;
  10.      ULONG OldValue;
  11.      SIZE_T RegionSize;
  12.      PVOID Base;
  13.      BOOLEAN UnProtect;
  14.  
  15.      /* Set parameters for protect call */
  16.      RegionSize = nSize;
  17.      Base = lpBaseAddress;
  18.  
  19.      /* Check the current status */
  20.      Status = NtProtectVirtualMemory(hProcess,
  21.                                      &Base,
  22.                                      &RegionSize,
  23.                                      PAGE_EXECUTE_READWRITE,
  24.                                      &OldValue);
  25.      if (NT_SUCCESS(Status))
  26.      {
  27.          /* Check if we are unprotecting */
  28.          UnProtect = OldValue & (PAGE_READWRITE |
  29.                                  PAGE_WRITECOPY |
  30.                                  PAGE_EXECUTE_READWRITE |
  31.                                  PAGE_EXECUTE_WRITECOPY) ? FALSE : TRUE;
  32.          if (!UnProtect)
  33.          {
  34.              /* Set the new protection */
  35.              Status = NtProtectVirtualMemory(hProcess,
  36.                                              &Base,
  37.                                              &RegionSize,
  38.                                              OldValue,
  39.                                              &OldValue);
  40.  
  41.              /* Write the memory */
  42.              Status = NtWriteVirtualMemory(hProcess,
  43.                                            lpBaseAddress,
  44.                                            (LPVOID)lpBuffer,
  45.                                            nSize,
  46.                                            &nSize);
  47.  
  48.              /* In Win32, the parameter is optional, so handle this case */
  49.              if (lpNumberOfBytesWritten) *lpNumberOfBytesWritten = nSize;
  50.  
  51.              if (!NT_SUCCESS(Status))
  52.              {
  53.                  /* We failed */
  54.                  BaseSetLastNTError(Status);
  55.                  return FALSE;
  56.              }
  57.  
  58.              /* Flush the ITLB */
  59.              NtFlushInstructionCache(hProcess, lpBaseAddress, nSize);
  60.              return TRUE;
  61.          }
  62.          else
  63.          {
  64.              /* Check if we were read only */
  65.              if (OldValue & (PAGE_NOACCESS | PAGE_READONLY))
  66.              {
  67.                  /* Restore protection and fail */
  68.                  NtProtectVirtualMemory(hProcess,
  69.                                         &Base,
  70.                                         &RegionSize,
  71.                                         OldValue,
  72.                                         &OldValue);
  73.                  BaseSetLastNTError(STATUS_ACCESS_VIOLATION);
  74.  
  75.                  /* Note: This is what Windows returns and code depends on it */
  76.                  return STATUS_ACCESS_VIOLATION;
  77.              }
  78.  
  79.              /* Otherwise, do the write */
  80.              Status = NtWriteVirtualMemory(hProcess,
  81.                                            lpBaseAddress,
  82.                                            (LPVOID)lpBuffer,
  83.                                            nSize,
  84.                                            &nSize);
  85.  
  86.              /* In Win32, the parameter is optional, so handle this case */
  87.              if (lpNumberOfBytesWritten) *lpNumberOfBytesWritten = nSize;
  88.  
  89.              /* And restore the protection */
  90.              NtProtectVirtualMemory(hProcess,
  91.                                     &Base,
  92.                                     &RegionSize,
  93.                                     OldValue,
  94.                                     &OldValue);
  95.              if (!NT_SUCCESS(Status))
  96.              {
  97.                  /* We failed */
  98.                  BaseSetLastNTError(STATUS_ACCESS_VIOLATION);
  99.  
  100.                  /* Note: This is what Windows returns and code depends on it */
  101.                  return STATUS_ACCESS_VIOLATION;
  102.              }
  103.  
  104.              /* Flush the ITLB */
  105.              NtFlushInstructionCache(hProcess, lpBaseAddress, nSize);
  106.              return TRUE;
  107.          }
  108.      }
  109.      else
  110.      {
  111.          /* We failed */
  112.          BaseSetLastNTError(Status);
  113.          return FALSE;
  114.      }
  115.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement