Autopsy  4.21.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-2021 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 org.sleuthkit.datamodel.BlackboardAttribute;
40 import static org.sleuthkit.datamodel.BlackboardAttribute.ATTRIBUTE_TYPE.TSK_CATEGORY;
41 import static org.sleuthkit.datamodel.BlackboardAttribute.ATTRIBUTE_TYPE.TSK_SET_NAME;
42 import org.sleuthkit.datamodel.Score;
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 
131  if (CustomFileTypesManager.getInstance().getUserDefinedFileTypes().isEmpty()) {
132  return null;
133  }
134 
135  /*
136  * Read in the beginning of the file once.
137  */
138  byte[] buf = new byte[1024];
139  int bufLen;
140  try {
141  bufLen = file.read(buf, 0, 1024);
142  } catch (TskCoreException ex) {
143  // Proceed for now - the error will likely get logged next time the file is read.
144  bufLen = 0;
145  }
146  return detectUserDefinedFileType(file, buf, bufLen);
147  }
148 
161  private FileType detectUserDefinedFileType(AbstractFile file, byte[] startOfFileBuffer, int bufLen) throws CustomFileTypesManager.CustomFileTypesException {
162  FileType retValue = null;
163 
164  CustomFileTypesManager customFileTypesManager = CustomFileTypesManager.getInstance();
165  List<FileType> fileTypesList = customFileTypesManager.getUserDefinedFileTypes();
166  for (FileType fileType : fileTypesList) {
167  if (fileType.matches(file, startOfFileBuffer, bufLen)) {
168  retValue = fileType;
169  break;
170  }
171  }
172 
173  return retValue;
174  }
175 
182  private void createInterestingFileHit(AbstractFile file, FileType fileType) {
183 
184  List<BlackboardAttribute> attributes = Arrays.asList(
185  new BlackboardAttribute(
186  TSK_SET_NAME, FileTypeIdModuleFactory.getModuleName(),
187  fileType.getInterestingFilesSetName()),
188  new BlackboardAttribute(
189  TSK_CATEGORY, FileTypeIdModuleFactory.getModuleName(),
190  fileType.getMimeType()));
191  try {
192  Case currentCase = Case.getCurrentCaseThrows();
193 
194  Blackboard tskBlackboard = currentCase.getSleuthkitCase().getBlackboard();
195  // Create artifact if it doesn't already exist.
196  if (!tskBlackboard.artifactExists(file, BlackboardArtifact.Type.TSK_INTERESTING_ITEM, attributes)) {
197  BlackboardArtifact artifact = file.newAnalysisResult(
198  BlackboardArtifact.Type.TSK_INTERESTING_ITEM, Score.SCORE_LIKELY_NOTABLE,
199  null, fileType.getInterestingFilesSetName(), null,
200  attributes)
201  .getAnalysisResult();
202  try {
203  /*
204  * post the artifact which will index the artifact for
205  * keyword search, and fire an event to notify UI of this
206  * new artifact
207  */
208  tskBlackboard.postArtifact(artifact, FileTypeIdModuleFactory.getModuleName(), jobId);
209  } catch (Blackboard.BlackboardException ex) {
210  logger.log(Level.SEVERE, String.format("Unable to index TSK_INTERESTING_ITEM blackboard artifact %d (file obj_id=%d)", artifact.getArtifactID(), file.getId()), ex); //NON-NLS
211  }
212  }
213 
214  } catch (TskCoreException ex) {
215  logger.log(Level.SEVERE, String.format("Unable to create TSK_INTERESTING_ITEM artifact for file (obj_id=%d)", file.getId()), ex); //NON-NLS
216  } catch (NoCurrentCaseException ex) {
217  logger.log(Level.SEVERE, "Exception while getting open case.", ex); //NON-NLS
218  }
219  }
220 
221  @Override
222  public void shutDown() {
227  if (refCounter.decrementAndGet(jobId) == 0) {
228  IngestJobTotals jobTotals;
229  synchronized (this) {
230  jobTotals = totalsForIngestJobs.remove(jobId);
231  }
232  if (jobTotals != null) {
233  StringBuilder detailsSb = new StringBuilder();
234  detailsSb.append("<table border='0' cellpadding='4' width='280'>"); //NON-NLS
235  detailsSb.append("<tr><td>").append(FileTypeIdModuleFactory.getModuleName()).append("</td></tr>"); //NON-NLS
236  detailsSb.append("<tr><td>") //NON-NLS
237  .append(NbBundle.getMessage(this.getClass(), "FileTypeIdIngestModule.complete.totalProcTime"))
238  .append("</td><td>").append(jobTotals.matchTime).append("</td></tr>\n"); //NON-NLS
239  detailsSb.append("<tr><td>") //NON-NLS
240  .append(NbBundle.getMessage(this.getClass(), "FileTypeIdIngestModule.complete.totalFiles"))
241  .append("</td><td>").append(jobTotals.numFiles).append("</td></tr>\n"); //NON-NLS
242  detailsSb.append("</table>"); //NON-NLS
244  NbBundle.getMessage(this.getClass(),
245  "FileTypeIdIngestModule.complete.srvMsg.text"),
246  detailsSb.toString()));
247  }
248  }
249  }
250 
258  private static synchronized void addToTotals(long jobId, long matchTimeInc) {
259  IngestJobTotals ingestJobTotals = totalsForIngestJobs.get(jobId);
260  if (ingestJobTotals == null) {
261  ingestJobTotals = new IngestJobTotals();
262  totalsForIngestJobs.put(jobId, ingestJobTotals);
263  }
264 
265  ingestJobTotals.matchTime += matchTimeInc;
266  ingestJobTotals.numFiles++;
267  totalsForIngestJobs.put(jobId, ingestJobTotals);
268  }
269 
270  private static class IngestJobTotals {
271 
272  long matchTime = 0;
273  long numFiles = 0;
274  }
275 }
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
FileType detectUserDefinedFileType(AbstractFile file, byte[] startOfFileBuffer, int bufLen)
static synchronized void addToTotals(long jobId, long matchTimeInc)
static synchronized IngestServices getInstance()

Copyright © 2012-2022 Basis Technology. Generated on: Tue Feb 6 2024
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.