Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Memory of other applications C++

I am thinking about a problem I have been having for some time now.. I would like to write a C/C++ program (under windows first) that can access(read/change values) the memory(stack, heap, everything) of other running programs. (Not like shared memory but any memory the computer has..) Without having to start the application from my own application.. I have seen something like this before but I just can't figure out how it's done.. If I were to access the memory of any running program I would get errors from the OS right? Any help is appreciated!

like image 733
slurp91 Avatar asked May 16 '11 10:05

slurp91


People also ask

Can you access memory in C?

Memory access syntax in C Because C is a system programming language, it must provide direct access to physical hardware, including memory. The above C code uses square brackets to access memory cell #248, counting from the beginning of memory. This code would compile just fine on PDP-7.

Can a program access another program's memory?

Unless the program is specifically built to be able to inject itself in another processes memory space (i.e. using specific Windows programming calls that need administrative access), a program cannot see another programs memory.

Can a process read another process memory?

Processes cannot access other processes' memory in principle. In practice the underlying operating system usually offers this mechanism to privileged processes.

How does C memory work?

In C, the library function malloc is used to allocate a block of memory on the heap. The program accesses this block of memory via a pointer that malloc returns. When the memory is no longer needed, the pointer is passed to free which deallocates the memory so that it can be used for other purposes.


1 Answers

As @sharptooth said, this requires support from the OS. Different OS does it differently. Since you are on Windows, there are a few steps you could follow:

  1. Call OpenProcess, or CreateProcess to access, or launch a new process. In this call, you must request PROCESS_VM_READ access.
  2. Call ReadProcessMemory to read a chunk of memory in that opened process.

If you want to change memory of another process, you then need PROCESS_VM_WRITE access and use WriteProcessMemory to achieve that.

In Linux, for example, you'd use ptrace to attach to a process and peek, poke its memory.

like image 93
Nam Nguyen Avatar answered Sep 28 '22 05:09

Nam Nguyen