Autopsy  4.1
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-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.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.HashSet;
28 import java.util.LinkedHashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Observable;
32 import java.util.Observer;
33 import java.util.Set;
34 import java.util.logging.Level;
35 import org.openide.nodes.ChildFactory;
36 import org.openide.nodes.Children;
37 import org.openide.nodes.Node;
38 import org.openide.nodes.Sheet;
39 import org.openide.util.NbBundle;
40 import org.openide.util.lookup.Lookups;
45 import org.sleuthkit.datamodel.BlackboardArtifact;
46 import org.sleuthkit.datamodel.BlackboardAttribute;
47 import org.sleuthkit.datamodel.SleuthkitCase;
48 import org.sleuthkit.datamodel.SleuthkitCase.CaseDbQuery;
49 import org.sleuthkit.datamodel.TskCoreException;
50 
51 public class InterestingHits implements AutopsyVisitableItem {
52 
53  private static final String INTERESTING_ITEMS = NbBundle
54  .getMessage(InterestingHits.class, "InterestingHits.interestingItems.text");
55  private static final String DISPLAY_NAME = NbBundle.getMessage(InterestingHits.class, "InterestingHits.displayName.text");
56  private static final Logger logger = Logger.getLogger(InterestingHits.class.getName());
57  private SleuthkitCase skCase;
59 
60  public InterestingHits(SleuthkitCase skCase) {
61  this.skCase = skCase;
62  interestingResults.update();
63  }
64 
65  private class InterestingResults extends Observable {
66 
67  // NOTE: the map can be accessed by multiple worker threads and needs to be synchronized
68  private final Map<String, Set<Long>> interestingItemsMap = new LinkedHashMap<>();
69 
70  public List<String> getSetNames() {
71  List<String> setNames;
72  synchronized (interestingItemsMap) {
73  setNames = new ArrayList<>(interestingItemsMap.keySet());
74  }
75  Collections.sort(setNames);
76  return setNames;
77  }
78 
79  public Set<Long> getArtifactIds(String setName) {
80  synchronized (interestingItemsMap) {
81  return interestingItemsMap.get(setName);
82  }
83  }
84 
85  public void update() {
86  synchronized (interestingItemsMap) {
87  interestingItemsMap.clear();
88  }
89  loadArtifacts(BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_FILE_HIT);
90  loadArtifacts(BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_ARTIFACT_HIT);
91  setChanged();
92  notifyObservers();
93  }
94 
95  /*
96  * Reads the artifacts of specified type, grouped by Set, and loads into
97  * the interestingItemsMap
98  */
99  @SuppressWarnings("deprecation")
100  private void loadArtifacts(BlackboardArtifact.ARTIFACT_TYPE artType) {
101  if (skCase == null) {
102  return;
103  }
104 
105  int setNameId = BlackboardAttribute.ATTRIBUTE_TYPE.TSK_SET_NAME.getTypeID();
106  int artId = artType.getTypeID();
107  String query = "SELECT value_text,blackboard_attributes.artifact_id,attribute_type_id " //NON-NLS
108  + "FROM blackboard_attributes,blackboard_artifacts WHERE " //NON-NLS
109  + "attribute_type_id=" + setNameId //NON-NLS
110  + " AND blackboard_attributes.artifact_id=blackboard_artifacts.artifact_id" //NON-NLS
111  + " AND blackboard_artifacts.artifact_type_id=" + artId; //NON-NLS
112 
113  try (CaseDbQuery dbQuery = skCase.executeQuery(query)) {
114  synchronized (interestingItemsMap) {
115  ResultSet resultSet = dbQuery.getResultSet();
116  while (resultSet.next()) {
117  String value = resultSet.getString("value_text"); //NON-NLS
118  long artifactId = resultSet.getLong("artifact_id"); //NON-NLS
119  if (!interestingItemsMap.containsKey(value)) {
120  interestingItemsMap.put(value, new HashSet<>());
121  }
122  interestingItemsMap.get(value).add(artifactId);
123  }
124  }
125  } catch (TskCoreException | SQLException ex) {
126  logger.log(Level.WARNING, "SQL Exception occurred: ", ex); //NON-NLS
127  }
128  }
129  }
130 
131  @Override
132  public <T> T accept(AutopsyItemVisitor<T> v) {
133  return v.visit(this);
134  }
135 
139  public class RootNode extends DisplayableItemNode {
140 
141  public RootNode() {
142  super(Children.create(new SetNameFactory(), true), Lookups.singleton(DISPLAY_NAME));
143  super.setName(INTERESTING_ITEMS);
144  super.setDisplayName(DISPLAY_NAME);
145  this.setIconBaseWithExtension("org/sleuthkit/autopsy/images/interesting_item.png"); //NON-NLS
146  }
147 
148  @Override
149  public boolean isLeafTypeNode() {
150  return false;
151  }
152 
153  @Override
154  public <T> T accept(DisplayableItemNodeVisitor<T> v) {
155  return v.visit(this);
156  }
157 
158  @Override
159  protected Sheet createSheet() {
160  Sheet s = super.createSheet();
161  Sheet.Set ss = s.get(Sheet.PROPERTIES);
162  if (ss == null) {
163  ss = Sheet.createPropertiesSet();
164  s.put(ss);
165  }
166 
167  ss.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.name"),
168  NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.displayName"),
169  NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.desc"),
170  getName()));
171 
172  return s;
173  }
174 
175  @Override
176  public String getItemType() {
177  return getClass().getName();
178  }
179  }
180 
181  private class SetNameFactory extends ChildFactory.Detachable<String> implements Observer {
182 
183  /*
184  * This should probably be in the top-level class, but the factory has
185  * nice methods for its startup and shutdown, so it seemed like a
186  * cleaner place to register the property change listener.
187  */
188  private final PropertyChangeListener pcl = new PropertyChangeListener() {
189  @Override
190  public void propertyChange(PropertyChangeEvent evt) {
191  String eventType = evt.getPropertyName();
192  if (eventType.equals(IngestManager.IngestModuleEvent.DATA_ADDED.toString())) {
199  try {
207  ModuleDataEvent eventData = (ModuleDataEvent) evt.getOldValue();
208  if (null != eventData && (eventData.getBlackboardArtifactType().getTypeID() == BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_ARTIFACT_HIT.getTypeID()
209  || eventData.getBlackboardArtifactType().getTypeID() == BlackboardArtifact.ARTIFACT_TYPE.TSK_INTERESTING_FILE_HIT.getTypeID())) {
210  interestingResults.update();
211  }
212  } catch (IllegalStateException notUsed) {
216  }
217  } else if (eventType.equals(IngestManager.IngestJobEvent.COMPLETED.toString())
218  || eventType.equals(IngestManager.IngestJobEvent.CANCELLED.toString())) {
225  try {
227  interestingResults.update();
228  } catch (IllegalStateException notUsed) {
232  }
233  } else if (eventType.equals(Case.Events.CURRENT_CASE.toString())) {
234  // case was closed. Remove listeners so that we don't get called with a stale case handle
235  if (evt.getNewValue() == null) {
236  removeNotify();
237  skCase = null;
238  }
239  }
240  }
241  };
242 
243  @Override
244  protected void addNotify() {
248  interestingResults.update();
249  interestingResults.addObserver(this);
250  }
251 
252  @Override
253  protected void removeNotify() {
257  interestingResults.deleteObserver(this);
258  }
259 
260  @Override
261  protected boolean createKeys(List<String> list) {
262  list.addAll(interestingResults.getSetNames());
263  return true;
264  }
265 
266  @Override
267  protected Node createNodeForKey(String key) {
268  return new SetNameNode(key);
269  }
270 
271  @Override
272  public void update(Observable o, Object arg) {
273  refresh(true);
274  }
275  }
276 
277  public class SetNameNode extends DisplayableItemNode implements Observer {
278 
279  private final String setName;
280 
281  public SetNameNode(String setName) {//, Set<Long> children) {
282  super(Children.create(new HitFactory(setName), true), Lookups.singleton(setName));
283  this.setName = setName;
284  super.setName(setName);
286  this.setIconBaseWithExtension("org/sleuthkit/autopsy/images/interesting_item.png"); //NON-NLS
287  interestingResults.addObserver(this);
288  }
289 
290  private void updateDisplayName() {
291  super.setDisplayName(setName + " (" + interestingResults.getArtifactIds(setName).size() + ")");
292  }
293 
294  @Override
295  public boolean isLeafTypeNode() {
296  return true;
297  }
298 
299  @Override
300  protected Sheet createSheet() {
301  Sheet s = super.createSheet();
302  Sheet.Set ss = s.get(Sheet.PROPERTIES);
303  if (ss == null) {
304  ss = Sheet.createPropertiesSet();
305  s.put(ss);
306  }
307 
308  ss.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.name"),
309  NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.name"),
310  NbBundle.getMessage(this.getClass(), "InterestingHits.createSheet.name.desc"),
311  getName()));
312 
313  return s;
314  }
315 
316  @Override
317  public <T> T accept(DisplayableItemNodeVisitor<T> v) {
318  return v.visit(this);
319  }
320 
321  @Override
322  public void update(Observable o, Object arg) {
324  }
325 
326  @Override
327  public String getItemType() {
332  return getClass().getName();
333  }
334  }
335 
336  private class HitFactory extends ChildFactory<Long> implements Observer {
337 
338  private final String setName;
339 
340  private HitFactory(String setName) {
341  super();
342  this.setName = setName;
343  interestingResults.addObserver(this);
344  }
345 
346  @Override
347  protected boolean createKeys(List<Long> list) {
348  for (long l : interestingResults.getArtifactIds(setName)) {
349  list.add(l);
350  }
351  return true;
352  }
353 
354  @Override
355  protected Node createNodeForKey(Long l) {
356  if (skCase == null) {
357  return null;
358  }
359  try {
360  return new BlackboardArtifactNode(skCase.getBlackboardArtifact(l));
361  } catch (TskCoreException ex) {
362  logger.log(Level.SEVERE, "Error creating new Blackboard Artifact node", ex); //NON-NLS
363  return null;
364  }
365  }
366 
367  @Override
368  public void update(Observable o, Object arg) {
369  refresh(true);
370  }
371  }
372 }
BlackboardArtifact.Type getBlackboardArtifactType()
void removeIngestModuleEventListener(final PropertyChangeListener listener)
static synchronized IngestManager getInstance()
static void removePropertyChangeListener(PropertyChangeListener listener)
Definition: Case.java:318
void loadArtifacts(BlackboardArtifact.ARTIFACT_TYPE artType)
void removeIngestJobEventListener(final PropertyChangeListener listener)
void addIngestJobEventListener(final PropertyChangeListener listener)
static void addPropertyChangeListener(PropertyChangeListener listener)
Definition: Case.java:306
void addIngestModuleEventListener(final PropertyChangeListener listener)
synchronized static Logger getLogger(String name)
Definition: Logger.java:161

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