0

我正在尝试解决我不熟悉的代码中的问题。我已经追踪到调用 WriteProcessMemory 总是以ERROR_INVALID_ADDRESS. 我不知道它为什么会失败。我试图检查我的进程是否具有写入其子进程所需的访问权限,VirtualQUery并且确实如此。任何人都可以对此有所了解吗?代码路径非常复杂,所以我跳过了很多。如果遗漏任何信息,请告诉我。

CreateProcessAsUserW(hToken, exe, cmd_line, 
NULL, // No security attribute.
NULL, // No thread attribute.
false, // do not inherit handles
CREATE_SUSPENDED | CREATE_UNICODE_ENVIRONMENT | DETACHED_PROCESS | EXTENDED_STARTUPINFO_PRESENT | CREATE_BREAKAWAY_FROM_JOB, // start suspended, extended startup info, break out of job
NULL, // Use the environment of the caller
NULL, // Use current directory of the caller.
&si, 
&pi);

/*
 ....lots of work here
*/

void* address = {...};
void* var = address;   // note this line

SIZE_T written;
if(!WriteProcessMemory( pi.handle, 
var, address,  // not completely sure what it is doing here - writing contents of address to address of var?
size, &written))
{
    DWORD error = GetLastError();     // ERROR_INVALID_ADDRESS
    MEMORY_BASIC_INFORMATION buffer;
    SIZE_T num = VirtualQuery(address,&buffer,sizeof(MEMORY_BASIC_INFORMATION));
    if(num > 0)
    {
        DWORD access = buffer.AllocationProtect;  // PAGE_EXECUTE_WRITECOPY
        DWORD state = buffer.State; // MEM_COMMIT
        DWORD type = buffer.Type;
    }
}

这是一个在 64 位 Win7 上运行的 32 位进程。

4

1 回答 1

0

在尝试写入另一个进程之前,您正在执行本地 VirtualQuery,其地址空间可能大不相同。

如果您想确保在该进程的地址空间中有一个有效的指针,您可以找到该进程将您感兴趣的内容放在哪里(祝您好运,ASLR),或者您在该进程中为您分配一些内存(例如 VirtualAllocEx( ))。

注意:如果你真的想要共享内存,你应该使用 CreateFileMapping(INVALID_HANDLE_VALUE) 代替。

于 2013-11-08T13:12:26.847 回答