19 package org.sleuthkit.autopsy.modules.vmextractor;
 
   22 import java.io.IOException;
 
   23 import java.nio.file.Files;
 
   24 import java.nio.file.Path;
 
   25 import java.nio.file.Paths;
 
   26 import java.text.SimpleDateFormat;
 
   27 import java.util.ArrayList;
 
   28 import java.util.Calendar;
 
   29 import java.util.HashMap;
 
   30 import java.util.List;
 
   31 import java.util.UUID;
 
   32 import java.util.logging.Level;
 
   33 import org.openide.util.NbBundle;
 
   34 import org.openide.util.NbBundle.Messages;
 
   62 @NbBundle.Messages({
"# {0} - output directory name", 
"VMExtractorIngestModule.cannotCreateOutputDir.message=Unable to create output directory: {0}." 
   64 final class VMExtractorIngestModule extends DataSourceIngestModuleAdapter {
 
   66     private static final Logger logger = Logger.getLogger(VMExtractorIngestModule.class.getName());
 
   67     private IngestJobContext context;
 
   68     private Path ingestJobOutputDir;
 
   69     private String parentDeviceId;
 
   70     private String parentTimeZone;
 
   72     private final HashMap<String, String> imageFolderToOutputFolder = 
new HashMap<>();
 
   73     private int folderId = 0;
 
   75     @Messages({
"# {0} - data source name", 
"deviceIdQueryErrMsg=Data source {0} missing Device ID"})
 
   77     public void startUp(IngestJobContext context) 
throws IngestModuleException {
 
   78         this.context = context;
 
   79         long dataSourceObjId = context.getDataSource().getId();
 
   81             Case currentCase = Case.getCurrentCase();
 
   82             SleuthkitCase caseDb = currentCase.getSleuthkitCase();
 
   83             DataSource dataSource = caseDb.getDataSource(dataSourceObjId);
 
   84             parentDeviceId = dataSource.getDeviceId();
 
   85             parentTimeZone = dataSource.getTimeZone();
 
   86             SimpleDateFormat dateFormat = 
new SimpleDateFormat(
"yyyy_MM_dd_HH_mm_ss");
 
   87             String timeStamp = dateFormat.format(Calendar.getInstance().getTime());
 
   88             String ingestJobOutputDirName = context.getDataSource().getName() + 
"_" + context.getDataSource().getId() + 
"_" + timeStamp;
 
   89             ingestJobOutputDir = Paths.get(Case.getCurrentCase().getModuleDirectory(), VMExtractorIngestModuleFactory.getModuleName(), ingestJobOutputDirName);
 
   91             Files.createDirectories(ingestJobOutputDir);
 
   92         } 
catch (IOException | SecurityException | UnsupportedOperationException ex) {
 
   93             throw new IngestModule.IngestModuleException(Bundle.VMExtractorIngestModule_cannotCreateOutputDir_message(ex.getLocalizedMessage()), ex);
 
   94         } 
catch (TskDataException | TskCoreException ex) {
 
   95             throw new IngestModule.IngestModuleException(Bundle.deviceIdQueryErrMsg(context.getDataSource().getName()), ex);
 
  100     public ProcessResult process(Content dataSource, DataSourceIngestModuleProgress progressBar) {
 
  102         String outputFolderForThisVM;
 
  103         List<AbstractFile> vmFiles;
 
  106         progressBar.progress(NbBundle.getMessage(
this.getClass(), 
"VMExtractorIngestModule.searchingImage.message"));
 
  108         progressBar.switchToIndeterminate();
 
  110         logger.log(Level.INFO, 
"Looking for virtual machine files in data source {0}", dataSource.getName()); 
 
  114             vmFiles = findVirtualMachineFiles(dataSource);
 
  115         } 
catch (TskCoreException ex) {
 
  116             logger.log(Level.SEVERE, 
"Error querying case database", ex); 
 
  117             return ProcessResult.ERROR;
 
  120         if (vmFiles.isEmpty()) {
 
  122             logger.log(Level.INFO, 
"No virtual machine files found in data source {0}", dataSource.getName()); 
 
  123             return ProcessResult.OK;
 
  126         progressBar.switchToDeterminate(vmFiles.size());
 
  127         progressBar.progress(NbBundle.getMessage(
this.getClass(), 
"VMExtractorIngestModule.exportingToDisk.message"));
 
  129         int numFilesSaved = 0;
 
  130         for (AbstractFile vmFile : vmFiles) {
 
  131             if (context.dataSourceIngestIsCancelled()) {
 
  135             logger.log(Level.INFO, 
"Saving virtual machine file {0} to disk", vmFile.getName()); 
 
  138             String vmFolderPathInsideTheImage = vmFile.getParentPath();
 
  141             if (imageFolderToOutputFolder.containsKey(vmFolderPathInsideTheImage)) {
 
  143                 outputFolderForThisVM = imageFolderToOutputFolder.get(vmFolderPathInsideTheImage);
 
  147                 outputFolderForThisVM = Paths.get(ingestJobOutputDir.toString(), Integer.toString(folderId)).toString();
 
  150                 imageFolderToOutputFolder.put(vmFolderPathInsideTheImage, outputFolderForThisVM);
 
  155                 writeVirtualMachineToDisk(vmFile, outputFolderForThisVM);
 
  156             } 
catch (Exception ex) {
 
  157                 logger.log(Level.SEVERE, 
"Failed to write virtual machine file " + vmFile.getName() + 
" to folder " + outputFolderForThisVM, ex); 
 
  158                 MessageNotifyUtil.Notify.error(NbBundle.getMessage(
this.getClass(), 
"VMExtractorIngestModule.msgNotify.failedExtractVM.title.txt"),
 
  159                         NbBundle.getMessage(
this.getClass(), 
"VMExtractorIngestModule.msgNotify.failedExtractVM.msg.txt", vmFile.getName()));
 
  164             progressBar.progress(NbBundle.getMessage(
this.getClass(), 
"VMExtractorIngestModule.exportingToDisk.message"), numFilesSaved);
 
  166         logger.log(Level.INFO, 
"Finished saving virtual machine files to disk"); 
 
  169         progressBar.switchToDeterminate(imageFolderToOutputFolder.size());
 
  170         progressBar.progress(NbBundle.getMessage(
this.getClass(), 
"VMExtractorIngestModule.queuingIngestJobs.message"));
 
  172         int numJobsQueued = 0;
 
  174         for (String folder : imageFolderToOutputFolder.values()) {
 
  175             if (context.dataSourceIngestIsCancelled()) {
 
  178             List<String> vmFilesToIngest = VirtualMachineFinder.identifyVirtualMachines(Paths.get(folder));
 
  179             for (String file : vmFilesToIngest) {
 
  181                     logger.log(Level.INFO, 
"Ingesting virtual machine file {0} in folder {1}", 
new Object[]{file, folder}); 
 
  185                     ingestVirtualMachineImage(Paths.get(folder, file));
 
  186                     logger.log(Level.INFO, 
"Ingest complete for virtual machine file {0} in folder {1}", 
new Object[]{file, folder}); 
 
  187                 } 
catch (InterruptedException ex) {
 
  188                     logger.log(Level.INFO, 
"Interrupted while ingesting virtual machine file " + file + 
" in folder " + folder, ex); 
 
  189                 } 
catch (IOException ex) {
 
  190                     logger.log(Level.SEVERE, 
"Failed to ingest virtual machine file " + file + 
" in folder " + folder, ex); 
 
  191                     MessageNotifyUtil.Notify.error(NbBundle.getMessage(
this.getClass(), 
"VMExtractorIngestModule.msgNotify.failedIngestVM.title.txt"),
 
  192                             NbBundle.getMessage(
this.getClass(), 
"VMExtractorIngestModule.msgNotify.failedIngestVM.msg.txt", file));
 
  197             progressBar.progress(NbBundle.getMessage(
this.getClass(), 
"VMExtractorIngestModule.queuingIngestJobs.message"), numJobsQueued);
 
  199         logger.log(Level.INFO, 
"VMExtractorIngestModule completed processing of data source {0}", dataSource.getName()); 
 
  200         return ProcessResult.OK;
 
  214     private static List<AbstractFile> findVirtualMachineFiles(Content dataSource) 
throws TskCoreException {
 
  215         List<AbstractFile> vmFiles = 
new ArrayList<>();
 
  216         for (String vmExtension : GeneralFilter.VIRTUAL_MACHINE_EXTS) {
 
  217             String searchString = 
"%" + vmExtension;    
 
  218             vmFiles.addAll(Case.getCurrentCase().getServices().getFileManager().findFiles(dataSource, searchString));
 
  231     private void writeVirtualMachineToDisk(AbstractFile vmFile, String outputFolderForThisVM) 
throws IOException {
 
  235         File destinationFolder = Paths.get(outputFolderForThisVM).toFile();
 
  236         if (!destinationFolder.exists()) {
 
  237             destinationFolder.mkdirs();
 
  242         File localFile = Paths.get(outputFolderForThisVM, vmFile.getName()).toFile();
 
  243         ContentUtils.writeToFile(vmFile, localFile, context::dataSourceIngestIsCancelled);
 
  252     private void ingestVirtualMachineImage(Path vmFile) 
throws InterruptedException, IOException {
 
  257         UUID taskId = UUID.randomUUID();
 
  258         Case.getCurrentCase().notifyAddingDataSource(taskId);
 
  259         ImageDSProcessor dataSourceProcessor = 
new ImageDSProcessor();
 
  260         AddDataSourceCallback dspCallback = 
new AddDataSourceCallback(vmFile);
 
  261         synchronized (
this) {
 
  262             dataSourceProcessor.run(parentDeviceId, vmFile.toString(), parentTimeZone, 
false, 
new AddDataSourceProgressMonitor(), dspCallback);
 
  273         if (!dspCallback.vmDataSources.isEmpty()) {
 
  274             Case.getCurrentCase().notifyDataSourceAdded(dspCallback.vmDataSources.get(0), taskId);
 
  275             List<Content> dataSourceContent = 
new ArrayList<>(dspCallback.vmDataSources);
 
  276             IngestJobSettings ingestJobSettings = 
new IngestJobSettings(context.getExecutionContext());
 
  277             for (String warning : ingestJobSettings.getWarnings()) {
 
  278                 logger.log(Level.WARNING, String.format(
"Ingest job settings warning for virtual machine file %s : %s", vmFile.toString(), warning)); 
 
  280             IngestServices.getInstance().postMessage(IngestMessage.createMessage(IngestMessage.MessageType.INFO,
 
  281                     VMExtractorIngestModuleFactory.getModuleName(),
 
  282                     NbBundle.getMessage(this.getClass(), 
"VMExtractorIngestModule.addedVirtualMachineImage.message", vmFile.toString())));
 
  283             IngestManager.getInstance().queueIngestJob(dataSourceContent, ingestJobSettings);
 
  285             Case.getCurrentCase().notifyFailedAddingDataSource(taskId);
 
  323             this.vmFile = vmFile;
 
  324             vmDataSources = 
new ArrayList<>();
 
  329             for (String error : errList) {
 
  330                 String logMessage = String.format(
"Data source processor error for virtual machine file %s: %s", vmFile.toString(), error); 
 
  332                     logger.log(Level.SEVERE, logMessage);
 
  334                     logger.log(Level.WARNING, logMessage);
 
  342             if (!content.isEmpty()) {
 
  343                 vmDataSources.add(content.get(0));
 
  349             synchronized (VMExtractorIngestModule.this) {
 
  350                 VMExtractorIngestModule.this.notify();
 
  356             done(result, errList, newContents);