Access to specific memory points

Asked

Viewed 111 times

7

How do I access an exact address in memory on Windows?

unsigned char * mem = {??};

2 answers

11


You cannot access a random address like this in most situations. Today there is protection for access to memory.

In some cases you will be able to access by doing:

#include <cstdint>

uintptr_t p = 0x0001FFFF;
int value = *reinterpret_cast<int *>(p);

Nothing guarantees that access will work as you expect. The result may be different depending on the situation.

I found this other reply from Guilherme Bernal that shows how to do something that works but again will not give consistent results:

#include <windows.h>
#include <stdio.h>

int main(void) {
    SYSTEM_INFO sysinfo;
    GetSystemInfo(&sysinfo);
    unsigned pageSize = sysinfo.dwPageSize;
    printf("page size: %d\n", pageSize);
    void* target = (void*)0x4e0f68;
    printf("trying to allocate exactly one page containing 0x%p...\n", target);
    void* ptr = VirtualAlloc(target, pageSize, MEM_COMMIT, PAGE_READWRITE);
    if (ptr) printf("got: 0x%p\n", ptr); //   ptr <= target < ptr+pageSize
    else printf("failed! OS wont let us use that address.\n");
}

I put in the Github for future reference.

Note that this situation is a little more controlled.

In C the void * means you’re using a pointer to anything.

  • Thanks brother, worked for what I wanted hahaha

5

Your operating system will not allow you to access memory that does not belong to your program.

#include <stdio.h>

int main(void) {
    unsigned char *mem = 0xdeadbeef; // ou = 3735928559;
    printf("O endereco %p tem %d\n", (void*)mem, *mem);
    return 0;
}
  • Allows yes, but my program needs to be in the same memory as the target program

Browser other questions tagged

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