Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // patch.cpp
- #include <iostream>
- #include <windows.h>
- // The patched function with modified behavior
- int Patched_Add(int a, int b) {
- // Let's return the product of the two numbers instead of the sum
- return a * b;
- }
- int main() {
- // Target process information
- const char* targetProcessName = "victim.exe";
- // Find the target process
- HWND hWnd = FindWindowA(NULL, targetProcessName);
- if (hWnd == NULL) {
- std::cout << "Failed to find the target process." << std::endl;
- return 1;
- }
- // Get the process ID of the target process
- DWORD pid;
- GetWindowThreadProcessId(hWnd, &pid);
- // Open the target process with PROCESS_ALL_ACCESS permission
- HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
- if (hProcess == NULL) {
- std::cout << "Failed to open the target process." << std::endl;
- return 1;
- }
- // Get the address of the function to be patched
- // In reality, this step can be more complex depending on the target function's location.
- // For demonstration purposes, let's assume the address of the "Add" function is known.
- DWORD addressToPatch = reinterpret_cast<DWORD>(Add);
- // Write the patched function into the target process
- DWORD oldProtect;
- VirtualProtectEx(hProcess, reinterpret_cast<LPVOID>(addressToPatch), sizeof(int), PAGE_EXECUTE_READWRITE, &oldProtect);
- WriteProcessMemory(hProcess, reinterpret_cast<LPVOID>(addressToPatch), Patched_Add, sizeof(int), NULL);
- VirtualProtectEx(hProcess, reinterpret_cast<LPVOID>(addressToPatch), sizeof(int), oldProtect, &oldProtect);
- // Close the process handle
- CloseHandle(hProcess);
- std::cout << "Patching complete." << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement