19package org.sleuthkit.autopsy.modules.vmextractor;
22import java.io.IOException;
23import java.nio.file.Files;
24import java.nio.file.Path;
25import java.nio.file.Paths;
26import java.text.SimpleDateFormat;
27import java.util.ArrayList;
28import java.util.Calendar;
29import java.util.HashMap;
32import java.util.logging.Level;
33import org.openide.util.NbBundle;
34import org.openide.util.NbBundle.Messages;
35import org.sleuthkit.autopsy.casemodule.Case;
36import org.sleuthkit.autopsy.casemodule.GeneralFilter;
37import org.sleuthkit.autopsy.casemodule.ImageDSProcessor;
38import org.sleuthkit.autopsy.casemodule.NoCurrentCaseException;
39import org.sleuthkit.autopsy.corecomponentinterfaces.DataSourceProcessorCallback;
40import org.sleuthkit.autopsy.corecomponentinterfaces.DataSourceProcessorProgressMonitor;
41import org.sleuthkit.autopsy.coreutils.Logger;
42import org.sleuthkit.autopsy.coreutils.MessageNotifyUtil;
43import org.sleuthkit.autopsy.datamodel.ContentUtils;
44import org.sleuthkit.autopsy.ingest.DataSourceIngestModuleAdapter;
45import org.sleuthkit.autopsy.ingest.DataSourceIngestModuleProgress;
46import org.sleuthkit.autopsy.ingest.IngestJobContext;
47import org.sleuthkit.autopsy.ingest.IngestJobSettings;
48import org.sleuthkit.autopsy.ingest.IngestManager;
49import org.sleuthkit.autopsy.ingest.IngestMessage;
50import org.sleuthkit.autopsy.ingest.IngestModule;
51import org.sleuthkit.autopsy.ingest.IngestServices;
52import org.sleuthkit.autopsy.modules.filetypeid.FileTypeDetector;
53import org.sleuthkit.datamodel.AbstractFile;
54import org.sleuthkit.datamodel.Content;
55import org.sleuthkit.datamodel.DataSource;
56import org.sleuthkit.datamodel.ReadContentInputStream.ReadContentInputStreamException;
57import org.sleuthkit.datamodel.SleuthkitCase;
58import org.sleuthkit.datamodel.TskCoreException;
59import org.sleuthkit.datamodel.TskDataException;
65@NbBundle.Messages({
"# {0} - output directory name",
"VMExtractorIngestModule.cannotCreateOutputDir.message=Unable to create output directory: {0}."
71 private Path ingestJobOutputDir;
72 private String parentDeviceId;
73 private String parentTimeZone;
75 private final HashMap<String, String> imageFolderToOutputFolder =
new HashMap<>();
76 private int folderId = 0;
78 @Messages({
"# {0} - data source name",
"deviceIdQueryErrMsg=Data source {0} missing Device ID",
79 "VMExtractorIngestModule.noOpenCase.errMsg=No open case available."})
82 this.context = context;
83 long dataSourceObjId = context.getDataSource().getId();
87 DataSource dataSource = caseDb.getDataSource(dataSourceObjId);
88 parentDeviceId = dataSource.getDeviceId();
89 parentTimeZone = dataSource.getTimeZone();
90 SimpleDateFormat dateFormat =
new SimpleDateFormat(
"yyyy_MM_dd_HH_mm_ss");
91 String timeStamp = dateFormat.format(Calendar.getInstance().getTime());
92 String ingestJobOutputDirName = context.getDataSource().getName() +
"_" + context.getDataSource().getId() +
"_" + timeStamp;
93 ingestJobOutputDirName = ingestJobOutputDirName.replace(
':',
'_');
96 Files.createDirectories(ingestJobOutputDir);
97 }
catch (IOException | SecurityException | UnsupportedOperationException ex) {
99 }
catch (TskDataException | TskCoreException ex) {
109 String outputFolderForThisVM;
110 List<AbstractFile> vmFiles;
113 progressBar.progress(NbBundle.getMessage(
this.getClass(),
"VMExtractorIngestModule.searchingImage.message"));
115 progressBar.switchToIndeterminate();
117 logger.log(Level.INFO,
"Looking for virtual machine files in data source {0}", dataSource.getName());
121 vmFiles = findVirtualMachineFiles(dataSource);
122 vmFiles = removeNonVMFiles(vmFiles);
123 }
catch (TskCoreException ex) {
124 logger.log(Level.SEVERE,
"Error querying case database", ex);
125 return ProcessResult.ERROR;
127 logger.log(Level.SEVERE,
"Exception while getting open case.", ex);
128 return ProcessResult.ERROR;
131 if (vmFiles.isEmpty()) {
133 logger.log(Level.INFO,
"No virtual machine files found in data source {0}", dataSource.getName());
134 return ProcessResult.OK;
137 progressBar.switchToDeterminate(vmFiles.size());
138 progressBar.progress(NbBundle.getMessage(
this.getClass(),
"VMExtractorIngestModule.exportingToDisk.message"));
140 int numFilesSaved = 0;
141 for (AbstractFile vmFile : vmFiles) {
142 if (context.dataSourceIngestIsCancelled()) {
146 logger.log(Level.INFO,
"Saving virtual machine file {0} to disk", vmFile.getName());
149 String vmFolderPathInsideTheImage = vmFile.getParentPath();
152 if (imageFolderToOutputFolder.containsKey(vmFolderPathInsideTheImage)) {
154 outputFolderForThisVM = imageFolderToOutputFolder.get(vmFolderPathInsideTheImage);
158 outputFolderForThisVM = Paths.get(ingestJobOutputDir.toString(), Integer.toString(folderId)).toString();
161 imageFolderToOutputFolder.put(vmFolderPathInsideTheImage, outputFolderForThisVM);
166 writeVirtualMachineToDisk(vmFile, outputFolderForThisVM);
167 }
catch (ReadContentInputStreamException ex) {
168 logger.log(Level.WARNING, String.format(
"Failed to read virtual machine file '%s' (id=%d).",
169 vmFile.getName(), vmFile.getId()), ex);
171 NbBundle.getMessage(
this.getClass(),
"VMExtractorIngestModule.msgNotify.failedExtractVM.msg.txt", vmFile.getName()));
172 }
catch (Exception ex) {
173 logger.log(Level.SEVERE, String.format(
"Failed to write virtual machine file '%s' (id=%d) to folder '%s'.",
174 vmFile.getName(), vmFile.getId(), outputFolderForThisVM), ex);
176 NbBundle.getMessage(
this.getClass(),
"VMExtractorIngestModule.msgNotify.failedExtractVM.msg.txt", vmFile.getName()));
181 progressBar.progress(NbBundle.getMessage(
this.getClass(),
"VMExtractorIngestModule.exportingToDisk.message"), numFilesSaved);
183 logger.log(Level.INFO,
"Finished saving virtual machine files to disk");
186 progressBar.switchToDeterminate(imageFolderToOutputFolder.size());
187 progressBar.progress(NbBundle.getMessage(
this.getClass(),
"VMExtractorIngestModule.queuingIngestJobs.message"));
189 int numJobsQueued = 0;
191 for (String folder : imageFolderToOutputFolder.values()) {
192 if (context.dataSourceIngestIsCancelled()) {
196 for (String file : vmFilesToIngest) {
198 logger.log(Level.INFO,
"Ingesting virtual machine file {0} in folder {1}",
new Object[]{file, folder});
202 ingestVirtualMachineImage(Paths.get(folder, file));
203 }
catch (InterruptedException ex) {
204 logger.log(Level.INFO,
"Interrupted while ingesting virtual machine file " + file +
" in folder " + folder, ex);
205 }
catch (IOException ex) {
206 logger.log(Level.SEVERE,
"Failed to ingest virtual machine file " + file +
" in folder " + folder, ex);
208 NbBundle.getMessage(
this.getClass(),
"VMExtractorIngestModule.msgNotify.failedIngestVM.msg.txt", file));
210 logger.log(Level.SEVERE,
"Exception while getting open case.", ex);
212 Bundle.VMExtractorIngestModule_noOpenCase_errMsg());
217 progressBar.progress(NbBundle.getMessage(
this.getClass(),
"VMExtractorIngestModule.queuingIngestJobs.message"), numJobsQueued);
219 logger.log(Level.INFO,
"VMExtractorIngestModule completed processing of data source {0}", dataSource.getName());
220 return ProcessResult.OK;
234 private static List<AbstractFile> findVirtualMachineFiles(Content dataSource)
throws TskCoreException,
NoCurrentCaseException {
235 List<AbstractFile> vmFiles =
new ArrayList<>();
237 String searchString =
"%" + vmExtension;
251 private static List<AbstractFile> removeNonVMFiles(List<AbstractFile> vmFiles) {
252 List<AbstractFile> vFile =
new ArrayList<>();
254 for (AbstractFile vmFile : vmFiles) {
255 if (vmFile.getNameExtension().equalsIgnoreCase(
"vhd")) {
256 String fileMimeType = vmFile.getMIMEType();
257 if (fileMimeType ==
null) {
261 logger.log(Level.WARNING, String.format(
"Unable to create file type detector for determining MIME type for file %s with id of %d", vmFile.getName(), vmFile.getId()));
265 fileMimeType = fileTypeDetector.getMIMEType(vmFile);
267 vmFile.setMIMEType(fileMimeType);
269 }
catch (TskCoreException ex) {
270 logger.log(Level.WARNING, String.format(
"Unable to save mimetype of %s for file %s with id of %d", fileMimeType, vmFile.getName(), vmFile.getId()));
273 if (fileMimeType.equalsIgnoreCase(
"application/x-vhd")) {
293 private void writeVirtualMachineToDisk(AbstractFile vmFile, String outputFolderForThisVM)
throws ReadContentInputStreamException, IOException {
297 File destinationFolder = Paths.get(outputFolderForThisVM).toFile();
298 if (!destinationFolder.exists()) {
299 destinationFolder.mkdirs();
304 File localFile = Paths.get(outputFolderForThisVM, vmFile.getName()).toFile();
314 private void ingestVirtualMachineImage(Path vmFile)
throws InterruptedException, IOException,
NoCurrentCaseException {
319 UUID taskId = UUID.randomUUID();
322 AddDataSourceCallback dspCallback =
new AddDataSourceCallback(vmFile);
323 synchronized (
this) {
324 dataSourceProcessor.run(parentDeviceId, vmFile.toString(), parentTimeZone,
false,
new AddDataSourceProgressMonitor(), dspCallback);
335 if (!dspCallback.vmDataSources.isEmpty()) {
337 List<Content> dataSourceContent =
new ArrayList<>(dspCallback.vmDataSources);
339 for (String warning : ingestJobSettings.getWarnings()) {
340 logger.log(Level.WARNING, String.format(
"Ingest job settings warning for virtual machine file %s : %s", vmFile.toString(), warning));
344 NbBundle.getMessage(
this.getClass(),
"VMExtractorIngestModule.addedVirtualMachineImage.message", vmFile.toString())));
391 for (String error : errList) {
392 String logMessage = String.format(
"Data source processor error for virtual machine file %s: %s",
vmFile.toString(), error);
394 logger.log(Level.SEVERE, logMessage);
396 logger.log(Level.WARNING, logMessage);
404 if (!content.isEmpty()) {
411 synchronized (VMExtractorIngestModule.this) {
412 VMExtractorIngestModule.this.notify();
418 done(result, errList, newContents);
void notifyFailedAddingDataSource(UUID addingDataSourceEventId)
SleuthkitCase getSleuthkitCase()
void notifyAddingDataSource(UUID eventId)
static Case getCurrentCaseThrows()
void notifyDataSourceAdded(Content dataSource, UUID addingDataSourceEventId)
static final List< String > VIRTUAL_MACHINE_EXTS
List< AbstractFile > findFiles(String fileName)
FileManager getFileManager()
synchronized static Logger getLogger(String name)
static void error(String title, String message)
static< T > long writeToFile(Content content, java.io.File outputFile, ProgressHandle progress, Future< T > worker, boolean source)
static synchronized IngestManager getInstance()
IngestJobStartResult beginIngestJob(Collection< Content > dataSources, IngestJobSettings settings)
static IngestMessage createMessage(MessageType messageType, String source, String subject, String detailsHtml)
void postMessage(final IngestMessage message)
static synchronized IngestServices getInstance()