Autopsy  4.5.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-2017 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;
47 import org.sleuthkit.datamodel.BlackboardArtifact;
48 import org.sleuthkit.datamodel.BlackboardAttribute;
49 import org.sleuthkit.datamodel.SleuthkitCase;
50 import org.sleuthkit.datamodel.SleuthkitCase.CaseDbQuery;
51 import org.sleuthkit.datamodel.TskCoreException;
52 
53 public class InterestingHits implements AutopsyVisitableItem {
54 
55  private static final String INTERESTING_ITEMS = NbBundle
56  .getMessage(InterestingHits.class, "InterestingHits.interestingItems.text");
57  private static final String DISPLAY_NAME = NbBundle.getMessage(InterestingHits.class, "InterestingHits.displayName.text");
58  private static final Logger logger = Logger.getLogger(InterestingHits.class.getName());
59  private SleuthkitCase skCase;
61 
62  public InterestingHits(SleuthkitCase skCase) {
63  this.skCase = skCase;
64  interestingResults.update();
65  }
66 
67  private class InterestingResults extends Observable {
68 
69  // NOTE: the map can be accessed by multiple worker threads and needs to be synchronized
70  private final Map<String, Map<String, Set<Long>>> interestingItemsMap = new LinkedHashMap<>();
71 
72  public List<String> getSetNames() {
73  List<String> setNames;
74  synchronized (interestingItemsMap) {
75  setNames = new ArrayList<>(interestingItemsMap.keySet());
76  }
77  Collections.sort(setNames);
78  return setNames;
79  }
80 
81  public Set<Long> getArtifactIds(String setName, String typeName) {
82  synchronized (interestingItemsMap) {
83  return interestingItemsMap.get(setName).get(typeName);
84  }
85  }
86 
87  public void update() {
88  synchronized (interestingItemsMap) {
89  interestingItemsMap.clear();
90  }
91  loadArtifacts(BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_FILE_HIT);
92  loadArtifacts(BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_ARTIFACT_HIT);
93  setChanged();
94  notifyObservers();
95  }
96 
97  /*
98  * Reads the artifacts of specified type, grouped by Set, and loads into
99  * the interestingItemsMap
100  */
101  @SuppressWarnings("deprecation")
102  private void loadArtifacts(BlackboardArtifact.ARTIFACT_TYPE artType) {
103  if (skCase == null) {
104  return;
105  }
106 
107  int setNameId = BlackboardAttribute.ATTRIBUTE_TYPE.TSK_SET_NAME.getTypeID();
108  int artId = artType.getTypeID();
109  String query = "SELECT value_text,blackboard_attributes.artifact_id,attribute_type_id " //NON-NLS
110  + "FROM blackboard_attributes,blackboard_artifacts WHERE " //NON-NLS
111  + "attribute_type_id=" + setNameId //NON-NLS
112  + " AND blackboard_attributes.artifact_id=blackboard_artifacts.artifact_id" //NON-NLS
113  + " AND blackboard_artifacts.artifact_type_id=" + artId; //NON-NLS
114 
115  try (CaseDbQuery dbQuery = skCase.executeQuery(query)) {
116  synchronized (interestingItemsMap) {
117  ResultSet resultSet = dbQuery.getResultSet();
118  while (resultSet.next()) {
119  String value = resultSet.getString("value_text"); //NON-NLS
120  long artifactId = resultSet.getLong("artifact_id"); //NON-NLS
121  if (!interestingItemsMap.containsKey(value)) {
122  interestingItemsMap.put(value, new LinkedHashMap<>());
123  interestingItemsMap.get(value).put(BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_FILE_HIT.getDisplayName(), new HashSet<>());
124  interestingItemsMap.get(value).put(BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_ARTIFACT_HIT.getDisplayName(), new HashSet<>());
125  }
126  interestingItemsMap.get(value).get(artType.getDisplayName()).add(artifactId);
127  }
128  }
129  } catch (TskCoreException | SQLException ex) {
130  logger.log(Level.WARNING, "SQL Exception occurred: ", ex); //NON-NLS
131  }
132  }
133  }
134 
135  @Override
136  public <T> T accept(AutopsyItemVisitor<T> v) {
137  return v.visit(this);
138  }
139 
143  public class RootNode extends DisplayableItemNode {
144 
145  public RootNode() {
146  super(Children.create(new SetNameFactory(), true), Lookups.singleton(DISPLAY_NAME));
147  super.setName(INTERESTING_ITEMS);
148  super.setDisplayName(DISPLAY_NAME);
149  this.setIconBaseWithExtension("org/sleuthkit/autopsy/images/interesting_item.png"); //NON-NLS
150  }
151 
152  @Override
153  public boolean isLeafTypeNode() {
154  return false;
155  }
156 
157  @Override
158  public <T> T accept(DisplayableItemNodeVisitor<T> v) {
159  return v.visit(this);
160  }
161 
162  @Override
163  protected Sheet createSheet() {
164  Sheet s = super.createSheet();
165  Sheet.Set ss = s.get(Sheet.PROPERTIES);
166  if (ss == null) {
167  ss = Sheet.createPropertiesSet();
168  s.put(ss);
169  }
170 
171  ss.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.name"),
172  NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.displayName"),
173  NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.desc"),
174  getName()));
175 
176  return s;
177  }
178 
179  @Override
180  public String getItemType() {
181  return getClass().getName();
182  }
183  }
184 
185  private class SetNameFactory extends ChildFactory.Detachable<String> implements Observer {
186 
187  /*
188  * This should probably be in the top-level class, but the factory has
189  * nice methods for its startup and shutdown, so it seemed like a
190  * cleaner place to register the property change listener.
191  */
192  private final PropertyChangeListener pcl = (PropertyChangeEvent evt) -> {
193  String eventType = evt.getPropertyName();
194  if (eventType.equals(IngestManager.IngestModuleEvent.DATA_ADDED.toString())) {
201  try {
209  ModuleDataEvent eventData = (ModuleDataEvent) evt.getOldValue();
210  if (null != eventData && (eventData.getBlackboardArtifactType().getTypeID() == BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_ARTIFACT_HIT.getTypeID()
211  || eventData.getBlackboardArtifactType().getTypeID() == BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_FILE_HIT.getTypeID())) {
212  interestingResults.update();
213  }
214  } catch (IllegalStateException notUsed) {
218  }
219  } else if (eventType.equals(IngestManager.IngestJobEvent.COMPLETED.toString())
220  || eventType.equals(IngestManager.IngestJobEvent.CANCELLED.toString())) {
227  try {
229  interestingResults.update();
230  } catch (IllegalStateException notUsed) {
234  }
235  } else if (eventType.equals(Case.Events.CURRENT_CASE.toString())) {
236  // case was closed. Remove listeners so that we don't get called with a stale case handle
237  if (evt.getNewValue() == null) {
238  removeNotify();
239  skCase = null;
240  }
241  }
242  };
243 
244  @Override
245  protected void addNotify() {
249  interestingResults.update();
250  interestingResults.addObserver(this);
251  }
252 
253  @Override
254  protected void removeNotify() {
258  interestingResults.deleteObserver(this);
259  }
260 
261  @Override
262  protected boolean createKeys(List<String> list) {
263  list.addAll(interestingResults.getSetNames());
264  return true;
265  }
266 
267  @Override
268  protected Node createNodeForKey(String key) {
269  return new SetNameNode(key);
270  }
271 
272  @Override
273  public void update(Observable o, Object arg) {
274  refresh(true);
275  }
276  }
277 
278  public class SetNameNode extends DisplayableItemNode implements Observer {
279 
280  private final String setName;
281 
282  public SetNameNode(String setName) {//, Set<Long> children) {
283  super(Children.create(new HitTypeFactory(setName), true), Lookups.singleton(setName));
284  this.setName = setName;
285  super.setName(setName);
287  this.setIconBaseWithExtension("org/sleuthkit/autopsy/images/interesting_item.png"); //NON-NLS
288  interestingResults.addObserver(this);
289  }
290 
291  private void updateDisplayName() {
292  int sizeOfSet = interestingResults.getArtifactIds(setName, BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_ARTIFACT_HIT.getDisplayName()).size()
293  + interestingResults.getArtifactIds(setName, BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_FILE_HIT.getDisplayName()).size();
294  super.setDisplayName(setName + " (" + sizeOfSet + ")");
295  }
296 
297  @Override
298  public boolean isLeafTypeNode() {
299  return false;
300  }
301 
302  @Override
303  protected Sheet createSheet() {
304  Sheet s = super.createSheet();
305  Sheet.Set ss = s.get(Sheet.PROPERTIES);
306  if (ss == null) {
307  ss = Sheet.createPropertiesSet();
308  s.put(ss);
309  }
310 
311  ss.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.name"),
312  NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.name"),
313  NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.desc"),
314  getName()));
315 
316  return s;
317  }
318 
319  @Override
320  public <T> T accept(DisplayableItemNodeVisitor<T> v) {
321  return v.visit(this);
322  }
323 
324  @Override
325  public void update(Observable o, Object arg) {
327  }
328 
329  @Override
330  public String getItemType() {
335  return getClass().getName();
336  }
337  }
338 
339  private class HitTypeFactory extends ChildFactory<String> implements Observer {
340 
341  private final String setName;
342  private final Map<Long, BlackboardArtifact> artifactHits = new HashMap<>();
343 
344  private HitTypeFactory(String setName) {
345  super();
346  this.setName = setName;
347  interestingResults.addObserver(this);
348  }
349 
350  @Override
351  protected boolean createKeys(List<String> list) {
352  list.add(BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_FILE_HIT.getDisplayName());
353  list.add(BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_ARTIFACT_HIT.getDisplayName());
354  return true;
355  }
356 
357  @Override
358  protected Node createNodeForKey(String key) {
359  return new InterestingItemTypeNode(setName, key);
360  }
361 
362  @Override
363  public void update(Observable o, Object arg) {
364  refresh(true);
365  }
366  }
367 
368  public class InterestingItemTypeNode extends DisplayableItemNode implements Observer {
369 
370  private final String typeName;
371  private final String setName;
372 
373  private InterestingItemTypeNode(String setName, String typeName) {
374  super(Children.create(new HitFactory(setName, typeName), true), Lookups.singleton(setName));
375  this.typeName = typeName;
376  this.setName = setName;
377  super.setName(typeName);
379  this.setIconBaseWithExtension("org/sleuthkit/autopsy/images/interesting_item.png"); //NON-NLS
380  interestingResults.addObserver(this);
381  }
382 
383  private void updateDisplayName() {
384  super.setDisplayName(typeName + " (" + interestingResults.getArtifactIds(setName, typeName).size() + ")");
385  }
386 
387  @Override
388  public boolean isLeafTypeNode() {
389  return true;
390  }
391 
392  @Override
393  protected Sheet createSheet() {
394  Sheet s = super.createSheet();
395  Sheet.Set ss = s.get(Sheet.PROPERTIES);
396  if (ss == null) {
397  ss = Sheet.createPropertiesSet();
398  s.put(ss);
399  }
400  ss.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.name"),
401  NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.name"),
402  NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.desc"),
403  getName()));
404  return s;
405  }
406 
407  @Override
408  public <T> T accept(DisplayableItemNodeVisitor<T> v) {
409  return v.visit(this);
410  }
411 
412  @Override
413  public void update(Observable o, Object arg) {
415  }
416 
417  @Override
418  public String getItemType() {
423  return getClass().getName();
424  }
425  }
426 
427  private class HitFactory extends ChildFactory<Long> implements Observer {
428 
429  private final String setName;
430  private final String typeName;
431  private final Map<Long, BlackboardArtifact> artifactHits = new HashMap<>();
432 
433  private HitFactory(String setName, String typeName) {
434  super();
435  this.setName = setName;
436  this.typeName = typeName;
437  interestingResults.addObserver(this);
438  }
439 
440  @Override
441  protected boolean createKeys(List<Long> list) {
442 
443  if (skCase == null) {
444  return true;
445  }
446 
447  interestingResults.getArtifactIds(setName, typeName).forEach((id) -> {
448  try {
449  if (!artifactHits.containsKey(id)) {
450  BlackboardArtifact art = skCase.getBlackboardArtifact(id);
451  artifactHits.put(id, art);
452  }
453  list.add(id);
454  } catch (TskCoreException ex) {
455  logger.log(Level.SEVERE, "TSK Exception occurred", ex); //NON-NLS
456  }
457  });
458  return true;
459  }
460 
461  @Override
462  protected Node createNodeForKey(Long l) {
463  BlackboardArtifact art = artifactHits.get(l);
464  return (null == art) ? null : new BlackboardArtifactNode(art);
465  }
466 
467  @Override
468  public void update(Observable o, Object arg) {
469  refresh(true);
470  }
471  }
472 }
BlackboardArtifact.Type getBlackboardArtifactType()
void removeIngestModuleEventListener(final PropertyChangeListener listener)
static synchronized IngestManager getInstance()
void loadArtifacts(BlackboardArtifact.ARTIFACT_TYPE artType)
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:419
static void removeEventTypeSubscriber(Set< Events > eventTypes, PropertyChangeListener subscriber)
Definition: Case.java:464
final Map< String, Map< String, Set< Long > > > interestingItemsMap

Copyright © 2012-2016 Basis Technology. Generated on: Tue Feb 20 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.