-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadmem.c
65 lines (52 loc) · 1.55 KB
/
readmem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ptrace.h>
#define PATHLEN 128
#define EXIT_FAILURE 1
int main(int argc, char **argv) {
if (argc < 4) {
fprintf(stderr,"%s [start] [end] [pid]\n",argv[0]);
exit(EXIT_FAILURE);
}
char * endptr;
unsigned long int base = strtol(argv[1],NULL,16);
unsigned long int fin = strtol(argv[2],NULL,16);
int pid = atoi(argv[3]);
char * mempath, * buffer ;
mempath = calloc(1,PATHLEN);
buffer = calloc(1,fin-base);
snprintf(mempath,PATHLEN-1,"/proc/%d/mem",pid);
int memfd = open(mempath,O_RDONLY);
if (memfd <= 0){
fprintf(stderr,"Error opening %s file\nerrno : %d\n",mempath,errno);
exit(EXIT_FAILURE);
}
char * filepath = calloc(PATHLEN,1);
snprintf(filepath,PATHLEN-1,"dump-from-%d-%d.txt",base,fin);
FILE * file = fopen(filepath,"wb+");
if (file == NULL) {
fprintf(stderr,"Error opening output file.\nerrno : %d\n",errno);
exit(EXIT_FAILURE);
}
if (ptrace(PTRACE_ATTACH,(pid_t)pid,NULL,NULL) == -EXIT_FAILURE) {
fprintf(stderr,"error PTRACE_ATTACH.\nerrno : %d\n",errno);
exit(EXIT_FAILURE);
}
fprintf(stdout,"Extracting %s from : %ld to :%ld\n",mempath,(long int) base,(long int) fin);
waitpid(pid,NULL,0);
lseek(memfd,base,SEEK_SET);
read(memfd,buffer,fin-base);
fwrite(buffer,fin-base,1,file);
fprintf(stdout,"Dump extracted to %s\n",filepath);
ptrace(PTRACE_DETACH,pid,NULL,NULL);
free(mempath);
free(buffer);
free(filepath);
close(memfd);
}