Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
- #include <sys/mman.h>
- #include <unistd.h>
- typedef unsigned char byte;
- /*
- bool ProtectMemory(void * addr, int flags)
- {
- // Constant holding the page size value
- const size_t pageSize = sysconf(_SC_PAGE_SIZE);
- // Calculate relative page offset
- size_t temp = (size_t) addr;
- temp -= temp % pageSize;
- // Update address
- addr = (void*) temp;
- // Update memory area protection
- return !mprotect(addr, pageSize, flags);
- }
- */
- typedef int (*testFunc)(int, int);
- int main(void)
- {
- char func[] = {0x55, 0x89, 0xe5, 0x8b, 0x45, 0x0c, 0x8b, 0x55, 0x08, 0x01, 0xd0, 0x5d, 0xc3 };
- /*testFunc func_copy = (testFunc) malloc(sizeof(func));
- memcpy((void*) func_copy, (void*) func, sizeof(func));
- ProtectMemory((void*)func_copy, PROT_WRITE | PROT_READ | PROT_EXEC);
- printf("1 + 2 = %d\n", func_copy(1,2)); // segfaults here
- free(func_copy);
- return 0;*/
- int (* func_copy)(int,int) = mmap(NULL, sizeof(func),
- PROT_WRITE | PROT_READ | PROT_EXEC,
- MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
- printf("1 + 2 = %d\n", func_copy(1,2));
- munmap(func_copy, sizeof(func));
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement