Skip to content

Commit

Permalink
Add an option to make virtual memory scanning safer. (on by default)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheatengine@gmail.com committed Nov 18, 2013
1 parent 756e7ef commit d9d050d
Show file tree
Hide file tree
Showing 4 changed files with 297 additions and 37 deletions.
53 changes: 52 additions & 1 deletion Cheat Engine/MainUnit.pas
Expand Up @@ -727,6 +727,7 @@ TMainForm = class(TForm)
procedure LoadCustomTypesFromRegistry;

procedure setGbScanOptionsEnabled(state: boolean);
procedure cbSaferPhysicalMemoryChange(sender: tobject);


function onhelp(Command: word; Data: PtrInt; var CallHelp: boolean): boolean;
Expand Down Expand Up @@ -787,6 +788,8 @@ TMainForm = class(TForm)
LuaFiles: TLuaFileList;
frmLuaTableScript: Tfrmautoinject;


cbsaferPhysicalMemory: TCheckbox;
mustClose: boolean;


Expand Down Expand Up @@ -894,6 +897,7 @@ implementation
strKeepList = 'Keep the current address list/code list?';
strInfoAboutTable = 'Info about this table:';
strPhysicalMemory = 'Physical Memory';
strSaferPhysicalMemory = 'Safer memory access';
rsThereAreOneOrMoreAutoAssemblerEntriesOrCodeChanges =
'There are one or more auto assembler entries or code changes enabled in this table. Do you want them disabled? (without '
+ 'executing the disable part)';
Expand Down Expand Up @@ -2441,6 +2445,33 @@ procedure TMainForm.openProcessEpilogue(oldprocessname: string; oldprocess: dwor

outputdebugstring('After setcodeanddatabase');

if processid = $FFFFFFFF then
begin
processlabel.Caption := strPhysicalMemory;
cbPauseWhileScanning.visible:=false;

if cbsaferPhysicalMemory=nil then
begin
cbsaferPhysicalMemory:=tcheckbox.create(self);
cbsaferPhysicalMemory.Caption:=strSaferPhysicalMemory;
cbsaferPhysicalMemory.Checked:=dbk32functions.saferQueryPhysicalMemory;
cbsaferPhysicalMemory.Parent:=cbPauseWhileScanning.Parent;
cbsaferPhysicalMemory.left:=cbPauseWhileScanning.left;
cbsaferPhysicalMemory.Top:=cbPauseWhileScanning.top;
cbsaferPhysicalMemory.OnChange:=cbSaferPhysicalMemoryChange;
end;
end
else
begin
//restore cbPauseWhileScanning if it was replaced
if cbSaferPhysicalMemory<>nil then
begin
freeandnil(cbsaferPhysicalMemory);
cbPauseWhileScanning.Visible:=true;
end;
end;



if (processhandle = 0) then
begin
Expand Down Expand Up @@ -2484,6 +2515,7 @@ procedure TMainForm.openProcessEpilogue(oldprocessname: string; oldprocess: dwor

if processid <> $FFFFFFFF then
begin

processlabel.Caption := strError;
raise Exception.Create(strErrorWhileOpeningProcess);
end
Expand Down Expand Up @@ -6989,7 +7021,10 @@ procedure TMainForm.cbFastScanClick(Sender: TObject);

end;


procedure TMainForm.cbSaferPhysicalMemoryChange(sender: tobject);
begin
DBK32functions.saferQueryPhysicalMemory:=cbsaferPhysicalMemory.checked;
end;

procedure TMainForm.cbPauseWhileScanningClick(Sender: TObject);

Expand Down Expand Up @@ -7794,8 +7829,23 @@ procedure TMainForm.Label59Click(Sender: TObject);
b: BOOL;
tid: dword;
h: thandle;

mr: TPhysicalMemoryRanges;

sl: tstringlist;
begin
if GetMemoryRanges(mr) then
begin
sl:=tstringlist.create;
for i:=0 to length(mr)-1 do
sl.add(inttohex(mr[i].base,16)+'-'+inttohex(mr[i].base+mr[i].size,16));

showmessage(sl.text);
sl.free;
end;


exit;
c:=getConnection;

if c.loadExtension(processhandle) then
Expand All @@ -7807,6 +7857,7 @@ procedure TMainForm.Label59Click(Sender: TObject);
b:=VirtualFreeEx(processhandle, addr, 0,0);
if b then
showmessage('freed')
else
showmessage('error');
Expand Down
41 changes: 25 additions & 16 deletions Cheat Engine/NewKernelHandler.pas
Expand Up @@ -1090,26 +1090,35 @@ procedure DBKFileAsMemory(filename:string); overload;
function VirtualQueryExPhysical(hProcess: THandle; lpAddress: Pointer; var lpBuffer: TMemoryBasicInformation; dwLength: DWORD): DWORD; stdcall;
var buf:_MEMORYSTATUS;
begin
GlobalMemoryStatus(buf);

lpBuffer.BaseAddress:=pointer((ptrUint(lpAddress) div $1000)*$1000);
lpbuffer.AllocationBase:=lpbuffer.BaseAddress;
lpbuffer.AllocationProtect:=PAGE_EXECUTE_READWRITE;
lpbuffer.RegionSize:=buf.dwTotalPhys-ptrUint(lpBuffer.BaseAddress);
lpbuffer.RegionSize:=lpbuffer.RegionSize+($1000-lpbuffer.RegionSize mod $1000);

lpbuffer.State:=mem_commit;
lpbuffer.Protect:=PAGE_EXECUTE_READWRITE;
lpbuffer._Type:=MEM_PRIVATE;

if (ptrUint(lpAddress)>buf.dwTotalPhys) //bigger than the total ammount of memory
then
if dbk32functions.hdevice<>INVALID_HANDLE_VALUE then
begin
zeromemory(@lpbuffer,dwlength);
result:=0
result:=dbk32functions.VirtualQueryExPhysical(hProcess, lpAddress, lpBuffer, dwLength);
end
else
result:=dwlength;
begin
GlobalMemoryStatus(buf);

lpBuffer.BaseAddress:=pointer((ptrUint(lpAddress) div $1000)*$1000);
lpbuffer.AllocationBase:=lpbuffer.BaseAddress;
lpbuffer.AllocationProtect:=PAGE_EXECUTE_READWRITE;
lpbuffer.RegionSize:=buf.dwTotalPhys-ptrUint(lpBuffer.BaseAddress);
lpbuffer.RegionSize:=lpbuffer.RegionSize+($1000-lpbuffer.RegionSize mod $1000);

lpbuffer.State:=mem_commit;
lpbuffer.Protect:=PAGE_EXECUTE_READWRITE;
lpbuffer._Type:=MEM_PRIVATE;

if (ptrUint(lpAddress)>buf.dwTotalPhys) //bigger than the total ammount of memory
then
begin
zeromemory(@lpbuffer,dwlength);
result:=0
end
else
result:=dwlength;

end;

end;

Expand Down

0 comments on commit d9d050d

Please sign in to comment.