Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Calculating offset to base pointer

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine
View previous topic :: View next topic  
Author Message
Erik9631
Newbie cheater
Reputation: 0

Joined: 24 Aug 2014
Posts: 10

PostPosted: Sat Feb 29, 2020 7:47 pm    Post subject: Calculating offset to base pointer Reply with quote

Hello.
I am a bit new to reverse engineering and I need some help figuring out how to calculate the static offset to the base pointer (I believe the offset should be the difference between the main application and the pointer address), so once the application is restarted, I always get to the address of the pointer. (But please correct me if I am wrong)

I created a simple application with the following code:

Code:

int main()
{
   HANDLE moduleHandle = GetModuleHandle("Delete.exe");
   cout << moduleHandle << endl;
   int* hackableValue = new int[1];
   *hackableValue = 5;
   system("PAUSE");

   while (1)
   {
      if (GetAsyncKeyState(VK_UP) & ((unsigned short)32768))
      {
         *hackableValue += 2;
         cout << *hackableValue << " Addr: " << hackableValue <<  " Ptr: "<< &hackableValue <<endl;
      }

   }
}


What I did was scanned for the hackableValue as it changed. Then I disassembled the memory region that writes to that address.

Here we can clearly see the
add dword ptr [eax],02
which is where the +2 is added on getAsyncKeystate
and
mov eax,[esp+08]
Which de-references the address (Which is the address where our value is allocated) on ESP + 08 (Meaning that ESP+08 contains the address of the base pointer[I also printed this address in the application itself and the values were equal]). So in our application this would be the address of (&hackableValue).

My question is, how should I go about finding the static offset, from which I can calculate the address(Pointer address) that is assigned to ESP + 08 on each restart of the application.

Thanks in advance for the help.

NOTE:
I attached the images to the post.



Capture.PNG
 Description:
 Filesize:  54.59 KB
 Viewed:  1861 Time(s)

Capture.PNG



Capture2.png
 Description:
 Filesize:  33.52 KB
 Viewed:  1861 Time(s)

Capture2.png


Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 152

Joined: 06 Jul 2014
Posts: 4695

PostPosted: Sat Feb 29, 2020 9:15 pm    Post subject: Reply with quote

That isn't how it works. The variable hackableValue is declared inside a function and therefore stored on the stack. Such a pointer can be used as a base address when the game is restarted by using the address of that thread's stack. Look at the function getStackStart in CE's source code here for more information on how to find the address the start of a stack is located at.

If you declare hackableValue outside main (and therefore make it static), subtract its address from the address the exe was loaded at and that will be your offset.

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8585
Location: 127.0.0.1

PostPosted: Sun Mar 01, 2020 4:19 am    Post subject: Reply with quote

You can't use GetModuleHandle like that from an external process. GetModuleHandle is only for local modules, not for remote ones. You need to use an external means of obtaining the process/module-info such as:
- CreateToolhelp32Snapshot
- Process32First / Process32Next
- Module32First / Module32Next

Or the PSAPI equivalents. (There's a dozen other means to do it depending on if you need to hide/be undetected, etc. but these are the main top-level two.)

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Erik9631
Newbie cheater
Reputation: 0

Joined: 24 Aug 2014
Posts: 10

PostPosted: Sun Mar 01, 2020 6:49 am    Post subject: Reply with quote

ParkourPenguin wrote:
That isn't how it works. The variable hackableValue is declared inside a function and therefore stored on the stack. Such a pointer can be used as a base address when the game is restarted by using the address of that thread's stack. Look at the function getStackStart in CE's source code URL... for more information on how to find the address the start of a stack is located at.

If you declare hackableValue outside main (and therefore make it static), subtract its address from the address the exe was loaded at and that will be your offset.


Thanks for the reply. The information was very helpful. I have one question however:

What if the variable I would be looking for was located in a thread that would not be the main thread. How would I go about looking for the correct thread?
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 152

Joined: 06 Jul 2014
Posts: 4695

PostPosted: Sun Mar 01, 2020 11:15 am    Post subject: Reply with quote

The first few threads will probably be started in a consistent order. If not, use some property of the thread to distinguish it from others. e.g. its start address (ThreadQuerySetWin32StartAddress) or something else in the TEB (wikipedia).
_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites