Autopsy  3.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
FileTypeDetector.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2014-2015 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.Map;
23 import java.util.SortedSet;
24 import org.apache.tika.Tika;
25 import org.apache.tika.mime.MediaType;
26 import org.apache.tika.mime.MimeTypes;
32 
36 public class FileTypeDetector {
37 
38  private static final Tika tika = new Tika();
39  private static final int BUFFER_SIZE = 64 * 1024;
40  private final byte buffer[] = new byte[BUFFER_SIZE];
41  private final Map<String, FileType> userDefinedFileTypes;
42 
51  try {
52  userDefinedFileTypes = UserDefinedFileTypesManager.getInstance().getFileTypes();
53  } catch (UserDefinedFileTypesManager.UserDefinedFileTypesException ex) {
54  throw new FileTypeDetectorInitException("Error loading user-defined file types", ex); //NON-NLS
55  }
56  }
57 
65  public boolean isDetectable(String mimeType) {
66  return isDetectableAsUserDefinedType(mimeType) || isDetectableByTika(mimeType);
67  }
68 
76  private boolean isDetectableAsUserDefinedType(String mimeType) {
77  return userDefinedFileTypes.containsKey(mimeType);
78  }
79 
86  private boolean isDetectableByTika(String mimeType) {
87  String[] split = mimeType.split("/");
88  if (split.length == 2) {
89  String type = split[0];
90  String subtype = split[1];
91  MediaType mediaType = new MediaType(type, subtype);
92  SortedSet<MediaType> m = MimeTypes.getDefaultMimeTypes().getMediaTypeRegistry().getTypes();
93  return m.contains(mediaType);
94  }
95  return false;
96  }
97 
107  public String getFileType(AbstractFile file) throws TskCoreException {
108  String fileType;
109  ArrayList<BlackboardAttribute> attributes = file.getGenInfoAttributes(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_FILE_TYPE_SIG);
110  for (BlackboardAttribute attribute : attributes) {
114  fileType = attribute.getValueString();
115  if (null != fileType && !fileType.isEmpty()) {
116  return fileType;
117  }
118  }
119  return detectAndPostToBlackboard(file);
120  }
121 
132  String mimeType = detect(file);
133  if (null != mimeType) {
140  BlackboardArtifact getInfoArt = file.getGenInfoArtifact();
142  getInfoArt.addAttribute(batt);
143  }
144  return mimeType;
145  }
146 
154  public String detect(AbstractFile file) throws TskCoreException {
155  // consistently mark non-regular files (refer TskData.TSK_FS_META_TYPE_ENUM),
156  // 0 sized files, unallocated, and unused blocks (refer TskData.TSK_DB_FILES_TYPE_ENUM)
157  // as octet-stream.
158  if (!file.isFile() || file.getSize() <= 0
159  || (file.getType() == TskData.TSK_DB_FILES_TYPE_ENUM.UNALLOC_BLOCKS)
160  || (file.getType() == TskData.TSK_DB_FILES_TYPE_ENUM.UNUSED_BLOCKS)) {
161  return MimeTypes.OCTET_STREAM;
162  }
163 
164  String fileType = detectUserDefinedType(file);
165  if (null == fileType) {
166  try {
167  byte buf[];
168  int len = file.read(buffer, 0, BUFFER_SIZE);
169  if (len < BUFFER_SIZE) {
170  buf = new byte[len];
171  System.arraycopy(buffer, 0, buf, 0, len);
172  } else {
173  buf = buffer;
174  }
175 
176  String mimetype = tika.detect(buf, file.getName());
177 
181  return mimetype.replace("tika-", ""); //NON-NLS
182 
183  } catch (Exception ignored) {
190  }
191  }
192  return fileType;
193  }
194 
206  for (FileType fileType : userDefinedFileTypes.values()) {
207  if (fileType.matches(file)) {
208  if (fileType.alertOnMatch()) {
209  BlackboardArtifact artifact;
210  artifact = file.newArtifact(BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_FILE_HIT);
211  BlackboardAttribute setNameAttribute = new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_SET_NAME.getTypeID(), FileTypeIdModuleFactory.getModuleName(), fileType.getFilesSetName());
212  artifact.addAttribute(setNameAttribute);
213 
219  BlackboardAttribute ruleNameAttribute = new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_CATEGORY.getTypeID(), FileTypeIdModuleFactory.getModuleName(), fileType.getMimeType());
220  artifact.addAttribute(ruleNameAttribute);
221  }
222  return fileType.getMimeType();
223  }
224  }
225  return null;
226  }
227 
228  public static class FileTypeDetectorInitException extends Exception {
229 
230  FileTypeDetectorInitException(String message) {
231  super(message);
232  }
233 
234  FileTypeDetectorInitException(String message, Throwable throwable) {
235  super(message, throwable);
236  }
237  }
238 
239 }
void addAttribute(BlackboardAttribute attr)

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.