forked from jessek/hashdeep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.cpp
622 lines (496 loc) · 18.2 KB
/
files.cpp
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/**
* MD5DEEP - files.cpp
*
* By Jesse Kornblum
* Substantially modified by Simson Garfinkel.
*
* 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.
*
* $Id$
*
* files.cpp was originally just part of the md5deep-family programs, not hashdeep.
* It reads hash database files and incorporates them into the in-memory database.
* Because it was designed for md5deep/sha1deep/etc, it would only look for a
* single hash per line.
*
* The original program had variables named h_ilook, h_ilook3 and h_ilook4.
* These variables were set to the variable number of the hash algorithm being looked for in each line.
*
*/
#include "main.h"
#include "common.h"
#ifndef HAVE_ISXDIGIT
bool isxdigit(char ch)
{
return isdigit(ch) || (ch >= 'a' && ch <= 'f') || (ch => 'A' && ch <= 'F');
}
#endif
/* ---------------------------------------------------------------------
How to add more file types that we can read known hashes from:
1. Add a definition of TYPE_[fileType] to main.h. Ex: for type "cows",
you would want to define TYPE_COWS.
2. If your filetype has a rigid header, you should define a HEADER
variable to make comparisons easier.
3. Add a check for your file type to the identify_hash_file_type() function.
4. Create a method to find a valid hash and filename in a line of
your file. You are encouraged to use find_plain_hash and
find_rigid_hash if possible!
5. Add this method to the find_hash_in_line() function.
6. Add a variable to the state in main.h to indicate if each
filetype supports this hash. The variable can be used to denote
a position if necessary. Look for h_[name] variables for examples.
7. Each hashing algorithm should set this variable in the
setup_hashing_algorithm function.
----------------------------------------------------------------------
*/
typedef struct _ENCASE_HASH_HEADER {
/* 000 */ char Signature[8];
/* 008 */ uint32_t Version;
/* 00c */ uint32_t Padding;
/* 010 */ uint32_t NumHashes;
} ENCASE_HASH_HEADER;
#define ENCASE_HEADER "HASH\x0d\x0a\xff\x00"
#define ILOOK_HEADER \
"V1Hash,HashType,SetDescription,FileName,FilePath,FileSize"
#define ILOOK3_HEADER \
"V3Hash,HashSHA1,FileName,FilePath,FileSize,HashSHA256,HashSHA384,HashSHA512"
#define ILOOK4_HEADER \
"V4Hash,HashSHA1,FileName,FilePath,FileSize,HashSHA256,HashSHA384,HashSHA512,CreateTime,ModTime,LastAccessTime"
#define NSRL_15_HEADER \
"\"SHA-1\",\"FileName\",\"FileSize\",\"ProductCode\",\"OpSystemCode\",\"MD4\",\"MD5\",\"CRC32\",\"SpecialCode\""
#define NSRL_20_HEADER \
"\"SHA-1\",\"MD5\",\"CRC32\",\"FileName\",\"FileSize\",\"ProductCode\",\"OpSystemCode\",\"SpecialCode\""
#define HASHKEEPER_HEADER \
"\"file_id\",\"hashset_id\",\"file_name\",\"directory\",\"hash\",\"file_size\",\"date_modified\",\"time_modified\",\"time_zone\",\"comments\",\"date_accessed\",\"time_accessed\""
#define HASH_STRING_LENGTH (hashes[opt_md5deep_mode_algorithm].bit_length/4)
/****************************************************************
*** Support Functions
****************************************************************/
// Find the index of the next comma in the string str starting at index start.
// quotes cause commas to be ingored until you are out of the quote.
// If there is no next comma, returns -1.
static int find_next_comma(char *str, unsigned int start)
{
assert(str);
size_t size = strlen(str);
unsigned int pos = start;
int in_quote = FALSE;
while (pos < size) {
switch (str[pos]) {
case '"':
in_quote = !in_quote;
break;
case ',':
if (in_quote) break;
// Although it's potentially unwise to cast an unsigned int back
// to an int, problems will only occur when the value is beyond
// the range of int. Because we're working with the index of a
// string that is probably less than 32,000 characters, we should
// be okay.
return (int)pos;
}
++pos;
}
return -1;
}
// Shift the contents of a string so that the values after 'new_start'
// will now begin at location 'start'
void shift_string(char *fn, size_t start, size_t new_start)
{
assert(fn!=0);
// TODO: Can shift_string be replaced with memmove?
if (start > strlen(fn) || new_start < start) return;
while (new_start < strlen(fn)) {
fn[start] = fn[new_start];
new_start++;
start++;
}
fn[start] = 0;
}
// Returns the string after the nth comma in the string str. If that
// string is quoted, the quotes are removed. If there is no valid
// string to be found, returns TRUE. Otherwise, returns FALSE
static int find_comma_separated_string(char *str, unsigned int n)
{
if (NULL == str) return TRUE;
int start = 0, end;
unsigned int count = 0;
while (count < n) {
if ((start = find_next_comma(str,start)) == -1) return TRUE;
++count;
// Advance the pointer past the current comma
++start;
}
// It's okay if there is no next comma, it just means that this is
// the last comma separated value in the string
if ((end = find_next_comma(str,start)) == -1)
end = strlen(str);
// Strip off the quotation marks, if necessary. We don't have to worry
// about uneven quotation marks (i.e quotes at the start but not the end
// as they are handled by the the find_next_comma function.
if (str[start] == '"')
++start;
if (str[end - 1] == '"')
end--;
str[end] = 0;
shift_string(str,0,start);
return FALSE;
}
/**
* looks for a valid hash in the provided buffer.
* returns TRUE if one is present.
* @param buf - input is the hash and the filename; the hash is left here.
* @param known_fn - the filename is copied there
*/
int state::find_plain_hash(char *buf, char *known_fn)
{
size_t p = HASH_STRING_LENGTH;
if ((strlen(buf) < HASH_STRING_LENGTH) || (buf[HASH_STRING_LENGTH] != ' '))
return FALSE;
if (known_fn != NULL) {
strncpy(known_fn,buf,PATH_MAX);
// Starting at the end of the hash, find the start of the filename
while(p < strlen(known_fn) && isspace(known_fn[p])){
++p;
}
shift_string(known_fn,0,p);
chop_line(known_fn);
}
buf[HASH_STRING_LENGTH] = 0;
/* We have to include a validity check here so that we don't
mistake SHA-1 hashes for MD5 hashes, among other things */
return algorithm_t::valid_hash(opt_md5deep_mode_algorithm,buf);
}
/**
* detect the md5deep_size_hash file type, which has a size, a hash, and a filename.
*/
int state::find_md5deep_size_hash(char *buf, char *known_fn)
{
size_t pos;
if (NULL == buf)
return FALSE;
// Extra 12 chars for size and space
if (strlen(buf) < HASH_STRING_LENGTH + 12)
return FALSE;
// Check for size. Spaces are legal here (e.g. " 20")
for (pos = 0 ; pos < 10 ; ++pos)
if (!(isdigit(buf[pos]) || 0x20 == buf[pos]))
return FALSE;
if (buf[10] != 0x20 && buf[11] != 0x20)
return FALSE;
shift_string(buf,0,12);;
return find_plain_hash(buf,known_fn);
}
/**
* Look for a hash in a bsd-style buffer.
* @param buf - input buffer; set to hash on output.
* @param fn - gets filename
*
* bsd hash lines look like this:
* MD5 (copying.txt) = 555b3e940c86b35d6e0c9976a05b3aa5
*/
int state::find_bsd_hash(char *buf, char *fn)
{
size_t buf_len = strlen(buf);
unsigned int hash_len = HASH_STRING_LENGTH;
assert(buf!=0);
if (buf_len < hash_len) return FALSE;
char *open = strchr(buf,'(');
char *close = strchr(buf,')');
char *equal = strchr(buf,'=');
if(open==0 || close==0 || equal==0) return FALSE; // not properly formatted
*close = '\000'; // termiante the string
strncpy(fn,open+1,PATH_MAX);
/* Scan past the equal sign for the beginning of the hash */
equal++;
while(*equal!='\000' && !isxdigit(*equal)) equal++;
/* Copy over buffer; */
char *dest = buf;
while(isxdigit(*equal)){
*dest = *equal;
dest++;
equal++;
}
*dest = 0; // terminate
return algorithm_t::valid_hex(buf);
}
/* This is a generic function to find the filename and hash from a rigid
(i.e. comma separated value) file format. Values may be quoted, but
the quotes are removed before values are returned. The location variables
refer to how many commas preceed the entry. For example, to get the
hash out of:
filename,junk,stuff,hash,stuff
you should call find_rigid_hash(buf,fn,1,4);
Note that columns start with #1, not zero.
*/
int state::find_rigid_hash(char *buf, char *fn, unsigned int fn_location, unsigned int hash_location)
{
char *temp = strdup(buf);
if (temp == NULL)
return FALSE;
if (find_comma_separated_string(temp,fn_location-1)) {
free(temp);
return FALSE;
}
strncpy(fn, temp, strlen(fn));
free(temp);
if (find_comma_separated_string(buf,hash_location-1)){
return FALSE;
}
return algorithm_t::valid_hash(opt_md5deep_mode_algorithm,buf);
}
#ifdef WORDS_BIGENDIAN
uint32_t byte_reverse(uint32_t n)
{
uint32_t res = 0, count, bytes[5];
for (count = 0 ; count < 4 ; count++)
{
bytes[count] = (n & (0xff << (count * 8))) >> (count * 8);
res |= bytes[count] << (24 - (count * 8));
}
return res;
}
#endif
/* iLook files have the MD5 hash as the first 32 characters.
As a result, we can just treat these files like plain hash files */
int state::find_ilook_hash(char *buf, char *known_fn)
{
return (find_plain_hash(buf,known_fn));
}
int state::check_for_encase(FILE *f,uint32_t *expected_hashes)
{
ENCASE_HASH_HEADER *h = (ENCASE_HASH_HEADER *)malloc(sizeof(ENCASE_HASH_HEADER));
if (NULL == h) ocb.fatal_error("Out of memory");
if (sizeof(ENCASE_HASH_HEADER) != fread(h,1,sizeof(ENCASE_HASH_HEADER),f)) {
free(h);
return FALSE;
}
if (memcmp(h->Signature,ENCASE_HEADER,8)) {
rewind(f);
free(h);
return FALSE;
}
#ifdef WORDS_BIGENDIAN
h->NumHashes = byte_reverse(h->NumHashes);
#endif
*expected_hashes = h->NumHashes;
return TRUE;
}
int state::identify_hash_file_type(FILE *f,uint32_t *expected_hashes)
{
char known_fn[PATH_MAX+1];
char buf[MAX_STRING_LENGTH + 1];
rewind(f);
/* The "rigid" file types all have their headers in the
* first line of the file. We check them first
*/
if (opt_md5deep_mode_algorithm == alg_md5 && check_for_encase(f,expected_hashes)) return TYPE_ENCASE;
if ((fgets(buf,MAX_STRING_LENGTH,f)) == NULL) {
return TYPE_UNKNOWN;
}
if (strlen(buf) > HASH_STRING_LENGTH) {
chop_line(buf);
/* Check for the algorithms that only have MD5 */
if (opt_md5deep_mode_algorithm == alg_md5){
if(STRINGS_EQUAL(buf,HASHKEEPER_HEADER)) return TYPE_HASHKEEPER;
if (STRINGS_EQUAL(buf,ILOOK_HEADER)) return TYPE_ILOOK;
}
/* Check for those that have md5 or sha1 */
if (opt_md5deep_mode_algorithm == alg_md5 || opt_md5deep_mode_algorithm == alg_sha1) {
if (STRINGS_EQUAL(buf,NSRL_15_HEADER)) return TYPE_NSRL_15;
if (STRINGS_EQUAL(buf,NSRL_20_HEADER)) return TYPE_NSRL_20;
}
/* Check for those that have md5 or sha1 or sha256 */
if (opt_md5deep_mode_algorithm == alg_md5 || opt_md5deep_mode_algorithm == alg_sha1 || opt_md5deep_mode_algorithm == alg_sha256) {
if (STRINGS_EQUAL(buf,ILOOK3_HEADER)) return TYPE_ILOOK3;
if (STRINGS_EQUAL(buf,ILOOK4_HEADER)) return TYPE_ILOOK3;
}
}
/* Plain files can have comments, so the first line(s) may not
* contain a valid hash. But if we should process this file
* if we can find even *one* valid hash
*/
do {
if (find_bsd_hash(buf,known_fn)) return TYPE_BSD;
if (find_md5deep_size_hash(buf,known_fn)) return TYPE_MD5DEEP_SIZE;
if (find_plain_hash(buf,known_fn)) return TYPE_PLAIN;
} while ((fgets(buf,MAX_STRING_LENGTH,f)) != NULL);
return TYPE_UNKNOWN;
}
/**
* Given an input string buf and the type of file it came from, finds
* the hash specified in the line if there is one and returns TRUE.
* If there is no valid hash in the line, returns FALSE. All
* functions called from here are required to check that the hash is
* valid before returning!
*/
int state::find_hash_in_line(char *buf, int fileType, char *fn)
{
switch(fileType) {
case TYPE_PLAIN: return find_plain_hash(buf,fn);
case TYPE_BSD: return find_bsd_hash(buf,fn);
case TYPE_HASHKEEPER: return find_rigid_hash(buf,fn,3,h_hashkeeper);
case TYPE_NSRL_15:
if(opt_md5deep_mode_algorithm == alg_md5) return find_rigid_hash(buf,fn,2,7);
if(opt_md5deep_mode_algorithm == alg_sha1) return find_rigid_hash(buf,fn,2,1);
return FALSE; // NSRL_15 only hash md5 and sha1
case TYPE_NSRL_20:
if(opt_md5deep_mode_algorithm == alg_md5) return find_rigid_hash(buf,fn,4,2);
if(opt_md5deep_mode_algorithm == alg_sha1) return find_rigid_hash(buf,fn,4,1);
return FALSE;
case TYPE_ILOOK: return find_ilook_hash(buf,fn);
case TYPE_ILOOK3:
if(opt_md5deep_mode_algorithm == alg_md5) return find_rigid_hash(buf,fn,3,1);
if(opt_md5deep_mode_algorithm == alg_sha1) return find_rigid_hash(buf,fn,3,2);
if(opt_md5deep_mode_algorithm == alg_sha256) return find_rigid_hash(buf,fn,3,6);
return FALSE; // ilook3 only has md5, sha1 and sha256
case TYPE_ILOOK4:
if(opt_md5deep_mode_algorithm == alg_md5) return find_rigid_hash(buf,fn,3,1);
if(opt_md5deep_mode_algorithm == alg_sha1) return find_rigid_hash(buf,fn,3,2);
if(opt_md5deep_mode_algorithm == alg_sha256) return find_rigid_hash(buf,fn,3,6);
return FALSE; // ilook4 only has md5, sha1 and sha256
case TYPE_MD5DEEP_SIZE: return find_md5deep_size_hash(buf,fn);
}
return FALSE;
}
/**
* explicitly add a hash that will be matched or not matched.
* @param h - The hash (in hex)
* @param fn - The file name (although -a and -A actually provide the hash again)
*/
void state::md5deep_add_hash(char *h, char *fn)
{
class file_data_t *fdt = new file_data_t();
fdt->hash_hex[opt_md5deep_mode_algorithm] = h;
fdt->file_name = fn;
ocb.add_fdt(fdt);
}
/* from md5deep_match.cpp */
int input_not_matched = FALSE;
int file_type_without_header(int file_type)
{
return (file_type == TYPE_PLAIN ||
file_type == TYPE_MD5DEEP_SIZE ||
file_type == TYPE_BSD);
}
#define ENCASE_START_HASHES 0x480
#define hash_length md5deep_mode_hash_length
int state::parse_encase_file(const char *fn, FILE *handle,uint32_t expected_hashes)
{
unsigned char buffer[64];
char result[1024]; // must be at least s->hash_length*2
uint32_t count = 0;
// Each hash entry is 18 bytes. 16 bytes for the hash and
// two \0 characters at the end. We reserve 19 characters
// as fread will append an extra \0 to the string
if (fseeko(handle,ENCASE_START_HASHES,SEEK_SET)) {
ocb.error("%s: Unable to seek to start of hashes", fn);
return status_t::STATUS_USER_ERROR;
}
while (!feof(handle)){ //
if (18 != fread(buffer,sizeof(unsigned char),18,handle)) {
if (feof(handle))
continue;
// Users expect the line numbers to start at one, not zero.
if ((!ocb.opt_silent) || (mode_warn_only)) {
ocb.error("%s: No hash found in line %"PRIu32, fn, count + 1);
ocb.error("%s: %s", fn, strerror(errno));
return status_t::STATUS_USER_ERROR;
}
}
++count;
snprintf(result,sizeof(result),
"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
buffer[0],
buffer[1],
buffer[2],
buffer[3],
buffer[4],
buffer[5],
buffer[6],
buffer[7],
buffer[8],
buffer[9],
buffer[10],
buffer[11],
buffer[12],
buffer[13],
buffer[14],
buffer[15]);
class file_data_t *fdt = new file_data_t();
fdt->hash_hex[opt_md5deep_mode_algorithm] = result;
fdt->file_name = fn;
ocb.add_fdt(fdt);
}
if (expected_hashes != count){
ocb.error("%s: Expecting %"PRIu32" hashes, found %"PRIu32"\n",
fn, expected_hashes, count);
}
return status_t::status_ok;
}
/**
* Load an md5deep-style match file.
* Previously this returned FALSE if failure and TRUE if success.
* The return value was always ignored, so now we don't return anything.
*/
void state::md5deep_load_match_file(const char *fn)
{
uint64_t line_number = 0;
uint32_t expected_hashes=0;
FILE *f= fopen(fn,"rb");
if (f == NULL) {
ocb.error("%s: %s", fn,strerror(errno));
return;
}
int ftype = identify_hash_file_type(f,&expected_hashes);
if (ftype == TYPE_UNKNOWN) {
ocb.error("%s: Unable to find any hashes in file, skipped.", fn);
fclose(f); f = 0;
return;
}
if (TYPE_ENCASE == ftype) {
// We can't use the normal file reading code which is based on
// a one-line-at-a-time approach. Encase files are binary records
parse_encase_file(fn,f,expected_hashes);
fclose(f); f = 0;
return;
}
// We skip the first line in every file type except plain files.
// All other file types have a header line that we need to ignore.
if (file_type_without_header(ftype)){
rewind(f);
}
else {
++line_number;
}
char buf[MAX_STRING_LENGTH + 1];
while (fgets(buf,MAX_STRING_LENGTH,f)) {
char *cc;
char known_fn[PATH_MAX+1]; // set to be the filename from the buffer
if((cc=strchr(buf,'\n'))!=0) *cc = 0; // remove \n at end of line
if((cc=strchr(buf,'\r'))!=0) *cc = 0; // remove \r at end of line
++line_number;
memset(known_fn,0,PATH_MAX);
/* This looks odd. The function find_hash_in_line modifies 'buf' so that it
* begins with the hash, and copies the filename to known_fn.
*/
if (!find_hash_in_line(buf,ftype,known_fn)) {
if ((!ocb.opt_silent) || (mode_warn_only)) {
std::cerr << progname << ": " << fn << ": No hash found in line " << line_number << std::endl;
}
} else {
// Invalid hashes are caught above
file_data_t *fdt = new file_data_t();
fdt->hash_hex[opt_md5deep_mode_algorithm] = buf; // the hex hash
fdt->file_name = known_fn; // the filename
ocb.add_fdt(fdt);
}
}
fclose(f);
f = 0;
}