Autopsy  4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
ExifParserFileIngestModule.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-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.exif;
20 
21 import com.drew.imaging.ImageMetadataReader;
22 import com.drew.imaging.ImageProcessingException;
23 import com.drew.lang.GeoLocation;
24 import com.drew.lang.Rational;
25 import com.drew.metadata.Metadata;
26 import com.drew.metadata.exif.ExifIFD0Directory;
27 import com.drew.metadata.exif.ExifSubIFDDirectory;
28 import com.drew.metadata.exif.GpsDirectory;
29 import java.io.BufferedInputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.Date;
35 import java.util.HashSet;
36 import java.util.List;
37 import java.util.TimeZone;
38 import java.util.concurrent.atomic.AtomicInteger;
39 import java.util.logging.Level;
40 import org.openide.util.NbBundle;
41 import org.openide.util.NbBundle.Messages;
62 
68 @NbBundle.Messages({
69  "CannotRunFileTypeDetection=Cannot run file type detection."
70 })
71 public final class ExifParserFileIngestModule implements FileIngestModule {
72 
73  private static final Logger logger = Logger.getLogger(ExifParserFileIngestModule.class.getName());
74  private final IngestServices services = IngestServices.getInstance();
75  private final AtomicInteger filesProcessed = new AtomicInteger(0);
76  private volatile boolean filesToFire = false;
77  private final List<BlackboardArtifact> listOfFacesDetectedArtifacts = new ArrayList<>();
78  private long jobId;
79  private static final IngestModuleReferenceCounter refCounter = new IngestModuleReferenceCounter();
81  private final HashSet<String> supportedMimeTypes = new HashSet<>();
82  private TimeZone timeZone = null;
84 
86  supportedMimeTypes.add("audio/x-wav"); //NON-NLS
87  supportedMimeTypes.add("image/jpeg"); //NON-NLS
88  supportedMimeTypes.add("image/tiff"); //NON-NLS
89  }
90 
91  @Override
92  public void startUp(IngestJobContext context) throws IngestModuleException {
93  jobId = context.getJobId();
94  refCounter.incrementAndGet(jobId);
95  try {
96  fileTypeDetector = new FileTypeDetector();
98  throw new IngestModuleException(Bundle.CannotRunFileTypeDetection(), ex);
99  }
100  }
101 
102  @Override
104  blackboard = Case.getCurrentCase().getServices().getBlackboard();
105 
106  //skip unalloc
107  if ((content.getType().equals(TSK_DB_FILES_TYPE_ENUM.UNALLOC_BLOCKS) ||
108  (content.getType().equals(TSK_DB_FILES_TYPE_ENUM.SLACK)))) {
109  return ProcessResult.OK;
110  }
111 
112  if (content.isFile() == false) {
113  return ProcessResult.OK;
114  }
115 
116  // skip known
117  if (content.getKnown().equals(TskData.FileKnown.KNOWN)) {
118  return ProcessResult.OK;
119  }
120 
121  // update the tree every 1000 files if we have EXIF data that is not being being displayed
122  final int filesProcessedValue = filesProcessed.incrementAndGet();
123  if ((filesProcessedValue % 1000 == 0)) {
124  if (filesToFire) {
126  filesToFire = false;
127  }
128  }
129 
130  //skip unsupported
131  if (!parsableFormat(content)) {
132  return ProcessResult.OK;
133  }
134 
135  return processFile(content);
136  }
137 
138  @Messages({"ExifParserFileIngestModule.indexError.message=Failed to index EXIF Metadata artifact for keyword search."})
139  ProcessResult processFile(AbstractFile f) {
140  InputStream in = null;
141  BufferedInputStream bin = null;
142 
143  try {
144  in = new ReadContentInputStream(f);
145  bin = new BufferedInputStream(in);
146 
147  Collection<BlackboardAttribute> attributes = new ArrayList<>();
148  Metadata metadata = ImageMetadataReader.readMetadata(bin);
149 
150  // Date
151  ExifSubIFDDirectory exifDir = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class);
152  if (exifDir != null) {
153 
154  // set the timeZone for the current datasource.
155  if (timeZone == null) {
156  try {
157  Content dataSource = f.getDataSource();
158  if ((dataSource != null) && (dataSource instanceof Image)) {
159  Image image = (Image) dataSource;
160  timeZone = TimeZone.getTimeZone(image.getTimeZone());
161  }
162  } catch (TskCoreException ex) {
163  logger.log(Level.INFO, "Error getting time zones", ex); //NON-NLS
164  }
165  }
166  Date date = exifDir.getDate(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL, timeZone);
167  if (date != null) {
168  attributes.add(new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_DATETIME_CREATED, ExifParserModuleFactory.getModuleName(), date.getTime() / 1000));
169  }
170  }
171 
172  // GPS Stuff
173  GpsDirectory gpsDir = metadata.getFirstDirectoryOfType(GpsDirectory.class);
174  if (gpsDir != null) {
175  GeoLocation loc = gpsDir.getGeoLocation();
176  if (loc != null) {
177  double latitude = loc.getLatitude();
178  double longitude = loc.getLongitude();
179  attributes.add(new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_GEO_LATITUDE, ExifParserModuleFactory.getModuleName(), latitude));
180  attributes.add(new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_GEO_LONGITUDE, ExifParserModuleFactory.getModuleName(), longitude));
181  }
182 
183  Rational altitude = gpsDir.getRational(GpsDirectory.TAG_ALTITUDE);
184  if (altitude != null) {
185  attributes.add(new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_GEO_ALTITUDE, ExifParserModuleFactory.getModuleName(), altitude.doubleValue()));
186  }
187  }
188 
189  // Device info
190  ExifIFD0Directory devDir = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
191  if (devDir != null) {
192  String model = devDir.getString(ExifIFD0Directory.TAG_MODEL);
193  if (model != null && !model.isEmpty()) {
194  attributes.add(new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_DEVICE_MODEL, ExifParserModuleFactory.getModuleName(), model));
195  }
196 
197  String make = devDir.getString(ExifIFD0Directory.TAG_MAKE);
198  if (make != null && !make.isEmpty()) {
199  attributes.add(new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_DEVICE_MAKE, ExifParserModuleFactory.getModuleName(), make));
200  }
201  }
202 
203  // Add the attributes, if there are any, to a new artifact
204  if (!attributes.isEmpty()) {
205  BlackboardArtifact bba = f.newArtifact(BlackboardArtifact.ARTIFACT_TYPE.TSK_METADATA_EXIF);
206  bba.addAttributes(attributes);
207 
208  try {
209  // index the artifact for keyword search
210  blackboard.indexArtifact(bba);
211  } catch (Blackboard.BlackboardException ex) {
212  logger.log(Level.SEVERE, "Unable to index blackboard artifact " + bba.getArtifactID(), ex); //NON-NLS
213  MessageNotifyUtil.Notify.error(
214  Bundle.ExifParserFileIngestModule_indexError_message(), bba.getDisplayName());
215  }
216  filesToFire = true;
217  }
218 
219  return ProcessResult.OK;
220  } catch (TskCoreException ex) {
221  logger.log(Level.WARNING, "Failed to create blackboard artifact for exif metadata ({0}).", ex.getLocalizedMessage()); //NON-NLS
222  return ProcessResult.ERROR;
223  } catch (ImageProcessingException ex) {
224  logger.log(Level.WARNING, "Failed to process the image file: {0}/{1}({2})", new Object[]{f.getParentPath(), f.getName(), ex.getLocalizedMessage()}); //NON-NLS
225  return ProcessResult.ERROR;
226  } catch (IOException ex) {
227  logger.log(Level.WARNING, "IOException when parsing image file: " + f.getParentPath() + "/" + f.getName(), ex); //NON-NLS
228  return ProcessResult.ERROR;
229  } finally {
230  try {
231  if (in != null) {
232  in.close();
233  }
234  if (bin != null) {
235  bin.close();
236  }
237  } catch (IOException ex) {
238  logger.log(Level.WARNING, "Failed to close InputStream.", ex); //NON-NLS
239  return ProcessResult.ERROR;
240  }
241  }
242  }
243 
252  private boolean parsableFormat(AbstractFile f) {
253  try {
254  String mimeType = fileTypeDetector.getFileType(f);
255  if (mimeType != null) {
256  return supportedMimeTypes.contains(mimeType);
257  } else {
258  return false;
259  }
260  } catch (TskCoreException ex) {
261  logger.log(Level.SEVERE, "Failed to detect file type", ex); //NON-NLS
262  return false;
263  }
264  }
265 
266  @Override
267  public void shutDown() {
268  // We only need to check for this final event on the last module per job
269  if (refCounter.decrementAndGet(jobId) == 0) {
270  timeZone = null;
271  if (filesToFire) {
272  //send the final new data event
274  }
275  }
276  }
277 }
TskData.TSK_DB_FILES_TYPE_ENUM getType()
void fireModuleDataEvent(ModuleDataEvent moduleDataEvent)
synchronized void indexArtifact(BlackboardArtifact artifact)
Definition: Blackboard.java:59
synchronized static Logger getLogger(String name)
Definition: Logger.java:161
static synchronized IngestServices getInstance()

Copyright © 2012-2016 Basis Technology. Generated on: Mon Apr 24 2017
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.