-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem-sniff.c
118 lines (98 loc) · 2.68 KB
/
mem-sniff.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MMAP 1
#define HEAP 2
#define STACK 3
struct position {
size_t start;
size_t end;
};
struct position parseMaps(char* pid, int type);
int main(int argc, char* argv[]){
FILE *mem;
char path[255], buff[256];
int count=0;
size_t startPos=0x7fa97b122000, endPos=0x7fa97b922000;
struct position pos;
pos=parseMaps(argv[1],atoi(argv[2]));
sprintf(path,"/proc/%s/mem",argv[1]);
mem=fopen(path,"r");
if(mem==NULL){
printf("Error: Unable do load file %s\n",path);
return 1;
}
fseek(mem,pos.start,SEEK_SET);
while(pos.start<pos.end){
fread(buff,1,1,mem);
printf("%s",buff);
pos.start+=1;
count++;
}
printf("\n");
fclose(mem);
}
struct position parseMaps(char* pid, int type){
char path[255], buff[512];
FILE *maps;
sprintf(path,"/proc/%s/maps",pid);
size_t *start, *end;
char *perms,*offset,*dev,*inode,*pathname;
struct position pos;
pos.start = 0;
pos.end = 0;
perms=malloc(5);
offset=malloc(9);
dev=malloc(6);
inode=malloc(8);
pathname=malloc(256);
start=malloc(sizeof(size_t));
end=malloc(sizeof(size_t));
maps=fopen(path,"r");
while(fgets(buff,512,maps)!=NULL){
sscanf(buff,"%lx-%lx %s %s %s %s %s",start,end,perms,offset,dev,inode,pathname);
switch(type){
case MMAP:
if(strcmp(pathname,"")==0){
printf("Private segment found: %lx-%lx \n",*start,*end);
pos.start=*start;
pos.end=*end;
free(perms);
free(offset);
free(dev);
free(inode);
free(pathname);
return pos;
};
break;
case HEAP:
if(strcmp(pathname,"[heap]")==0){
printf("Heap segment found: %lx-%lx \n",*start,*end);
pos.start=*start;
pos.end=*end;
free(perms);
free(offset);
free(dev);
free(inode);
free(pathname);
return pos;
};
break;
case STACK:
if(strcmp(pathname,"[stack]")==0){
printf("Stack segment found: %lx-%lx \n",*start,*end);
pos.start=*start;
pos.end=*end;
free(perms);
free(offset);
free(dev);
free(inode);
free(pathname);
return pos;
};
break;
}
strcpy(pathname,"");
}
return pos;
}