Autopsy  4.14.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
FileTypeIdIngestModule.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2013-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.filetypeid;
20 
21 import java.util.Arrays;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.logging.Level;
25 import org.openide.util.NbBundle;
36 import org.sleuthkit.datamodel.AbstractFile;
37 import org.sleuthkit.datamodel.Blackboard;
38 import org.sleuthkit.datamodel.BlackboardArtifact;
39 import static org.sleuthkit.datamodel.BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_FILE_HIT;
40 import org.sleuthkit.datamodel.BlackboardAttribute;
41 import static org.sleuthkit.datamodel.BlackboardAttribute.ATTRIBUTE_TYPE.TSK_CATEGORY;
42 import static org.sleuthkit.datamodel.BlackboardAttribute.ATTRIBUTE_TYPE.TSK_SET_NAME;
43 import org.sleuthkit.datamodel.TskCoreException;
44 
49 @NbBundle.Messages({"CannotRunFileTypeDetection=Unable to run file type detection."})
50 public class FileTypeIdIngestModule implements FileIngestModule {
51 
52  private static final Logger logger = Logger.getLogger(FileTypeIdIngestModule.class.getName());
53  private static final HashMap<Long, IngestJobTotals> totalsForIngestJobs = new HashMap<>();
54  private static final IngestModuleReferenceCounter refCounter = new IngestModuleReferenceCounter();
55 
56  private long jobId;
58 
68  @Deprecated
69  public static boolean isMimeTypeDetectable(String mimeType) {
70  try {
71  return new FileTypeDetector().isDetectable(mimeType);
73  logger.log(Level.SEVERE, "Failed to create file type detector", ex); //NON-NLS
74  return false;
75  }
76  }
77 
83  }
84 
85  @Override
86  public void startUp(IngestJobContext context) throws IngestModuleException {
87  jobId = context.getJobId();
88  refCounter.incrementAndGet(jobId);
89  try {
90  fileTypeDetector = new FileTypeDetector();
92  throw new IngestModuleException(Bundle.CannotRunFileTypeDetection(), ex);
93  }
94  }
95 
96  @Override
97  public ProcessResult process(AbstractFile file) {
103  try {
104  long startTime = System.currentTimeMillis();
105  String mimeType = fileTypeDetector.getMIMEType(file);
106  file.setMIMEType(mimeType);
107  FileType fileType = detectUserDefinedFileType(file);
108  if (fileType != null && fileType.shouldCreateInterestingFileHit()) {
109  createInterestingFileHit(file, fileType);
110  }
111  addToTotals(jobId, (System.currentTimeMillis() - startTime));
112  return ProcessResult.OK;
113  } catch (Exception e) {
114  logger.log(Level.WARNING, String.format("Error while attempting to determine file type of file %d", file.getId()), e); //NON-NLS
115  return ProcessResult.ERROR;
116  }
117  }
118 
129  private FileType detectUserDefinedFileType(AbstractFile file) throws CustomFileTypesManager.CustomFileTypesException {
130  FileType retValue = null;
131 
132  CustomFileTypesManager customFileTypesManager = CustomFileTypesManager.getInstance();
133  List<FileType> fileTypesList = customFileTypesManager.getUserDefinedFileTypes();
134  for (FileType fileType : fileTypesList) {
135  if (fileType.matches(file)) {
136  retValue = fileType;
137  break;
138  }
139  }
140 
141  return retValue;
142  }
143 
150  private void createInterestingFileHit(AbstractFile file, FileType fileType) {
151 
152  List<BlackboardAttribute> attributes = Arrays.asList(
153  new BlackboardAttribute(
154  TSK_SET_NAME, FileTypeIdModuleFactory.getModuleName(),
155  fileType.getInterestingFilesSetName()),
156  new BlackboardAttribute(
157  TSK_CATEGORY, FileTypeIdModuleFactory.getModuleName(),
158  fileType.getMimeType()));
159  try {
160  Case currentCase = Case.getCurrentCaseThrows();
161 
162  Blackboard tskBlackboard = currentCase.getSleuthkitCase().getBlackboard();
163  // Create artifact if it doesn't already exist.
164  if (!tskBlackboard.artifactExists(file, TSK_INTERESTING_FILE_HIT, attributes)) {
165  BlackboardArtifact artifact = file.newArtifact(TSK_INTERESTING_FILE_HIT);
166  artifact.addAttributes(attributes);
167  try {
168  /*
169  * post the artifact which will index the artifact for
170  * keyword search, and fire an event to notify UI of this
171  * new artifact
172  */
173  tskBlackboard.postArtifact(artifact, FileTypeIdModuleFactory.getModuleName());
174  } catch (Blackboard.BlackboardException ex) {
175  logger.log(Level.SEVERE, String.format("Unable to index TSK_INTERESTING_FILE_HIT blackboard artifact %d (file obj_id=%d)", artifact.getArtifactID(), file.getId()), ex); //NON-NLS
176  }
177  }
178 
179  } catch (TskCoreException ex) {
180  logger.log(Level.SEVERE, String.format("Unable to create TSK_INTERESTING_FILE_HIT artifact for file (obj_id=%d)", file.getId()), ex); //NON-NLS
181  } catch (NoCurrentCaseException ex) {
182  logger.log(Level.SEVERE, "Exception while getting open case.", ex); //NON-NLS
183  }
184  }
185 
186  @Override
187  public void shutDown() {
192  if (refCounter.decrementAndGet(jobId) == 0) {
193  IngestJobTotals jobTotals;
194  synchronized (this) {
195  jobTotals = totalsForIngestJobs.remove(jobId);
196  }
197  if (jobTotals != null) {
198  StringBuilder detailsSb = new StringBuilder();
199  detailsSb.append("<table border='0' cellpadding='4' width='280'>"); //NON-NLS
200  detailsSb.append("<tr><td>").append(FileTypeIdModuleFactory.getModuleName()).append("</td></tr>"); //NON-NLS
201  detailsSb.append("<tr><td>") //NON-NLS
202  .append(NbBundle.getMessage(this.getClass(), "FileTypeIdIngestModule.complete.totalProcTime"))
203  .append("</td><td>").append(jobTotals.matchTime).append("</td></tr>\n"); //NON-NLS
204  detailsSb.append("<tr><td>") //NON-NLS
205  .append(NbBundle.getMessage(this.getClass(), "FileTypeIdIngestModule.complete.totalFiles"))
206  .append("</td><td>").append(jobTotals.numFiles).append("</td></tr>\n"); //NON-NLS
207  detailsSb.append("</table>"); //NON-NLS
209  NbBundle.getMessage(this.getClass(),
210  "FileTypeIdIngestModule.complete.srvMsg.text"),
211  detailsSb.toString()));
212  }
213  }
214  }
215 
223  private static synchronized void addToTotals(long jobId, long matchTimeInc) {
224  IngestJobTotals ingestJobTotals = totalsForIngestJobs.get(jobId);
225  if (ingestJobTotals == null) {
226  ingestJobTotals = new IngestJobTotals();
227  totalsForIngestJobs.put(jobId, ingestJobTotals);
228  }
229 
230  ingestJobTotals.matchTime += matchTimeInc;
231  ingestJobTotals.numFiles++;
232  totalsForIngestJobs.put(jobId, ingestJobTotals);
233  }
234 
235  private static class IngestJobTotals {
236 
237  long matchTime = 0;
238  long numFiles = 0;
239  }
240 }
static IngestMessage createMessage(MessageType messageType, String source, String subject, String detailsHtml)
void postMessage(final IngestMessage message)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static synchronized void addToTotals(long jobId, long matchTimeInc)
static synchronized IngestServices getInstance()

Copyright © 2012-2020 Basis Technology. Generated on: Wed Apr 8 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.