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
|
/*
** ifind (inode find)
** The Sleuth Kit
**
** Given an image and block number, identify which inode it is used by
**
** Brian Carrier [carrier <at> sleuthkit [dot] org]
** Copyright (c) 2006-2011 Brian Carrier, Basis Technology. All Rights reserved
** Copyright (c) 2003-2005 Brian Carrier. All rights reserved
**
** TASK
** Copyright (c) 2002 Brian Carrier, @stake Inc. All rights reserved
**
** TCTUTILs
** Copyright (c) 2001 Brian Carrier. All rights reserved
**
**
** This software is distributed under the Common Public License 1.0
**
*/
/**
* \file ifind_lib.c
* Contains the library API functions used by the TSK ifind command
* line tool.
*/
#include "tsk_fs_i.h"
#include "tsk_hfs.h"
/*******************************************************************************
* Find an unallocated NTFS MFT entry based on its parent directory
*/
typedef struct {
TSK_INUM_T parinode;
TSK_FS_IFIND_FLAG_ENUM flags;
uint8_t found;
} IFIND_PAR_DATA;
/* inode walk call back for tsk_fs_ifind_par to find unallocated files
* based on parent directory
*/
static TSK_WALK_RET_ENUM
ifind_par_act(TSK_FS_FILE * fs_file, void *ptr)
{
IFIND_PAR_DATA *data = (IFIND_PAR_DATA *) ptr;
TSK_FS_META_NAME_LIST *fs_name_list;
/* go through each file name attribute for this file */
fs_name_list = fs_file->meta->name2;
while (fs_name_list) {
/* we found a file that has the target parent directory.
* Make a FS_NAME structure and print it. */
if (fs_name_list->par_inode == data->parinode) {
int i, cnt;
uint8_t printed;
TSK_FS_NAME *fs_name;
if ((fs_name = tsk_fs_name_alloc(256, 0)) == NULL)
return TSK_WALK_ERROR;
/* Fill in the basics of the fs_name entry
* so we can print in the fls formats */
fs_name->meta_addr = fs_file->meta->addr;
fs_name->flags = TSK_FS_NAME_FLAG_UNALLOC;
strncpy(fs_name->name, fs_name_list->name, fs_name->name_size);
// now look for the $Data and $IDXROOT attributes
fs_file->name = fs_name;
printed = 0;
// cycle through the attributes
cnt = tsk_fs_file_attr_getsize(fs_file);
for (i = 0; i < cnt; i++) {
const TSK_FS_ATTR *fs_attr =
tsk_fs_file_attr_get_idx(fs_file, i);
if (!fs_attr)
continue;
if ((fs_attr->type == TSK_FS_ATTR_TYPE_NTFS_DATA)
|| (fs_attr->type == TSK_FS_ATTR_TYPE_NTFS_IDXROOT)) {
if (data->flags & TSK_FS_IFIND_PAR_LONG) {
tsk_fs_name_print_long(stdout, fs_file, NULL,
fs_file->fs_info, fs_attr, 0, 0);
tsk_printf("\n");
}
else {
tsk_fs_name_print(stdout, fs_file, NULL,
fs_file->fs_info, fs_attr, 0);
tsk_printf("\n");
}
printed = 1;
}
}
// if there were no attributes, print what we got
if (printed == 0) {
if (data->flags & TSK_FS_IFIND_PAR_LONG) {
tsk_fs_name_print_long(stdout, fs_file, NULL,
fs_file->fs_info, NULL, 0, 0);
tsk_printf("\n");
}
else {
tsk_fs_name_print(stdout, fs_file, NULL,
fs_file->fs_info, NULL, 0);
tsk_printf("\n");
}
}
tsk_fs_name_free(fs_name);
data->found = 1;
}
fs_name_list = fs_name_list->next;
}
return TSK_WALK_CONT;
}
/**
* Searches for unallocated MFT entries that have a given
* MFT entry as their parent directory (as reported in FILE_NAME).
* @param fs File system to search
* @param lclflags Flags
* @param par Parent directory MFT entry address
* @returns 1 on error and 0 on success
*/
uint8_t
tsk_fs_ifind_par(TSK_FS_INFO * fs, TSK_FS_IFIND_FLAG_ENUM lclflags,
TSK_INUM_T par)
{
IFIND_PAR_DATA data;
data.found = 0;
data.flags = lclflags;
data.parinode = par;
/* Walk unallocated MFT entries */
if (fs->inode_walk(fs, fs->first_inum, fs->last_inum,
TSK_FS_META_FLAG_UNALLOC, ifind_par_act, &data)) {
return 1;
}
return 0;
}
/**
* \ingroup fslib
*
* Find the meta data address for a given file name (UTF-8).
* The basic idea of the function is to break the given name into its
* subdirectories and start looking for each (starting in the root
* directory).
*
* @param a_fs FS to analyze
* @param a_path UTF-8 path of file to search for
* @param [out] a_result Meta data address of file
* @param [out] a_fs_name Copy of name details (or NULL if details not wanted)
* @returns -1 on (system) error, 0 if found, and 1 if not found
*/
int8_t
tsk_fs_path2inum(TSK_FS_INFO * a_fs, const char *a_path,
TSK_INUM_T * a_result, TSK_FS_NAME * a_fs_name)
{
char *cpath;
size_t clen;
char *cur_dir; // The "current" directory or file we are looking for
char *cur_attr; // The "current" attribute of the dir we are looking for
TSK_INUM_T next_meta;
uint8_t is_done;
char *strtok_last;
*a_result = 0;
// copy path to a buffer that we can modify
clen = strlen(a_path) + 1;
if ((cpath = (char *) tsk_malloc(clen)) == NULL) {
return -1;
}
strncpy(cpath, a_path, clen);
// Get the first part of the directory path.
cur_dir = (char *) strtok_r(cpath, "/", &strtok_last);
cur_attr = NULL;
/* If there is no token, then only a '/' was given */
if (cur_dir == NULL) {
free(cpath);
*a_result = a_fs->root_inum;
// create the dummy entry if needed
if (a_fs_name) {
a_fs_name->meta_addr = a_fs->root_inum;
// Note that we are not filling in meta_seq -- we could, we just aren't
a_fs_name->type = TSK_FS_NAME_TYPE_DIR;
a_fs_name->flags = TSK_FS_NAME_FLAG_ALLOC;
if (a_fs_name->name)
a_fs_name->name[0] = '\0';
if (a_fs_name->shrt_name)
a_fs_name->shrt_name[0] = '\0';
}
return 0;
}
/* If this is NTFS, separate out the attribute of the current directory */
if (TSK_FS_TYPE_ISNTFS(a_fs->ftype)
&& ((cur_attr = strchr(cur_dir, ':')) != NULL)) {
*(cur_attr) = '\0';
cur_attr++;
}
if (tsk_verbose)
tsk_fprintf(stderr, "Looking for %s\n", cur_dir);
// initialize the first place to look, the root dir
next_meta = a_fs->root_inum;
// we loop until we know the outcome and then exit.
// everything should return from inside the loop.
is_done = 0;
while (is_done == 0) {
size_t i;
TSK_FS_FILE *fs_file_alloc = NULL; // set to the allocated file that is our target
TSK_FS_FILE *fs_file_del = NULL; // set to an unallocated file that matches our criteria
TSK_FS_DIR *fs_dir = NULL;
// open the next directory in the recursion
if ((fs_dir = tsk_fs_dir_open_meta(a_fs, next_meta)) == NULL) {
free(cpath);
return -1;
}
/* Verify this is indeed a directory. We had one reported
* problem where a file was a disk image and opening it as
* a directory found the directory entries inside of the file
* and this caused problems... */
if ( !TSK_FS_IS_DIR_META(fs_dir->fs_file->meta->type)) {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_GENFS);
tsk_error_set_errstr("Address %" PRIuINUM
" is not for a directory\n", next_meta);
free(cpath);
return -1;
}
// cycle through each entry
for (i = 0; i < tsk_fs_dir_getsize(fs_dir); i++) {
TSK_FS_FILE *fs_file;
uint8_t found_name = 0;
if ((fs_file = tsk_fs_dir_get(fs_dir, i)) == NULL) {
tsk_fs_dir_close(fs_dir);
free(cpath);
return -1;
}
/*
* Check if this is the name that we are currently looking for,
* as identified in 'cur_dir'
*/
if ((fs_file->name->name)
&& (a_fs->name_cmp(a_fs, fs_file->name->name,
cur_dir) == 0)) {
found_name = 1;
}
else if ((fs_file->name->shrt_name)
&& (a_fs->name_cmp(a_fs, fs_file->name->shrt_name,
cur_dir) == 0)) {
found_name = 1;
}
/* For NTFS, we have to check the attribute name. */
if ((found_name == 1) && (TSK_FS_TYPE_ISNTFS(a_fs->ftype))) {
/* ensure we have the right attribute name */
if (cur_attr != NULL) {
found_name = 0;
if (fs_file->meta) {
int cnt, i;
// cycle through the attributes
cnt = tsk_fs_file_attr_getsize(fs_file);
for (i = 0; i < cnt; i++) {
const TSK_FS_ATTR *fs_attr =
tsk_fs_file_attr_get_idx(fs_file, i);
if (!fs_attr)
continue;
if ((fs_attr->name)
&& (a_fs->name_cmp(a_fs, fs_attr->name,
cur_attr) == 0)) {
found_name = 1;
break;
}
}
}
}
}
if (found_name) {
/* If we found our file and it is allocated, then stop. If
* it is unallocated, keep on going to see if we can get
* an allocated hit */
if (fs_file->name->flags & TSK_FS_NAME_FLAG_ALLOC) {
fs_file_alloc = fs_file;
break;
}
else {
// if we already have an unalloc and its addr is 0, then use the new one
if ((fs_file_del)
&& (fs_file_del->name->meta_addr == 0)) {
tsk_fs_file_close(fs_file_del);
}
fs_file_del = fs_file;
}
}
// close the file if we did not save it for future analysis.
else {
tsk_fs_file_close(fs_file);
fs_file = NULL;
}
}
// we found a directory, go into it
if ((fs_file_alloc) || (fs_file_del)) {
const char *pname;
TSK_FS_FILE *fs_file_tmp;
// choose the alloc one first (if they both exist)
if (fs_file_alloc)
fs_file_tmp = fs_file_alloc;
else
fs_file_tmp = fs_file_del;
pname = cur_dir; // save a copy of the current name pointer
// advance to the next name
cur_dir = (char *) strtok_r(NULL, "/", &(strtok_last));
cur_attr = NULL;
if (tsk_verbose)
tsk_fprintf(stderr,
"Found it (%s), now looking for %s\n", pname, cur_dir);
/* That was the last name in the path -- we found the file! */
if (cur_dir == NULL) {
*a_result = fs_file_tmp->name->meta_addr;
// make a copy if one was requested
if (a_fs_name) {
tsk_fs_name_copy(a_fs_name, fs_file_tmp->name);
}
if (fs_file_alloc)
tsk_fs_file_close(fs_file_alloc);
if (fs_file_del)
tsk_fs_file_close(fs_file_del);
tsk_fs_dir_close(fs_dir);
free(cpath);
return 0;
}
// update the attribute field, if needed
if (TSK_FS_TYPE_ISNTFS(a_fs->ftype)
&& ((cur_attr = strchr(cur_dir, ':')) != NULL)) {
*(cur_attr) = '\0';
cur_attr++;
}
// update the value for the next directory to open
next_meta = fs_file_tmp->name->meta_addr;
if (fs_file_alloc) {
tsk_fs_file_close(fs_file_alloc);
fs_file_alloc = NULL;
}
if (fs_file_del) {
tsk_fs_file_close(fs_file_del);
fs_file_del = NULL;
}
}
// no hit in directory
else {
is_done = 1;
}
tsk_fs_dir_close(fs_dir);
fs_dir = NULL;
}
free(cpath);
return 1;
}
/**
* Find the meta data address for a given file TCHAR name
*
* @param fs FS to analyze
* @param tpath Path of file to search for
* @param [out] result Meta data address of file
* @returns -1 on error, 0 if found, and 1 if not found
*/
int8_t
tsk_fs_ifind_path(TSK_FS_INFO * fs, TSK_TCHAR * tpath, TSK_INUM_T * result)
{
#ifdef TSK_WIN32
// Convert the UTF-16 path to UTF-8
{
size_t clen;
UTF8 *ptr8;
UTF16 *ptr16;
int retval;
char *cpath;
clen = TSTRLEN(tpath) * 4;
if ((cpath = (char *) tsk_malloc(clen)) == NULL) {
return -1;
}
ptr8 = (UTF8 *) cpath;
ptr16 = (UTF16 *) tpath;
retval =
tsk_UTF16toUTF8_lclorder((const UTF16 **) &ptr16, (UTF16 *)
& ptr16[TSTRLEN(tpath) + 1], &ptr8,
(UTF8 *) ((uintptr_t) ptr8 + clen), TSKlenientConversion);
if (retval != TSKconversionOK) {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_UNICODE);
tsk_error_set_errstr
("tsk_fs_ifind_path: Error converting path to UTF-8: %d",
retval);
free(cpath);
return -1;
}
return tsk_fs_path2inum(fs, cpath, result, NULL);
}
#else
return tsk_fs_path2inum(fs, (const char *) tpath, result, NULL);
#endif
}
/*******************************************************************************
* Find an inode given a data unit
*/
typedef struct {
TSK_DADDR_T block; /* the block to find */
TSK_FS_IFIND_FLAG_ENUM flags;
uint8_t found;
TSK_INUM_T curinode; /* the inode being analyzed */
uint32_t curtype; /* the type currently being analyzed: NTFS */
uint16_t curid;
} IFIND_DATA_DATA;
/*
* file_walk action for non-ntfs
*/
static TSK_WALK_RET_ENUM
ifind_data_file_act(TSK_FS_FILE * fs_file, TSK_OFF_T a_off,
TSK_DADDR_T addr, char *buf, size_t size, TSK_FS_BLOCK_FLAG_ENUM flags,
void *ptr)
{
TSK_FS_INFO *fs = fs_file->fs_info;
IFIND_DATA_DATA *data = (IFIND_DATA_DATA *) ptr;
/* Ignore sparse blocks because they do not reside on disk */
if (flags & TSK_FS_BLOCK_FLAG_SPARSE)
return TSK_WALK_CONT;
if (addr == data->block) {
if (TSK_FS_TYPE_ISNTFS(fs->ftype))
tsk_printf("%" PRIuINUM "-%" PRIu32 "-%" PRIu16 "\n",
data->curinode, data->curtype, data->curid);
else
tsk_printf("%" PRIuINUM "\n", data->curinode);
data->found = 1;
return TSK_WALK_STOP;
}
return TSK_WALK_CONT;
}
/*
** find_inode
**
** Callback action for inode_walk
*/
static TSK_WALK_RET_ENUM
ifind_data_act(TSK_FS_FILE * fs_file, void *ptr)
{
IFIND_DATA_DATA *data = (IFIND_DATA_DATA *) ptr;
int file_flags =
(TSK_FS_FILE_WALK_FLAG_AONLY | TSK_FS_FILE_WALK_FLAG_SLACK);
int i, cnt;
data->curinode = fs_file->meta->addr;
/* Search all attributes */
cnt = tsk_fs_file_attr_getsize(fs_file);
for (i = 0; i < cnt; i++) {
const TSK_FS_ATTR *fs_attr = tsk_fs_file_attr_get_idx(fs_file, i);
if (!fs_attr)
continue;
data->curtype = fs_attr->type;
data->curid = fs_attr->id;
if (fs_attr->flags & TSK_FS_ATTR_NONRES) {
if (tsk_fs_attr_walk(fs_attr,
file_flags, ifind_data_file_act, ptr)) {
if (tsk_verbose)
tsk_fprintf(stderr,
"Error walking file %" PRIuINUM
" Attribute: %i", fs_file->meta->addr, i);
/* Ignore these errors */
tsk_error_reset();
}
// stop if we only want one hit
if ((data->found) && (!(data->flags & TSK_FS_IFIND_ALL)))
break;
}
}
if ((data->found) && (!(data->flags & TSK_FS_IFIND_ALL)))
return TSK_WALK_STOP;
else
return TSK_WALK_CONT;
}
/*
* Find the inode that has allocated block blk
* Return 1 on error, 0 if no error */
uint8_t
tsk_fs_ifind_data(TSK_FS_INFO * fs, TSK_FS_IFIND_FLAG_ENUM lclflags,
TSK_DADDR_T blk)
{
IFIND_DATA_DATA data;
memset(&data, 0, sizeof(IFIND_DATA_DATA));
data.flags = lclflags;
data.block = blk;
if (fs->inode_walk(fs, fs->first_inum, fs->last_inum,
TSK_FS_META_FLAG_ALLOC | TSK_FS_META_FLAG_UNALLOC,
ifind_data_act, &data)) {
return 1;
}
/* If we did not find an inode yet, get the block's
* flags so we can identify it as a meta data block */
if (!data.found) {
TSK_FS_BLOCK *fs_block;
if ((fs_block = tsk_fs_block_get(fs, NULL, blk)) != NULL) {
if (fs_block->flags & TSK_FS_BLOCK_FLAG_META) {
tsk_printf("Meta Data\n");
data.found = 1;
}
tsk_fs_block_free(fs_block);
}
}
if (!data.found) {
tsk_printf("Inode not found\n");
}
return 0;
}
|