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 ingestJobOutputDirName = ingestJobOutputDirName.replace(
':',
'_');
90 ingestJobOutputDir = Paths.get(Case.getCurrentCase().getModuleDirectory(), VMExtractorIngestModuleFactory.getModuleName(), ingestJobOutputDirName);
92 Files.createDirectories(ingestJobOutputDir);
93 }
catch (IOException | SecurityException | UnsupportedOperationException ex) {
94 throw new IngestModule.IngestModuleException(Bundle.VMExtractorIngestModule_cannotCreateOutputDir_message(ex.getLocalizedMessage()), ex);
95 }
catch (TskDataException | TskCoreException ex) {
96 throw new IngestModule.IngestModuleException(Bundle.deviceIdQueryErrMsg(context.getDataSource().getName()), ex);
101 public ProcessResult process(Content dataSource, DataSourceIngestModuleProgress progressBar) {
103 String outputFolderForThisVM;
104 List<AbstractFile> vmFiles;
107 progressBar.progress(NbBundle.getMessage(
this.getClass(),
"VMExtractorIngestModule.searchingImage.message"));
109 progressBar.switchToIndeterminate();
111 logger.log(Level.INFO,
"Looking for virtual machine files in data source {0}", dataSource.getName());
115 vmFiles = findVirtualMachineFiles(dataSource);
116 }
catch (TskCoreException ex) {
117 logger.log(Level.SEVERE,
"Error querying case database", ex);
118 return ProcessResult.ERROR;
121 if (vmFiles.isEmpty()) {
123 logger.log(Level.INFO,
"No virtual machine files found in data source {0}", dataSource.getName());
124 return ProcessResult.OK;
127 progressBar.switchToDeterminate(vmFiles.size());
128 progressBar.progress(NbBundle.getMessage(
this.getClass(),
"VMExtractorIngestModule.exportingToDisk.message"));
130 int numFilesSaved = 0;
131 for (AbstractFile vmFile : vmFiles) {
132 if (context.dataSourceIngestIsCancelled()) {
136 logger.log(Level.INFO,
"Saving virtual machine file {0} to disk", vmFile.getName());
139 String vmFolderPathInsideTheImage = vmFile.getParentPath();
142 if (imageFolderToOutputFolder.containsKey(vmFolderPathInsideTheImage)) {
144 outputFolderForThisVM = imageFolderToOutputFolder.get(vmFolderPathInsideTheImage);
148 outputFolderForThisVM = Paths.get(ingestJobOutputDir.toString(), Integer.toString(folderId)).toString();
151 imageFolderToOutputFolder.put(vmFolderPathInsideTheImage, outputFolderForThisVM);
156 writeVirtualMachineToDisk(vmFile, outputFolderForThisVM);
157 }
catch (Exception ex) {
158 logger.log(Level.SEVERE,
"Failed to write virtual machine file " + vmFile.getName() +
" to folder " + outputFolderForThisVM, ex);
159 MessageNotifyUtil.Notify.error(NbBundle.getMessage(
this.getClass(),
"VMExtractorIngestModule.msgNotify.failedExtractVM.title.txt"),
160 NbBundle.getMessage(
this.getClass(),
"VMExtractorIngestModule.msgNotify.failedExtractVM.msg.txt", vmFile.getName()));
165 progressBar.progress(NbBundle.getMessage(
this.getClass(),
"VMExtractorIngestModule.exportingToDisk.message"), numFilesSaved);
167 logger.log(Level.INFO,
"Finished saving virtual machine files to disk");
170 progressBar.switchToDeterminate(imageFolderToOutputFolder.size());
171 progressBar.progress(NbBundle.getMessage(
this.getClass(),
"VMExtractorIngestModule.queuingIngestJobs.message"));
173 int numJobsQueued = 0;
175 for (String folder : imageFolderToOutputFolder.values()) {
176 if (context.dataSourceIngestIsCancelled()) {
179 List<String> vmFilesToIngest = VirtualMachineFinder.identifyVirtualMachines(Paths.get(folder));
180 for (String file : vmFilesToIngest) {
182 logger.log(Level.INFO,
"Ingesting virtual machine file {0} in folder {1}",
new Object[]{file, folder});
186 ingestVirtualMachineImage(Paths.get(folder, file));
187 logger.log(Level.INFO,
"Ingest complete for virtual machine file {0} in folder {1}",
new Object[]{file, folder});
188 }
catch (InterruptedException ex) {
189 logger.log(Level.INFO,
"Interrupted while ingesting virtual machine file " + file +
" in folder " + folder, ex);
190 }
catch (IOException ex) {
191 logger.log(Level.SEVERE,
"Failed to ingest virtual machine file " + file +
" in folder " + folder, ex);
192 MessageNotifyUtil.Notify.error(NbBundle.getMessage(
this.getClass(),
"VMExtractorIngestModule.msgNotify.failedIngestVM.title.txt"),
193 NbBundle.getMessage(
this.getClass(),
"VMExtractorIngestModule.msgNotify.failedIngestVM.msg.txt", file));
198 progressBar.progress(NbBundle.getMessage(
this.getClass(),
"VMExtractorIngestModule.queuingIngestJobs.message"), numJobsQueued);
200 logger.log(Level.INFO,
"VMExtractorIngestModule completed processing of data source {0}", dataSource.getName());
201 return ProcessResult.OK;
215 private static List<AbstractFile> findVirtualMachineFiles(Content dataSource)
throws TskCoreException {
216 List<AbstractFile> vmFiles =
new ArrayList<>();
217 for (String vmExtension : GeneralFilter.VIRTUAL_MACHINE_EXTS) {
218 String searchString =
"%" + vmExtension;
219 vmFiles.addAll(Case.getCurrentCase().getServices().getFileManager().findFiles(dataSource, searchString));
232 private void writeVirtualMachineToDisk(AbstractFile vmFile, String outputFolderForThisVM)
throws IOException {
236 File destinationFolder = Paths.get(outputFolderForThisVM).toFile();
237 if (!destinationFolder.exists()) {
238 destinationFolder.mkdirs();
243 File localFile = Paths.get(outputFolderForThisVM, vmFile.getName()).toFile();
244 ContentUtils.writeToFile(vmFile, localFile, context::dataSourceIngestIsCancelled);
253 private void ingestVirtualMachineImage(Path vmFile)
throws InterruptedException, IOException {
258 UUID taskId = UUID.randomUUID();
259 Case.getCurrentCase().notifyAddingDataSource(taskId);
260 ImageDSProcessor dataSourceProcessor =
new ImageDSProcessor();
261 AddDataSourceCallback dspCallback =
new AddDataSourceCallback(vmFile);
262 synchronized (
this) {
263 dataSourceProcessor.run(parentDeviceId, vmFile.toString(), parentTimeZone,
false,
new AddDataSourceProgressMonitor(), dspCallback);
274 if (!dspCallback.vmDataSources.isEmpty()) {
275 Case.getCurrentCase().notifyDataSourceAdded(dspCallback.vmDataSources.get(0), taskId);
276 List<Content> dataSourceContent =
new ArrayList<>(dspCallback.vmDataSources);
277 IngestJobSettings ingestJobSettings =
new IngestJobSettings(context.getExecutionContext());
278 for (String warning : ingestJobSettings.getWarnings()) {
279 logger.log(Level.WARNING, String.format(
"Ingest job settings warning for virtual machine file %s : %s", vmFile.toString(), warning));
281 IngestServices.getInstance().postMessage(IngestMessage.createMessage(IngestMessage.MessageType.INFO,
282 VMExtractorIngestModuleFactory.getModuleName(),
283 NbBundle.getMessage(this.getClass(),
"VMExtractorIngestModule.addedVirtualMachineImage.message", vmFile.toString())));
284 IngestManager.getInstance().queueIngestJob(dataSourceContent, ingestJobSettings);
286 Case.getCurrentCase().notifyFailedAddingDataSource(taskId);
324 this.vmFile = vmFile;
325 vmDataSources =
new ArrayList<>();
330 for (String error : errList) {
331 String logMessage = String.format(
"Data source processor error for virtual machine file %s: %s", vmFile.toString(), error);
333 logger.log(Level.SEVERE, logMessage);
335 logger.log(Level.WARNING, logMessage);
343 if (!content.isEmpty()) {
344 vmDataSources.add(content.get(0));
350 synchronized (VMExtractorIngestModule.this) {
351 VMExtractorIngestModule.this.notify();
357 done(result, errList, newContents);