Skip to content

Commit

Permalink
Fix memory write bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackINT3 committed Aug 25, 2020
1 parent 3da8e17 commit ccfc205
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/OpenArk/kernel/object/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ void KernelObject::InitObjectSectionsView()
prefix = L"";
map_name = section_name;
}
map_hd = OpenFileMappingW(FILE_MAP_READ, FALSE, map_name.c_str());
map_hd = OpenFileMappingW(FILE_MAP_READ|FILE_MAP_WRITE, FALSE, map_name.c_str());
if (map_hd) {
map_addr = (ULONG64)MapViewOfFileEx(map_hd, FILE_MAP_READ, 0, 0, size, NULL);
map_addr = (ULONG64)MapViewOfFileEx(map_hd, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, size, NULL);
if (!map_addr) {
CloseHandle(map_hd);
return;
Expand Down
22 changes: 17 additions & 5 deletions src/OpenArkDrv/arkdrv-api/api-memory/api-memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,12 @@ bool MemoryRead(ULONG pid, ULONG64 addr, ULONG size, std::string &readbuf)
std::string data;
data.resize(size);
BOOL ret = ReadProcessMemory(phd, (PVOID)addr, (PVOID)data.data(), (SIZE_T)size, (SIZE_T*)&readlen);
if (!ret) {
ERR(L"ReadProcessMemory pid:%d, err:%d", pid, GetLastError());
CloseHandle(phd);
return FALSE;
}
CloseHandle(phd);
if (!ret) return FALSE;

readbuf = std::move(data);
return true;
Expand Down Expand Up @@ -105,14 +109,22 @@ bool MemoryWrite(ULONG pid, ULONG64 addr, std::string &writebuf)
PVOID buf = (PVOID)writebuf.data();
SIZE_T bufsize = (SIZE_T)writebuf.size();
DWORD written, oldprotect;
VirtualProtectEx(phd, (PVOID)addr, bufsize, PAGE_READWRITE, &oldprotect);
BOOL ret = WriteProcessMemory(phd, (PVOID)addr, buf, bufsize, (SIZE_T*)&written);
VirtualProtectEx(phd, (PVOID)addr, bufsize, oldprotect, &oldprotect);
CloseHandle(phd);
BOOL ret = FALSE;
ret = VirtualProtectEx(phd, (PVOID)addr, bufsize, PAGE_READWRITE, &oldprotect);
if (!ret) {
ERR(L"VirtualProtectEx pid:%d, err:%d", pid, GetLastError());
CloseHandle(phd);
return FALSE;
}
ret = WriteProcessMemory(phd, (PVOID)addr, buf, bufsize, (SIZE_T*)&written);
if (!ret) {
ERR(L"WriteProcessMemory pid:%d, err:%d", pid, GetLastError());
VirtualProtectEx(phd, (PVOID)addr, bufsize, oldprotect, &oldprotect);
CloseHandle(phd);
return FALSE;
}
VirtualProtectEx(phd, (PVOID)addr, bufsize, oldprotect, &oldprotect);
CloseHandle(phd);

return true;
}
Expand Down
2 changes: 0 additions & 2 deletions src/OpenArkDrv/arkdrv-api/api-object/api-object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,6 @@ bool ObjectSectionEnumR3(std::vector<ARK_OBJECT_SECTION_ITEM> &items, ULONG sess
item.section_size = (ULONG)mbi.RegionSize;
UnmapViewOfFile(mapaddr);
CloseHandle(maphd);
} else {
ERR(L"%s %d", map_name.c_str(), GetLastError());
}
item.session_id = session;
items.push_back(item);
Expand Down

0 comments on commit ccfc205

Please sign in to comment.