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 


[help]Inconsistent memory locations?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
N00bcoder
How do I cheat?
Reputation: 0

Joined: 13 May 2008
Posts: 6

PostPosted: Tue Jun 10, 2008 4:39 pm    Post subject: [help]Inconsistent memory locations? Reply with quote

In trying to teach my self the OpenProcess /ReadProcess/ WriteProcess/ CloseHandle functions, I ran into a problem with memory addresses changing.

Background info: All written in C#(.NET)
Clickme.exe
The form has one checkbox, and one button. The only code I've written in it is as follows
Code:
private void button1_Click(object sender, EventArgs e)
        {
            checkBox1.Checked = (checkBox1.Checked) ? false : true;
        }

Extremely simple so I could teach my self the basics.

Click him.exe has one label and one button. the code that I've writen is as follows:
Code:
private void button1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcessesByName("Clickme");           
            byte[] buffer = new byte[1];
            IntPtr ptrBytesRead;
            OpenProcess(0x1F0FFF, 1, (uint)myProcesses[0].Id);
            ReadProcessMemory(myProcesses[0].Handle, (IntPtr)0x013AC2F4, buffer, 1, out ptrBytesRead);
            this.label1.Text = buffer[0].ToString();
            CloseHandle(myProcesses[0].Handle);
}


I got the memory address that is used in the "Click Him" code from cheat engine.

The issue that I've run into is that the memory address changes. When I run the app's in the folders that they were created in, I get one address, if I run them from the desktop I get a different address.

Thinking that it had something to do with Visual Studio I edited the program for the new address and stuck them both on a flash drive, ran from there and was fine. I handed the flash drive off to a friend to run the programs and he got a completely different outcome than expected.

I tried looking for a pointer to the address and cheat engine came up with nothing. I tried looking at the assembly, but with my limited knowledge came up with nothing. ( I asked someone that knew more than I did and basically confirmed what I thought the line of code was doing. Moving data from one register to another.)

Now there are several bots in this forum for various things, and I highly doubt that they were programed to search through the programs finding the values through trial and error.

How do I go about making sure that I have the correct memory address no matter the location?

Or is it a case that larger programs are more predictable?


Last edited by N00bcoder on Wed Jun 11, 2008 12:43 pm; edited 1 time in total
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 472

Joined: 09 May 2003
Posts: 25867
Location: The netherlands

PostPosted: Tue Jun 10, 2008 4:48 pm    Post subject: Reply with quote

use pointers and module baseaddresses
0x013AC2F4 looks like it's allocated mem instead of module mem, so use pointers

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
N00bcoder
How do I cheat?
Reputation: 0

Joined: 13 May 2008
Posts: 6

PostPosted: Tue Jun 10, 2008 5:12 pm    Post subject: Reply with quote

Not sure what "module baseaddresses" are.

I may have the settings on CE incorrect, but I'm able to get the pointer in step 6(?) in the Tut that came with CE. I'm leaving the settings alone since I still know next to nothing about that aspect of the application.

So if there's a pointer pointing to that address, it's either out of range of what I'm searching through... or my settings are incorrect in someway.

Writable memory as base only is the only checkbox selected

Clickme.exe selected

from 00400000 to 00408000
pointer must reside in
00400000 to 70000000

The rest of the options look like they only deal with the scanner rather than the data being scanned.

With everything setup this way, I come up with 0 pointers.
Back to top
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Tue Jun 10, 2008 5:58 pm    Post subject: Reply with quote

First, two notes on your code:

1) Name your buttons/check boxes. It's been discussed a lot in this section, and it's annoying for people to read through your code with things like "button1" "checkbox1" etc.

2) Instead of doing those weird expressions, I prefer to just do something like

Code:

chkMyBox.Checked = !chkMyBox.Checked;


I'm guessing that's what you were doing, anyway... I hate those stupid things.

~~

I'm assuming this is C# (.NET).

In .NET, the garbage collector is going to move around the things in memory, so you're going to have to dynamically find the button each time, or whatever you're doing.

I'm not quite sure why it would change in different locations, but dynamically finding the button or whatever would probably be best (AOBs, or storing the offset and dynamically finding the module).

