3

I know how to work with pointers. But I don't know how do this:

I have an hex address that's of course it has a any value from any app.

I know to find the address that I want. I want to write a C Code to pass this address to a pointer variable, and then I could capture the value from that address and so on.

For example:

hex 0x00010010
int *pointer;

How can I pass this address to the pointer, what's the syntax for that?

unwind
  • 391,730
  • 64
  • 469
  • 606
Jacklyin
  • 41
  • 1
  • 1
  • 3

2 Answers2

7

By using int* you are assuming the data you are point to is an int (4 bytes, on most OS's).

Shouldn't we use a void* instead? Later, if you want to access it's data using a particular type, all you need to do is cast.

void *addr = (void*)0x0023FF74; // valid memory address within my app

printf("Address: %p\n", addr);
getchar();

printf("Data (4bytes): %d\n", *(int*)addr); // print the first 4 bytes of data as int
getchar();

EDIT:

Help me understand what you are trying to do, because this statement confused me:

There is app using that address, I know the value in it, but the (int)pointer don't access the value.

Are you trying to write an application that will modify the memory of another application that is running your system? Your current approach won't work, and this is why: When the Operating System loads your application into a process, it reserves a memory region to be used by the process and assigns a range of virtual addresses to this region. These virtual addresses do not map directly to memory addresses in RAM, so the OS has to keep an internal table for that.

On Windows, each process loaded receives the same range of virtual addresses, but this area is only visible to the process that is running inside it. For instance, (on Windows) processes are loaded in memory address 0x00400000, which mean each process has it's own memory address 0x00400000, and therefore you can't assign X memory address to a pointer in your application and expect Windows to magically know that you are reffering to address X that is inside another application.

What you are trying to accomplish it's called Code Injection, and there's a lot of information on the web about it.

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • Now I changed to void. He passed the hex address to the pointer variable. The first print confirmed it. He show the pointer address correctly. The problem is that the value can't be accessed. There is app using that address, I know the value in it, but the *(int*)pointer don't access the value. – Jacklyin Apr 28 '11 at 14:13
  • @Jacklyin Update my answer. It's import to us that you review all the questions and accept the one that best answered your question. There's a little check box near every answer, click on it to select the official answer to your question. – karlphillip Apr 28 '11 at 14:39
  • Ow.. thank you very much. I go googler about Code Injection. And sorry for wrong edits, my first time here. Thank you all. ByeBye – Jacklyin Apr 28 '11 at 14:43
4

In typical modern operating systems, you cannot access another process' (app's) address space. The addresses are virtual, so the same numerical value means different things to different processes.

If you are on a system with a true "flat" address space (where processes directly work with actual physical memory addresses) such as Amiga OS on the 680x0, you can do something like:

int *pointer = (int *) 0x00010010;
unwind
  • 391,730
  • 64
  • 469
  • 606