Autopsy  4.7.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
HashsetHits.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.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.BlackboardArtifact.ARTIFACT_TYPE;
50 import org.sleuthkit.datamodel.BlackboardAttribute.ATTRIBUTE_TYPE;
51 import org.sleuthkit.datamodel.SleuthkitCase;
52 import org.sleuthkit.datamodel.SleuthkitCase.CaseDbQuery;
53 import org.sleuthkit.datamodel.TskCoreException;
54 import org.sleuthkit.datamodel.TskException;
55 
59 public class HashsetHits implements AutopsyVisitableItem {
60 
61  private static final String HASHSET_HITS = BlackboardArtifact.ARTIFACT_TYPE.TSK_HASHSET_HIT.getLabel();
62  private static final String DISPLAY_NAME = BlackboardArtifact.ARTIFACT_TYPE.TSK_HASHSET_HIT.getDisplayName();
63  private static final Logger logger = Logger.getLogger(HashsetHits.class.getName());
64  private SleuthkitCase skCase;
66 
67  public HashsetHits(SleuthkitCase skCase) {
68  this.skCase = skCase;
69  hashsetResults = new HashsetResults();
70  }
71 
72  @Override
73  public <T> T accept(AutopsyItemVisitor<T> visitor) {
74  return visitor.visit(this);
75  }
76 
81  private class HashsetResults extends Observable {
82 
83  // maps hashset name to list of artifacts for that set
84  // NOTE: the map can be accessed by multiple worker threads and needs to be synchronized
85  private final Map<String, Set<Long>> hashSetHitsMap = new LinkedHashMap<>();
86 
87  HashsetResults() {
88  update();
89  }
90 
91  List<String> getSetNames() {
92  List<String> names;
93  synchronized (hashSetHitsMap) {
94  names = new ArrayList<>(hashSetHitsMap.keySet());
95  }
96  Collections.sort(names);
97  return names;
98  }
99 
100  Set<Long> getArtifactIds(String hashSetName) {
101  synchronized (hashSetHitsMap) {
102  return hashSetHitsMap.get(hashSetName);
103  }
104  }
105 
106  @SuppressWarnings("deprecation")
107  final void update() {
108  synchronized (hashSetHitsMap) {
109  hashSetHitsMap.clear();
110  }
111 
112  if (skCase == null) {
113  return;
114  }
115 
116  int setNameId = ATTRIBUTE_TYPE.TSK_SET_NAME.getTypeID();
117  int artId = ARTIFACT_TYPE.TSK_HASHSET_HIT.getTypeID();
118  String query = "SELECT value_text,blackboard_attributes.artifact_id,attribute_type_id " //NON-NLS
119  + "FROM blackboard_attributes,blackboard_artifacts WHERE " //NON-NLS
120  + "attribute_type_id=" + setNameId //NON-NLS
121  + " AND blackboard_attributes.artifact_id=blackboard_artifacts.artifact_id" //NON-NLS
122  + " AND blackboard_artifacts.artifact_type_id=" + artId; //NON-NLS
123 
124  try (CaseDbQuery dbQuery = skCase.executeQuery(query)) {
125  ResultSet resultSet = dbQuery.getResultSet();
126  synchronized (hashSetHitsMap) {
127  while (resultSet.next()) {
128  String setName = resultSet.getString("value_text"); //NON-NLS
129  long artifactId = resultSet.getLong("artifact_id"); //NON-NLS
130  if (!hashSetHitsMap.containsKey(setName)) {
131  hashSetHitsMap.put(setName, new HashSet<Long>());
132  }
133  hashSetHitsMap.get(setName).add(artifactId);
134  }
135  }
136  } catch (TskCoreException | SQLException ex) {
137  logger.log(Level.WARNING, "SQL Exception occurred: ", ex); //NON-NLS
138  }
139 
140  setChanged();
141  notifyObservers();
142  }
143  }
144 
148  public class RootNode extends DisplayableItemNode {
149 
150  public RootNode() {
151  super(Children.create(new HashsetNameFactory(), true), Lookups.singleton(DISPLAY_NAME));
152  super.setName(HASHSET_HITS);
153  super.setDisplayName(DISPLAY_NAME);
154  this.setIconBaseWithExtension("org/sleuthkit/autopsy/images/hashset_hits.png"); //NON-NLS
155  }
156 
157  @Override
158  public boolean isLeafTypeNode() {
159  return false;
160  }
161 
162  @Override
163  public <T> T accept(DisplayableItemNodeVisitor<T> visitor) {
164  return visitor.visit(this);
165  }
166 
167  @Override
168  protected Sheet createSheet() {
169  Sheet sheet = super.createSheet();
170  Sheet.Set sheetSet = sheet.get(Sheet.PROPERTIES);
171  if (sheetSet == null) {
172  sheetSet = Sheet.createPropertiesSet();
173  sheet.put(sheetSet);
174  }
175 
176  sheetSet.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "HashsetHits.createSheet.name.name"),
177  NbBundle.getMessage(this.getClass(), "HashsetHits.createSheet.name.displayName"),
178  NbBundle.getMessage(this.getClass(), "HashsetHits.createSheet.name.desc"),
179  getName()));
180 
181  return sheet;
182  }
183 
184  @Override
185  public String getItemType() {
186  return getClass().getName();
187  }
188  }
189 
193  private class HashsetNameFactory extends ChildFactory.Detachable<String> implements Observer {
194 
195  /*
196  * This should probably be in the HashsetHits class, but the factory has
197  * nice methods for its startup and shutdown, so it seemed like a
198  * cleaner place to register the property change listener.
199  */
200  private final PropertyChangeListener pcl = new PropertyChangeListener() {
201  @Override
202  public void propertyChange(PropertyChangeEvent evt) {
203  String eventType = evt.getPropertyName();
204  if (eventType.equals(IngestManager.IngestModuleEvent.DATA_ADDED.toString())) {
211  try {
218  ModuleDataEvent eventData = (ModuleDataEvent) evt.getOldValue();
219  if (null != eventData && eventData.getBlackboardArtifactType().getTypeID() == ARTIFACT_TYPE.TSK_HASHSET_HIT.getTypeID()) {
220  hashsetResults.update();
221  }
222  } catch (NoCurrentCaseException notUsed) {
226  }
227  } else if (eventType.equals(IngestManager.IngestJobEvent.COMPLETED.toString())
228  || eventType.equals(IngestManager.IngestJobEvent.CANCELLED.toString())) {
235  try {
237  hashsetResults.update();
238  } catch (NoCurrentCaseException notUsed) {
242  }
243  } else if (eventType.equals(Case.Events.CURRENT_CASE.toString())) {
244  // case was closed. Remove listeners so that we don't get called with a stale case handle
245  if (evt.getNewValue() == null) {
246  removeNotify();
247  skCase = null;
248  }
249  }
250  }
251  };
252 
253  @Override
254  protected void addNotify() {
258  hashsetResults.update();
259  hashsetResults.addObserver(this);
260  }
261 
262  @Override
263  protected void removeNotify() {
267  hashsetResults.deleteObserver(this);
268  }
269 
270  @Override
271  protected boolean createKeys(List<String> list) {
272  list.addAll(hashsetResults.getSetNames());
273  return true;
274  }
275 
276  @Override
277  protected Node createNodeForKey(String key) {
278  return new HashsetNameNode(key);
279  }
280 
281  @Override
282  public void update(Observable o, Object arg) {
283  refresh(true);
284  }
285  }
286 
290  public class HashsetNameNode extends DisplayableItemNode implements Observer {
291 
292  private final String hashSetName;
293 
294  public HashsetNameNode(String hashSetName) {
295  super(Children.create(new HitFactory(hashSetName), true), Lookups.singleton(hashSetName));
296  super.setName(hashSetName);
297  this.hashSetName = hashSetName;
299  this.setIconBaseWithExtension("org/sleuthkit/autopsy/images/hashset_hits.png"); //NON-NLS
300  hashsetResults.addObserver(this);
301  }
302 
306  private void updateDisplayName() {
307  super.setDisplayName(hashSetName + " (" + hashsetResults.getArtifactIds(hashSetName).size() + ")");
308  }
309 
310  @Override
311  public boolean isLeafTypeNode() {
312  return true;
313  }
314 
315  @Override
316  protected Sheet createSheet() {
317  Sheet sheet = super.createSheet();
318  Sheet.Set sheetSet = sheet.get(Sheet.PROPERTIES);
319  if (sheetSet == null) {
320  sheetSet = Sheet.createPropertiesSet();
321  sheet.put(sheetSet);
322  }
323 
324  sheetSet.put(new NodeProperty<>(NbBundle.getMessage(this.getClass(), "HashsetHits.createSheet.name.name"),
325  NbBundle.getMessage(this.getClass(), "HashsetHits.createSheet.name.displayName"),
326  NbBundle.getMessage(this.getClass(), "HashsetHits.createSheet.name.desc"),
327  getName()));
328 
329  return sheet;
330  }
331 
332  @Override
333  public <T> T accept(DisplayableItemNodeVisitor<T> visitor) {
334  return visitor.visit(this);
335  }
336 
337  @Override
338  public void update(Observable o, Object arg) {
340  }
341 
342  @Override
343  public String getItemType() {
348  return getClass().getName();
349  }
350  }
351 
355  private class HitFactory extends ChildFactory.Detachable<Long> implements Observer {
356 
357  private String hashsetName;
358  private Map<Long, BlackboardArtifact> artifactHits = new HashMap<>();
359 
360  private HitFactory(String hashsetName) {
361  super();
362  this.hashsetName = hashsetName;
363  }
364 
365  @Override
366  protected void addNotify() {
367  hashsetResults.addObserver(this);
368  }
369 
370  @Override
371  protected void removeNotify() {
372  hashsetResults.deleteObserver(this);
373  }
374 
375  @Override
376  protected boolean createKeys(List<Long> list) {
377 
378  if (skCase == null) {
379  return true;
380  }
381 
382  hashsetResults.getArtifactIds(hashsetName).forEach((id) -> {
383  try {
384  if (!artifactHits.containsKey(id)) {
385  BlackboardArtifact art = skCase.getBlackboardArtifact(id);
386  artifactHits.put(id, art);
387  }
388  list.add(id);
389  } catch (TskException ex) {
390  logger.log(Level.SEVERE, "TSK Exception occurred", ex); //NON-NLS
391  }
392  });
393  return true;
394  }
395 
396  @Override
397  protected Node createNodeForKey(Long id) {
398  BlackboardArtifact art = artifactHits.get(id);
399  return (null == art) ? null : new BlackboardArtifactNode(art);
400  }
401 
402  @Override
403  public void update(Observable o, Object arg) {
404  refresh(true);
405  }
406  }
407 }
BlackboardArtifact.Type getBlackboardArtifactType()
void removeIngestModuleEventListener(final PropertyChangeListener listener)
static synchronized IngestManager getInstance()
void removeIngestJobEventListener(final PropertyChangeListener listener)
void addIngestJobEventListener(final PropertyChangeListener listener)
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:420
static void removeEventTypeSubscriber(Set< Events > eventTypes, PropertyChangeListener subscriber)
Definition: Case.java:465

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