For modules, you'd simply do something like (note that the ProcessModuleCollection does not inherit many LINQ extension methods for some reason, so you'd probably be better off doing a simple foreach loop:)

Code:

IntPtr baseAddress = IntPtr.Zero;

foreach (ProcessModule pMod in myProc.Modules)
{
    if (pMod.ModuleName == "module")
    {
        baseAddress = pMod.BaseAddress;
        break;
    }
}

if (baseAddress == IntPtr.Zero) return; //not found

IntPtr goodAddress = baseAddress + Offset; //Offset was already defined


Alternatively, one could simply do

Code:

IntPtr baseAddress = myProc.MainModule.BaseAddress;

IntPtr goodAddress = baseAddress + Offset;

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
N00bcoder
How do I cheat?
Reputation: 0

Joined: 13 May 2008
Posts: 6

PostPosted: Wed Jun 11, 2008 1:14 pm    Post subject: Reply with quote

I understand the reason for renaming the controls, however I didn't expect to be posting the code anywhere, or even to be using the code for longer than a 24 hour period of time. It was just some quick and dirty code to do the job. I do have to point out, that with as simple as this code is, leaving the names alone does give a clear indication of what's going on in this program. Anymore complex, and I would agree 100% with you. In the future, all code I post will have the names changed just to stifle this kind of comment.

As far as the trinary, I can only say that my coding practices are heavily influenced by my friends at school, sometimes not always for the better. I originally wrote an if-else statement then changed it to it's current form. Had I given it a little more thought I would have ended up with what you suggested.

For the last 24 hours I've been on a journey trying to figure this out. I ended up using the second part of what you suggested:

Code:
IntPtr baseAddress = myProc.MainModule.BaseAddress;

IntPtr goodAddress = baseAddress + Offset;


Every time I ran the program, I would end up with 4194304(0x00400000) as the base address. I have yet to try the larger portion of the code you suggested. However, you indicated that the two code snip-its were equivalent. I still ran into the same problem: the address changes.

This leads me to believe that there is a pointer being used in some way, as dark suggested. Try as I might, I can not get cheat engine to provide a pointer that points to that memory address. I'm halfway thinking about looking through the assembly, armed with your guide, and see what I can find. But I'm really close to just saying the heck with this mess, and moving on to the next step. (searching through a program for a value with out the use of ce)
Back to top
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Wed Jun 11, 2008 6:51 pm    Post subject: Reply with quote

First of all, the code that you were using was the faster one, but not necessarily always the correct one; keep that in mind. It is safest to use the loop, but then again not always necessary. You should check first, though, before deciding on a way to do it.

As for the issue, I'm still not quite positive what you're trying to accomplish with CE.

Edit:

I played around this, and basically, you just need to declare the value to change as static.

Here's how it works in memory:

Dynamic Int (Not Static):

Code:

mov [eax],value


Static Int:

Code:

mov [address],value


There's no pointer, that's just how that works. Note that this is probably considered pretty advanced, so don't hesitate to ask questions.

Basically, you have a form. Instead of thinking of a class (which it is), let's just consider it a struct, with sequential order, just for visualization purposes.

In, a struct, the value are laid out in memory one after another.

For example:

Code:

struct a
{
    int b;
    int c;
}

a _a;


Code:

xxxxxxxx       ;_a
xxxxxxxx + 0 ;_a.b
xxxxxxxx + 4 ;_a.c


