One week ago I started making a project to scan another processes memory. First I was thinking in just inject a DLL into the processes and then access its memory, but due to all difficulty I started doing this with VirtualQueryEx / ReadProcessMemory APIs. Some doubts emerged in my mind, below is the code:
var
Form1: TForm1;
PIDHandle: THandle;
PID: dword;
MemInfo: MEMORY_BASIC_INFORMATION;
SysInfo: SYSTEM_INFO;
PageSize, MemStart, ReceivedBytes: cardinal;
Buff: PChar;
IsWow64Process: TIsWow64Process;
IsWow64: BOOL;
while (VirtualQueryEx(PIDHandle, Pointer(MemStart), MemInfo, SizeOf(MemInfo)) > 0) do
begin
if ((MemInfo.State = MEM_COMMIT) and (not (MemInfo.Protect = PAGE_GUARD)
or (MemInfo.Protect = PAGE_NOACCESS))) then
begin
if (0<>MemInfo.Protect and PAGE_READWRITE) then
begin
GetMem(Buff, MemInfo.RegionSize);
if (ReadProcessMemory(PIDHandle, Pointer($00636ED0), Buff,
MemInfo.RegionSize, ReceivedBytes)) then
begin
Memo1.Lines.Append(PAnsiChar(Buff));
end;
FreeMem(Buff);
end;
end;
MemStart:= MemStart + MemInfo.RegionSize;
end;
CloseHandle(PIDHandle);
end;
First All - This while method:
while (VirtualQueryEx(PIDHandle, Pointer(MemStart), MemInfo, SizeOf(MemInfo)) > 0) do
...
MemStart:= MemStart + MemInfo.RegionSize;
Am I scanning all the memory addresses? Or it's just 'jumping' addresses without reading??! Second and most important problem- I'm using notepad to make tests, and my objective is to read some string wrote there. If I make like this:
if (0<>MemInfo.Protect and PAGE_READWRITE) then
begin
GetMem(Buff, MemInfo.RegionSize);
if (ReadProcessMemory(PIDHandle, Pointer($00636ED0), Buff,
MemInfo.RegionSize, ReceivedBytes)) then
begin
Memo1.Lines.Append(PAnsiChar(Buff));
end;
FreeMem(Buff);
end;
Passing the Address directly in the ReadProcessMemory, it works and get the first letter(or first byte) of my string. Remembering Buff is PChar... But if I do the same thing but ReadProcessMemory like this:
if (ReadProcessMemory(PIDHandle, MemInfo.BaseAddress, Buff,
MemInfo.RegionSize, ReceivedBytes)) then
begin
Memo1.Lines.Append(PAnsiChar(Buff));
end;
FreeMem(Buff);
end;
It add on the Memo a bunch of chinese and invalid characters. How can I automatically scan every single memory address and get my string wrote there? Any questions about the code, just ask-me... Waiting for your help