Skip to content

Instantly share code, notes, and snippets.

@wotchin
Created March 25, 2023 03:04
Show Gist options
  • Save wotchin/3887ec38866561291e44a5717c62887f to your computer and use it in GitHub Desktop.
Save wotchin/3887ec38866561291e44a5717c62887f to your computer and use it in GitHub Desktop.
A demo to show how to modify another process' memory
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <error.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc , char ** argv){
char buffer[50];
char * despath = (char *)malloc( sizeof(char)*( sizeof(argv[1]) + sizeof("/proc//mem")) );
sprintf(despath , "/proc/%s/mem",argv[1]);
printf("despath %s\n" , despath);
long long offset = 0x7ffd848e7040;
printf("offset %p\n" , offset);
int file = open(despath , O_RDWR );
if(file <= 0){
printf("fopen error %s\n",strerror(errno));
exit(0);
}
else
printf("fopen succuessful\n");
int err;
printf("lseek %d\n" ,file);
if(lseek(file , offset ,SEEK_SET) == -1){
printf("fseek error %s \n" ,strerror(errno));
}
int lenth;
if(( lenth = read(file , buffer , sizeof(buffer)) )> 0 )
printf("buffer %s \n" , buffer);
else
{
printf("lenth %d\n" , lenth);
}
lseek(file , offset ,SEEK_SET);
write(file , "you must be changed one more time\n" ,
sizeof("you must be changed one more time\n"));
close(file);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment