Advertisement
Wolfrost

MemoryManager.h | Read/Write only

Mar 8th, 2016
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// MemoryManager.h
  2. // All-inclusive packet to read-write memory easily.
  3. // Use with caution
  4.  
  5. /// CODED BY DOUBLE V
  6. /// Credits: TeamGamerFood
  7.  
  8. /// Oh, I forgot, this is not documented because I'm a lazy ass.
  9. /// But you should understand how to use it...
  10.  
  11. #pragma once
  12.  
  13. #include <Windows.h>
  14. #include <TlHelp32.h>
  15.  
  16. class MemoryManager
  17. {
  18. private:
  19.     DWORD dwPID;
  20.     HANDLE hProcess;
  21. public:
  22.     MODULEENTRY32 Client, Engine;
  23.     DWORD ClientBase, EngineBase;
  24.  
  25.     bool AttachProcess(char* ProcessName)
  26.     {
  27.         HANDLE hPID = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
  28.         if (hPID == INVALID_HANDLE_VALUE) return false;
  29.         PROCESSENTRY32 procEntry;
  30.         procEntry.dwSize = sizeof(procEntry);
  31.  
  32.         const WCHAR* procNameChar;
  33.         int nChars = MultiByteToWideChar(CP_ACP, 0, ProcessName, -1, NULL, 0);
  34.         procNameChar = new WCHAR[nChars];
  35.         MultiByteToWideChar(CP_ACP, 0, ProcessName, -1, (LPWSTR)procNameChar, nChars);
  36.  
  37.         do
  38.             if (!wcscmp(procEntry.szExeFile, procNameChar))
  39.             {
  40.                 this->dwPID = procEntry.th32ProcessID;
  41.                 CloseHandle(hPID);
  42.                 this->hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, this->dwPID);
  43.                 return true;
  44.             }
  45.         while (Process32Next(hPID, &procEntry));
  46.  
  47.         CloseHandle(hPID);
  48.         return false;
  49.     }
  50.     MODULEENTRY32 GetModule(char* ModuleName)
  51.     {
  52.         HANDLE hModule = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID);
  53.         MODULEENTRY32 mEntry;
  54.         if (hModule == INVALID_HANDLE_VALUE)
  55.         {
  56.             mEntry.modBaseAddr = 0x0;
  57.             return mEntry;
  58.         }
  59.         mEntry.dwSize = sizeof(mEntry);
  60.  
  61.         const WCHAR *modNameChar;
  62.         int nChars = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
  63.         modNameChar = new WCHAR[nChars];
  64.         MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, (LPWSTR)modNameChar, nChars);
  65.  
  66.         do
  67.             if (!wcscmp(mEntry.szModule, modNameChar))
  68.             {
  69.                 CloseHandle(hModule);
  70.                 return mEntry;
  71.             }
  72.         while (Module32Next(hModule, &mEntry));
  73.  
  74.         CloseHandle(hModule);
  75.         mEntry.modBaseAddr = 0x0;
  76.         return mEntry;
  77.     }
  78.  
  79.     bool Init()
  80.     {
  81.         dwPID = NULL;
  82.         hProcess = NULL;
  83.         if (!AttachProcess("csgo.exe")) return false;
  84.         Client = GetModule("client.dll");
  85.         Engine = GetModule("engine.dll");
  86.         ClientBase = (DWORD)Client.modBaseAddr;
  87.         EngineBase = (DWORD)Engine.modBaseAddr;
  88.         if (ClientBase == 0x0 || EngineBase == 0x0) return false;
  89.     }
  90.     bool Clear()
  91.     {
  92.         CloseHandle(hProcess);
  93.     }
  94.     MemoryManager()
  95.     {
  96.         Init();
  97.     }
  98.     ~MemoryManager()
  99.     {
  100.         Clear();
  101.     }
  102.  
  103.     template <class T>
  104.     T Read(DWORD dwAddress)
  105.     {
  106.         T value;
  107.         if (!ReadProcessMemory(hProcess, (LPVOID)dwAddress, &value, sizeof(T), NULL)) value = NULL;
  108.         return value;
  109.     }
  110.  
  111.     template <class T>
  112.     T ReadSize(DWORD dwAddress, size_t Size)
  113.     {
  114.         T value;
  115.         if (!ReadProcessMemory(hProcess, (LPVOID)dwAddress, &value, Size, NULL)) value = NULL;
  116.         return value;
  117.     }
  118.  
  119.     template <class T>
  120.     T* ReadArray(DWORD dwAddress, size_t ArraySize)
  121.     {
  122.         T* arr;
  123.         if (!ReadProcessMemory(hProcess, (LPVOID)dwAddress, arr, sizeof(T)*ArraySize, NULL)) arr = nullptr;
  124.         return arr;
  125.     }
  126.  
  127.     template <class T>
  128.     bool Write(DWORD dwAddress, T ValueToWrite)
  129.     {
  130.         return WriteProcessMemory(hProcess, (LPVOID)dwAddress, &ValueToWrite, sizeof(T), NULL) == TRUE;
  131.     }
  132.  
  133.     template <class T>
  134.     bool WriteSize(DWORD dwAddress, T ValueToWrite, size_t Size)
  135.     {
  136.         return WriteProcessMemory(hProcess, (LPVOID)dwAddress, &ValueToWrite, Size, NULL) == TRUE;
  137.     }
  138.  
  139.     template <class T>
  140.     bool WriteArray(DWORD dwAddress, T* ArrayToWrite, size_t ArraySize)
  141.     {
  142.         return WriteProcessMemory(hProcess, (LPVOID)dwAddress, ArrayToWrite, sizeof(T)*ArraySize, NULL) == TRUE;
  143.     }
  144.  
  145.     template <class T>
  146.     bool WriteProtected(DWORD dwAddress, T ValueToWrite)
  147.     {
  148.         DWORD_PTR oldprotect;
  149.         if (!VirtualProtectEx(hProcess, (LPVOID)dwAddress, sizeof(T), PAGE_EXECUTE_READWRITE, &oldprotect)) return false;
  150.         if (!Write(dwAddress, ValueToWrite)) return false;
  151.         if (!VirtualProtectEx(hProcess, (LPVOID)dwAddress, sizeof(T), oldprotect, NULL)) return false;
  152.         return true;
  153.     }
  154.  
  155.     template <class T>
  156.     bool WriteProtectedSize(DWORD dwAddress, T ValueToWrite, size_t Size)
  157.     {
  158.         DWORD_PTR oldprotect;
  159.         if (!VirtualProtectEx(hProcess, (LPVOID)dwAddress, sizeof(T), PAGE_EXECUTE_READWRITE, &oldprotect)) return false;
  160.         if (!WriteSize(dwAddress, ValueToWrite, Size)) return false;
  161.         if (!VirtualProtectEx(hProcess, (LPVOID)dwAddress, sizeof(T), oldprotect, NULL)) return false;
  162.         return true;
  163.     }
  164.  
  165.     template <class T>
  166.     bool WriteArrayProtected(DWORD dwAddress, T* ArrayToWrite, size_t ArraySize)
  167.     {
  168.         DWORD_PTR oldprotect;
  169.         if (!VirtualProtectEx(hProcess, (LPVOID)dwAddress, sizeof(T), PAGE_EXECUTE_READWRITE, &oldprotect)) return false;
  170.         if (!WriteArray(dwAddress, ArrayToWrite, ArraySize)) return false;
  171.         if (!VirtualProtectEx(hProcess, (LPVOID)dwAddress, sizeof(T), oldprotect, NULL)) return false;
  172.         return true;
  173.     }
  174.  
  175.     DWORD GetProcID() { return dwPID; }
  176.     HANDLE GetProcHandle() { return hProcess; }
  177.  
  178. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement