Basic Memory Scanning - Like Cheat Engine

Share your advanced PureBasic knowledge/code with the community.
epidemicz
User
User
Posts: 86
Joined: Thu Jan 22, 2009 8:05 am
Location: USA
Contact:

Basic Memory Scanning - Like Cheat Engine

Post by epidemicz »

Very quick, very dirty test proof of how to scan a program's memory for values ala Cheat Engine.

This code based off some info found in this thread.

How to Test?
-------------------
-Open a blank notepad.
-Check Unicode Exe in compile options.
-Type random text in notepad.
-Change findString.
-Press F5.

Testing against Cheat Engine's results give 100% exact matches.

There's not many comments, but there's not really much to say either. VirtualQueryEx gives valid memory blocks, ReadProcessMemory grabs the data in the blocks and we loop through to see if we have a match.

Code: Select all

;Epidemicz - 12/30/2010 PB 4.51 x86
;====================================
;Basic Memory Scanner Test Proof
;====================================
;Compile in unicode to test on notepad

;Find Window
HWND = FindWindow_(NULL, "Untitled - Notepad")

;Get ProcessID
GetWindowThreadProcessId_(HWND, @pid)

address=0
maxAddress=$7FFFFFFF

; findNumber=0  - uncomment to use with number method
findString$="LOL123LOL456"
len=Len(findString$)

;Opens Process With full access
hProcess = OpenProcess_(#PROCESS_ALL_ACCESS, #False, pid);

Repeat
  result=VirtualQueryEx_(hProcess, address, @mbi.MEMORY_BASIC_INFORMATION, SizeOf(MEMORY_BASIC_INFORMATION))
  If mbi\State = #MEM_COMMIT And mbi\Protect <> #PAGE_READONLY And mbi\Protect <> #PAGE_EXECUTE_READ And mbi\Protect <> #PAGE_GUARD And mbi\Protect <> #PAGE_NOACCESS
    sBuffer=AllocateMemory(mbi\RegionSize)
    res=ReadProcessMemory_(hProcess, address, sBuffer, mbi\RegionSize, @written)
    If written > 0
      For x = 0 To written
          ;string method - use to find text
          ;================
           tmp$=PeekS(sBuffer+x, len) ;string length ! important
           If FindString(tmp$, findString$, 1)
             Debug "FOUND MATCH - " + Hex(mbi\BaseAddress+x) + "=" + tmp$
           EndIf
           
          ;number method - use to find number value
          ;==============
;           tmp=PeekL(sBuffer+x)
;           If tmp=findNumber
;             Debug "FOUND MATCH - " + Hex(mbi\BaseAddress + x) + "=" + Str(tmp)
;           EndIf
      Next
    EndIf  
    FreeMemory(sBuffer)
  EndIf
  address=mbi\BaseAddress+mbi\RegionSize
Until  address >= maxAddress Or result=0
End
Image
Nituvious
Addict
Addict
Posts: 1027
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Re: Basic Memory Scanning - Like Cheat Engine

Post by Nituvious »

That's really neat! Thanks for sharing!
▓▓▓▓▓▒▒▒▒▒░░░░░
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: Basic Memory Scanning - Like Cheat Engine

Post by Rook Zimbabwe »

I love it!!! With a little work this can be better than Cheat-O-Matic!!! :mrgreen:
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Basic Memory Scanning - Like Cheat Engine

Post by SFSxOI »

Great job, thank you :)

BTW, is the memory location returned with or without any memory offset?
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
epidemicz
User
User
Posts: 86
Joined: Thu Jan 22, 2009 8:05 am
Location: USA
Contact:

Re: Basic Memory Scanning - Like Cheat Engine

Post by epidemicz »

mbi\BaseAddress should give you the chunk where the memory is , x should be the offset from that point. I think that's what you're asking.
Image
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: Basic Memory Scanning - Like Cheat Engine

Post by DarkDragon »

