-
Notifications
You must be signed in to change notification settings - Fork 14
/
memfetch.c
372 lines (304 loc) · 8.97 KB
/
memfetch.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
memfetch 0.05b - Linux on-demand process image dump
---------------------------------------------------
Copyright (C) 2002, 2003 by Michal Zalewski <lcamtuf@coredump.cx>
Licensed under terms and conditions of GNU Public License version 2.
This is a simple code, but there are some tricks. First of all,
mmaping /proc/pid/mem isn't as straightforward as it could seem. We
have to 'touch' each page to ensure its physical allocation before
being able to mmap it.
Why some dumbass removed the ability to mmap() /proc/pid/mem in 2.4?!
It should be easy to port this to *BSD.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ptrace.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
#include <signal.h>
#include <time.h>
#include <sys/mman.h>
#include <linux/a.out.h>
//#include <asm/page.h>
#include <getopt.h>
#include <errno.h>
// Servicable parts:
#define MAINFILE "mfetch.lst"
#define MAPPREFIX "map-"
#define MEMPREFIX "mem-"
#define BINSUFFIX ".bin"
#define MAXBUF 1024
// End of servicable parts.
#define debug(x...) fprintf(stderr,x)
#define fatal(x...) { \
fprintf(stderr,"[-] ERROR: " x); \
if (outfile && !textout) { \
fprintf(outfile,"** An error occured while generating this file.\n"); \
fprintf(outfile,"** Error message: " x); \
fclose(outfile); \
} \
if (tracepid>0) ptrace(PTRACE_DETACH,tracepid,NULL,(void*)lastsig); \
exit(1); \
}
static FILE* outfile;
static int tracepid;
static int lastsig;
static char textout;
static char avoid_mmap;
void exitsig(int x) {
static int sig_reentry;
if (sig_reentry) exit(1);
sig_reentry=1;
fatal("Whoops. Exiting on signal %d.\n",x);
}
void usage(char* myname) {
debug("Usage: %s [ -sawm ] [ -S xxx ] PID\n"
" -s - wait for fault signal before generating a dump\n"
" -a - skip non-anonymous maps (libraries etc)\n"
" -w - write index file to stdout instead of mfetch.lst\n"
" -m - avoid mmap(), helps prevent hanging on some 2.2 boxes\n"
" -S xxx - dump segment containing address xxx (hex) only\n",myname);
exit(3);
}
int wait_sig(void)
{
int st;
while (1) {
ptrace(PTRACE_CONT,tracepid,0,(void*)lastsig);
if (wait(&st)<=0) {
debug("[-] Process gone before receiving a fault signal.\n");
exit(2);
}
if (WIFEXITED(st)) {
debug("[-] Process exited with code %d before receiving a fault "
"signal.\n",WEXITSTATUS(st));
exit(2);
}
if (WIFSIGNALED(st)) {
debug("[-] Process killed with signal %d before receiving a fault "
"signal.\n",WTERMSIG(st));
exit(2);
}
if (!WIFSTOPPED(st))
fatal("Suddenly, the process disappears! You die!\n");
lastsig=WSTOPSIG(st);
debug("[+] Process received sig(%d)\n", lastsig);
if (lastsig==SIGTRAP || lastsig==SIGSTOP) lastsig=0;
switch (lastsig) {
case SIGSEGV:
debug("[+] Process received SIGSEGV, let's have a look...\n");
return 0;
case SIGILL:
debug("[+] Process received SIGILL, let's have a look...\n");
return 0;
case SIGPIPE:
debug("[+] Process received SIGPIPE, let's have a look...\n");
return 0;
case SIGFPE:
debug("[+] Process received SIGFPE, let's have a look...\n");
return 0;
case SIGBUS:
debug("[+] Process received SIGBUS, let's have a look...\n");
return 0;
}
}
return 0;
}
int page_size;
int dump_memory(int memfile, int offset, int len, int dumpfile)
{
int i = 0;
int count = len/page_size;
int* writeptr = malloc(page_size);
if (lseek(memfile, offset, SEEK_SET)==offset)
{
//debug("count=%d\n", count);
for (i=0; i<count; i++) {
if (read(memfile, writeptr, page_size) != page_size) {
break;
}
if (write(dumpfile, writeptr, page_size) != page_size) {
break;
}
}
}
if (i==count)
debug("[N] ");
else {
debug("[S] ");
lseek(dumpfile, 0, SEEK_SET);
for (i=0; i<count; i++) {
int j=0;
for (j=0; j<page_size/4; j++) {
writeptr[j]=ptrace(PTRACE_PEEKDATA, tracepid, (void*)(offset+i*page_size+j*4), 0);
}
if (write(dumpfile, writeptr, page_size) != page_size) {
break;
}
}
}
free(writeptr);
return (i==count)?0:1;
}
int main(int argc,char* argv[]) {
FILE* mapfile;
static char exename[MAXBUF+1];
static char tmp[MAXBUF+1];
char waitsig=0,skipmap=0;
int st,memfile,dumpcnt=0;
unsigned int onlyseg=0;
int opt;
debug("memfetch 0.05b by Michal Zalewski <lcamtuf@coredump.cx>\n");
signal(SIGINT,exitsig);
signal(SIGQUIT,exitsig);
signal(SIGHUP,exitsig);
signal(SIGPIPE,exitsig);
signal(SIGTERM,exitsig);
signal(SIGSEGV,exitsig);
signal(SIGBUS,exitsig);
signal(SIGILL,exitsig);
if (argc<2) usage(argv[0]);
while ((opt=getopt(argc,(void*)argv, "+samwS:h"))!=-1) {
switch(opt) {
case 's': waitsig=1; break;
case 'a': skipmap=1; break;
case 'w': textout=1; break;
case 'm': avoid_mmap=1; break;
case 'S':
if (sscanf(optarg,"%x",&onlyseg)!=1)
fatal("Incorrect -S syntax (hex address expected).\n");
break;
default: usage(argv[0]); break;
}
}
if (skipmap && onlyseg) fatal("Options -S and -e are mutually exclusive.\n");
if (argc-optind!=1) usage(argv[0]);
tracepid=atoi(argv[optind]);
if (kill(tracepid,0))
fatal("Process does not exist or is not accessible.\n");
if (ptrace(PTRACE_ATTACH,tracepid,0,0))
fatal("Cannot attach to this process (already traced?).\n");
if ( wait(&st)<=0 || !WIFSTOPPED(st) ) {
if (errno==ECHILD && !kill(tracepid,0)) {
if (waitsig) {
fatal("This is a thread, -s is not compatible with threds.\n");
}
debug("[!] This process is likely a thread (use -m if your box hangs).\n");
} else
fatal("Process gone during attach (magic).\n");
}
debug("[+] Attached to PID %d",tracepid);
sprintf(tmp,"/proc/%d/exe",tracepid);
if (readlink(tmp,exename,MAXBUF-1)<=0) strcpy(exename,"<unknown>");
debug(" (%s).\n",exename);
if (waitsig) {
debug("[*] Waiting for fault signal (SIGSEGV, SIGBUS, SIGILL, SIGPIPE "
"or SIGFPE)...\n");
wait_sig();
}
leavewait: // GOTOs for president!
page_size = getpagesize();
printf("PAGE_SIZE is %d\n", page_size);
sprintf(tmp,"/proc/%d/maps",tracepid);
mapfile=fopen(tmp,"r");
if (!mapfile)
fatal("Cannot open %s for reading.\n",tmp);
if (textout) {
outfile=stdout;
debug("[*] Writing master information to standard output.\n\n");
setbuffer(stdout,0,0);
} else {
int tmpfd;
unlink(MAINFILE);
tmpfd = open(MAINFILE, O_WRONLY|O_TRUNC|O_CREAT|O_EXCL, 0600);
if (tmpfd<0)
fatal("Cannot open output file " MAINFILE ".\n");
debug("[*] Writing master information to " MAINFILE "...\n");
outfile = fdopen(tmpfd,"w");
if (!outfile)
fatal("Stange error during fdopen().\n");
}
st=time(0);
if (!textout)
fprintf(outfile,"# This memory data dump generated by memfetch by "
"<lcamtuf@coredump.cx>\n"
"# PID %d, declared executable: %s\n"
"# Date: %s\n\n",tracepid,exename,ctime((void*)&st));
sprintf(tmp, "/proc/%d/mem", tracepid);
memfile = open(tmp, O_RDONLY);
if (memfile<0)
fatal("Cannot open %s for reading.\n",tmp);
while (fgets(tmp,1024,mapfile)) {
int dumpfile;
char small[256];
unsigned int st,en,len,i;
char* filepath;
int* writeptr;
if (sscanf(tmp,"%x-%x", &st, &en)!=2) {
debug("[!] Parse error in /proc/%d/maps (mockery?): %s", tracepid, tmp);
continue;
}
len = en - st;
if ((filepath=strchr(tmp,'/'))) {
*(filepath-1) = 0;
sprintf(small, MAPPREFIX "%03d" BINSUFFIX, dumpcnt);
} else {
if (strchr(tmp,'\n'))
*strchr(tmp,'\n')=0;
sprintf(small, MEMPREFIX "%03d" BINSUFFIX, dumpcnt);
}
if (onlyseg && (onlyseg<st || onlyseg>en)) {
if (!textout)
debug(" Skipping %s at 0x%08x (%d bytes).\n",filepath?"map":"mem",
st,len);
continue;
}
if (filepath && skipmap) {
if (!textout)
debug(" Skipping map at 0x%08x (%d bytes).\n",st,len);
continue;
}
fprintf(outfile,"[%03d] %s:\n"
" Memory range 0x%08x to 0x%08x (%d bytes)\n",
dumpcnt,small,st,en,len);
if (filepath)
fprintf(outfile," MAPPED FROM: %s",filepath);
fprintf(outfile," %s\n\n",tmp);
unlink(small);
dumpfile=open(small,O_WRONLY|O_TRUNC|O_CREAT|O_EXCL,0600);
if (dumpfile<0)
fatal("Cannot open output file %s.\n",small);
if (!textout)
debug(" Writing %s at 0x%08x (%d bytes)... ",filepath?"map":"mem",st,len);
if (avoid_mmap)
writeptr = MAP_FAILED;
else {
for (i=st; i<en; i+=page_size) {
ptrace(PTRACE_PEEKDATA, tracepid, (void*)i, 0);
}
writeptr = mmap(0, len, PROT_READ, MAP_PRIVATE, memfile, st);
}
if (writeptr==MAP_FAILED) {
if (dump_memory(memfile, st, len, dumpfile) != 0)
fatal("Short write to %s.\n", small);
} else {
if (write(dumpfile,writeptr,len)!=len)
fatal("Short write to %s.\n", small);
munmap(writeptr, len);
}
dumpcnt++;
if (!textout)
debug("done (%s)\n",small);
close(dumpfile);
}
if (!dumpcnt)
fatal("No matching entries found in /proc/%d/maps.\n",tracepid);
if (!textout)
fprintf(outfile,"# End of file.\n");
debug("[*] Done (%d matching). Have a nice day.\n", dumpcnt);
fclose(outfile);
ptrace(PTRACE_DETACH,tracepid,0,(void*)lastsig);
exit(0);
}