How to read another program’s variable?

Asked

Viewed 2,578 times

0

Guys, I am learning Programming and I would like to know: how do I read the value of a variable of another program (theoretically this would be without "awareness" of the program read, I do not know exactly how it works, if I would have to ask for some permission... Something like does the Cheat Engine, only without attribution, just reading it)? I have the basics (very basic, I only finish the discipline of Intro to Computing in my college) of Python, C and I risk a little in C++ and Java

  • If you want to communicate between the two programs, you can use sockets network.

3 answers

8

I regret to inform you that only with basic knowledge you will not be able to do this - even with advanced knowledge this is not trivial. You need to somehow enter the process space of the other program, learn or discover how your data structures are defined from memory, know the local and the time exactly where you should read the memory of the program, and map the information read in its concept of "variable".

There are many other considerations. For example, it depends a lot on the environment in which the program is running. Is your program a phone app (iOS / Android / Winphone) or tablets (iOS / Android / Windows)? No chance - the operating system isolates apps so they don’t have access to other applications. Is it a desktop application (Windows or Mac)? Your program would have to have access to the virtual memory space of the other program, which is not the case most of the time (you may need to use one driver (another task for sure non-trivial) to gain access to the memory space of the other process).

Ok, assuming you can at a certain point read the entire memory of the other program (as I said above, it is not a probable hypothesis). What kind of variable do you want to read? If it is a local variable of a method, it will be allocated in the program’s execution stack, then you will have to access the memory exactly at the time that function is running, and know within the stack which value corresponds to the variable you want (another non-trivial task). Another thing: it may be that the variable represents not a primitive value, but an object - which is stored not in the stack, but in the heap of objects (what happens when you use the new in Java, for example). Then in the stack you will only have the address of the memory space where the value of the variable is. And that value can change - in Java or C#, when the Garbage Collector rotate, the object has chances to be moved.

Anyway, if you want to hack another program, trying to get the value of one of its variables is not the best way :)

  • The program I was in mind would be for Windows... I thought it would be simpler, since the Cheat Engine itself adapts to Desktops with a certain ease (I never had to configure anything or install drivers) and I believe that the variable is not global, because I’m trying to take values from an emulator (which varies according to the game), but I believe that the function that has it is being performed all the time during the game.

  • Just by way of clarification, my intention was not to hack a program, but just take these parameters that are kind of "subjective" in the game and turn it into something visible to the user :) EDIT: I don’t know if this fits what is debatable here, but know if it would have to incorporate the Cheat Engine itself in a program?

  • A Cheat engine is a form of hacking :). I don’t know how the CE works internally, but I imagine that they are configured for specific programs so that they know how the program is compiled, and can change the binary code according to the Cheats that he wants to apply - and not accessing the variables of the program not dynamically modified.

3

As has already been said, it is a bit complex (apologies carlosfigueira, but you exaggerated a little is not so difficult so, if he has ever worked with C, must understand well how works the allocation system and data structure in memory, of course assuming that he really learned well C, and not only 'hello world') more impossible...

If it is in windows OS you can study the DLL 'kernel32.dll', from windows itself, which deals with this subject, more specifically the following functions : 'Openprocess', 'Readprocessmemory' and 'Writeprocessmemory';

You can also search for a ready-made library with these abstracted functions for the language you are working on.

  • Thank you for the @Diogo Rosa de Matos reply, I am entering a new project in the company I work in now, and we are working with reverse engineering for a new program. THIS will be used for safety testing systems! Good... my point eh.. please.. we need answers.. on how to solve the above problem. NEVER negative or negative You shouldn’t do that because blablabla The use and legal care is on the account of the user... neh? GOOD on this... not giving the answer to a "possible" hacker of all I would be without the information. Thank you again!!

  • I didn’t say it was impossible, just not trivial. Read the memory of a program is not difficult (see the kernel functions mentioned by @Diogo). But the question was about accessing specific variables - This implies that you need to know how the program’s memory is structured at a certain point. This is the part that is not trivial (memory can be moved, for example via Garbage Collection, among other things).

0

I’m looking for the same answer and that’s the closest I’ve come so far:

getenv char * getenv (const char * name); Get chain of environment Recovers a C string containing the value of the environment variable whose name is specified as argument. If the requested variable is not part of the environment list, the function will return a null pointer.

The returned pointer points to a block of internal memory whose content or validity can be changed by other getenv (but not by other library functions).

The string pointed by the pointer returned by this function should not be modified by the program. Some library systems and implementations may allow changing environmental variables with specific functions (putenv, setenv ...), but this functionality is not portable.

http://www.cplusplus.com/reference/cstdlib/getenv/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.