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
|
/*
* The Sleuth Kit
*
* Brian Carrier [carrier <at> sleuthkit [dot] org]
* Copyright (c) 2003-2014 Brian Carrier. All rights reserved
*
*
* This software is distributed under the Common Public License 1.0
*
*/
#include "tsk_hashdb_i.h"
/**
* \file hashkeeper.c
* Contains functions to read and process hash keeper database files
*/
/**
* Test the file to see if it is a hashkeeper database
*
* @param hFile File handle to hash database
*
* @return 1 if hk and 0 if not
*/
uint8_t
hk_test(FILE * hFile)
{
char buf[TSK_HDB_MAXLEN];
int cnt = 0;
char *ptr;
fseek(hFile, 0, SEEK_SET);
// read in header line
if (NULL == fgets(buf, TSK_HDB_MAXLEN, hFile))
return 0;
if (strlen(buf) < 150)
return 0;
ptr = buf;
// "file_id","hashset_id","file_name","directory","hash","file_size","date_modified","time_modified","time_zone","comments","date_accessed","time_accessed"
if (strncmp(ptr, "\"file_id\"", strlen("\"file_id\"")) != 0)
return 0;
/* Cycle through the line looking at the fields in between the commas */
while (NULL != (ptr = strchr(ptr, ','))) {
cnt++;
if (cnt == 1) {
if (strncmp(ptr, ",\"hashset_id\"", strlen(",\"hashset_id\"")) != 0)
return 0;
}
else if (cnt == 2) {
if (strncmp(ptr, ",\"file_name\"", strlen(",\"file_name\"")) != 0)
return 0;
}
else if (cnt == 3) {
if (strncmp(ptr, ",\"directory\"", strlen(",\"directory\"")) != 0)
return 0;
}
else if (cnt == 4) {
if (strncmp(ptr, ",\"hash\"", strlen(",\"hash\"")) != 0)
return 0;
}
else {
break;
}
ptr++;
}
return 1;
}
TSK_HDB_INFO *hk_open(FILE *hDb, const TSK_TCHAR *db_path)
{
TSK_HDB_BINSRCH_INFO *hdb_binsrch_info = NULL;
// get the basic binary-search info struct
hdb_binsrch_info = hdb_binsrch_open(hDb, db_path);
if (NULL == hdb_binsrch_info) {
return NULL;
}
// overwrite the database-specific ones
hdb_binsrch_info->base.db_type = TSK_HDB_DBTYPE_HK_ID;
hdb_binsrch_info->base.make_index = hk_makeindex;
hdb_binsrch_info->get_entry = hk_getentry;
return (TSK_HDB_INFO*)hdb_binsrch_info;
}
/**
* Give a line from a hash keeper database, parse out the
* MD5 (and other) text. NOTE that this will add NULL values
* to the input text.
*
* @param str [in] String to parse
* @param md5 [out] Pointer to a pointer, which will be assigned to the MD5 text in original string
* @param name [in] Poiner to buffer where name can be copied into (can be NULL)
* @param n_len [in] Length of name buffer
* @param other [in] Pointer to buffer where extended data should be copied to (can be NULL)
* @param o_len [in] Length of other buffer
*/
static int
hk_parse_md5(char *str, char **md5, char *name, int n_len,
char *other, int o_len)
{
char *ptr = str;
char *file = NULL, *dir = NULL, *file_id = NULL, *hash_id = NULL;
int cnt = 0;
if ((str == NULL) || (strlen(str) < TSK_HDB_HTYPE_MD5_LEN))
return 1;
if ((md5 == NULL) && (name == NULL) && (other == NULL))
return 0;
/*
* 0 file_id
* 1 hashset_id
* 2 file_name
* 3 directory
* 4 hash
* 5 file_size
* 6 date_modified
* 7 time modified
* 8 time_zone
* 9 comments
* 10 date_accessed
* 11 time_accessed
*/
/* Assign the file_id if we are looking for it */
if (other != NULL) {
file_id = ptr;
}
while (NULL != (ptr = strchr(ptr, ','))) {
cnt++;
/* End of file_id, begin hash_id */
if ((cnt == 1) && (other != NULL)) {
*ptr = '\0';
ptr++;
hash_id = ptr;
}
/* End of hash_id, begin name */
else if (cnt == 2) {
/* Finish the 'other' stuff */
if (other != NULL) {
*ptr = '\0';
snprintf(other, o_len, "Hash ID: %s File ID: %s",
hash_id, file_id);
}
/* Are we done? */
if ((name == NULL) && (md5 == NULL))
return 0;
/* get the pointer to the name */
if (name) {
if (ptr[1] != '"')
return 1;
file = &ptr[2];
/* We utilize the other loop code to find the end of
* the name */
}
}
/* end of the name, begin directory - which may not exist */
else if ((cnt == 3) && (name != NULL)) {
/* finish up the name */
if (ptr[-1] != '"')
return 1;
ptr[-1] = '\0';
/* get the directory start, if it exists */
if (ptr[1] == '"') {
dir = &ptr[2];
}
else {
dir = NULL;
}
}
/* end of directory, begin MD5 value */
else if (cnt == 4) {
/* Copy the name into the buffer */
if (name != NULL) {
name[0] = '\0';
if (dir) {
/* finish up the dir */
if (ptr[-1] != '"')
return 1;
ptr[-1] = '\0';
strncpy(name, dir, n_len);
strncat(name, "\\", n_len);
}
if (file) {
strncat(name, file, n_len);
}
else {
return 1;
}
}
if (md5 == NULL)
return 0;
/* Do a length check and more sanity checks */
if ((strlen(ptr) < 2 + TSK_HDB_HTYPE_MD5_LEN)
|| (ptr[1] != '"')
|| (ptr[2 + TSK_HDB_HTYPE_MD5_LEN] != '"')) {
return 1;
}
ptr = &ptr[2];
ptr[TSK_HDB_HTYPE_MD5_LEN] = '\0';
*md5 = ptr;
/* Final sanity check */
if (NULL != strchr(ptr, ',')) {
return 1;
}
return 0;
}
/* If the next field is in quotes then we need to skip to the
* next quote and ignore any ',' in there
*/
if (ptr[1] == '"') {
if (NULL == (ptr = strchr(&ptr[2], '"'))) {
return 1;
}
}
else {
ptr++;
}
}
return 1;
}
/**
* Process the database to create a sorted index of it. Consecutive
* entries with the same hash value are not added to the index, but
* will be found during lookup.
*
* @param hdb_info_base Hash database to make index of
* @param dbtype Text of database type (should always be TSK_HDB_DBTYPE_HK_STR)
*
* @return 1 on error and 0 on success.
*/
uint8_t
hk_makeindex(TSK_HDB_INFO * hdb_info_base, TSK_TCHAR * dbtype)
{
TSK_HDB_BINSRCH_INFO *hdb_binsrch_info = (TSK_HDB_BINSRCH_INFO*)hdb_info_base;
int i;
size_t len = 0;
char buf[TSK_HDB_MAXLEN];
char *hash = NULL, phash[TSK_HDB_HTYPE_MD5_LEN + 1];
TSK_OFF_T offset = 0;
int db_cnt = 0, idx_cnt = 0, ig_cnt = 0;
if (hdb_binsrch_idx_initialize(hdb_binsrch_info, dbtype)) {
tsk_error_set_errstr2( "hk_makeindex");
return 1;
}
/* Status */
if (tsk_verbose)
TFPRINTF(stderr, _TSK_T("Extracting Data from Database (%s)\n"),
hdb_binsrch_info->base.db_fname);
/* Allocate a buffer to hold the previous hash values */
memset(phash, '0', TSK_HDB_HTYPE_MD5_LEN + 1);
/* read each line of the file */
fseek(hdb_binsrch_info->hDb, 0, SEEK_SET);
for (i = 0; NULL != fgets(buf, TSK_HDB_MAXLEN, hdb_binsrch_info->hDb);
offset += (TSK_OFF_T) len, i++) {
// skip the header line
if (i == 0) {
ig_cnt++;
continue;
}
len = strlen(buf);
/* Parse each line to get the MD5 value */
if (hk_parse_md5(buf, &hash, NULL, 0, NULL, 0)) {
ig_cnt++;
continue;
}
db_cnt++;
/* If this entry is for the same hash value as the last entry,
* the skip it -- we'll look for it during lookup */
if (memcmp(hash, phash, TSK_HDB_HTYPE_MD5_LEN) == 0) {
continue;
}
/* Add the entry to the index */
if (hdb_binsrch_idx_add_entry_str(hdb_binsrch_info, hash, offset)) {
tsk_error_set_errstr2( "hk_makeindex");
return 1;
}
idx_cnt++;
/* Set the previous hash value */
strncpy(phash, hash, TSK_HDB_HTYPE_MD5_LEN + 1);
}
if (idx_cnt > 0) {
if (tsk_verbose) {
fprintf(stderr, " Valid Database Entries: %d\n", db_cnt);
fprintf(stderr,
" Invalid Database Entries (headers or errors): %d\n",
ig_cnt);
fprintf(stderr, " Index File Entries %s: %d\n",
(idx_cnt == db_cnt) ? "" : "(optimized)", idx_cnt);
}
/* Finish the index making process */
if (hdb_binsrch_idx_finalize(hdb_binsrch_info)) {
tsk_error_set_errstr2( "hk_makeindex");
return 1;
}
}
else {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_HDB_CORRUPT);
tsk_error_set_errstr(
"hk_makeindex: No valid entries found in database");
return 1;
}
return 0;
}
/**
* Find the corresponding name at the
* given offset. The offset was likely determined from the index.
* The entries in the DB following the one specified are also processed
* if they have the same hash value and their name is different.
* The callback is called for each entry.
*
* Note: This routine assumes that &hdb_info->lock is locked by the caller.
*
* @param hdb_info Data base to get data from.
* @param hash MD5 hash value that was searched for
* @param offset Byte offset where hash value should be located in db_file
* @param flags
* @param action Callback used for each entry found in lookup
* @param cb_ptr Pointer to data passed to callback
*
* @return 1 on error and 0 on success
*/
uint8_t
hk_getentry(TSK_HDB_INFO * hdb_info, const char *hash, TSK_OFF_T offset,
TSK_HDB_FLAG_ENUM flags,
TSK_HDB_LOOKUP_FN action, void *cb_ptr)
{
TSK_HDB_BINSRCH_INFO *hdb_binsrch_info = (TSK_HDB_BINSRCH_INFO*)hdb_info;
char buf[TSK_HDB_MAXLEN], name[TSK_HDB_MAXLEN], *ptr =
NULL, pname[TSK_HDB_MAXLEN], other[TSK_HDB_MAXLEN];
int found = 0;
if (tsk_verbose)
fprintf(stderr,
"hk_getentry: Lookup up hash %s at offset %" PRIdOFF "\n",
hash, offset);
if (strlen(hash) != TSK_HDB_HTYPE_MD5_LEN) {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_HDB_ARG);
tsk_error_set_errstr(
"hk_getentry: Invalid hash value: %s", hash);
return 1;
}
memset(pname, '0', TSK_HDB_MAXLEN);
/* Loop so that we can find multiple occurrences of the same hash */
while (1) {
size_t len;
if (0 != fseeko(hdb_binsrch_info->hDb, offset, SEEK_SET)) {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_HDB_READDB);
tsk_error_set_errstr(
"hk_getentry: Error seeking to get file name: %lu",
(unsigned long) offset);
return 1;
}
if (NULL ==
fgets(buf, TSK_HDB_MAXLEN, hdb_binsrch_info->hDb)) {
if (feof(hdb_binsrch_info->hDb)) {
break;
}
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_HDB_READDB);
tsk_error_set_errstr(
"hk_getentry: Error reading database");
return 1;
}
len = strlen(buf);
if (len < TSK_HDB_HTYPE_MD5_LEN) {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_HDB_CORRUPT);
tsk_error_set_errstr(
"hk_getentry: Invalid entry in database (too short): %s",
buf);
return 1;
}
if (hk_parse_md5(buf, &ptr, name, TSK_HDB_MAXLEN,
((flags & TSK_HDB_FLAG_EXT) ? other : NULL),
((flags & TSK_HDB_FLAG_EXT) ? TSK_HDB_MAXLEN :
0))) {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_HDB_CORRUPT);
tsk_error_set_errstr(
"hk_getentry: Invalid entry in database: %s", buf);
return 1;
}
/* Is this the one that we want? */
if (0 != strcasecmp(ptr, hash)) {
break;
}
if (strcmp(name, pname) != 0) {
int retval;
retval = action(hdb_info, hash, name, cb_ptr);
if (retval == TSK_WALK_ERROR) {
return 1;
}
else if (retval == TSK_WALK_STOP) {
return 0;
}
found = 1;
strncpy(pname, name, TSK_HDB_MAXLEN);
}
/* Advance to the next row */
offset += len;
}
if (found == 0) {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_HDB_ARG);
tsk_error_set_errstr(
"hk_getentry: Hash not found in file at offset: %lu",
(unsigned long) offset);
return 1;
}
return 0;
}
|