So now let's say we've got our form. You need to find the base address of the form dynamically (since you can't have a static form), then find the offset of the int on there.

In the memory, that would look like

Code:

xxxxxxxx             ;form
xxxxxxxx + offset ;int


The assembly, somewhere, moves the address of the int into eax, so that it can just do

Code:

mov [eax],value


Because it's dynamic (not static), there could be more than one of the form, and therefore you'll have to find the one that you want.

However, if the int was static, that would mean that there is only one of those in memory, at one specific address.

That's why, instead of loading the value into eax, it simply moves the value straight into address.

~~

So, you've got two options here:

Make your value to find static (declare it with the static attribute)

or

Do something really advanced, like first find the offset, then find the form in memory (I'm thinking AOBs), and finally do something like

Code:

IntPtr Address = formBase + valueOffset;


I'm thinking the first one is probably easiest, but it's sometimes fun to have a challenge. Maybe in a little bit I'll take a crack at the second one.

Edit Again:

lol...

Code:

Offset = System.Runtime.InteropServices.Marshal.OffsetOf(frmMyForm, "myValue");


That will only work on the current application, though. =P

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
N00bcoder
How do I cheat?
Reputation: 0

Joined: 13 May 2008
Posts: 6

PostPosted: Thu Jun 12, 2008 12:41 pm    Post subject: Reply with quote

I'm weighing my choices here... If I was sure that I would be using these techniques in order to effectively find the same value in a game, then I would be more than happy to continue along this path.

However, most games are written in C++. I find C#.NET a little annoying atm to work in. I'm sure it will grow on me eventually when I start having to create tools. It's just how can I trust a book of code that I don't write? I'm sure it's because of this code that I'm having weird memory address issues.

Or it may just be that I don't understand everything I need to know about how to consistently find a memory address in any application and need to continue figuring out this conundrum.

The whole point of these two little programs was to give myself a relatively easy goal to achieve: Display the value that controls what the checkbox displays (checked/unchecked).

Something clicked a while ago. I keep seeing esi+0000009c again and again when I tell CE to look at what writes to this value. Going off what you explained, the esi value should be the base of the form. Well, I took the address of the byte that controls the checkbox, and subtracted 9c from it.................. and that address doesn't have a pointer to it either. I still haven't tried implementing the safe code you suggested yet because I've only really had time to sleep eat and go to class in the last 36 hours. Maya is a pain sometimes.

BTW what is AOB?
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Thu Jun 12, 2008 1:12 pm    Post subject: Reply with quote

Quote:
BTW what is AOB?


AoB stands for Array Of Bytes.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
N00bcoder
How do I cheat?
Reputation: 0

Joined: 13 May 2008
Posts: 6

PostPosted: Thu Jun 12, 2008 2:23 pm    Post subject: Reply with quote

I have an answer to this whole mess, it's not favorable, but I doubt anyone here is going to have to find a work around.
I've been talking to my C# teacher over the last couple of days, trying to figure out why things are acting like they are (memory addresses changing with out an apparent pointer). The realization is: this is managed code. Meaning that it's running on something akin(not exactly) to a virtual machine. So the memory model is different than what you would find in just about any game.

Thank you samuri25404 for all the help you have provided on this.

Quote:
AoB stands for Array Of Bytes.

Thanks Wiccaan for clearing that up.

_________________
Currently enrolled at FullSail University for game development.
Games created:
Snake
Pong
(More to come)
Back to top
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Thu Jun 12, 2008 4:01 pm    Post subject: Reply with quote

N00bcoder wrote:
The realization is: this is managed code. Meaning that it's running on something akin(not exactly) to a virtual machine. So the memory model is different than what you would find in just about any game. .


Actually, no.

I was taking a look around the code, and basically, it does Managed to Unmanaged conversions when entering methods and what not.

It still has to work as a regular .EXE like any other program to run. You will still be able to hack it like a regular game.

Btw,

I'm looking into the hacking, and I'll play around with it some more. Once I get a definite bit, I'll post.

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
Estx
Expert Cheater
Reputation: 0

Joined: 04 Mar 2008
Posts: 172

PostPosted: Fri Jun 13, 2008 11:37 am    Post subject: Reply with quote

If you need code references or something to read over to learn how you can use memory management in C#, check out my MemEdit class. Link is in sig. (:

As for topic problem, my initial suggestion was already suggested (pointers).
I'm a little tired at the moment so haven't really reviewed all the posts - will repost tomorrow and try to help solve whatever the problem is.
Back to top
View user's profile Send private message
N00bcoder
How do I cheat?
Reputation: 0

Joined: 13 May 2008
Posts: 6

PostPosted: Tue Jun 17, 2008 11:32 pm    Post subject: Reply with quote

I haven't had time to work on this of late. I will once I catch up on the projects I've been assigned in class. Why oh why didn't I go to a normal college?
_________________
Currently enrolled at FullSail University for game development.
Games created:
Snake
Pong
(More to come)
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming 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