Autopsy  4.16.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
RecentFilesSummary.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2020 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.datasourcesummary.datamodel;
20 
22 import java.nio.file.Paths;
23 import java.text.DateFormat;
24 import java.text.SimpleDateFormat;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Locale;
31 import java.util.Objects;
32 import java.util.Set;
33 import java.util.SortedMap;
34 import java.util.TreeMap;
35 import org.sleuthkit.datamodel.AbstractFile;
36 import org.sleuthkit.datamodel.BlackboardArtifact;
37 import org.sleuthkit.datamodel.BlackboardAttribute;
38 import org.sleuthkit.datamodel.Content;
39 import org.sleuthkit.datamodel.DataSource;
40 import org.sleuthkit.datamodel.SleuthkitCase;
41 import org.sleuthkit.datamodel.TskCoreException;
43 import org.sleuthkit.datamodel.BlackboardArtifact.ARTIFACT_TYPE;
44 
49 
50  private final static BlackboardAttribute.Type DATETIME_ACCESSED_ATT = new BlackboardAttribute.Type(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_ACCESSED);
51  private final static BlackboardAttribute.Type DOMAIN_ATT = new BlackboardAttribute.Type(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DOMAIN);
52  private final static BlackboardAttribute.Type PATH_ATT = new BlackboardAttribute.Type(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PATH);
53  private final static BlackboardAttribute.Type DATETIME_ATT = new BlackboardAttribute.Type(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME);
54  private final static BlackboardAttribute.Type ASSOCATED_ATT = new BlackboardAttribute.Type(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_ASSOCIATED_ARTIFACT);
55  private final static BlackboardAttribute.Type EMAIL_FROM_ATT = new BlackboardAttribute.Type(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_EMAIL_FROM);
56  private final static BlackboardAttribute.Type MSG_DATEIME_SENT_ATT = new BlackboardAttribute.Type(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_SENT);
57  private final static BlackboardArtifact.Type ASSOCATED_OBJ_ART = new BlackboardArtifact.Type(ARTIFACT_TYPE.TSK_ASSOCIATED_OBJECT);
58 
59  private static final DateFormat DATETIME_FORMAT = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.getDefault());
60 
61  private static final Set<Integer> ARTIFACT_UPDATE_TYPE_IDS = new HashSet<>(Arrays.asList(
62  ARTIFACT_TYPE.TSK_RECENT_OBJECT.getTypeID(),
63  ARTIFACT_TYPE.TSK_WEB_DOWNLOAD.getTypeID(),
64  ARTIFACT_TYPE.TSK_ASSOCIATED_OBJECT.getTypeID(),
65  ARTIFACT_TYPE.TSK_EMAIL_MSG.getTypeID(),
66  ARTIFACT_TYPE.TSK_MESSAGE.getTypeID()
67  ));
68 
70 
74  public RecentFilesSummary() {
76  }
77 
84  if (provider == null) {
85  throw new IllegalArgumentException("Unable to construct RecentFileSummary object. SleuthkitCaseProvider cannot be null");
86  }
87 
88  this.provider = provider;
89  }
90 
91  @Override
92  public Set<Integer> getArtifactTypeIdsForRefresh() {
94  }
95 
110  public List<RecentFileDetails> getRecentlyOpenedDocuments(DataSource dataSource, int maxCount) throws SleuthkitCaseProviderException, TskCoreException {
111  if (dataSource == null) {
112  return Collections.emptyList();
113  }
114 
115  List<BlackboardArtifact> artifactList
116  = DataSourceInfoUtilities.getArtifacts(provider.get(),
117  new BlackboardArtifact.Type(ARTIFACT_TYPE.TSK_RECENT_OBJECT),
118  dataSource,
119  DATETIME_ATT,
120  DataSourceInfoUtilities.SortOrder.DESCENDING,
121  10);
122 
123  List<RecentFileDetails> fileDetails = new ArrayList<>();
124  for (BlackboardArtifact artifact : artifactList) {
125  Long accessedTime = null;
126  String path = "";
127 
128  // Get all the attributes in one call.
129  List<BlackboardAttribute> attributeList = artifact.getAttributes();
130  for (BlackboardAttribute attribute : attributeList) {
131 
132  if (attribute.getAttributeType().equals(DATETIME_ATT)) {
133  accessedTime = attribute.getValueLong();
134  } else if (attribute.getAttributeType().equals(PATH_ATT)) {
135  path = attribute.getValueString();
136  }
137 
138  if (accessedTime != null) {
139  fileDetails.add(new RecentFileDetails(path, accessedTime));
140  }
141  }
142 
143  }
144 
145  return fileDetails;
146  }
147 
162  public List<RecentDownloadDetails> getRecentDownloads(DataSource dataSource, int maxCount) throws TskCoreException, SleuthkitCaseProviderException {
163  if (dataSource == null) {
164  return Collections.emptyList();
165  }
166 
167  List<BlackboardArtifact> artifactList
168  = DataSourceInfoUtilities.getArtifacts(provider.get(),
169  new BlackboardArtifact.Type(ARTIFACT_TYPE.TSK_WEB_DOWNLOAD),
170  dataSource,
172  DataSourceInfoUtilities.SortOrder.DESCENDING,
173  maxCount);
174 
175  List<RecentDownloadDetails> fileDetails = new ArrayList<>();
176  for (BlackboardArtifact artifact : artifactList) {
177  // Get all the attributes in one call.
178  Long accessedTime = null;
179  String domain = "";
180  String path = "";
181 
182  List<BlackboardAttribute> attributeList = artifact.getAttributes();
183  for (BlackboardAttribute attribute : attributeList) {
184 
185  if (attribute.getAttributeType().equals(DATETIME_ACCESSED_ATT)) {
186  accessedTime = attribute.getValueLong();
187  } else if (attribute.getAttributeType().equals(DOMAIN_ATT)) {
188  domain = attribute.getValueString();
189  } else if (attribute.getAttributeType().equals(PATH_ATT)) {
190  path = attribute.getValueString();
191  }
192  }
193  if (accessedTime != null) {
194  fileDetails.add(new RecentDownloadDetails(path, accessedTime, domain));
195  }
196  }
197 
198  return fileDetails;
199  }
200 
213  public List<RecentAttachmentDetails> getRecentAttachments(DataSource dataSource, int maxCount) throws SleuthkitCaseProviderException, TskCoreException {
214  if (dataSource == null) {
215  return Collections.emptyList();
216  }
217 
218  return createListFromMap(buildAttachmentMap(dataSource), maxCount);
219  }
220 
232  private SortedMap<Long, List<RecentAttachmentDetails>> buildAttachmentMap(DataSource dataSource) throws SleuthkitCaseProviderException, TskCoreException {
233  SleuthkitCase skCase = provider.get();
234  TreeMap<Long, List<RecentAttachmentDetails>> sortedMap = new TreeMap<>();
235 
236  List<BlackboardArtifact> associatedArtifacts = skCase.getBlackboard().getArtifacts(ASSOCATED_OBJ_ART.getTypeID(), dataSource.getId());
237  for (BlackboardArtifact artifact : associatedArtifacts) {
238  BlackboardAttribute attribute = artifact.getAttribute(ASSOCATED_ATT);
239  if (attribute == null) {
240  continue;
241  }
242 
243  BlackboardArtifact messageArtifact = skCase.getBlackboardArtifact(attribute.getValueLong());
244  if (isMessageArtifact(messageArtifact)) {
245  Content content = artifact.getParent();
246  if (content instanceof AbstractFile) {
247  String sender;
248  Long date = null;
249  String path;
250 
251  BlackboardAttribute senderAttribute = messageArtifact.getAttribute(EMAIL_FROM_ATT);
252  if (senderAttribute != null) {
253  sender = senderAttribute.getValueString();
254  } else {
255  sender = "";
256  }
257  senderAttribute = messageArtifact.getAttribute(MSG_DATEIME_SENT_ATT);
258  if (senderAttribute != null) {
259  date = senderAttribute.getValueLong();
260  }
261 
262  AbstractFile abstractFile = (AbstractFile) content;
263 
264  path = Paths.get(abstractFile.getParentPath(), abstractFile.getName()).toString();
265 
266  if (date != null && date != 0) {
267  List<RecentAttachmentDetails> list = sortedMap.get(date);
268  if (list == null) {
269  list = new ArrayList<>();
270  sortedMap.put(date, list);
271  }
272  RecentAttachmentDetails details = new RecentAttachmentDetails(path, date, sender);
273  if (!list.contains(details)) {
274  list.add(details);
275  }
276  }
277  }
278  }
279  }
280  return sortedMap.descendingMap();
281  }
282 
293  private List<RecentAttachmentDetails> createListFromMap(SortedMap<Long, List<RecentAttachmentDetails>> sortedMap, int maxCount) {
294  List<RecentAttachmentDetails> fileList = new ArrayList<>();
295 
296  for (List<RecentAttachmentDetails> mapList : sortedMap.values()) {
297  if (maxCount == 0 || fileList.size() + mapList.size() <= maxCount) {
298  fileList.addAll(mapList);
299  continue;
300  }
301 
302  if (maxCount == fileList.size()) {
303  break;
304  }
305 
306  for (RecentAttachmentDetails details : mapList) {
307  if (fileList.size() < maxCount) {
308  fileList.add(details);
309  } else {
310  break;
311  }
312  }
313  }
314 
315  return fileList;
316  }
317 
326  private boolean isMessageArtifact(BlackboardArtifact nodeArtifact) {
327  final int artifactTypeID = nodeArtifact.getArtifactTypeID();
328  return artifactTypeID == ARTIFACT_TYPE.TSK_EMAIL_MSG.getTypeID()
329  || artifactTypeID == ARTIFACT_TYPE.TSK_MESSAGE.getTypeID();
330  }
331 
335  public static class RecentFileDetails {
336 
337  private final String path;
338  private final long date;
339 
346  RecentFileDetails(String path, long date) {
347  this.path = path;
348  this.date = date;
349  }
350 
357  public String getDateAsString() {
358  return DATETIME_FORMAT.format(date * 1000);
359  }
360 
366  Long getDateAsLong() {
367  return date;
368  }
369 
375  public String getPath() {
376  return path;
377  }
378 
379  }
380 
384  public static class RecentDownloadDetails extends RecentFileDetails {
385 
386  private final String webDomain;
387 
395  RecentDownloadDetails(String path, long date, String webDomain) {
396  super(path, date);
397  this.webDomain = webDomain;
398  }
399 
406  public String getWebDomain() {
407  return webDomain;
408  }
409  }
410 
414  public static class RecentAttachmentDetails extends RecentFileDetails {
415 
416  private final String sender;
417 
427  RecentAttachmentDetails(String path, long date, String sender) {
428  super(path, date);
429  this.sender = sender;
430  }
431 
438  public String getSender() {
439  return sender;
440  }
441 
442  @Override
443  public boolean equals(Object obj) {
444  if (!(obj instanceof RecentAttachmentDetails)) {
445  return false;
446  }
447  RecentAttachmentDetails compareObj = (RecentAttachmentDetails) obj;
448 
449  return compareObj.getSender().equals(this.sender)
450  && compareObj.getPath().equals(this.getPath())
451  && compareObj.getDateAsLong().equals(this.getDateAsLong());
452  }
453 
454  @Override
455  public int hashCode() {
456  int hash = 5;
457  hash = 73 * hash + Objects.hashCode(this.sender);
458  return hash;
459  }
460  }
461 }
SortedMap< Long, List< RecentAttachmentDetails > > buildAttachmentMap(DataSource dataSource)
List< RecentFileDetails > getRecentlyOpenedDocuments(DataSource dataSource, int maxCount)
List< RecentAttachmentDetails > createListFromMap(SortedMap< Long, List< RecentAttachmentDetails >> sortedMap, int maxCount)
List< RecentDownloadDetails > getRecentDownloads(DataSource dataSource, int maxCount)
List< RecentAttachmentDetails > getRecentAttachments(DataSource dataSource, int maxCount)

Copyright © 2012-2020 Basis Technology. Generated on: Tue Sep 22 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.