Advertisement
Broihon

Untitled

Jan 2nd, 2017
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2.  
  3. #ifndef MEMORY_EXT_H
  4. #define MEMORY_EXT_H
  5.  
  6. #include <Windows.h>
  7. #include "NT Func.h"
  8.  
  9. #define H_CURRENT_PROCESS (void*)-1
  10.  
  11. typedef unsigned __int64 QWORD;
  12.  
  13. UINT_PTR GetDMA(HANDLE hProc, UINT_PTR BaseAddress, UINT_PTR * Offsets, UINT PointerLevel);
  14.  
  15. bool SetDebugPrivilege(bool Enable = true);
  16.  
  17. bool IsValidHandle(HANDLE hObject);
  18.  
  19. HANDLE GetProcessByNameA(const char     * szProcess, DWORD Access = PROCESS_ALL_ACCESS);
  20.  
  21. HANDLE GetProcessByNameW(const wchar_t  * szProcess, DWORD Access = PROCESS_ALL_ACCESS);
  22.  
  23. UINT_PTR GetModuleBaseA(const char      * szModule, DWORD ProcID);
  24.  
  25. UINT_PTR GetModuleBaseW(const wchar_t   * szModule, DWORD ProcID);
  26.  
  27. bool NopCode(HANDLE hProc, UINT_PTR Address, SIZE_T Size);
  28.  
  29. ULONG_PTR GetThreadStartAddress(HANDLE hThread);
  30.  
  31. #ifdef UNICODE
  32. #define GetProcessByName GetProcessByNameW
  33. #define GetModuleBase GetModuleBaseW
  34. #else
  35. #define GetProcessByName GetProcessByNameA
  36. #define GetModuleBase GetModuleBaseA
  37. #endif
  38.  
  39. #pragma region READ
  40.  
  41. template <class T>
  42. T ReadMemory(HANDLE hProc, UINT_PTR Address)
  43. {
  44.     T Ret;
  45.     ReadProcessMemory(hProc, reinterpret_cast<void*>(Address), &Ret, sizeof(T), nullptr);
  46.     return Ret;
  47. }
  48.  
  49. template <class T>
  50. BOOL ReadMemory(HANDLE hProc, UINT_PTR Address, T & Out)
  51. {
  52.     return ReadProcessMemory(hProc, reinterpret_cast<void*>(Address), Out, sizeof(T), nullptr);
  53. }
  54.  
  55. template <class T>
  56. BOOL ReadMemory(HANDLE hProc, UINT_PTR Address, T & Out, SIZE_T Count)
  57. {
  58.     return ReadProcessMemory(hProc, reinterpret_cast<void*>(Address), Out, sizeof(T) * Count, nullptr);
  59. }
  60.  
  61. template <class T>
  62. T ReadDMA(HANDLE hProc, UINT_PTR BaseAddress, DWORD * Offset, UINT PointerLevel)
  63. {
  64.     return ReadMemory<T>(hProc, GetDMA(hProc, BaseAddress, Offset, PointerLevel));
  65. }
  66.  
  67. template <class T>
  68. BOOL ReadDMA(HANDLE hProc, UINT_PTR BaseAddress, DWORD * Offset, UINT PointerLevel, T & Out)
  69. {
  70.     return ReadMemory<T>(hProc, GetDMA(hProc, BaseAddress, Offset, PointerLevel), Out);
  71. }
  72.  
  73. template <class T>
  74. BOOL ReadDMA(HANDLE hProc, UINT_PTR BaseAddress, DWORD * Offset, UINT PointerLevel, T & Out, SIZE_T Count)
  75. {
  76.     return ReadMemory<T>(hProc, GetDMA(hProc, BaseAddress, Offset, PointerLevel), Out, Count);
  77. }
  78.  
  79. #define ReadByte    ReadMemory<BYTE>
  80. #define ReadWord    ReadMemory<WORD>
  81. #define ReadDword   ReadMemory<DWORD>
  82. #define ReadQword   ReadMemory<QWORD>
  83. #define ReadChar    ReadMemory<char>
  84. #define ReadShort   ReadMemory<short>
  85. #define ReadLong    ReadMemory<long>
  86. #define ReadLLong   ReadMemory<long long>
  87. #define ReadInt8    ReadMemory<__int8>
  88. #define ReadInt16   ReadMemory<__int16>
  89. #define ReadInt32   ReadMemory<__int32>
  90. #define ReadInt64   ReadMemory<__int64>
  91.  
  92. #pragma endregion
  93.  
  94. #pragma region WRITE
  95.  
  96. template <class T>
  97. BOOL WriteMemory(HANDLE hProc, UINT_PTR Address, const T & Data)
  98. {
  99.     return WriteProcessMemory(hProc, reinterpret_cast<void*>(Address), &Ret, sizeof(T), nullptr);
  100. }
  101.  
  102. template <class T>
  103. BOOL WriteMemory(HANDLE hProc, UINT_PTR Address, const T * Data, SIZE_T Count)
  104. {
  105.     return WriteProcessMemory(hProc, reinterpret_cast<void*>(Address), Data, sizeof(T) * Count, nullptr);
  106. }
  107.  
  108. template <class T>
  109. BOOL WriteDMA(HANDLE hProc, UINT_PTR BaseAddress, DWORD * Offset, UINT PointerLevel, const T & Data)
  110. {
  111.     return WriteMemory<T>(hProc, GetDMA(hProc, BaseAddress, Offset, PointerLevel), Data);
  112. }
  113.  
  114. template <class T>
  115. BOOL WriteDMA(HANDLE hProc, UINT_PTR BaseAddress, DWORD * Offset, UINT PointerLevel, const T & Data, SIZE_T Count)
  116. {
  117.     return WriteMemory<T>(hProc, GetDMA(hProc, BaseAddress, Offset, PointerLevel), Data, Count);
  118. }
  119.  
  120. #define WriteByte   WriteMemory<BYTE>
  121. #define WriteWord   WriteMemory<WORD>
  122. #define WriteDword  WriteMemory<DWORD>
  123. #define WriteQword  WriteMemory<QWORD>
  124. #define WriteChar   WriteMemory<char>
  125. #define WriteShort  WriteMemory<short>
  126. #define WriteLong   WriteMemory<long>
  127. #define WriteLLong  WriteMemory<long long>
  128. #define WriteInt8   WriteMemory<__int8>
  129. #define WriteInt16  WriteMemory<__int16>
  130. #define WriteInt32  WriteMemory<__int32>
  131. #define WriteInt64  WriteMemory<__int64>
  132.  
  133. #pragma endregion
  134.  
  135. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement