-
-
Save jakobrs/2060c1ed6026f4882705f49d8114d649 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/mman.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
/* | |
* Calls mprotect on the entire page that address is located in. | |
*/ | |
static int mprotect_page(void *address, int prot) { | |
int pagesize = getpagesize(); | |
address -= (long int)address % pagesize; | |
return mprotect((void *)address, pagesize, prot); | |
} | |
int main() { | |
/* "Hello world from 0x401000" */ | |
printf("Hello world from 0x%lx\n", (long int)&main); | |
/* "Page size: 0x1000" */ | |
printf("Page size: 0x%x\n", getpagesize()); | |
if (mprotect_page(&main, PROT_READ | PROT_WRITE | PROT_EXEC)) { | |
perror("error"); | |
} | |
/* 0x4010db is the address of the number 1 below */ | |
*((int *)0x4010db) = 42; | |
/* Ensures that the above instruction is run *before* 1 is moved into edx */ | |
asm(""); | |
/* "Some number: 42" */ | |
printf("some number: %d", 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment