Advertisement
CasualGamer

Helper.cpp

Oct 23rd, 2019 (edited)
4,911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. #include "Helper.h"
  2.  
  3. Helper::Helper()
  4. {
  5.     pID = NULL;
  6.     processHandle = NULL;
  7. }
  8.  
  9. Helper::Helper(DWORD pID) {
  10.     this->pID = pID;
  11.     HANDLE processHandle = NULL;
  12.     processHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pID);
  13.     if (processHandle == INVALID_HANDLE_VALUE || processHandle == NULL) {
  14.         std::cerr << "Failed to open process -- invalid handle" << std::endl;
  15.         std::cerr << "Error code: " << GetLastError() << std::endl;
  16.         throw "Failed to open process";
  17.     }
  18.     else {
  19.         //std::cout << "Helper:: process handle sucessfully created!" << std::endl;
  20.         this->processHandle = processHandle;
  21.     }
  22. }
  23.  
  24.  
  25. Helper::~Helper()
  26. {
  27.     CloseHandle(this->processHandle);
  28. }
  29.  
  30. void Helper::SetpID(DWORD pID) {this->pID = pID;}
  31. DWORD Helper::GetpID(){ return this->pID; }
  32. HANDLE Helper::GetprocessHandle() { return this->processHandle; }
  33.  
  34. uintptr_t Helper::GetModuleBaseAddress(TCHAR* lpszModuleName) {
  35.     uintptr_t dwModuleBaseAddress = 0;
  36.     HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pID);
  37.     MODULEENTRY32 ModuleEntry32 = { 0 };
  38.     ModuleEntry32.dwSize = sizeof(MODULEENTRY32);
  39.  
  40.     if (Module32First(hSnapshot, &ModuleEntry32))
  41.     {
  42.         do {
  43.             if (_tcscmp(ModuleEntry32.szModule, lpszModuleName) == 0)
  44.             {
  45.                 dwModuleBaseAddress = (uintptr_t )ModuleEntry32.modBaseAddr;
  46.                 break;
  47.             }
  48.         } while (Module32Next(hSnapshot, &ModuleEntry32));
  49.  
  50.  
  51.     }
  52.     CloseHandle(hSnapshot);
  53.     return dwModuleBaseAddress;
  54. }
  55.  
  56. uintptr_t Helper::GetDynamicAddress(uintptr_t baseAddress, vector<DWORD> offsets) {
  57.     uintptr_t dynamicAddress = baseAddress;
  58.     for (int i = 0; i < offsets.size() - 1; i++)
  59.     {
  60.         ReadProcessMemory(this->processHandle, (LPCVOID)(dynamicAddress + offsets[i]), &dynamicAddress, sizeof(offsets.at(i)), NULL);
  61.         //std::cout << "Current Adress: " << std::hex << healthAddress << std::endl;
  62.     }
  63.     dynamicAddress += offsets[offsets.size() - 1];
  64.     return dynamicAddress;
  65. }
  66.  
  67. void Helper::SetpBaseAddress(char moduleName[]) {
  68.     this->pBaseAddress = this->GetModuleBaseAddress(_T(moduleName));
  69. }
  70.  
  71. uintptr_t Helper::GetAddressFromSignature(vector<int> signature) {
  72.     if (this->pBaseAddress == NULL || this->processHandle == NULL) {
  73.         return NULL;
  74.     }
  75.     std::vector<byte> memBuffer(this->pSize);
  76.     if (!ReadProcessMemory(this->processHandle, (LPCVOID)(this->pBaseAddress), memBuffer.data(), this->pSize, NULL)) {
  77.         std::cout << GetLastError() << std::endl;
  78.         return NULL;
  79.     }
  80.     for (int i = 0; i < this->pSize; i++){
  81.         for (uintptr_t j = 0; j < signature.size();j++) {
  82.             if (signature.at(j) != -1 && signature[j] != memBuffer[i + j])
  83.                 //std::cout << std::hex << signature.at(j) << std::hex << memBuffer[i + j] << std::endl;
  84.                 break;
  85.             if(signature[j] == memBuffer[i + j] && j>0)
  86.                 std::cout << std::hex << int(signature[j]) << std::hex << int(memBuffer[i + j]) << j <<std::endl;
  87.             if(j+1 == signature.size())
  88.                 return this->pBaseAddress + i;
  89.         }
  90.     }
  91.     return NULL;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement