Advertisement
R3v3rs3r

Get base address easily

Jul 24th, 2023
623
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ (WinAPI) 1.90 KB | Source Code | 0 0
  1. #include <Windows.h>
  2. #include <iostream>
  3.  
  4. int main() {
  5.     const char* processPath = "C:\\Path\\To\\Your\\Process.exe";
  6.     STARTUPINFOA startupInfo;
  7.     PROCESS_INFORMATION processInfo;
  8.  
  9.     ZeroMemory(&startupInfo, sizeof(startupInfo));
  10.     startupInfo.cb = sizeof(startupInfo);
  11.     ZeroMemory(&processInfo, sizeof(processInfo));
  12.  
  13.     // Create the suspended process
  14.     if (!CreateProcessA(
  15.             NULL,
  16.             (LPSTR)processPath,
  17.             NULL,
  18.             NULL,
  19.             FALSE,
  20.             CREATE_SUSPENDED,
  21.             NULL,
  22.             NULL,
  23.             &startupInfo,
  24.             &processInfo
  25.         ))
  26.     {
  27.         std::cerr << "Failed to create the process: " << GetLastError() << std::endl;
  28.         return 1;
  29.     }
  30.  
  31.     // Process handle
  32.     HANDLE hProcess = processInfo.hProcess;
  33.  
  34.     // Get the base address of the main module (executable) of the process
  35.     HMODULE hModule = NULL;
  36.     if (!EnumProcessModules(hProcess, &hModule, sizeof(hModule), NULL)) {
  37.         std::cerr << "Failed to get the process module: " << GetLastError() << std::endl;
  38.         return 1;
  39.     }
  40.  
  41.     // Calculate the memory address to modify (example: modify the first integer in the process's memory)
  42.     uintptr_t baseAddress = (uintptr_t)hModule;
  43.     int newValue = 999;
  44.  
  45.     // Write the new value to the process's memory
  46.     if (!WriteProcessMemory(hProcess, (LPVOID)baseAddress, &newValue, sizeof(newValue), NULL)) {
  47.         std::cerr << "Failed to write to process memory: " << GetLastError() << std::endl;
  48.         return 1;
  49.     }
  50.  
  51.     // Resume the process and allow it to run
  52.     if (ResumeThread(processInfo.hThread) == -1) {
  53.         std::cerr << "Failed to resume the process: " << GetLastError() << std::endl;
  54.         return 1;
  55.     }
  56.  
  57.     // Close process and thread handles
  58.     CloseHandle(processInfo.hThread);
  59.     CloseHandle(processInfo.hProcess);
  60.  
  61.     return 0;
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement