Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WriteProcessMemory ERROR_PARTIAL_COPY 299

I am trying to write to the memory of another process, I open it with permission 38h (VM_OPERATION, VM_READ, VM_WRITE), then i use VirtualProtectEx with permission 4h(PAGE_READWRITE), but i also tried PAGE_EXECUTEREADWRITE - same error later.

Then I invoke ReadProcessMemory, and successfully read out the value of a fix address. But as I try to write to that address with WriteProcessMemory i get the Errorcode 299 - ERROR_PARTIAL_COPY.

Does anyone know how to solve this?

EDIT: SOLVED - didnt pass the buffer to write as a reference but as a value

like image 840
0x90 Avatar asked Nov 15 '22 07:11

0x90


1 Answers

WriteProcessMemory is giving the error ERROR_PARTIAL_COPY 299 because the third argument, lpBuffer, needs to be a pointer. Specifically a pointer to a local buffer that contains the data you intend to write to the target process. If for instance, the buffer is let's say an integer like:

int x = 5;

Then you would use &x as the lpBuffer argument. & is the "address of` operator that returns a pointer to the variable.

Normally if you don't pass a pointer for this argument you would get a compiler error. Regardless, ERROR_PARTIAL_COPY means not all the bytes from the source buffer were written to the target process. This could happen for several reasons, especially if the lpBuffer argument did not point to a committed memory address with the correct permissions. Same things go if you messed up the 2nd argument of WriteProcessMemory also

Using VirtualProtectEx to get write permissions to the target memory page is also a recommended step like I do here:

void PatchEx(HANDLE hProcess, char* dst, char* src, int size)
{
    DWORD oldprotect;
    VirtualProtectEx(hProcess, dst, size, PAGE_EXECUTE_READWRITE, &oldprotect);
    WriteProcessMemory(hProcess, dst, src, size, NULL);
    VirtualProtectEx(hProcess, dst, size, oldprotect, &oldprotect);
}
like image 187
GuidedHacking Avatar answered Dec 12 '22 13:12

GuidedHacking