Skip to content

Instantly share code, notes, and snippets.

@antekone
Created March 27, 2017 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antekone/fec31143d791a4f4ee09930f9f07bd20 to your computer and use it in GitHub Desktop.
Save antekone/fec31143d791a4f4ee09930f9f07bd20 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int global_var = 0;
int main() {
int fd = open("/proc/self/mem", O_RDWR);
if(!fd) {
printf("can't open /proc/self/mem as O_RDWR\n");
return 1;
}
printf("- opened /proc/self/mem as rdwr\n");
int* addr = &global_var;
printf("- global var address: %p\n", addr);
int buf;
lseek(fd, (off_t) addr, SEEK_SET);
read(fd, &buf, 4);
printf("- contents of global_var: 0x%08x (read through /proc/self/mem)\n", buf);
buf = 0x12345;
printf("- saving 0x%08lx to global_var through /proc/self/mem...\n", buf);
lseek(fd, (off_t) addr, SEEK_SET);
write(fd, &buf, 4);
printf("- contents of global_var: 0x%08x (read directly)\n", global_var);
return 0;
}
@antekone
Copy link
Author

antekone commented Mar 27, 2017

antek@antekpc ~/dev/tests/proc_mem $ uname -a
Linux antekpc 4.8.13-1-ARCH #1 SMP PREEMPT Fri Dec 9 07:24:34 CET 2016 x86_64 GNU/Linux

antek@antekpc ~/dev/tests/proc_mem $ gcc test.c -o test && ./test
- opened /proc/self/mem as rdwr
- global var address: 0x60105c
- contents of global_var: 0x00000000 (read through /proc/self/mem)
- saving 0x00012345 to global_var through /proc/self/mem...
- contents of global_var: 0x00012345 (read directly)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment