[英]C++ - ReadProcessMemory to buffer, WriteProcessMemory (with new value on same buffer) reverts buffer to old value
我不知道為什么會這樣,而且我很好奇。 我設法不使用WriteProcessMemory上的緩沖區成功寫入/讀取,但是我想知道為什么會這樣。
這就是它的工作方式。 你拋出測試
target.cpp(輸出變量地址以進行讀取/寫入的進程)
#include <iostream>
#include <string>
using namespace std;
int main(){
int test = 5;
string pero = "hola";
cout << sizeof(test);
cout<<"Address of test is :" <<&test <<endl; //Address to put on Principal.cpp
cin.get(); //Wait that other process writes then I manually continue.
cout << "Value of test is: " << test<<endl; //Outputs 5, should output 20.
cin.get();
return 0;
}
Principal.cpp(在地址上讀寫)。 從主開始。
#include <windows.h>
#include <tlhelp32.h>
#include <iostream> // For STL i/o
#include <ctime> // For std::chrono
#include <thread> // For std::this_thread
using namespace std;
DWORD FindProcessId(const char *name)
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE)
return 0;
PROCESSENTRY32 ProcEntry;
ProcEntry.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hSnapshot, &ProcEntry))
{
{
if (stricmp(ProcEntry.szExeFile, name) == 0)
return ProcEntry.th32ProcessID;
while (Process32Next(hSnapshot, &ProcEntry))
if (stricmp(ProcEntry.szExeFile, name) == 0)
{
return ProcEntry.th32ProcessID;
}
}
}
}
// IMPORTANT PART STARTS HERE.
int main()
{
int buffer;
DWORD Address = 0x28ff28; //Address of int test.
DWORD ProcessId = FindProcessId("test.exe");
if (ProcessId == 0)
cout << "Doesn't exist.";
else
cout << "Process Id is : " << ProcessId << endl;
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, ProcessId);
if (hProcess == INVALID_HANDLE_VALUE)
{
cout << "Error in HANDLE";
}
if (ReadProcessMemory(hProcess, (LPCVOID)Address, (LPVOID)&buffer, sizeof(buffer), 0) == 0)
{
DWORD error = GetLastError();
cout << "Error is " << error << endl;
}
cout << "Buffer is: " << buffer << endl; //Outputs 5
buffer = 20; // Want to write 20 and then read variable on test.exe.
if (WriteProcessMemory(hProcess, (LPVOID)Address, (LPCVOID)&buffer, sizeof(buffer), 0) == 0)
{
DWORD error = GetLastError();
cout << "Error is " << error << endl;
}
cout << "Buffer is: " << buffer << endl; //Outputs 5, should output 20.
CloseHandle(hProcess);
return 0;
}
這里發生什么。
如果有人可以向我解釋,我將不勝感激!
一些東西。
DWORD Address = 0x28ff28;
您如何獲得此信息,是從上面程序告訴您的地址中輸入的內容嗎?
如果要從上述地址獲取地址,則需要找到程序的入口點 ,並在該數字后添加0x28ff28 。
編輯:如果您使用內存查看器,您可以看到程序吐出的地址沒有指向該int,我所說的偏移量也不是。 您需要找出相對於程序開始路徑的存儲位置。 我以為您可以使用上面程序發出的數字是錯誤的。
窗口的設計方式是,出於安全原因,它將在不同的地址處隨機啟動程序,並且您看到的地址與程序開始處的地址有所偏移。
您應該能夠從nModule.modBaseAddr中找到基地址,但是如果您正在以64位運行程序,則可能需要將其轉換為uint64_t 。
我希望這有幫助。
編輯:您也可以執行此操作,而不是遍歷快照
hWnd_ = FindWindow(nullptr, processName.c_str());
GetWindowThreadProcessId(hWnd_, &pid_);
還請確保您以管理員身份運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.