#include #include #include #include #include #include #include void print_file(char *filename) { int fd = open(filename,O_RDONLY); if(fd == -1) { printf("Cannot open file %s...\n",filename); exit(1); } char buffer[4096]; int bytes; while((bytes = read(fd,buffer,4096)) > 0) { write(1,buffer,bytes); } } int main() { printf("Before we create a memory map, here's a view of this process's current memory map:\n"); print_file("/proc/self/maps"); int fd = open("/bin/bash",O_RDONLY); char *ptr = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); if(ptr == NULL) { printf("Cannot mmap!\n"); exit(1); } printf("Okay, we have mapped '/bin/bash' in, let's print its first few bytes:\n"); *(ptr+4) = 0; printf("\t Here's the elf: '%s'!\n",ptr+1); printf("\n=-=-=-=-=-=\n\n"); printf("Okay, we have created a memory map, here's the new view of this process's current memory map:\n"); print_file("/proc/self/maps"); }