forked from korczis/foremost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·341 lines (271 loc) · 6.75 KB
/
main.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
/* FOREMOST
*
* By Jesse Kornblum and Kris Kendall
*
* This is a work of the US Government. In accordance with 17 USC 105,
* copyright protection is not available for any work of the US Government.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*
*/
#include "main.h"
#ifdef __WIN32
/* Allows us to open standard input in binary mode by default
See http://gnuwin32.sourceforge.net/compile.html for more */
int _CRT_fmode = _O_BINARY;
#endif
void catch_alarm(int signum)
{
signal_caught = signum;
signal(signum, catch_alarm);
}
void register_signal_handler(void)
{
signal_caught = 0;
if (signal(SIGINT, catch_alarm) == SIG_IGN)
signal(SIGINT, SIG_IGN);
if (signal(SIGTERM, catch_alarm) == SIG_IGN)
signal(SIGTERM, SIG_IGN);
#ifndef __WIN32
/* Note: I haven't found a way to get notified of
console resize events in Win32. Right now the statusbar
will be too long or too short if the user decides to resize
their console window while foremost runs.. */
/* RBF - Handle TTY events */
// The function setttywidth is in the old helpers.c
// signal(SIGWINCH, setttywidth);
#endif
}
void try_msg(void)
{
fprintf(stderr, "Try `%s -h` for more information.%s", __progname, NEWLINE);
}
/* The usage function should, at most, display 22 lines of text to fit
on a single screen */
void usage(void)
{
fprintf(stderr, "%s version %s by %s.%s", __progname, VERSION, AUTHOR, NEWLINE);
fprintf(stderr,
"%s %s [-v|-V|-h|-T|-Q|-q|-a|-w-d] [-t <type>] [-s <blocks>] [-k <size>] \n\t[-b <size>] [-c <file>] [-o <dir>] [-i <file] %s%s",
CMD_PROMPT,
__progname,
NEWLINE,
NEWLINE);
fprintf(stderr, "-V - display copyright information and exit%s", NEWLINE);
fprintf(stderr, "-t - specify file type. (-t jpeg,pdf ...) %s", NEWLINE);
fprintf(stderr, "-d - turn on indirect block detection (for UNIX file-systems) %s", NEWLINE);
fprintf(stderr, "-i - specify input file (default is stdin) %s", NEWLINE);
fprintf(stderr,
"-a - Write all headers, perform no error detection (corrupted files) %s",
NEWLINE);
fprintf(stderr,
"-w - Only write the audit file, do not write any detected files to the disk %s",
NEWLINE);
fprintf(stderr,
"-o - set output directory (defaults to %s)%s",
DEFAULT_OUTPUT_DIRECTORY,
NEWLINE);
fprintf(stderr,
"-c - set configuration file to use (defaults to %s)%s",
DEFAULT_CONFIG_FILE,
NEWLINE);
fprintf(stderr,
"-q - enables quick mode. Search are performed on 512 byte boundaries.%s",
NEWLINE);
fprintf(stderr, "-Q - enables quiet mode. Suppress output messages. %s", NEWLINE);
/* RBF - What should verbose mode be? */
fprintf(stderr, "-v - verbose mode. Logs all messages to screen%s", NEWLINE);
}
void process_command_line(int argc, char **argv, f_state *s)
{
int i;
char *ptr1, *ptr2;
while ((i = getopt(argc, argv, "o:b:c:t:s:i:k:hqmQTadvVw")) != -1)
{
switch (i)
{
case 'v':
set_mode(s, mode_verbose);
break;
case 'd':
set_mode(s, mode_ind_blk);
break;
case 'w':
set_mode(s, mode_write_audit); /*Only write audit*/
break;
case 'a':
set_mode(s, mode_write_all); /*Write all headers*/
break;
case 'b':
set_block(s, atoi(optarg));
break;
case 'o':
set_output_directory(s, optarg);
break;
case 'q':
set_mode(s, mode_quick);
break;
case 'Q':
set_mode(s, mode_quiet);
break;
case 'c':
set_config_file(s, optarg);
break;
case 'm':
set_mode(s, mode_multi_file);
case 'k':
set_chunk(s, atoi(optarg));
break;
case 's':
set_skip(s, atoi(optarg));
break;
case 'i':
set_input_file(s, optarg);
break;
case 'T':
s->time_stamp = TRUE;
break;
case 't':
/*See if we have multiple file types to define*/
ptr1 = ptr2 = optarg;
while (1)
{
if (!*ptr2)
{
if (!set_search_def(s, ptr1, 0))
{
usage();
exit(EXIT_SUCCESS);
}
break;
}
if (*ptr2 == ',')
{
*ptr2 = '\0';
if (!set_search_def(s, ptr1, 0))
{
usage();
exit(EXIT_SUCCESS);
}
*ptr2++ = ',';
ptr1 = ptr2;
}
else
{
ptr2++;
}
}
break;
case 'h':
usage();
exit(EXIT_SUCCESS);
case 'V':
printf("%s%s", VERSION, NEWLINE);
/* We could just say printf(COPYRIGHT), but that's a good way
to introduce a format string vulnerability. Better to always
use good programming practice... */
printf("%s", COPYRIGHT);
exit(EXIT_SUCCESS);
default:
try_msg();
exit(EXIT_FAILURE);
}
}
#ifdef __DEBUG
dump_state(s);
#endif
}
int main(int argc, char **argv)
{
FILE *testFile = NULL;
f_state *s = (f_state *)malloc(sizeof(f_state));
int input_files = 0;
char **temp = argv;
DIR* dir;
#ifndef __GLIBC__
__progname = basename(argv[0]);
#endif
/*Initialize the global state struct*/
if (initialize_state(s, argc, argv))
fatal_error(s, "Unable to initialize state");
register_signal_handler();
process_command_line(argc, argv, s);
load_config_file(s);
if (s->num_builtin == 0)
{
/*Nothing specified via the command line or the conf
file so default to all builtin search types*/
set_search_def(s, "all", 0);
}
if (create_output_directory(s))
fatal_error(s, "Unable to open output directory");
if (!get_mode(s, mode_write_audit))
{
create_sub_dirs(s);
}
if (open_audit_file(s))
fatal_error(s, "Can't open audit file");
/* Scan for valid files to open */
while (*argv != NULL)
{
if(strcmp(*argv,"-c")==0)
{
/*jump past the conf file so we don't process it.*/
argv+=2;
}
testFile = fopen(*argv, "rb");
if (testFile)
{
fclose(testFile);
dir = opendir(*argv);
if(!strstr(s->config_file,*argv)!=0 && !dir)
{
input_files++;
}
if(dir) closedir(dir);
}
++argv;
}
argv = temp;
if (input_files > 1)
{
set_mode(s, mode_multi_file);
}
++argv;
while (*argv != NULL)
{
testFile = fopen(*argv, "rb");
if (testFile)
{
fclose(testFile);
dir = opendir(*argv);
if(!strstr(s->config_file,*argv)!=0 && !dir)
{
set_input_file(s, *argv);
process_file(s);
}
if(dir) closedir(dir);
}
++argv;
}
if (input_files == 0)
{
//printf("using stdin\n");
process_stdin(s);
}
print_stats(s);
/*Lets try to clean up some of the extra sub_dirs*/
cleanup_output(s);
if (close_audit_file(s))
{
/* Hells bells. This is bad, but really, what can we do about it?
Let's just report the error and try to get out of here! */
print_error(s, AUDIT_FILE_NAME, "Error closing audit file");
}
free_state(s);
free(s);
return EXIT_SUCCESS;
}