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

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