Advertisement
atomen

The code...

May 4th, 2012
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #include <sys/mman.h>
  6. #include <unistd.h>
  7.  
  8. typedef unsigned char byte;
  9.  /*
  10. bool ProtectMemory(void * addr, int flags)
  11. {
  12.         // Constant holding the page size value
  13.         const size_t pageSize = sysconf(_SC_PAGE_SIZE);
  14.        
  15.         // Calculate relative page offset
  16.         size_t temp = (size_t) addr;
  17.         temp -= temp % pageSize;
  18.        
  19.         // Update address
  20.         addr = (void*) temp;
  21.        
  22.         // Update memory area protection
  23.         return !mprotect(addr, pageSize, flags);
  24. }
  25.  */
  26. typedef int (*testFunc)(int, int);
  27.  
  28. int main(void)
  29. {
  30.     char func[] = {0x55, 0x89, 0xe5, 0x8b, 0x45, 0x0c, 0x8b, 0x55, 0x08, 0x01, 0xd0, 0x5d, 0xc3 };
  31.  
  32.     /*testFunc func_copy = (testFunc) malloc(sizeof(func));
  33.     memcpy((void*) func_copy, (void*) func, sizeof(func));
  34.  
  35.     ProtectMemory((void*)func_copy, PROT_WRITE | PROT_READ | PROT_EXEC);
  36.  
  37.     printf("1 + 2 = %d\n", func_copy(1,2)); // segfaults here
  38.  
  39.     free(func_copy);
  40.     return 0;*/
  41.  
  42.     int (* func_copy)(int,int) = mmap(NULL, sizeof(func),
  43.         PROT_WRITE | PROT_READ | PROT_EXEC,
  44.         MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  45.  
  46.     printf("1 + 2 = %d\n", func_copy(1,2));
  47.  
  48.     munmap(func_copy, sizeof(func));
  49.     return EXIT_SUCCESS;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement