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 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
|
// Scalpel Copyright (C) 2005-6 by Golden G. Richard III.
// Written by Golden G. Richard III.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// 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. See the GNU
// General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
// 02110-1301, USA.
// Scalpel is a complete rewrite of the foremost 0.69 file carver to
// increase speed and support execution on machines with minimal
// resources (e.g., < 256MB RAM).
//
// Thanks to Kris Kendall, Jesse Kornblum, et al for their work on
// foremost. Additional thanks to Vassil Roussev and Vico Marziale, of the
// Department of Computer Science at the University of New Orleans, for
// ongoing support of Scalpel.
//
// Scalpel currently uses the SAME configuration file format as
// foremost 0.69. This may change in the future, because of some
// planned new features.
//
#include "scalpel.h"
void usage() {
printf("Carves files from a disk image based on file headers and footers.\n");
printf("\nUsage: scalpel [-b] [-c <config file>] [-d] [-h|V] [-i <file>]\n");
printf(" [-m blocksize] [-n] [-o <outputdir>] [-O num] [-q clustersize]\n");
printf(" [-r] [-s num] [-t <blockmap file>] [-u] [-v]\n");
printf(" <imgfile> [<imgfile>] ...\n\n");
printf("-b Carve files even if defined footers aren't discovered within\n");
printf(" maximum carve size for file type [foremost 0.69 compat mode].\n");
printf("-c Choose configuration file.\n");
printf("-d Generate header/footer database; will bypass certain optimizations\n");
printf(" and discover all footers, so performance suffers. Doesn't affect\n");
printf(" the set of files carved. **EXPERIMENTAL**\n");
printf("-h Print this help message and exit.\n");
printf("-i Read names of disk images from specified file.\n");
printf("-m Generate/update carve coverage blockmap file. The first 32bit\n");
printf(" unsigned int in the file identifies the block size. Thereafter\n");
printf(" each 32bit unsigned int entry in the blockmap file corresponds\n");
printf(" to one block in the image file. Each entry counts how many\n");
printf(" carved files contain this block. Requires more memory and\n");
printf(" disk. **EXPERIMENTAL**\n");
printf("-n Don't add extensions to extracted files.\n");
printf("-o Set output directory for carved files.\n");
printf("-O Don't organize carved files by type. Default is to organize carved files\n");
printf(" into subdirectories.\n");
printf("-p Perform image file preview; audit log indicates which files\n");
printf(" would have been carved, but no files are actually carved.\n");
printf("-q Carve only when header is cluster-aligned.\n");
printf("-r Find only first of overlapping headers/footers [foremost 0.69 compat mode].\n");
printf("-s Skip n bytes in each disk image before carving.\n");
printf("-t Set directory for coverage blockmap. **EXPERIMENTAL**\n");
printf("-u Use carve coverage blockmap when carving. Carve only sections\n");
printf(" of the image whose entries in the blockmap are 0. These areas\n");
printf(" are treated as contiguous regions. **EXPERIMENTAL**\n");
printf("-V Print copyright information and exit.\n");
printf("-v Verbose mode.\n");
}
// signal handler, sets global variable 'signal_caught' which is
// checked periodically during carve operations. Allows clean
// shutdown.
void catch_alarm (int signum) {
signal_caught = signum;
signal(signum,catch_alarm);
#ifdef __DEBUG
fprintf(stderr,"\nCaught signal: %s.\n",(char*) strsignal(signum));
#endif
fprintf (stderr, "\nKill signal detected. Cleaning up...\n");
}
int extractSearchSpecData(struct SearchSpecLine *s,char **tokenarray) {
// process one line from config file:
// token[0] = suffix
// token[1] = case sensitive?
// token[2] = maximum carve size
// token[3] = begintag
// token[4] = endtag
// token[5] = search type (optional)
s->suffix = malloc(MAX_SUFFIX_LENGTH*sizeof(char));
s->begin = malloc(MAX_STRING_LENGTH*sizeof(char));
s->end = malloc(MAX_STRING_LENGTH*sizeof(char));
if (!strncasecmp(tokenarray[0],
SCALPEL_NOEXTENSION_SUFFIX,
strlen(SCALPEL_NOEXTENSION_SUFFIX))) {
s->suffix[0] = SCALPEL_NOEXTENSION;
s->suffix[1] = 0;
}
else {
memcpy(s->suffix,tokenarray[0],MAX_SUFFIX_LENGTH);
}
// case sensitivity check
s->casesensitive = (!strncasecmp(tokenarray[1],"y",1) ||
!strncasecmp(tokenarray[1],"yes",3));
//#ifdef __WIN32
// s->length = _atoi64(tokenarray[2]);
//#else
// s->length = atoull(tokenarray[2]);
//#endif
s->length = strtoull(tokenarray[2], 0, 10);
// determine search type for this needle
s->searchtype = SEARCHTYPE_FORWARD;
if (!strncasecmp(tokenarray[5],"REVERSE",strlen("REVERSE"))) {
s->searchtype = SEARCHTYPE_REVERSE;
}
else if (!strncasecmp(tokenarray[5],"NEXT",strlen("NEXT"))) {
s->searchtype = SEARCHTYPE_FORWARD_NEXT;
}
// FORWARD is the default, but OK if the user defines it explicitly
else if (!strncasecmp(tokenarray[5],"FORWARD",strlen("FORWARD"))) {
s->searchtype = SEARCHTYPE_FORWARD;
}
s->beginlength = translate(tokenarray[3]);
memcpy(s->begin,tokenarray[3],s->beginlength);
s->endlength = translate(tokenarray[4]);
memcpy(s->end,tokenarray[4],s->endlength);
init_bm_table(s->begin,s->begin_bm_table,s->beginlength,
s->casesensitive);
init_bm_table(s->end,s->end_bm_table,s->endlength,
s->casesensitive);
return SCALPEL_OK;
}
int processSearchSpecLine(struct scalpelState *state, char *buffer,
int lineNumber) {
char *buf = buffer;
char *token;
char **tokenarray = (char **) malloc(6*sizeof(char[MAX_STRING_LENGTH+1]));
int i = 0, err=0, len = strlen(buffer);
// murder CTRL-M (0x0d) characters
if (buffer[len-2] == 0x0d && buffer[len-1] == 0x0a) {
buffer[len-2] = buffer[len-1];
buffer[len-1] = buffer[len];
}
buf = (char*) skipWhiteSpace(buf);
token = strtok(buf," \t\n");
// lines beginning with # are comments
if(token == NULL || token[0] == '#'){
return SCALPEL_OK;
}
// allow wildcard to be changed
if (!strncasecmp(token,"wildcard",9)) {
if ((token = strtok(NULL," \t\n")) != NULL) {
translate(token);
}
else {
fprintf (stdout,"Warning: Empty wildcard in configuration file line %d. Ignoring.\n",
lineNumber);
return SCALPEL_OK;
}
if (strlen(token) > 1) {
fprintf(stderr,"Warning: Wildcard can only be one character,"
" but you specified %d characters.\n"
" Using the first character, \"%c\", as the wildcard.\n",
(int)strlen(token),token[0]);
}
wildcard = token[0];
return SCALPEL_OK;
}
while (token && (i < NUM_SEARCH_SPEC_ELEMENTS)){
tokenarray[i] = token;
i++;
token = strtok(NULL," \t\n");
}
switch(NUM_SEARCH_SPEC_ELEMENTS-i){
case 2:
tokenarray[NUM_SEARCH_SPEC_ELEMENTS-1] = "";
tokenarray[NUM_SEARCH_SPEC_ELEMENTS-2] = "";
break;
case 1:
tokenarray[NUM_SEARCH_SPEC_ELEMENTS-1] = "";
break;
case 0:
break;
default:
fprintf(stderr,
"\nERROR: In line %d of the configuration file, expected %d tokens,\n"
" but instead found only %d.\n",
lineNumber,NUM_SEARCH_SPEC_ELEMENTS,i);
return SCALPEL_ERROR_NO_SEARCH_SPEC;
break;
}
if ((err = extractSearchSpecData(&(state->SearchSpec[state->specLines]),
tokenarray))) {
switch(err) {
default:
fprintf(stderr,
"\nERROR: Unknown error on line %d of the configuration file.\n"
,lineNumber);
}
}
state->specLines++;
return SCALPEL_OK;
}
// process configuration file
int readSearchSpecFile(struct scalpelState *state) {
int lineNumber=0, status;
FILE *f;
char *buffer = malloc((NUM_SEARCH_SPEC_ELEMENTS * MAX_STRING_LENGTH + 1) * sizeof(char));
f = fopen(state->conffile,"r");
if (f == NULL) {
fprintf (stderr,
"ERROR: Couldn't open configuration file: %s -- %s\n",
state->conffile,strerror(errno));
free(buffer);
return SCALPEL_ERROR_FILE_OPEN;
}
while (fgets(buffer,NUM_SEARCH_SPEC_ELEMENTS * MAX_STRING_LENGTH, f)) {
lineNumber++;
if (state->specLines > MAX_FILE_TYPES) {
fprintf(stderr,"Your conf file contains too many file types.\n");
fprintf(stderr,"This version was compiled with MAX_FILE_TYPES == %d.\n",
MAX_FILE_TYPES);
fprintf(stderr,"Increase MAX_FILE_TYPES, recompile, and try again.\n");
free(buffer);
return SCALPEL_ERROR_TOO_MANY_TYPES;
}
if ((status = processSearchSpecLine(state,buffer,lineNumber)) != SCALPEL_OK) {
free(buffer);
return status;
}
}
// add an empty object to the end of the list as a marker
state->SearchSpec[state->specLines].suffix = NULL;
state->SearchSpec[state->specLines].casesensitive = 0;
state->SearchSpec[state->specLines].length = 0;
state->SearchSpec[state->specLines].begin = NULL;
state->SearchSpec[state->specLines].beginlength = 0;
state->SearchSpec[state->specLines].end = NULL;
state->SearchSpec[state->specLines].endlength = 0;
// GGRIII: offsets field is uninitialized--it doesn't
// matter, since we won't use this entry.
fclose(f);
free(buffer);
return SCALPEL_OK;
}
// Register the signal-handler that will write to the audit file and
// close it if we catch a SIGINT or SIGTERM
void registerSignalHandlers() {
if (signal (SIGINT, catch_alarm) == SIG_IGN) {
signal (SIGINT, SIG_IGN);
}
if (signal (SIGTERM, catch_alarm) == SIG_IGN) {
signal (SIGTERM, SIG_IGN);
}
#ifndef __WIN32
// *****GGRIII: is this problematic?
// From foremost 0.69:
/* 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.. */
signal(SIGWINCH, setttywidth);
#endif
}
// initialize state variable and copy command line arguments
void initializeState(char **argv, struct scalpelState *state) {
char** argvcopy = argv;
int sss;
int i;
// Allocate memory for the state
state->imagefile = (char*) malloc(MAX_STRING_LENGTH * sizeof(char));
state->inputFileList = (char*) malloc(MAX_STRING_LENGTH * sizeof(char));
state->conffile = (char*) malloc(MAX_STRING_LENGTH * sizeof(char));
state->outputdirectory = (char*) malloc(MAX_STRING_LENGTH * sizeof(char));
state->invocation = (char*) malloc(MAX_STRING_LENGTH * sizeof(char));
// GGRIII: memory allocation made more sane, because we're storing
// more information in Scalpel than foremost had to, for each file
// type.
sss = (MAX_FILE_TYPES+1)*sizeof(struct SearchSpecLine);
state->SearchSpec = (struct SearchSpecLine*) malloc(sss);
state->specLines = 0;
// GGRIII: initialize header/footer offset data, carved file count,
// et al. The header/footer database is re-initialized in "dig.c"
// after each image file is processed (numfilestocarve and
// organizeDirNum are not). Storage for the header/footer offsets
// will be reallocated as needed.
for (i=0; i < MAX_FILE_TYPES; i++) {
state->SearchSpec[i].offsets.headers=0;
state->SearchSpec[i].offsets.footers=0;
state->SearchSpec[i].offsets.numheaders=0;
state->SearchSpec[i].offsets.numfooters=0;
state->SearchSpec[i].offsets.headerstorage=0;
state->SearchSpec[i].offsets.footerstorage=0;
state->SearchSpec[i].numfilestocarve=0;
state->SearchSpec[i].organizeDirNum=0;
}
state->fileswritten = 0;
state->skip = 0;
state->organizeMaxFilesPerSub=MAX_FILES_PER_SUBDIRECTORY;
state->modeVerbose = FALSE;
state->modeNoSuffix = FALSE;
state->useInputFileList = FALSE;
state->carveWithMissingFooters=FALSE;
state->noSearchOverlap=FALSE;
state->generateHeaderFooterDatabase=FALSE;
state->updateCoverageBlockmap=FALSE;
state->useCoverageBlockmap=FALSE;
state->blockAlignedOnly=FALSE;
state->organizeSubdirectories=TRUE;
state->previewMode=FALSE;
state->ignoreEmbedded=FALSE;
state->auditFile = NULL;
// default values for output directory, config file, wildcard character,
// coverage blockmap directory
strncpy(state->outputdirectory,SCALPEL_DEFAULT_OUTPUT_DIR,
strlen(SCALPEL_DEFAULT_OUTPUT_DIR));
strncpy(state->conffile,SCALPEL_DEFAULT_CONFIG_FILE,
MAX_STRING_LENGTH);
state->coveragedirectory = state->outputdirectory;
wildcard = SCALPEL_DEFAULT_WILDCARD;
signal_caught = 0;
state->invocation[0]=0;
// copy the invocation string into the state
do {
strncat(state->invocation,
*argvcopy,
MAX_STRING_LENGTH-strlen(state->invocation));
strncat(state->invocation,
" ",
MAX_STRING_LENGTH-strlen(state->invocation));
++argvcopy;
} while (*argvcopy);
registerSignalHandlers();
}
// parse command line arguments
void processCommandLineArgs(int argc, char **argv,
struct scalpelState *state) {
char i;
while ((i = getopt(argc, argv, "bhvVundpq:rt:c:o:s:i:m:O")) != -1) {
switch (i) {
case 'V':
fprintf (stdout,SCALPEL_COPYRIGHT_STRING);
exit(1);
case 'h':
usage();
exit(1);
case 's':
state->skip = strtoull(optarg,NULL,10);
fprintf (stdout,"Skipping the first %lld bytes of each image file.\n",
state->skip);
break;
case 'c':
strncpy(state->conffile,optarg,MAX_STRING_LENGTH);
break;
case 'd':
state->generateHeaderFooterDatabase=TRUE;
break;
// -e support is currently a work-in-progress and will be enabled in a future release
// case 'e':
// state->ignoreEmbedded=TRUE;
// break;
case 'm':
state->updateCoverageBlockmap=TRUE;
state->coverageblocksize=strtoul(optarg,NULL,10);
if (state->coverageblocksize <= 0) {
fprintf(stderr,
"\nERROR: Invalid blocksize for -m command line option.\n");
exit(1);
}
break;
case 'o':
strncpy(state->outputdirectory,optarg,MAX_STRING_LENGTH);
break;
case 't':
state->coveragedirectory = (char *) malloc(MAX_STRING_LENGTH * sizeof(char));
strncpy(state->coveragedirectory,optarg,MAX_STRING_LENGTH);
break;
case 'O':
state->organizeSubdirectories=FALSE;
break;
case 'p':
state->previewMode=TRUE;
break;
case 'b':
state->carveWithMissingFooters=TRUE;
break;
case 'i':
state->useInputFileList = TRUE;
state->inputFileList = optarg;
break;
case 'n':
state->modeNoSuffix = TRUE;
fprintf (stdout,"Extracting files without filename extensions.\n");
break;
case 'q':
state->blockAlignedOnly=TRUE;
state->alignedblocksize=strtoul(optarg,NULL,10);
if (state->alignedblocksize <= 0) {
fprintf(stderr,
"\nERROR: Invalid blocksize for -q command line option.\n");
exit(1);
}
break;
case 'r':
state->noSearchOverlap=TRUE;
break;
case 'u':
state->useCoverageBlockmap=TRUE;
break;
case 'v':
state->modeVerbose=TRUE;
break;
default:
exit(1);
}
}
}
// full pathnames for all files used
void convertFileNames(struct scalpelState *state) {
char fn[MAX_STRING_LENGTH];
realpath(state->outputdirectory,fn);
strncpy(state->outputdirectory,fn,MAX_STRING_LENGTH);
// realpath(state->conffile,fn);
// strncpy(state->conffile,fn,MAX_STRING_LENGTH);
}
// GGRIII: for each file, build header/footer offset database first,
// then carve files based on this database. Need to clear the
// header/footer offset database after processing of each file.
void digAllFiles(int argc, char **argv, struct scalpelState *state) {
int i = 0, j = 0;
FILE* listoffiles = NULL;
if (state->useInputFileList) {
fprintf(stdout, "Batch mode: reading list of images from %s.",
state->inputFileList);
listoffiles = fopen(state->inputFileList,"r");
if (listoffiles == NULL) {
fprintf(stderr, "Couldn't open file: %s -- %s\n",
(*(state->inputFileList)=='\0')?"<blank>":state->inputFileList,
strerror(errno));
closeFile(state->auditFile);
exit(-1);
}
j=0;
do {
j++;
if (fgets(state->imagefile,MAX_STRING_LENGTH,listoffiles) == NULL) {
fprintf(stderr,
"Error reading line %d of %s. Skipping line.\n",
j,state->inputFileList);
continue;
}
if(state->imagefile[strlen(state->imagefile)-1] == '\n'){
state->imagefile[strlen(state->imagefile)-1] = '\x00';
}
// GGRIII: this function now *only* builds the header/footer
// database. Carving is handled afterward, in carveImageFile().
if ((i = digImageFile(state))) {
handleError(state,i);
}
else {
// GGRIII: "digging" is now complete and header/footer database
// has been built. The function carveImageFile() performs
// extraction of files based on this database.
if ((i = carveImageFile(state))) {
handleError(state,i);
}
}
}
while (!feof(listoffiles));
closeFile(listoffiles);
}
else {
do {
state->imagefile = *argv;
// GGRIII: this function now *only* builds the header/footer
// database. Carving is handled afterward, in carveImageFile().
if ((i = digImageFile(state))) {
handleError(state,i);
}
else {
// GGRIII: "digging" is now complete and header/footer database
// has been built. The function carveImageFile() performs extraction
// of files based on this database.
if ((i = carveImageFile(state))) {
handleError(state,i);
}
}
++argv;
} while (*argv);
}
}
int main(int argc, char **argv) {
time_t starttime=time(0);
struct scalpelState state;
if (ldiv(SIZE_OF_BUFFER,SCALPEL_BLOCK_SIZE).rem != 0) {
fprintf (stderr, SCALPEL_SIZEOFBUFFER_PANIC_STRING);
exit (-1);
}
#ifndef __GLIBC__
setProgramName(argv[0]);
#endif
fprintf (stdout,SCALPEL_BANNER_STRING);
initializeState(argv,&state);
processCommandLineArgs(argc,argv,&state);
convertFileNames(&state);
if (state.modeVerbose) {
fprintf (stdout,"Output directory: \"%s\"\n", state.outputdirectory);
fprintf (stdout,"Configuration file: \"%s\"\n", state.conffile);
fprintf (stdout,"Coverage maps directory: \"%s\"\n", state.coveragedirectory);
}
// read configuration file
if (readSearchSpecFile(&state)) {
// error in configuration file, msg has already been output
exit(-1);
}
setttywidth();
argv += optind;
if (*argv != NULL || state.useInputFileList) {
// prepare audit file and make sure output directory is empty.
if(openAuditFile(&state)){
fprintf(stderr, "Aborting.\n\n");
exit(-1);
}
digAllFiles(argc,argv,&state);
closeFile(state.auditFile);
} else {
usage();
fprintf(stdout,"\nERROR: No image files specified.\n\n");
}
#ifdef __WIN32
fprintf (stdout,"\nScalpel is done, files carved = %I64u, elapsed = %ld seconds.\n",
state.fileswritten,
(int)time(0) - starttime);
#else
fprintf (stdout,"\nScalpel is done, files carved = %llu, elapsed = %ld seconds.\n",
state.fileswritten,
(int)time(0) - starttime);
#endif
return 0;
}
|