Created
November 1, 2017 23:31
-
-
Save amriunix/8aa9861a094740b40bf243e433aa6276 to your computer and use it in GitHub Desktop.
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 <stdint.h> | |
#include <unistd.h> | |
#include <sys/mman.h> | |
#include <fcntl.h> | |
#include <sys/stat.h> | |
#include <string.h> | |
int main(int argc, char *argv[]) | |
{ | |
char *content="**New content**"; | |
char buffer[30]; | |
struct stat st; | |
void *map; | |
// open the file in read only mode. | |
int f=open(argv[1], O_RDONLY); | |
fstat(f, &st); | |
// mapping the file into memory with read only flag because we use O_RDONLY! | |
// MAP_PRIVATE = Create a private copy-on-write mapping. | |
map=mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, f, 0); | |
// Since we using the read only flag let's bypass it and open the current process memory file in Read and Write | |
int fm=open("/proc/self/mem", O_RDWR); | |
// Write some new content to the memory, | |
// Starting at the 5th byte from the beginning. | |
lseek(fm, map + 5, SEEK_SET); | |
int result = write(fm, content, strlen(content)); | |
if (result == -1) | |
printf("write: failed.\n"); | |
else | |
printf("write: succeeded.\n"); | |
//Read the first 29 bytes from the memory | |
memcpy(buffer, map, 29); | |
buffer[29] = 0; | |
printf("read: %s\n", buffer); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment