Last active
March 9, 2022 19:19
-
-
Save mr1337357/ffb73b5a1f00856b2891 to your computer and use it in GitHub Desktop.
peek and poke for linux
This file contains 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 <stdio.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <sys/mman.h> | |
int main(int argc,char **argv) { | |
int devmem = -1; | |
unsigned int addr = 0; | |
unsigned int val = 0; | |
unsigned char *buffer; | |
if(argc<2) { | |
printf("Usage: %s addr\n",argv[0]); | |
return -1; | |
} | |
devmem = open("/dev/mem",O_RDWR); | |
if(devmem<0) { | |
printf("couldn't open /dev/mem. Are you root?\n"); | |
return -1; | |
} | |
sscanf(argv[1],"%x",&addr); | |
buffer = mmap(0,65536,PROT_WRITE|PROT_READ,MAP_SHARED,devmem,addr&0xFFFF0000); | |
if(!buffer) { | |
close(devmem); | |
return -1; | |
} | |
buffer += (addr&0x0000FFFF); | |
val = *(unsigned int *)buffer; | |
printf("%x\n",val); | |
munmap(buffer&0xFFFF0000,1); | |
close(devmem); | |
return 0; | |
} |
This file contains 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 <stdio.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <sys/mman.h> | |
int main(int argc,char **argv) { | |
int devmem = -1; | |
unsigned int addr = 0; | |
unsigned int val = 0; | |
unsigned char *buffer; | |
if(argc<3) { | |
printf("Usage: %s addr val\n",argv[0]); | |
return -1; | |
} | |
devmem = open("/dev/mem",O_RDWR); | |
if(devmem<0) { | |
printf("couldn't open /dev/mem. Are you root?\n"); | |
return -1; | |
} | |
sscanf(argv[1],"%x",&addr); | |
sscanf(argv[2],"%x",&val); | |
buffer = mmap(0,65536,PROT_WRITE|PROT_READ,MAP_SHARED,devmem,addr&0xFFFF0000); | |
if(!buffer) { | |
close(devmem); | |
return -1; | |
} | |
buffer+=(addr&0x0000FFFF); | |
*(unsigned int *)buffer = val; | |
munmap(buffer&0xFFFF0000,1); | |
close(devmem); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment