2016-03-25 113 views
1

可能有幾個帖子以幾種方式解釋我的問題......但我一直在谷歌和stackoverflow搜索框中搜索,但我沒有找到任何東西。所以,我走了。用WriteProcessMemory和指針在另一個進程的內存中寫入

我想在一個進程內存寫的字符串,在C++中更改,但我甚至不很清楚地知道它是如何工作,所以..

我有這個指針: Image of the pointer 請,有人可以幫我在做什麼?

我已經試過了,但它不工作..

#include <windows.h> 
#include <iostream> 

int main() { 
    HWND hWnd = FindWindow(0, "WindowName"); 
    if (hWnd == 0) { 
     std::cout << "Cannot find window." << std::endl; 
    } 
    DWORD pId; 
    GetWindowThreadProcessId(hWnd, &pId); 
    HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pId); 
    DWORD baseAddress = 0x009B03D0; 
    DWORD offset = 0xA7; 
    DWORD ptrAddress; 
    char *newString = "newvalue"; 
    ReadProcessMemory(hProc, (void*)baseAddress, &ptrAddress, sizeof(DWORD), 0); 
    WriteProcessMemory(hProc, (void*)(ptrAddress + offset), newString, strlen(newString), 0); 
    std::cout << "Done. " << &ptrAddress << std::endl; 
    std::getchar(); 
} 

我應該得到的指針和jumpt到最後一個,因爲我只有一個偏移。但我沒有得到正確的..


編輯:

這裏是我的新的代碼,它的工作原理,直到WriteProcessMemory的功能。什麼可能是錯誤的?

CODE的實際工作:

int main() 
{ 
    unsigned long Pointer; /* to hold the final value */ 
    unsigned long temp;  /* hold the temp values */ 
    unsigned long address = 0x009B03D0; 
    unsigned long offset = 0xA7; 
    unsigned long newString = 0; 
    DWORD pid; 
    HWND hwnd; 
    hwnd = FindWindow(0, TEXT("NewWindow")); 
    if (!hwnd) 
    { 
     cout << "No!\n"; 
     cin.get(); 
    } 
    else 
    { 
     GetWindowThreadProcessId(hwnd, &pid); 
     HANDLE phandle = OpenProcess(PROCESS_ALL_ACCESS, 0, pid); 
     if (!phandle) 
     { 
      cout << "None!\n"; 
      cin.get(); 
     } 
     else 
     { 
      while (1) 
      { 

       ReadProcessMemory(phandle, reinterpret_cast<LPVOID>(address), &temp, sizeof(temp), 0); 
       Pointer = temp + offset; 
       //Good 
       ReadProcessMemory(phandle, reinterpret_cast<LPVOID>(Pointer), &newString, 16, 0); 
       cout << reinterpret_cast<LPVOID>(Pointer) << " en " << newString; 
       Sleep(1000); 
      } 
      return 0; 
     } 
    } 
} 

CODE不是工作:

int main() 
{ 
    unsigned int Pointer; /* to hold the final value */ 
    unsigned int temp;  /* hold the temp values */ 
    unsigned int address = 0x009B03D0; 
    unsigned int offset = 0xA7; 
    unsigned int newString = 1768060259; 
    DWORD pid; 
    HWND hwnd; 
    hwnd = FindWindow(0, TEXT("NewWindow")); 
    if (!hwnd) 
    { 
     cout << "NO\n"; 
     cin.get(); 
    } 
    else 
    { 
     GetWindowThreadProcessId(hwnd, &pid); 
     HANDLE phandle = OpenProcess(PROCESS_ALL_ACCESS, 0, pid); 
     if (!phandle) 
     { 
      cout << "NONE\n"; 
      cin.get(); 
     } 
     else 
     { 
      while (1) 
      { 

       ReadProcessMemory(phandle, reinterpret_cast<LPVOID>(address), &temp, sizeof(temp), 0); 
       Pointer = temp + offset; 
       //Good 
       if (!WriteProcessMemory(phandle, reinterpret_cast<LPVOID>(Pointer), &newString, sizeof(newString), 0)) 
        std::cerr << "Couldn't write process memory:" << GetLastError() << std::endl; 
       cout << reinterpret_cast<LPVOID>(Pointer) << " en " << newString; 
       Sleep(1000); 
      } 
      return 0; 
     } 
    } 
} 
+2

請不要發佈'void main':它是非標準的,據我所知只有Visual C++接受它,即其他編譯器*不接受它。 –

+0

我不能,因爲我什至不知道代碼xD – Onelio

+0

所以,什麼是錯的。你沒有檢查錯誤。也許其中一個API調用失敗。你期望發生什麼?你是如何確認它沒有發生的?發生了什麼?你沒有寫空終止符。 –

回答

1

每個進程都有自己的內存和地址空間。所以ReadProcessMemory()和WriteProcessMemory()使用中間緩衝區來完成他們訪問另一個進程的內存的工作。

不幸的是,您的通話ReadProcessMemory()問題:

  • 你不初始化ptrAddress指向一個緩衝區
  • 你傳遞的ptrAddress地址,而不是它的值應該指向有效緩衝區
  • 您傳遞0(即nullptr)而不是傳遞應該包含可以讀取的字節數的zie變量的地址。

另請注意,您使用DWORDLPCVOID管理目標進程中的地址。第一個總是32位,而後者取決於您的編譯選項(32位代碼或64位代碼)。

如果發生故障,您還應該驗證error code。幾乎可以肯定,特殊的特權需要在不同的流程中進行讀寫。

這裏有一個調整後的代碼,帶有一些診斷信息以幫助您進一步。

HWND hWnd = FindWindow(0, TEXT("WindowName")); 
if (hWnd == 0) { 
    std::cerr << "Cannot find window." << std::endl; 
} 
else { 
    DWORD pId; 
    GetWindowThreadProcessId(hWnd, &pId); 
    HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pId); 
    if (hProc) { 
     char *newString = "newvalue"; 
     size_t sz = strlen(newString) + 1; 
     LPVOID baseAddress = (LPVOID)0x009B03D0; 
     DWORD offset = 0xA7; 
     LPVOID ptrAddress = new char[sz]; 
     SIZE_T bytes_read = 0, bytes_written=0; 
     if (ReadProcessMemory(hProc, baseAddress, ptrAddress, sz, &bytes_read) || GetLastError()== ERROR_PARTIAL_COPY) { 
      if (bytes_read == 0) 
       std::cerr << "Houston, we have a problem..." << std::endl; 
      if(!WriteProcessMemory(hProc, baseAddress, (LPCVOID)newString, sz, &bytes_written)) 
       std::cerr << "Couldn't write process memory:" << GetLastError() << std::endl; 
      std::cout << "Done. " << bytes_read <<" bytes read and "<<bytes_written<<" bytes written"<< std::endl; 
     } 
     else { 
      std::cerr<< "Couldn't read process memory:" << GetLastError() << std::endl; 
     } 
     delete[] ptrAddress; 
    } 
    else { 
     std::cerr << "Couldn't open process " << pId << ": " << GetLastError() << std::endl; 
    } 
} 
std::getchar(); 
+0

你忘了「)」in(「WindowName」)); XD(讓我試試) – Onelio

+0

作爲管理員執行我得到這個:https://i.gyazo.com/b2ec7c5fb53e6c410df801ffb51db94e.png 但我注意到在CE中更改了一些東西,因爲它停止顯示值(但它仍然在Memory區域) – Onelio

+0

@James對不起,我剛剛意識到寫入條件中缺少'!',導致它顯示寫入錯誤消息,儘管一切正常。我編輯了答案。 – Christophe

相關問題