Thread: Read an arbitrary memory location?

  1. #1
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355

    Read an arbitrary memory location?

    The problem:
    I am trying to create a small memory dumper.

    Sample code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/mman.h>
    
    int main()
    {
            int *x;
            int err;
    
            x = (int *)0xFF;
            err = mprotect(x, 256, PROT_READ);
            if(err)
            {
                    printf("mprotect failed. Memory can not be read.\n");
                    exit(1);
            }
            printf("%p[%x]\n", x, *x);
    }
    I though the call to mprotect should succeed when the program is run with super user permissions, but it doesn't.
    Any ideas?
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    mprotect() can't be called on a region that was not obtained via mmap(). At least on Linux. Also, you passed a count of 256, which is not a multiple of the page size, so it could never possibly work. Also, 0xFF is not a multiple of 4096, so again, could never possibly work. Also, you know about virtual memory, right? 0xFF is not a physical address and won't be interpreted as one.

    Besides, 0xFF is in the null page, so you will never, ever be able to access or map that page.

    To look inside physical memory, just open /dev/mem and start looking.

  3. #3
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    That's what i was thinking atm. Just pondered if there was a way through system calls.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by xuftugulus View Post
    That's what i was thinking atm. Just pondered if there was a way through system calls.
    No, /dev/mem is the only way to look at physical memory.

  5. #5
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Code:
    #include <stdio.h>
    
    int main()
    {
            int x;
            FILE *m;
    
            m = fopen("/dev/mem", "r");
            if(m == NULL)
            {
                    printf("Could not open memory!\n");
                    return 1;
            }
            fseek(m, SEEK_SET, 0xFF);
            fread(&x, 1, sizeof(x), m);
            printf("&#37;x: %x\n", 0xFF, x);
            fclose(m);
    }
    This much simpler version works. Thanks!
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  2. Pointing to arbitrary location
    By elmutt in forum C Programming
    Replies: 2
    Last Post: 02-28-2008, 06:30 AM
  3. "sorting news" assignment
    By prljavibluzer in forum C Programming
    Replies: 7
    Last Post: 02-06-2008, 06:45 AM
  4. allocate apecified memory location for a c variable
    By BharathKumar in forum Linux Programming
    Replies: 5
    Last Post: 06-01-2007, 03:47 PM
  5. Can you read BIOS info from memory? 0xffff:0x0005
    By zahid in forum C Programming
    Replies: 5
    Last Post: 03-18-2003, 05:35 AM