views:

375

answers:

4

Lets assume I am a game and I have a global int* that contains my health. A game trainer's job is to modify this value to whatever in order to achieve god mode. I've looked up tutorials on game trainers to understand how they work, and the general idea is to use a memory scanner to try and find the address of a certain value. Then modify this address by injecting a dll or whatever.

But I made a simple program with a global int* and its address changes every time I run the app, so I don't get how game trainers can hard code these addresses? Or is my example wrong?

What am I missing?

A: 

EDIT: never mind it seems it was just good luck, however the last 3 numbers of the pointer seem to stay the same. Perhaps this is ASLR kicking in and changing the base image address or something?

aaahhhh my bad, i was using %d for printf to print the address and not %p. After using %p the address stayed the same

#include <stdio.h>

int *something = NULL;

int main()
{
    something = new int;
    *something = 5;

    fprintf(stdout, "Address of something: %p\nValue of something: %d\nPointer Address of something: %p", &something, *something, something);
    getchar();
    return 0;
}
GameTrainersWTF
+1  A: 

Example for a dynamicaly allocated varible

The value I want to find is the number of lives to stop my lives from being reduced to 0 and getting game over.

  1. Play the Game and search for the location of the lifes variable this instance.
  2. Once found use a disassembler/debugger to watch that location for changes.
  3. Lose a life.
  4. The debugger should have reported the address that the decrement occurred.
  5. Replace that instruction with no-ops

Got this pattern from the program called tsearch


A few related websites found from researching this topic:

mikek3332002
Yeah fairly straight forward stuff if want to prevent myself from dieing but I wan't to read/change the actual value instead of cancelling it out
GameTrainersWTF
Not that familar, but you could possibly grab the address the addess that it is using. Also don't have to use No-ops you can fill it with anything eg Jumps.
mikek3332002
I tried making a code cave in my program, i changed a jump that would jump to my code then jump back, but for some reason when i saved it in olly, and then reloaded it, my code in the code cave turned into jibberish DB xyz commands :S I even changed the section header to add more virtual memory space, and that didn't help either :( Perhaps you know what is happening here?
GameTrainersWTF
Sorry I have no idea how ollydbg is used. :(
mikek3332002
+3  A: 

The way this is usually done is by tracing the pointer chain from a static variable up to the heap address containing the variable in question. For example:

struct CharacterStats
{
    int health;
    // ...
}

class Character
{
public:
    CharacterStats* stats;

    // ...

    void hit(int damage)
    {
        stats->health -= damage;
        if (stats->health <= 0)
            die();
    }
}


class Game
{
public:
    Character* main_character;
    vector<Character*> enemies;
    // ...
}

Game* game;

void main()
{
    game = new Game();
    game->main_character = new Character();
    game->main_character->stats = new CharacterStats;

    // ...

}

In this case, if you follow mikek3332002's advice and set a breakpoint inside the Character::hit() function and nop out the subtraction, it would cause all characters, including enemies, to be invulnerable. The solution is to find the address of the "game" variable (which should reside in the data segment or a function's stack), and follow all the pointers until you find the address of the health variable.

Some tools, e.g. Cheat Engine, have functionality to automate this, and attempt to find the pointer chain by themselves. You will probably have to resort to reverse-engineering for more complicated cases, though.

CyberShadow
Very nice example thanks, so all game hacks are relying on static pointers? Meaning the root of the pointer chain must be static somewhere along the line?
GameTrainersWTF
Yes, if it's in the data segment (a global variable from the source code point of view), and if we don't count ASLR. It could also be a stack variable (a local variable inside main() for example), in which case its address could vary from one system to another, and you'd need to walk the stack to find it, which is more complicated.
CyberShadow
I guess now i need to find out how game trainers get around ASLR lol
GameTrainersWTF
It's easy, just calculate the new address based on the base address the module was loaded at. For example, most executables are linked with an initial base address of 0x00400000. If you find a variable at 0x00543210, and the module was loaded at 0x76400000, the valiable will be at 0x76543210.
CyberShadow
Ah that makes a lot of sense, also explains why the last 3 numbers on my variable didn't change and only the first part did. Thanks you have been very helpful!!!
GameTrainersWTF
+1  A: 

The way things like Gameshark codes were figured out were by dumping the memory image of the application, then doing one thing, then looking to see what changed. There might be a few things changing, but there should be patterns to look for. E.g. dump memory, shoot, dump memory, shoot again, dump memory, reload. Then look for changes and get an idea for where/how ammo is stored. For health it'll be similar, but a lot more things will be changing (since you'll be moving at the very least). It'll be easiest though to do it when minimizing the "external effects," e.g. don't try to diff memory dumps during a firefight because a lot is happening, do your diffs while standing in lava, or falling off a building, or something of that nature.

dash-tom-bang