Autopsy  4.11.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
InterestingHits.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2019 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.datamodel;
20 
21 import java.beans.PropertyChangeEvent;
22 import java.beans.PropertyChangeListener;
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.EnumSet;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.LinkedHashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Observable;
34 import java.util.Observer;
35 import java.util.Set;
36 import java.util.logging.Level;
37 import org.openide.nodes.ChildFactory;
38 import org.openide.nodes.Children;
39 import org.openide.nodes.Node;
40 import org.openide.nodes.Sheet;
41 import org.openide.util.NbBundle;
42 import org.openide.util.lookup.Lookups;
48 import org.sleuthkit.datamodel.BlackboardArtifact;
49 import org.sleuthkit.datamodel.BlackboardAttribute;
50 import org.sleuthkit.datamodel.SleuthkitCase;
51 import org.sleuthkit.datamodel.SleuthkitCase.CaseDbQuery;
52 import org.sleuthkit.datamodel.TskCoreException;
53 
54 public class InterestingHits implements AutopsyVisitableItem {
55 
56  private static final String INTERESTING_ITEMS = NbBundle
57  .getMessage(InterestingHits.class, "InterestingHits.interestingItems.text");
58  private static final String DISPLAY_NAME = NbBundle.getMessage(InterestingHits.class, "InterestingHits.displayName.text");
59  private static final Logger logger = Logger.getLogger(InterestingHits.class.getName());
60  private SleuthkitCase skCase;
62  private final long filteringDSObjId; // 0 if not filtering/grouping by data source
63 
70  public InterestingHits(SleuthkitCase skCase) {
71  this(skCase, 0);
72  }
73 
81  public InterestingHits(SleuthkitCase skCase, long objId) {
82  this.skCase = skCase;
83  this.filteringDSObjId = objId;
84  interestingResults.update();
85  }
86 
87  private class InterestingResults extends Observable {
88 
89  // NOTE: the map can be accessed by multiple worker threads and needs to be synchronized
90  private final Map<String, Map<String, Set<Long>>> interestingItemsMap = new LinkedHashMap<>();
91 
92  public List<String> getSetNames() {
93  List<String> setNames;
94  synchronized (interestingItemsMap) {
95  setNames = new ArrayList<>(interestingItemsMap.keySet());
96  }
97  Collections.sort(setNames);
98  return setNames;
99  }
100 
101  public Set<Long> getArtifactIds(String setName, String typeName) {
102  synchronized (interestingItemsMap) {
103  return interestingItemsMap.get(setName).get(typeName);
104  }
105  }
106 
107  public void update() {
108  synchronized (interestingItemsMap) {
109  interestingItemsMap.clear();
110  }
111  loadArtifacts(BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_FILE_HIT);
112  loadArtifacts(BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_ARTIFACT_HIT);
113  setChanged();
114  notifyObservers();
115  }
116 
117  /*
118  * Reads the artifacts of specified type, grouped by Set, and loads into
119  * the interestingItemsMap
120  */
121  @SuppressWarnings("deprecation")
122  private void loadArtifacts(BlackboardArtifact.ARTIFACT_TYPE artType) {
123  if (skCase == null) {
124  return;
125  }
126 
127  int setNameId = BlackboardAttribute.ATTRIBUTE_TYPE.TSK_SET_NAME.getTypeID();
128  int artId = artType.getTypeID();
129  String query = "SELECT value_text,blackboard_attributes.artifact_id,attribute_type_id " //NON-NLS
130  + "FROM blackboard_attributes,blackboard_artifacts WHERE " //NON-NLS
131  + "attribute_type_id=" + setNameId //NON-NLS
132  + " AND blackboard_attributes.artifact_id=blackboard_artifacts.artifact_id" //NON-NLS
133  + " AND blackboard_artifacts.artifact_type_id=" + artId; //NON-NLS
134  if (filteringDSObjId > 0) {
135  query += " AND blackboard_artifacts.data_source_obj_id = " + filteringDSObjId;
136  }
137 
138  try (CaseDbQuery dbQuery = skCase.executeQuery(query)) {
139  synchronized (interestingItemsMap) {
140  ResultSet resultSet = dbQuery.getResultSet();
141  while (resultSet.next()) {
142  String value = resultSet.getString("value_text"); //NON-NLS
143  long artifactId = resultSet.getLong("artifact_id"); //NON-NLS
144  if (!interestingItemsMap.containsKey(value)) {
145  interestingItemsMap.put(value, new LinkedHashMap<>());
146  interestingItemsMap.get(value).put(BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_FILE_HIT.getDisplayName(), new HashSet<>());
147  interestingItemsMap.get(value).put(BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_ARTIFACT_HIT.getDisplayName(), new HashSet<>());
148  }
149  interestingItemsMap.get(value).get(artType.getDisplayName()).add(artifactId);
150  }
151  }
152  } catch (TskCoreException | SQLException ex) {
153  logger.log(Level.WARNING, "SQL Exception occurred: ", ex); //NON-NLS
154  }
155  }
156  }
157 
158  @Override
159  public <T> T accept(AutopsyItemVisitor<T> visitor) {
160  return visitor.visit(this);
161  }
162 
166  public class RootNode extends DisplayableItemNode {
167 
168  public RootNode() {
169  super(Children.create(new SetNameFactory(), true), Lookups.singleton(DISPLAY_NAME));
170  super.setName(INTERESTING_ITEMS);
171  super.setDisplayName(DISPLAY_NAME);
172  this.setIconBaseWithExtension("org/sleuthkit/autopsy/images/interesting_item.png"); //NON-NLS
173  }
174 
175  @Override
176  public boolean isLeafTypeNode() {
177  return false;
178  }
179 
180  @Override
181  public <T> T accept(DisplayableItemNodeVisitor<T> visitor) {
182  return visitor.visit(this);
183  }
184 
185  @Override
186  protected Sheet createSheet() {
187  Sheet sheet = super.createSheet();
188  Sheet.Set sheetSet = sheet.get(Sheet.PROPERTIES);
189  if (sheetSet == null) {
190  sheetSet = Sheet.createPropertiesSet();
191  sheet.put(sheetSet);
192  }
193 
194  sheetSet.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.name"),
195  NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.displayName"),
196  NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.desc"),
197  getName()));
198 
199  return sheet;
200  }
201 
202  @Override
203  public String getItemType() {
204  return getClass().getName();
205  }
206  }
207 
208  private class SetNameFactory extends ChildFactory.Detachable<String> implements Observer {
209 
210  /*
211  * This should probably be in the top-level class, but the factory has
212  * nice methods for its startup and shutdown, so it seemed like a
213  * cleaner place to register the property change listener.
214  */
215  private final PropertyChangeListener pcl = (PropertyChangeEvent evt) -> {
216  String eventType = evt.getPropertyName();
217  if (eventType.equals(IngestManager.IngestModuleEvent.DATA_ADDED.toString())) {
224  try {
232  ModuleDataEvent eventData = (ModuleDataEvent) evt.getOldValue();
233  if (null != eventData && (eventData.getBlackboardArtifactType().getTypeID() == BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_ARTIFACT_HIT.getTypeID()
234  || eventData.getBlackboardArtifactType().getTypeID() == BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_FILE_HIT.getTypeID())) {
235  interestingResults.update();
236  }
237  } catch (NoCurrentCaseException notUsed) {
241  }
242  } else if (eventType.equals(IngestManager.IngestJobEvent.COMPLETED.toString())
243  || eventType.equals(IngestManager.IngestJobEvent.CANCELLED.toString())) {
250  try {
252  interestingResults.update();
253  } catch (NoCurrentCaseException notUsed) {
257  }
258  } else if (eventType.equals(Case.Events.CURRENT_CASE.toString())) {
259  // case was closed. Remove listeners so that we don't get called with a stale case handle
260  if (evt.getNewValue() == null) {
261  removeNotify();
262  skCase = null;
263  }
264  }
265  };
266 
267  @Override
268  protected void addNotify() {
272  interestingResults.update();
273  interestingResults.addObserver(this);
274  }
275 
276  @Override
277  protected void removeNotify() {
281  interestingResults.deleteObserver(this);
282  }
283 
284  @Override
285  protected boolean createKeys(List<String> list) {
286  list.addAll(interestingResults.getSetNames());
287  return true;
288  }
289 
290  @Override
291  protected Node createNodeForKey(String key) {
292  return new SetNameNode(key);
293  }
294 
295  @Override
296  public void update(Observable o, Object arg) {
297  refresh(true);
298  }
299  }
300 
301  public class SetNameNode extends DisplayableItemNode implements Observer {
302 
303  private final String setName;
304 
305  public SetNameNode(String setName) {//, Set<Long> children) {
306  super(Children.create(new HitTypeFactory(setName), true), Lookups.singleton(setName));
307  this.setName = setName;
308  super.setName(setName);
310  this.setIconBaseWithExtension("org/sleuthkit/autopsy/images/interesting_item.png"); //NON-NLS
311  interestingResults.addObserver(this);
312  }
313 
314  private void updateDisplayName() {
315  int sizeOfSet = interestingResults.getArtifactIds(setName, BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_ARTIFACT_HIT.getDisplayName()).size()
316  + interestingResults.getArtifactIds(setName, BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_FILE_HIT.getDisplayName()).size();
317  super.setDisplayName(setName + " (" + sizeOfSet + ")");
318  }
319 
320  @Override
321  public boolean isLeafTypeNode() {
322  return false;
323  }
324 
325  @Override
326  protected Sheet createSheet() {
327  Sheet sheet = super.createSheet();
328  Sheet.Set sheetSet = sheet.get(Sheet.PROPERTIES);
329  if (sheetSet == null) {
330  sheetSet = Sheet.createPropertiesSet();
331  sheet.put(sheetSet);
332  }
333 
334  sheetSet.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.name"),
335  NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.name"),
336  NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.desc"),
337  getName()));
338 
339  return sheet;
340  }
341 
342  @Override
343  public <T> T accept(DisplayableItemNodeVisitor<T> visitor) {
344  return visitor.visit(this);
345  }
346 
347  @Override
348  public void update(Observable o, Object arg) {
350  }
351 
352  @Override
353  public String getItemType() {
358  return getClass().getName();
359  }
360  }
361 
362  private class HitTypeFactory extends ChildFactory<String> implements Observer {
363 
364  private final String setName;
365  private final Map<Long, BlackboardArtifact> artifactHits = new HashMap<>();
366 
367  private HitTypeFactory(String setName) {
368  super();
369  this.setName = setName;
370  interestingResults.addObserver(this);
371  }
372 
373  @Override
374  protected boolean createKeys(List<String> list) {
375  list.add(BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_FILE_HIT.getDisplayName());
376  list.add(BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_ARTIFACT_HIT.getDisplayName());
377  return true;
378  }
379 
380  @Override
381  protected Node createNodeForKey(String key) {
382  return new InterestingItemTypeNode(setName, key);
383  }
384 
385  @Override
386  public void update(Observable o, Object arg) {
387  refresh(true);
388  }
389  }
390 
391  public class InterestingItemTypeNode extends DisplayableItemNode implements Observer {
392 
393  private final String typeName;
394  private final String setName;
395 
396  private InterestingItemTypeNode(String setName, String typeName) {
397  super(Children.create(new HitFactory(setName, typeName), true), Lookups.singleton(setName));
398  this.typeName = typeName;
399  this.setName = setName;
400  super.setName(typeName);
402  this.setIconBaseWithExtension("org/sleuthkit/autopsy/images/interesting_item.png"); //NON-NLS
403  interestingResults.addObserver(this);
404  }
405 
406  private void updateDisplayName() {
407  super.setDisplayName(typeName + " (" + interestingResults.getArtifactIds(setName, typeName).size() + ")");
408  }
409 
410  @Override
411  public boolean isLeafTypeNode() {
412  return true;
413  }
414 
415  @Override
416  protected Sheet createSheet() {
417  Sheet sheet = super.createSheet();
418  Sheet.Set sheetSet = sheet.get(Sheet.PROPERTIES);
419  if (sheetSet == null) {
420  sheetSet = Sheet.createPropertiesSet();
421  sheet.put(sheetSet);
422  }
423  sheetSet.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.name"),
424  NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.name"),
425  NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.desc"),
426  getName()));
427  return sheet;
428  }
429 
430  @Override
431  public <T> T accept(DisplayableItemNodeVisitor<T> visitor) {
432  return visitor.visit(this);
433  }
434 
435  @Override
436  public void update(Observable o, Object arg) {
438  }
439 
440  @Override
441  public String getItemType() {
446  return getClass().getName();
447  }
448  }
449 
450  private class HitFactory extends BaseChildFactory<BlackboardArtifact> implements Observer {
451 
452  private final String setName;
453  private final String typeName;
454  private final Map<Long, BlackboardArtifact> artifactHits = new HashMap<>();
455 
456  private HitFactory(String setName, String typeName) {
457  super(typeName);
458  this.setName = setName;
459  this.typeName = typeName;
460  interestingResults.addObserver(this);
461  }
462 
463  @Override
464  protected List<BlackboardArtifact> makeKeys() {
465 
466  if (skCase != null) {
467  interestingResults.getArtifactIds(setName, typeName).forEach((id) -> {
468  try {
469  if (!artifactHits.containsKey(id)) {
470  BlackboardArtifact art = skCase.getBlackboardArtifact(id);
471  artifactHits.put(id, art);
472  }
473  } catch (TskCoreException ex) {
474  logger.log(Level.SEVERE, "TSK Exception occurred", ex); //NON-NLS
475  }
476  });
477 
478  return new ArrayList<>(artifactHits.values());
479  }
480  return Collections.emptyList();
481  }
482 
483  @Override
484  protected Node createNodeForKey(BlackboardArtifact art) {
485  return new BlackboardArtifactNode(art);
486  }
487 
488  @Override
489  public void update(Observable o, Object arg) {
490  refresh(true);
491  }
492 
493  @Override
494  protected void onAdd() {
495  // No-op
496  }
497 
498  @Override
499  protected void onRemove() {
500  // No-op
501  }
502  }
503 }
BlackboardArtifact.Type getBlackboardArtifactType()
void removeIngestModuleEventListener(final PropertyChangeListener listener)
static synchronized IngestManager getInstance()
void loadArtifacts(BlackboardArtifact.ARTIFACT_TYPE artType)
InterestingHits(SleuthkitCase skCase, long objId)
void removeIngestJobEventListener(final PropertyChangeListener listener)
void addIngestJobEventListener(final PropertyChangeListener listener)
Set< Long > getArtifactIds(String setName, String typeName)
void addIngestModuleEventListener(final PropertyChangeListener listener)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static void addEventTypeSubscriber(Set< Events > eventTypes, PropertyChangeListener subscriber)
Definition: Case.java:441
static void removeEventTypeSubscriber(Set< Events > eventTypes, PropertyChangeListener subscriber)
Definition: Case.java:486
final Map< String, Map< String, Set< Long > > > interestingItemsMap

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