Autopsy  4.5.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.ArrayList;
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.logging.Level;
26 import org.openide.util.NbBundle;
34 import org.sleuthkit.datamodel.AbstractFile;
37 import org.sleuthkit.datamodel.BlackboardArtifact;
38 import org.sleuthkit.datamodel.BlackboardAttribute;
39 import org.sleuthkit.datamodel.TskCoreException;
40 
45 @NbBundle.Messages({
46  "CannotRunFileTypeDetection=Unable to run file type detection."
47 })
48 public class FileTypeIdIngestModule implements FileIngestModule {
49 
50  private static final Logger logger = Logger.getLogger(FileTypeIdIngestModule.class.getName());
51  private long jobId;
52  private static final HashMap<Long, IngestJobTotals> totalsForIngestJobs = new HashMap<>();
53  private static final IngestModuleReferenceCounter refCounter = new IngestModuleReferenceCounter();
55 
65  @Deprecated
66  public static boolean isMimeTypeDetectable(String mimeType) {
67  try {
68  return new FileTypeDetector().isDetectable(mimeType);
70  logger.log(Level.SEVERE, "Failed to create file type detector", ex); //NON-NLS
71  return false;
72  }
73  }
74 
80  }
81 
82  @Override
83  public void startUp(IngestJobContext context) throws IngestModuleException {
84  jobId = context.getJobId();
85  refCounter.incrementAndGet(jobId);
86  try {
87  fileTypeDetector = new FileTypeDetector();
89  throw new IngestModuleException(Bundle.CannotRunFileTypeDetection(), ex);
90  }
91  }
92 
93  @Override
94  public ProcessResult process(AbstractFile file) {
100  try {
101  long startTime = System.currentTimeMillis();
102  String mimeType = fileTypeDetector.getMIMEType(file);
103  file.setMIMEType(mimeType);
104  FileType fileType = detectUserDefinedFileType(file);
105  if (fileType != null && fileType.createInterestingFileHit()) {
106  createInterestingFileHit(file, fileType);
107  }
108  addToTotals(jobId, (System.currentTimeMillis() - startTime));
109  return ProcessResult.OK;
110  } catch (Exception e) {
111  logger.log(Level.WARNING, String.format("Error while attempting to determine file type of file %d", file.getId()), e); //NON-NLS
112  return ProcessResult.ERROR;
113  }
114  }
115 
126  private FileType detectUserDefinedFileType(AbstractFile file) throws CustomFileTypesManager.CustomFileTypesException {
127  FileType retValue = null;
128 
129  CustomFileTypesManager customFileTypesManager = CustomFileTypesManager.getInstance();
130  List<FileType> fileTypesList = customFileTypesManager.getUserDefinedFileTypes();
131  for (FileType fileType : fileTypesList) {
132  if (fileType.matches(file)) {
133  retValue = fileType;
134  break;
135  }
136  }
137 
138  return retValue;
139  }
140 
147  private void createInterestingFileHit(AbstractFile file, FileType fileType) {
148  try {
149  BlackboardArtifact artifact;
150  artifact = file.newArtifact(BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_FILE_HIT);
151  Collection<BlackboardAttribute> attributes = new ArrayList<>();
152  BlackboardAttribute setNameAttribute = new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_SET_NAME, FileTypeIdModuleFactory.getModuleName(), fileType.getInterestingFilesSetName());
153  attributes.add(setNameAttribute);
154  BlackboardAttribute ruleNameAttribute = new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_CATEGORY, FileTypeIdModuleFactory.getModuleName(), fileType.getMimeType());
155  attributes.add(ruleNameAttribute);
156  artifact.addAttributes(attributes);
157  try {
159  } catch (Blackboard.BlackboardException ex) {
160  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
161  }
162  } catch (TskCoreException ex) {
163  logger.log(Level.SEVERE, String.format("Unable to create TSK_INTERESTING_FILE_HIT artifact for file (obj_id=%d)", file.getId()), ex); //NON-NLS
164  }
165  }
166 
167  @Override
168  public void shutDown() {
173  if (refCounter.decrementAndGet(jobId) == 0) {
174  IngestJobTotals jobTotals;
175  synchronized (this) {
176  jobTotals = totalsForIngestJobs.remove(jobId);
177  }
178  if (jobTotals != null) {
179  StringBuilder detailsSb = new StringBuilder();
180  detailsSb.append("<table border='0' cellpadding='4' width='280'>"); //NON-NLS
181  detailsSb.append("<tr><td>").append(FileTypeIdModuleFactory.getModuleName()).append("</td></tr>"); //NON-NLS
182  detailsSb.append("<tr><td>") //NON-NLS
183  .append(NbBundle.getMessage(this.getClass(), "FileTypeIdIngestModule.complete.totalProcTime"))
184  .append("</td><td>").append(jobTotals.matchTime).append("</td></tr>\n"); //NON-NLS
185  detailsSb.append("<tr><td>") //NON-NLS
186  .append(NbBundle.getMessage(this.getClass(), "FileTypeIdIngestModule.complete.totalFiles"))
187  .append("</td><td>").append(jobTotals.numFiles).append("</td></tr>\n"); //NON-NLS
188  detailsSb.append("</table>"); //NON-NLS
190  NbBundle.getMessage(this.getClass(),
191  "FileTypeIdIngestModule.complete.srvMsg.text"),
192  detailsSb.toString()));
193  }
194  }
195  }
196 
204  private static synchronized void addToTotals(long jobId, long matchTimeInc) {
205  IngestJobTotals ingestJobTotals = totalsForIngestJobs.get(jobId);
206  if (ingestJobTotals == null) {
207  ingestJobTotals = new IngestJobTotals();
208  totalsForIngestJobs.put(jobId, ingestJobTotals);
209  }
210 
211  ingestJobTotals.matchTime += matchTimeInc;
212  ingestJobTotals.numFiles++;
213  totalsForIngestJobs.put(jobId, ingestJobTotals);
214  }
215 
216  private static class IngestJobTotals {
217 
218  long matchTime = 0;
219  long numFiles = 0;
220  }
221 
222 }
static IngestMessage createMessage(MessageType messageType, String source, String subject, String detailsHtml)
void postMessage(final IngestMessage message)
synchronized void indexArtifact(BlackboardArtifact artifact)
Definition: Blackboard.java:59
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static synchronized void addToTotals(long jobId, long matchTimeInc)
static synchronized IngestServices getInstance()

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