Doesn't work here (Windows 7 x64, German "Unbenannt - Editor" instead of "Untitled - Notepad" and windowhandle is valid, but it doesn't find the string), result is 0 but hProcess is not 0.
bye,
Daniel
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Basic Memory Scanning - Like Cheat Engine

Post by Thorium »

DarkDragon wrote:Doesn't work here (Windows 7 x64, German "Unbenannt - Editor" instead of "Untitled - Notepad" and windowhandle is valid, but it doesn't find the string), result is 0 but hProcess is not 0.
Maybe because access rights are not set correctly. On Vista and 7 you might need PROCESS_QUERY_LIMITED_INFORMATION set to query memory information. This flag is not supported on XP, so PROCESS_ALL_ACCESS is different for XP and 7.
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Basic Memory Scanning - Like Cheat Engine

Post by Thorium »

Just tried it and it takes ages.
Using PeekS and FindString is way to slow.
epidemicz
User
User
Posts: 86
Joined: Thu Jan 22, 2009 8:05 am
Location: USA
Contact:

Re: Basic Memory Scanning - Like Cheat Engine

Post by epidemicz »

If it doesnt work or retrieve instantly something isn't right. I have to have unicode checked on compile options, make sure that is on.

Also debug mode slows it down a bit but should still be instant on notepad, is for me at least. Try calculator, the val is in string on xp num on win 7
Image
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Basic Memory Scanning - Like Cheat Engine

Post by SFSxOI »

Works great here, fast. Windows 7 Ultimate, x86. Are you guys compiling it in unicode?
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Basic Memory Scanning - Like Cheat Engine

Post by SFSxOI »

epidemicz wrote:mbi\BaseAddress should give you the chunk where the memory is , x should be the offset from that point. I think that's what you're asking.
Yes, thats it. Thank You
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
epidemicz
User
User
Posts: 86
Joined: Thu Jan 22, 2009 8:05 am
Location: USA
Contact:

Re: Basic Memory Scanning - Like Cheat Engine

Post by epidemicz »

SFSxOI:
Awesome, glad to hear it's working for you.

All:

I'm working on a full scale app based off this snippet, so I'll see about releasing that to you guys soon. I wanna see if the CheatEngine guy will give me any probs for copying his UI.
Image
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Basic Memory Scanning - Like Cheat Engine

Post by Thorium »

epidemicz wrote: I'm working on a full scale app based off this snippet, so I'll see about releasing that to you guys soon. I wanna see if the CheatEngine guy will give me any probs for copying his UI.
Do you use this string search methode, or coded a faster one?
If not i can make you a way faster one, if you want.
epidemicz
User
User
Posts: 86
Joined: Thu Jan 22, 2009 8:05 am
Location: USA
Contact:

Re: Basic Memory Scanning - Like Cheat Engine

Post by epidemicz »

Thorium wrote:
epidemicz wrote: I'm working on a full scale app based off this snippet, so I'll see about releasing that to you guys soon. I wanna see if the CheatEngine guy will give me any probs for copying his UI.
Do you use this string search methode, or coded a faster one?
If not i can make you a way faster one, if you want.
It's quick and dirty right now, I just used this method. So, I certainly would be grateful for any code contributions.
Image
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Basic Memory Scanning - Like Cheat Engine

Post by Thorium »

Ok,
i did a quick faster one:

Code: Select all

          If CompareMemoryString(sBuffer + x, @findString$, #PB_Default, len)
             Debug "FOUND MATCH - " + Hex(mbi\BaseAddress+x) + "=" + PeekS(sBuffer + x, len)
          EndIf
Just replace the string search part with this.

However is does not find anything and i dont know why yet. It also does not find anything with your procedure.
But i know why it took endless long on my computer. It actualy was in a endless loop because i executed it as x86 on a x64 system. The addresses are all quads on x64, so the code only used the lower dword of it. It should work on x64 if you remove the check for the max address. Because max address is much to low for x64.

But it does not find anything. Must take a better look tomorrow.
Post Reply