Autopsy  4.12.0
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-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.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.util.ArrayList;
32 import java.util.Collection;
33 import java.util.Date;
34 import java.util.HashSet;
35 import java.util.TimeZone;
36 import java.util.logging.Level;
37 import org.apache.commons.lang3.StringUtils;
38 import org.openide.util.NbBundle;
39 import org.openide.util.NbBundle.Messages;
48 import org.sleuthkit.datamodel.AbstractFile;
49 import org.sleuthkit.datamodel.Blackboard;
50 import org.sleuthkit.datamodel.BlackboardArtifact;
51 import static org.sleuthkit.datamodel.BlackboardArtifact.ARTIFACT_TYPE.TSK_METADATA_EXIF;
52 import org.sleuthkit.datamodel.BlackboardAttribute;
53 import static org.sleuthkit.datamodel.BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_CREATED;
54 import static org.sleuthkit.datamodel.BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DEVICE_MAKE;
55 import static org.sleuthkit.datamodel.BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DEVICE_MODEL;
56 import static org.sleuthkit.datamodel.BlackboardAttribute.ATTRIBUTE_TYPE.TSK_GEO_ALTITUDE;
57 import static org.sleuthkit.datamodel.BlackboardAttribute.ATTRIBUTE_TYPE.TSK_GEO_LATITUDE;
58 import static org.sleuthkit.datamodel.BlackboardAttribute.ATTRIBUTE_TYPE.TSK_GEO_LONGITUDE;
59 import org.sleuthkit.datamodel.Content;
60 import org.sleuthkit.datamodel.Image;
61 import org.sleuthkit.datamodel.ReadContentInputStream;
62 import org.sleuthkit.datamodel.ReadContentInputStream.ReadContentInputStreamException;
63 import org.sleuthkit.datamodel.TskCoreException;
64 import org.sleuthkit.datamodel.TskData;
65 import org.sleuthkit.datamodel.TskData.TSK_DB_FILES_TYPE_ENUM;
66 
72 @NbBundle.Messages({"CannotRunFileTypeDetection=Cannot run file type detection."})
73 public final class ExifParserFileIngestModule implements FileIngestModule {
74 
75  private static final Logger logger = Logger.getLogger(ExifParserFileIngestModule.class.getName());
76  private static final String MODULE_NAME = ExifParserModuleFactory.getModuleName();
77  private long jobId;
78  private static final IngestModuleReferenceCounter refCounter = new IngestModuleReferenceCounter();
80  private final HashSet<String> supportedMimeTypes = new HashSet<>();
81  private TimeZone timeZone = null;
82  private Blackboard blackboard;
83 
85  supportedMimeTypes.add("audio/x-wav"); //NON-NLS
86  supportedMimeTypes.add("image/jpeg"); //NON-NLS
87  supportedMimeTypes.add("image/tiff"); //NON-NLS
88  }
89 
90  @Override
91  public void startUp(IngestJobContext context) throws IngestModuleException {
92  jobId = context.getJobId();
93  refCounter.incrementAndGet(jobId);
94  try {
95  fileTypeDetector = new FileTypeDetector();
97  throw new IngestModuleException(Bundle.CannotRunFileTypeDetection(), ex);
98  }
99  }
100 
101  @Messages({"ExifParserFileIngestModule.indexError.message=Failed to post EXIF Metadata artifact(s)."})
102  @Override
103  public ProcessResult process(AbstractFile content) {
104  try {
105  blackboard = Case.getCurrentCaseThrows().getSleuthkitCase().getBlackboard();
106  } catch (NoCurrentCaseException ex) {
107  logger.log(Level.INFO, "Exception while getting open case.", ex); //NON-NLS
108  return ProcessResult.ERROR;
109  }
110  //skip unalloc
111  if ((content.getType().equals(TSK_DB_FILES_TYPE_ENUM.UNALLOC_BLOCKS)
112  || (content.getType().equals(TSK_DB_FILES_TYPE_ENUM.SLACK)))) {
113  return ProcessResult.OK;
114  }
115 
116  if (content.isFile() == false) {
117  return ProcessResult.OK;
118  }
119 
120  // skip known
121  if (content.getKnown().equals(TskData.FileKnown.KNOWN)) {
122  return ProcessResult.OK;
123  }
124 
125  //skip unsupported
126  if (!parsableFormat(content)) {
127  return ProcessResult.OK;
128  }
129 
130  return processFile(content);
131  }
132 
133  private ProcessResult processFile(AbstractFile file) {
134 
135  try (BufferedInputStream bin = new BufferedInputStream(new ReadContentInputStream(file));) {
136 
137  Collection<BlackboardAttribute> attributes = new ArrayList<>();
138  Metadata metadata = ImageMetadataReader.readMetadata(bin);
139 
140  // Date
141  ExifSubIFDDirectory exifDir = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class);
142  if (exifDir != null) {
143 
144  // set the timeZone for the current datasource.
145  if (timeZone == null) {
146  try {
147  Content dataSource = file.getDataSource();
148  if ((dataSource != null) && (dataSource instanceof Image)) {
149  Image image = (Image) dataSource;
150  timeZone = TimeZone.getTimeZone(image.getTimeZone());
151  }
152  } catch (TskCoreException ex) {
153  logger.log(Level.INFO, "Error getting time zones", ex); //NON-NLS
154  }
155  }
156  Date date = exifDir.getDate(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL, timeZone);
157  if (date != null) {
158  attributes.add(new BlackboardAttribute(TSK_DATETIME_CREATED, MODULE_NAME, date.getTime() / 1000));
159  }
160  }
161 
162  // GPS Stuff
163  GpsDirectory gpsDir = metadata.getFirstDirectoryOfType(GpsDirectory.class);
164  if (gpsDir != null) {
165  GeoLocation loc = gpsDir.getGeoLocation();
166  if (loc != null) {
167  attributes.add(new BlackboardAttribute(TSK_GEO_LATITUDE, MODULE_NAME, loc.getLatitude()));
168  attributes.add(new BlackboardAttribute(TSK_GEO_LONGITUDE, MODULE_NAME, loc.getLongitude()));
169  }
170 
171  Rational altitude = gpsDir.getRational(GpsDirectory.TAG_ALTITUDE);
172  if (altitude != null) {
173  attributes.add(new BlackboardAttribute(TSK_GEO_ALTITUDE, MODULE_NAME, altitude.doubleValue()));
174  }
175  }
176 
177  // Device info
178  ExifIFD0Directory devDir = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
179  if (devDir != null) {
180  String model = devDir.getString(ExifIFD0Directory.TAG_MODEL);
181  if (StringUtils.isNotBlank(model)) {
182  attributes.add(new BlackboardAttribute(TSK_DEVICE_MODEL, MODULE_NAME, model));
183  }
184 
185  String make = devDir.getString(ExifIFD0Directory.TAG_MAKE);
186  if (StringUtils.isNotBlank(make)) {
187  attributes.add(new BlackboardAttribute(TSK_DEVICE_MAKE, MODULE_NAME, make));
188  }
189  }
190 
191  // Add the attributes, if there are any, to a new artifact
192  if (!attributes.isEmpty()) {
193  // Create artifact if it doesn't already exist.
194  if (!blackboard.artifactExists(file, TSK_METADATA_EXIF, attributes)) {
195  BlackboardArtifact bba = file.newArtifact(TSK_METADATA_EXIF);
196  bba.addAttributes(attributes);
197 
198  try {
199  // index the artifact for keyword search
200  blackboard.postArtifact(bba, MODULE_NAME);
201  } catch (Blackboard.BlackboardException ex) {
202  logger.log(Level.SEVERE, "Unable to index blackboard artifact " + bba.getArtifactID(), ex); //NON-NLS
204  Bundle.ExifParserFileIngestModule_indexError_message(), bba.getDisplayName());
205  }
206  }
207  }
208 
209  return ProcessResult.OK;
210  } catch (TskCoreException ex) {
211  logger.log(Level.WARNING, "Failed to create blackboard artifact for exif metadata ({0}).", ex.getLocalizedMessage()); //NON-NLS
212  return ProcessResult.ERROR;
213  } catch (ImageProcessingException ex) {
214  logger.log(Level.WARNING, String.format("Failed to process the image file '%s/%s' (id=%d).", file.getParentPath(), file.getName(), file.getId()), ex);
215  return ProcessResult.ERROR;
216  } catch (ReadContentInputStreamException ex) {
217  logger.log(Level.WARNING, String.format("Error while trying to read image file '%s/%s' (id=%d).", file.getParentPath(), file.getName(), file.getId()), ex); //NON-NLS
218  return ProcessResult.ERROR;
219  } catch (IOException ex) {
220  logger.log(Level.WARNING, String.format("IOException when parsing image file '%s/%s' (id=%d).", file.getParentPath(), file.getName(), file.getId()), ex); //NON-NLS
221  return ProcessResult.ERROR;
222  }
223  }
224 
233  private boolean parsableFormat(AbstractFile f) {
234  String mimeType = fileTypeDetector.getMIMEType(f);
235  return supportedMimeTypes.contains(mimeType);
236  }
237 
238  @Override
239  public void shutDown() {
240  // We only need to check for this final event on the last module per job
241  if (refCounter.decrementAndGet(jobId) == 0) {
242  timeZone = null;
243  }
244  }
245 }
static void error(String title, String message)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2018 Basis Technology. Generated on: Wed Sep 18 2019
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.