1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
| #include "WindowUtils.h" #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <tlhelp32.h> #include <iostream>
ProcessManager::ProcessManager(const std::string& title, const std::string& className) : _windowTitle(title), _className(className) { }
ProcessManager::~ProcessManager() { if (_mainThreadHandle) CloseHandle(_mainThreadHandle); if (_processHandle) CloseHandle(_processHandle); } bool ProcessManager::IsTargetValid() {
if (_processHandle) { DWORD exitCode = 0; GetExitCodeProcess((HANDLE)_processHandle, &exitCode); if (exitCode == STILL_ACTIVE) return true;
_cachedOffset = 0; CloseHandle((HANDLE)_processHandle); if (_mainThreadHandle) CloseHandle((HANDLE)_mainThreadHandle); _processHandle = nullptr; _mainThreadHandle = nullptr; _pid = 0; }
HWND hwnd = FindWindowA(_className.empty() ? NULL : _className.c_str(), _windowTitle.c_str()); if (!hwnd) return false;
DWORD tid = GetWindowThreadProcessId(hwnd, (LPDWORD)&_pid);
_processHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, _pid);
if (tid != 0) { _mainThreadHandle = OpenThread(THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME, FALSE, tid); }
return (_processHandle != nullptr && _mainThreadHandle != nullptr); }
HANDLE _mainThreadHandle = nullptr;
void ProcessManager::UpdateThreadHandle(DWORD tid) { if (_mainThreadHandle) CloseHandle(_mainThreadHandle);
_mainThreadHandle = OpenThread(THREAD_GET_CONTEXT | THREAD_SUSPEND_RESUME, FALSE, tid); }
ClientAreaInfo ProcessManager::GetClientInfo() { ClientAreaInfo info = { 0, 0, 0, 0, false }; HWND hwnd = FindWindowA(_className.empty() ? NULL : _className.c_str(), _windowTitle.c_str());
if (hwnd && IsWindow(hwnd)) { info.isVisible = (GetForegroundWindow() == hwnd);
RECT cRect; GetClientRect(hwnd, &cRect); info.width = cRect.right - cRect.left; info.height = cRect.bottom - cRect.top;
POINT pt = { 0, 0 }; ClientToScreen(hwnd, &pt); info.x = pt.x; info.y = pt.y; } return info; }
void ProcessManager::SyncOverlayPosition(void* overlayHandle) { HWND myHwnd = (HWND)overlayHandle; ClientAreaInfo info = GetClientInfo();
if (info.width > 0 && info.isVisible) { if (!IsWindowVisible(myHwnd)) ShowWindow(myHwnd, SW_SHOWNOACTIVATE);
static int lastX = 0, lastY = 0; if (info.x != lastX || info.y != lastY) { SetWindowPos(myHwnd, HWND_TOPMOST, info.x, info.y, info.width, info.height, SWP_NOACTIVATE); lastX = info.x; lastY = info.y; } } else { if (IsWindowVisible(myHwnd)) ShowWindow(myHwnd, SW_HIDE); } }
bool ProcessManager::ReadMemoryInternal(uintptr_t address, void* buffer, size_t size) { if (!_processHandle) return false; SIZE_T read; return ReadProcessMemory((HANDLE)_processHandle, (LPCVOID)address, buffer, size, &read) && (read == size); }
uintptr_t ProcessManager::GetModuleBase(const std::wstring& moduleName) { uintptr_t dwModuleBaseAddress = 0; HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, _pid); if (hSnapshot != INVALID_HANDLE_VALUE) { MODULEENTRY32 ModuleEntry32; ModuleEntry32.dwSize = sizeof(MODULEENTRY32W); if (Module32FirstW(hSnapshot, &ModuleEntry32)) { do { if (moduleName == ModuleEntry32.szModule) { dwModuleBaseAddress = (uintptr_t)ModuleEntry32.modBaseAddr; break; } } while (Module32NextW(hSnapshot, &ModuleEntry32)); } CloseHandle(hSnapshot); } return dwModuleBaseAddress; }
uintptr_t ProcessManager::GetModuleBase(const std::string& moduleName) { uintptr_t dwModuleBaseAddress = 0; HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, _pid); if (hSnapshot != INVALID_HANDLE_VALUE) { MODULEENTRY32W ModuleEntry32; ModuleEntry32.dwSize = sizeof(MODULEENTRY32W);
std::wstring wModuleName(moduleName.begin(), moduleName.end());
if (Module32FirstW(hSnapshot, &ModuleEntry32)) { do { if (wModuleName == ModuleEntry32.szModule) { dwModuleBaseAddress = (uintptr_t)ModuleEntry32.modBaseAddr; break; } } while (Module32NextW(hSnapshot, &ModuleEntry32)); } CloseHandle(hSnapshot); } return dwModuleBaseAddress; }
uintptr_t ProcessManager::GetThreadStackBase() { if (!_mainThreadHandle) return 0;
alignas(16) CONTEXT ctx = { 0 }; ctx.ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER;
if (SuspendThread((HANDLE)_mainThreadHandle) != (DWORD)-1) { if (GetThreadContext((HANDLE)_mainThreadHandle, &ctx)) { ResumeThread((HANDLE)_mainThreadHandle); return ctx.Rbp; } ResumeThread((HANDLE)_mainThreadHandle); } return 0; }
uintptr_t ProcessManager::FindPattern(const char* pattern, const std::string& moduleName) { uintptr_t base = GetModuleBase(moduleName); std::cout << "[Debug] Searching " << moduleName << " at Base: " << std::hex << base << std::endl; if (!base) { std::cout << "[Error] Cannot find Base for: " << moduleName << std::endl; return 0; }
size_t searchRange = 0x6400000; std::vector<uint8_t> moduleMemory(searchRange); SIZE_T bytesRead = 0;
if (!ReadProcessMemory((HANDLE)_processHandle, (LPCVOID)base, moduleMemory.data(), searchRange, &bytesRead)) { std::cout << "[Debug] RPM Failed, Error: " << GetLastError() << " BytesRead: " << bytesRead << std::endl; }
std::vector<int> patternBytes; std::string p = pattern; for (size_t i = 0; i < p.length(); i++) { if (p[i] == ' ') continue; if (p[i] == '?') { patternBytes.push_back(-1); if (p[i + 1] == '?') i++; } else { patternBytes.push_back(std::stoi(p.substr(i, 2), nullptr, 16)); i++; } }
for (size_t i = 0; i < bytesRead - patternBytes.size(); i++) { bool found = true; for (size_t j = 0; j < patternBytes.size(); j++) { if (patternBytes[j] != -1 && moduleMemory[i + j] != patternBytes[j]) { found = false; break; } } if (found) return base + i; } return 0; }
uint8_t ProcessManager::ExtractStackOffset() { uintptr_t instructionAddr = FindPattern("F3 0F 10 45 ??", "UseRay.exe");
if (instructionAddr != 0) { uint8_t offset = 0; ReadProcessMemory((HANDLE)_processHandle, (LPCVOID)(instructionAddr + 4), &offset, 1, NULL); std::cout << "[Success] Found Pattern at: " << std::hex << instructionAddr << " Offset: " << (int)offset << std::endl; return offset; } std::cout << "[Error] Pattern not found!" << std::endl; return 0; }
uintptr_t ProcessManager::GetThreadStackBaseWithCache(int intervalMs) { DWORD currentTick = GetTickCount();
if (_cachedRbp == 0 || (currentTick - _lastRbpTick) > (DWORD)intervalMs) { _cachedRbp = this->GetThreadStackBase(); _lastRbpTick = currentTick; }
return _cachedRbp; }
bool ProcessManager::SetThreadRbp(uintptr_t newValue) { if (!_mainThreadHandle || _mainThreadHandle == INVALID_HANDLE_VALUE) return false;
if (SuspendThread(_mainThreadHandle) == (DWORD)-1) return false;
alignas(16) CONTEXT ctx = { 0 }; ctx.ContextFlags = CONTEXT_FULL;
bool success = false; if (GetThreadContext(_mainThreadHandle, &ctx)) { ctx.Rbp = (DWORD64)newValue;
if (SetThreadContext(_mainThreadHandle, &ctx)) { success = true; } }
ResumeThread(_mainThreadHandle); return success; }
|