Skip to content

Instantly share code, notes, and snippets.

@jakobrs
Created May 9, 2021 12:01
Show Gist options
  • Save jakobrs/2060c1ed6026f4882705f49d8114d649 to your computer and use it in GitHub Desktop.
Save jakobrs/2060c1ed6026f4882705f49d8114d649 to your computer and use it in GitHub Desktop.
#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