bool
DBK_ReadProcessMem(UINT64 pid, UINT64 toAddr, UINT64 fromAddr, DWORD size,
bool
failToContinue)
{
struct InputBuffer
{
UINT64 processid;
UINT64 startaddress;
WORD bytestoread;
};
UINT64 remaining
=
size;
UINT64 offset
=
0
;
do
{
UINT64 toRead
=
remaining;
if
(remaining >
4096
)
{
toRead
=
4096
;
}
InputBuffer inputBuffer;
inputBuffer.processid
=
pid;
inputBuffer.startaddress
=
fromAddr
+
offset;
inputBuffer.bytestoread
=
toRead;
DWORD retSize;
if
(!DeviceIoControl(g_DBKDevice, IOCTL_CE_READMEMORY, (LPVOID)&inputBuffer, sizeof(inputBuffer), (LPVOID)(toAddr
+
offset), toRead, &retSize, NULL))
{
if
(!failToContinue)
{
LOG(
"DeviceIoControl IOCTL_CE_READMEMORY failed"
);
return
false;
}
}
remaining
-
=
toRead;
offset
+
=
toRead;
}
while
(remaining >
0
);
return
true;
}
bool
DBK_WriteProcessMem(UINT64 pid, UINT64 targetAddr, UINT64 srcAddr, DWORD size)
{
struct InputBuffer
{
UINT64 processid;
UINT64 startaddress;
WORD bytestowrite;
};
UINT64 remaining
=
size;
UINT64 offset
=
0
;
do
{
UINT64 toWrite
=
remaining;
if
(remaining > (
512
-
sizeof(InputBuffer)))
{
toWrite
=
512
-
sizeof(InputBuffer);
}
InputBuffer
*
pInputBuffer
=
(InputBuffer
*
)malloc(toWrite
+
sizeof(InputBuffer));
if
(NULL
=
=
pInputBuffer)
{
LOG(
"malloc failed"
);
return
false;
}
pInputBuffer
-
>processid
=
pid;
pInputBuffer
-
>startaddress
=
targetAddr
+
offset;
pInputBuffer
-
>bytestowrite
=
toWrite;
memcpy((PCHAR)pInputBuffer
+
sizeof(InputBuffer), (PCHAR)srcAddr
+
offset, toWrite);
DWORD retSize;
if
(!DeviceIoControl(g_DBKDevice, IOCTL_CE_WRITEMEMORY, (LPVOID)pInputBuffer, (sizeof(InputBuffer)
+
toWrite), NULL,
0
, &retSize, NULL))
{
LOG(
"DeviceIoControl IOCTL_CE_WRITEMEMORY failed"
);
free(pInputBuffer);
return
false;
}
free(pInputBuffer);
remaining
-
=
toWrite;
offset
+
=
toWrite;
}
while
(remaining >
0
);
return
true;
}