gpt4 book ai didi

c++ - 函数 ReadProcessMemory 不断返回 ERROR_PARTIAL_COPY

转载 作者:行者123 更新时间:2023-11-27 22:54:16 31 4
gpt4 key购买 nike

我知道还有其他人问过这个问题,但似乎没有人得出令人满意或可以理解的结论。我无法使用未回答的内容。我不太确定问题出在哪里,我尝试了各种不同的解决方案但都没有成功,所以这是我的代码:

#include <windows.h>
#include <iostream>
using namespace std;

int main()
{
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS | PROCESS_QUERY_INFORMATION, FALSE, (DWORD)7312);

if(hProc == NULL)
{
cout << "Error: " << GetLastError() << endl;
}

HANDLE token;

OpenProcessToken(hProc, TOKEN_ALL_ACCESS, &token);

void *baseAddr = VirtualAllocEx(hProc, NULL, 500, MEM_RESERVE, PAGE_EXECUTE_READWRITE);

if(baseAddr == NULL)
{
cout << "VirtualAllocEx has failed" << endl;
}
else
{
cout << "Base Address: " << baseAddr << "\n" << endl;
}

DWORD prevProt;

if(VirtualProtectEx(hProc, &baseAddr, sizeof(DWORD), PAGE_EXECUTE_READWRITE, &prevProt) == 0)
{
if(GetLastError() == 87)
{
cout << "ERROR_INVALID_PARAMETER\n" << endl;
}
else if(GetLastError() == 487)
{
cout << "ERROR_INVALID_ADDRESS\n" << endl;
}
}

void *buffer;

if(ReadProcessMemory(hProc, baseAddr, &buffer, sizeof(SIZE_T), NULL) == 0)
{
if(GetLastError() == 299)
{
cout << "ERROR_PARTIAL_COPY" << endl;
}
}
}

非常感谢您提供的任何贡献和知识! :)

最佳答案

我发现您的代码存在一些问题。

  1. 糟糕的错误处理。如果发生错误,您将其记录下来,但继续处理错误数据。如果发生错误,请停止。而且您滥用了 GetLastError()

  2. 您向 VirtualProtectEx() 传递了错误的基地址。 &baseAddr 需要改为 baseAddr。此外,您正在使用 EXECUTE 权限分配和保护内存,除非您打算将可执行代码存储在内存中(此代码未执行此操作),否则不应使用该权限。

  3. 您正在使用 sizeof(DWORD) 在远程内存上设置保护标志,但您正在使用 sizeof(SIZE_T) 来读取内存。 DWORD 是固定的 32 位大小,但 SIZE_T 是 32 位或 64 位,具体取决于您要编译的平台。将 SIZE_T 更改为 DWORD 以匹配其余代码。

  4. 您没有在调用进程中为 ReadProcessMemory() 分配任何内存以写入。将 void *buffer; 更改为 DWORD buffer;

试试这个:

#include <windows.h>
#include <iostream>
using namespace std;

int main()
{
DWORD dwError;

HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS | PROCESS_QUERY_INFORMATION, FALSE, (DWORD)7312);
if (hProc == NULL)
{
dwError = GetLastError();
cout << "OpenProcess has failed. Error: " << dwError << endl;
return 0;
}

HANDLE token;
if (!OpenProcessToken(hProc, TOKEN_ALL_ACCESS, &token))
{
dwError = GetLastError();
cout << "OpenProcessToken has failed. Error: " << dwError << endl;
return 0;
}

void *baseAddr = VirtualAllocEx(hProc, NULL, 500, MEM_RESERVE, PAGE_READWRITE);
if (baseAddr == NULL)
{
dwError = GetLastError();
cout << "VirtualAllocEx has failed. Error: " << dwError << endl;
return 0;
}

cout << "Base Address: " << baseAddr << endl;

DWORD prevProt;
if (!VirtualProtectEx(hProc, baseAddr, sizeof(DWORD), PAGE_READWRITE, &prevProt))
{
dwError = GetLastError();
cout << "VirtualAllocEx has failed. Error: ";
if (dwError == ERROR_INVALID_PARAMETER)
{
cout << "ERROR_INVALID_PARAMETER";
}
else if (dwError == ERROR_INVALID_ADDRESS)
{
cout << "ERROR_INVALID_ADDRESS";
}
else
{
cout << dwError;
}
cout << endl;
return 0;
}

DWORD buffer;
if (ReadProcessMemory(hProc, baseAddr, &buffer, sizeof(DWORD), NULL))
{
dwError = GetLastError();
cout << "ReadProcessMemory has failed. Error: ";
if (dwError == ERROR_PARTIAL_COPY)
{
cout << "ERROR_PARTIAL_COPY";
}
else
{
cout << dwError;
}
cout << endl;
return 0;
}

cout << "Value: " << buffer << endl;
return 0;
}

还有一些问题:

  1. 您在远程进程中保留内存,但您没有提交该内存的物理存储,并且您之前没有向内存写入任何内容从中读取。读取保留的未提交内存不是很有用,并且可能是导致错误的罪魁祸首:

    https://stackoverflow.com/a/4457745/65863

    ReadProcessMemory would return FALSE and GetLastError would return ERROR_PARTIAL_COPY when the copy hits a page fault.

    Working Set

    When a process references pageable memory that is not currently in its working set, a page fault occurs.

  2. 您没有使用 OpenProcessToken() 返回的 token ,因此调用没有用。

  3. 您正在通过 VirtualProtectEx() 使用您在分配内存时指定的相同保护标志来保护远程内存。所以这个调用也是没用的。

关于c++ - 函数 ReadProcessMemory 不断返回 ERROR_PARTIAL_COPY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34648061/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com