Autopsy  4.9.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
FileExtMismatchIngestModule.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2018 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.modules.fileextmismatch;
20 
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.Set;
24 import java.util.logging.Level;
25 import org.openide.util.NbBundle;
26 import org.openide.util.NbBundle.Messages;
40 import org.sleuthkit.datamodel.AbstractFile;
41 import org.sleuthkit.datamodel.BlackboardArtifact;
42 import org.sleuthkit.datamodel.BlackboardArtifact.ARTIFACT_TYPE;
43 import org.sleuthkit.datamodel.TskData;
44 import org.sleuthkit.datamodel.TskData.FileKnown;
45 import org.sleuthkit.datamodel.TskException;
46 
50 @NbBundle.Messages({
51  "CannotRunFileTypeDetection=Unable to run file type detection.",
52  "FileExtMismatchIngestModule.readError.message=Could not read settings."
53 })
55 
56  private static final Logger logger = Logger.getLogger(FileExtMismatchIngestModule.class.getName());
57  private final IngestServices services = IngestServices.getInstance();
58  private final FileExtMismatchDetectorModuleSettings settings;
59  private HashMap<String, Set<String>> mimeTypeToExtsMap = new HashMap<>();
60  private long jobId;
61  private static final HashMap<Long, IngestJobTotals> totalsForIngestJobs = new HashMap<>();
62  private static final IngestModuleReferenceCounter refCounter = new IngestModuleReferenceCounter();
63  private static Blackboard blackboard;
65 
66  private static class IngestJobTotals {
67 
68  private long processTime = 0;
69  private long numFiles = 0;
70  }
71 
78  private static synchronized void addToTotals(long ingestJobId, long processTimeInc) {
79  IngestJobTotals ingestJobTotals = totalsForIngestJobs.get(ingestJobId);
80  if (ingestJobTotals == null) {
81  ingestJobTotals = new IngestJobTotals();
82  totalsForIngestJobs.put(ingestJobId, ingestJobTotals);
83  }
84 
85  ingestJobTotals.processTime += processTimeInc;
86  ingestJobTotals.numFiles++;
87  totalsForIngestJobs.put(ingestJobId, ingestJobTotals);
88  }
89 
90  FileExtMismatchIngestModule(FileExtMismatchDetectorModuleSettings settings) {
91  this.settings = settings;
92  }
93 
94  @Override
95  public void startUp(IngestJobContext context) throws IngestModuleException {
96  jobId = context.getJobId();
97  refCounter.incrementAndGet(jobId);
98 
99  try {
100  mimeTypeToExtsMap = FileExtMismatchSettings.readSettings().getMimeTypeToExtsMap();
101  this.detector = new FileTypeDetector();
102  } catch (FileExtMismatchSettings.FileExtMismatchSettingsException ex) {
103  throw new IngestModuleException(Bundle.FileExtMismatchIngestModule_readError_message(), ex);
105  throw new IngestModuleException(Bundle.CannotRunFileTypeDetection(), ex);
106  }
107  }
108 
109  @Override
110  @Messages({"FileExtMismatchIngestModule.indexError.message=Failed to index file extension mismatch artifact for keyword search."})
111  public ProcessResult process(AbstractFile abstractFile) {
112  try {
114  } catch (NoCurrentCaseException ex) {
115  logger.log(Level.WARNING, "Exception while getting open case.", ex); //NON-NLS
116  return ProcessResult.ERROR;
117  }
118  if (this.settings.skipKnownFiles() && (abstractFile.getKnown() == FileKnown.KNOWN)) {
119  return ProcessResult.OK;
120  }
121 
122  // skip non-files
123  if ((abstractFile.getType() == TskData.TSK_DB_FILES_TYPE_ENUM.UNALLOC_BLOCKS)
124  || (abstractFile.getType() == TskData.TSK_DB_FILES_TYPE_ENUM.UNUSED_BLOCKS)
125  || (abstractFile.getType() == TskData.TSK_DB_FILES_TYPE_ENUM.SLACK)
126  || (abstractFile.isFile() == false)) {
127  return ProcessResult.OK;
128  }
129 
130  // deleted files often have content that was not theirs and therefor causes mismatch
131  if ((abstractFile.isMetaFlagSet(TskData.TSK_FS_META_FLAG_ENUM.UNALLOC))
132  || (abstractFile.isDirNameFlagSet(TskData.TSK_FS_NAME_FLAG_ENUM.UNALLOC))) {
133  return ProcessResult.OK;
134  }
135 
136  try {
137  long startTime = System.currentTimeMillis();
138 
139  boolean mismatchDetected = compareSigTypeToExt(abstractFile);
140 
141  addToTotals(jobId, System.currentTimeMillis() - startTime);
142 
143  if (mismatchDetected) {
144  // add artifact
145  BlackboardArtifact bart = abstractFile.newArtifact(ARTIFACT_TYPE.TSK_EXT_MISMATCH_DETECTED);
146 
147  try {
148  // index the artifact for keyword search
149  blackboard.indexArtifact(bart);
150  } catch (Blackboard.BlackboardException ex) {
151  logger.log(Level.SEVERE, "Unable to index blackboard artifact " + bart.getArtifactID(), ex); //NON-NLS
152  MessageNotifyUtil.Notify.error(FileExtMismatchDetectorModuleFactory.getModuleName(), Bundle.FileExtMismatchIngestModule_indexError_message());
153  }
154 
155  services.fireModuleDataEvent(new ModuleDataEvent(FileExtMismatchDetectorModuleFactory.getModuleName(), ARTIFACT_TYPE.TSK_EXT_MISMATCH_DETECTED, Collections.singletonList(bart)));
156  }
157  return ProcessResult.OK;
158  } catch (TskException ex) {
159  logger.log(Level.WARNING, "Error matching file signature", ex); //NON-NLS
160  return ProcessResult.ERROR;
161  }
162  }
163 
171  private boolean compareSigTypeToExt(AbstractFile abstractFile) {
172  String currActualExt = abstractFile.getNameExtension();
173 
174  // If we are skipping names with no extension
175  if (settings.skipFilesWithNoExtension() && currActualExt.isEmpty()) {
176  return false;
177  }
178  String currActualSigType = detector.getMIMEType(abstractFile);
179  if (settings.getCheckType() != CHECK_TYPE.ALL) {
180  if (settings.getCheckType() == CHECK_TYPE.NO_TEXT_FILES) {
181  if (!currActualExt.isEmpty() && currActualSigType.equals("text/plain")) { //NON-NLS
182  return false;
183  }
184  }
185  if (settings.getCheckType() == CHECK_TYPE.ONLY_MEDIA_AND_EXE) {
186  if (!FileExtMismatchDetectorModuleSettings.MEDIA_AND_EXE_MIME_TYPES.contains(currActualSigType)) {
187  return false;
188  }
189  }
190  }
191 
192  //get known allowed values from the map for this type
193  Set<String> allowedExtSet = mimeTypeToExtsMap.get(currActualSigType);
194  if (allowedExtSet != null) {
195  // see if the filename ext is in the allowed list
196  for (String e : allowedExtSet) {
197  if (e.equals(currActualExt)) {
198  return false;
199  }
200  }
201  return true; //potential mismatch
202  }
203 
204  return false;
205  }
206 
207  @Override
208  public void shutDown() {
209  // We only need to post the summary msg from the last module per job
210  if (refCounter.decrementAndGet(jobId) == 0) {
211  IngestJobTotals jobTotals;
212  synchronized (this) {
213  jobTotals = totalsForIngestJobs.remove(jobId);
214  }
215  if (jobTotals != null) {
216  StringBuilder detailsSb = new StringBuilder();
217  detailsSb.append("<table border='0' cellpadding='4' width='280'>"); //NON-NLS
218  detailsSb.append("<tr><td>").append(FileExtMismatchDetectorModuleFactory.getModuleName()).append("</td></tr>"); //NON-NLS
219  detailsSb.append("<tr><td>").append( //NON-NLS
220  NbBundle.getMessage(this.getClass(), "FileExtMismatchIngestModule.complete.totalProcTime"))
221  .append("</td><td>").append(jobTotals.processTime).append("</td></tr>\n"); //NON-NLS
222  detailsSb.append("<tr><td>").append( //NON-NLS
223  NbBundle.getMessage(this.getClass(), "FileExtMismatchIngestModule.complete.totalFiles"))
224  .append("</td><td>").append(jobTotals.numFiles).append("</td></tr>\n"); //NON-NLS
225  detailsSb.append("</table>"); //NON-NLS
226 
228  NbBundle.getMessage(this.getClass(),
229  "FileExtMismatchIngestModule.complete.svcMsg.text"),
230  detailsSb.toString()));
231  }
232  }
233  }
234 }
static IngestMessage createMessage(MessageType messageType, String source, String subject, String detailsHtml)
void postMessage(final IngestMessage message)
void fireModuleDataEvent(ModuleDataEvent moduleDataEvent)
static void error(String title, String message)
synchronized void indexArtifact(BlackboardArtifact artifact)
Definition: Blackboard.java:58
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static synchronized void addToTotals(long ingestJobId, long processTimeInc)
static synchronized IngestServices getInstance()

Copyright © 2012-2018 Basis Technology. Generated on: Tue Dec 18 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.