Advertisement
Broihon

Untitled

Jan 2nd, 2017
572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2.  
  3. #ifndef MEMORY_INT_H
  4. #define MEMORY_INT_H
  5.  
  6. #include <Windows.h>
  7.  
  8. typedef unsigned __int64 QWORD;
  9. #ifdef ReCa
  10. #undef ReCa
  11. #endif
  12. #define ReCa reinterpret_cast
  13.  
  14. UINT_PTR GetDMA(UINT_PTR BaseAddress, UINT_PTR * Offsets, UINT PointerLevel);
  15. UINT_PTR GetDMA_s(UINT_PTR BaseAddress, UINT_PTR * Offsets, UINT PointerLevel);
  16. bool IsValidWritePtr(void * Ptr);
  17. bool IsValidReadPtr(void * Ptr);
  18. HANDLE CreateThreadAtAddress(PTHREAD_START_ROUTINE pFunc, void * pArg, BYTE * pAddress = nullptr);
  19.  
  20. template <class T>
  21. T CreateFunctionTrp(T TargetFunc, BYTE * pLocation = nullptr)
  22. {
  23.     #ifdef _WIN64
  24.  
  25.     BYTE Codecave[] =
  26.     {
  27.         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // + 0x00 (+ 0x00)  -> db: pFunc
  28.         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // + 0x08           -> db: pOldRet
  29.         0x48, 0x8B, 0x04, 0x24,                         // + 0x10           -> mov rax, [rsp]
  30.         0x48, 0x89, 0x05, 0xED, 0xFF, 0xFF, 0xFF,       // + 0x14           -> mov [ppOldRet], rax
  31.         0xC7, 0x04, 0x24, 0x00, 0x00, 0x00, 0x00,       // + 0x1B (+ 0x1E)  -> mov [rsp], LO_DWORD(Codecave + 0x30)
  32.         0xC7, 0x44, 0x24, 0x04, 0x00, 0x00, 0x00, 0x00, // + 0x22 (+ 0x26)  -> mov [rsp + 4], HI_DWORD(Codecave + 0x30)
  33.         0xFF, 0x25, 0xD0, 0xFF, 0xFF, 0xFF,             // + 0x2A           -> jmp [ppFunc]
  34.         0xFF, 0x25, 0xD2, 0xFF, 0xFF, 0xFF              // + 0x30           -> jmp [ppOldRet]
  35.     };
  36.  
  37.     #else
  38.  
  39.     BYTE Codecave[] =
  40.     {
  41.         0x00, 0x00, 0x00, 0x00,                     // + 0x00           -> db: pOldRet
  42.         0x8B, 0x04, 0x24,                           // + 0x04           -> mov eax, [esp]
  43.         0x89, 0x05, 0x00, 0x00, 0x00, 0x00,         // + 0x07 (+ 0x09)  -> mov [ppOldRet], eax
  44.         0xC7, 0x04, 0x24, 0x00, 0x00, 0x00, 0x00,   // + 0x0D (+ 0x10)  -> mov [esp], Codecave + 0x19
  45.         0xE9, 0x00, 0x00, 0x00, 0x00,               // + 0x14 (+ 0x15)  -> jmp Func
  46.         0xFF, 0x25, 0x00, 0x00, 0x00, 0x00          // + 0x19 (+ 0x1B)  -> jmp [ppOldRet]
  47.     };
  48.  
  49.     #endif
  50.  
  51.     DWORD dwOld;
  52.     if (!pLocation)
  53.         pLocation = reinterpret_cast<BYTE*>(VirtualAlloc(nullptr, sizeof(Codecave), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE));
  54.     else
  55.         if (!VirtualProtect(pLocation, sizeof(Codecave), PAGE_EXECUTE_READWRITE, &dwOld))
  56.             return (T)nullptr;
  57.  
  58.     if (!pLocation)
  59.         return (T)nullptr;
  60.  
  61.     #ifdef _WIN64
  62.  
  63.     DWORD dwLoFunc = (DWORD)(((UINT_PTR)pLocation + 0x30) & 0xFFFFFFFF);
  64.     DWORD dwHiFunc = (DWORD)((((UINT_PTR)pLocation + 0x30) >> 0x20) & 0xFFFFFFFF);
  65.  
  66.     *reinterpret_cast<T*>(Codecave + 0x00) = TargetFunc;
  67.     *reinterpret_cast<DWORD*>(Codecave + 0x1E) = dwLoFunc;
  68.     *reinterpret_cast<DWORD*>(Codecave + 0x26) = dwHiFunc;
  69.  
  70.     #else
  71.  
  72.     *reinterpret_cast<DWORD*>(Codecave + 0x09) = reinterpret_cast<DWORD>(pLocation);
  73.     *reinterpret_cast<DWORD*>(Codecave + 0x10) = reinterpret_cast<DWORD>(pLocation) + 0x19;
  74.     *reinterpret_cast<DWORD*>(Codecave + 0x15) = (DWORD)TargetFunc - reinterpret_cast<DWORD>(pLocation + 0x14) - 5;
  75.     *reinterpret_cast<DWORD*>(Codecave + 0x1B) = reinterpret_cast<DWORD>(pLocation);
  76.  
  77.     #endif
  78.  
  79.     memcpy(pLocation, Codecave, sizeof(Codecave));
  80.     VirtualProtect(pLocation, sizeof(Codecave), dwOld, &dwOld);
  81.  
  82.     #ifdef _WIN64
  83.     return (T)(pLocation + 0x10);
  84.     #else
  85.     return (T)(pLocation + 0x4);
  86.     #endif
  87. }
  88.  
  89. #pragma region READ
  90.  
  91. template <class T>
  92. T Read(UINT_PTR Address)
  93. {
  94.     if (Address)
  95.         return *ReCa<T*>(Address);
  96.     return 0;
  97. }
  98.  
  99. template <class T>
  100. T Read(UINT_PTR BaseAddress, UINT_PTR * Offset, UINT PointerLevel)
  101. {
  102.     return Read<T>(GetDMA(BaseAddress, Offset, PointerLevel));
  103. }
  104.  
  105. template <class T>
  106. T Read_s(UINT_PTR Address)
  107. {
  108.     if (IsValidReadPtr(ReCa<void*>(Address)))
  109.         return *ReCa<T*>(Address);
  110.     return 0;
  111. }
  112.  
  113. template <class T>
  114. T Read_s(UINT_PTR BaseAddress, UINT_PTR * Offset, UINT PointerLevel)
  115. {
  116.     return Read_s<T>(GetDMA(BaseAddress, Offset, PointerLevel));
  117. }
  118.  
  119. #pragma endregion
  120.  
  121. #pragma region WRITE
  122.  
  123. template <class T>
  124. bool Write(UINT_PTR Address, T Data)
  125. {
  126.     if (!Address)
  127.         return false;
  128.  
  129.     *ReCa<T*>(Address) = Data;
  130.     return true;
  131. }
  132.  
  133. template <class T>
  134. bool Write(UINT_PTR BaseAddress, UINT_PTR * Offset, UINT PointerLevel, T Data)
  135. {
  136.     return Write(GetDMA(BaseAddress, Offset, PointerLevel), Data);
  137. }
  138.  
  139. template <class T>
  140. bool Write_s(UINT_PTR Address, T Data)
  141. {
  142.     if (!IsValidWritePtr(ReCa<void*>(Address)))
  143.         return false;
  144.  
  145.     *ReCa<T*>(Address) = Data;
  146.     return true;
  147. }
  148.  
  149. template <class T>
  150. bool Write_s(UINT_PTR BaseAddress, UINT_PTR * Offset, UINT PointerLevel, T Data)
  151. {
  152.     return Write_s(GetDMA(BaseAddress, Offset, PointerLevel), Data);
  153. }
  154. #pragma endregion
  155.  
  156. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement