Autopsy  4.5.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
QueryResults.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.keywordsearch;
20 
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.logging.Level;
28 import java.util.stream.Collectors;
29 import javax.swing.SwingWorker;
30 import org.apache.commons.lang.StringUtils;
31 import org.netbeans.api.progress.ProgressHandle;
32 import org.netbeans.api.progress.aggregate.ProgressContributor;
33 import org.openide.util.NbBundle;
40 import org.sleuthkit.datamodel.AbstractFile;
41 import org.sleuthkit.datamodel.BlackboardArtifact;
42 import org.sleuthkit.datamodel.BlackboardAttribute;
43 import org.sleuthkit.datamodel.Content;
44 import org.sleuthkit.datamodel.SleuthkitCase;
45 import org.sleuthkit.datamodel.TskCoreException;
46 
53 class QueryResults {
54 
55  private static final Logger logger = Logger.getLogger(QueryResults.class.getName());
56  private static final String MODULE_NAME = KeywordSearchModuleFactory.getModuleName();
57  private final KeywordSearchQuery query;
58  private final Map<Keyword, List<KeywordHit>> results = new HashMap<>();
59 
70  QueryResults(KeywordSearchQuery query) {
71  this.query = query;
72  }
73 
80  KeywordSearchQuery getQuery() {
81  return query;
82  }
83 
92  void addResult(Keyword keyword, List<KeywordHit> hits) {
93  results.put(keyword, hits);
94  }
95 
103  List<KeywordHit> getResults(Keyword keyword) {
104  return results.get(keyword);
105  }
106 
113  Set<Keyword> getKeywords() {
114  return results.keySet();
115  }
116 
145  void process(ProgressHandle progress, ProgressContributor subProgress, SwingWorker<?, ?> worker, boolean notifyInbox) {
146  /*
147  * Initialize the progress indicator to the number of keywords that will
148  * be processed.
149  */
150  if (null != progress) {
151  progress.start(getKeywords().size());
152  }
153 
154  /*
155  * Process the keyword hits for each keyword.
156  */
157  int keywordsProcessed = 0;
158  final Collection<BlackboardArtifact> hitArtifacts = new ArrayList<>();
159  for (final Keyword keyword : getKeywords()) {
160  /*
161  * Cancellation check.
162  */
163  if (worker.isCancelled()) {
164  logger.log(Level.INFO, "Processing cancelled, exiting before processing search term {0}", keyword.getSearchTerm()); //NON-NLS
165  break;
166  }
167 
168  /*
169  * Update the progress indicator and the show the current keyword
170  * via the progress contributor.
171  */
172  if (progress != null) {
173  progress.progress(keyword.toString(), keywordsProcessed);
174  }
175  if (subProgress != null) {
176  String hitDisplayStr = keyword.getSearchTerm();
177  if (hitDisplayStr.length() > 50) {
178  hitDisplayStr = hitDisplayStr.substring(0, 49) + "...";
179  }
180  subProgress.progress(query.getKeywordList().getName() + ": " + hitDisplayStr, keywordsProcessed);
181  }
182 
183  /*
184  * Reduce the hits for this keyword to one hit per text source
185  * object so that only one hit artifact is generated per text source
186  * object, no matter how many times the keyword was actually found.
187  */
188  for (KeywordHit hit : getOneHitPerTextSourceObject(keyword)) {
189  /*
190  * Get a snippet (preview) for the hit. Regex queries always
191  * have snippets made from the content_str pulled back from Solr
192  * for executing the search. Other types of queries may or may
193  * not have snippets yet.
194  */
195  String snippet = hit.getSnippet();
196  if (StringUtils.isBlank(snippet)) {
197  final String snippetQuery = KeywordSearchUtil.escapeLuceneQuery(keyword.getSearchTerm());
198  try {
199  snippet = LuceneQuery.querySnippet(snippetQuery, hit.getSolrObjectId(), hit.getChunkId(), !query.isLiteral(), true);
200  } catch (NoOpenCoreException e) {
201  logger.log(Level.SEVERE, "Solr core closed while executing snippet query " + snippetQuery, e); //NON-NLS
202  break; // Stop processing.
203  } catch (Exception e) {
204  logger.log(Level.SEVERE, "Error executing snippet query " + snippetQuery, e); //NON-NLS
205  continue; // Try processing the next hit.
206  }
207  }
208 
209  /*
210  * Get the content (file or artifact) that is the text source
211  * for the hit.
212  */
213  Content content = null;
214  try {
215  SleuthkitCase tskCase = Case.getCurrentCase().getSleuthkitCase();
216  content = tskCase.getContentById(hit.getContentID());
217  } catch (TskCoreException | IllegalStateException tskCoreException) {
218  logger.log(Level.SEVERE, "Failed to get text source object for ", tskCoreException); //NON-NLS
219  }
220 
221  /*
222  * Post an artifact for the hit to the blackboard.
223  */
224  BlackboardArtifact artifact = query.postKeywordHitToBlackboard(content, keyword, hit, snippet, query.getKeywordList().getName());
225 
226  /*
227  * Send an ingest inbox message for the hit.
228  */
229  if (null != artifact) {
230  hitArtifacts.add(artifact);
231  if (notifyInbox) {
232  try {
233  writeSingleFileInboxMessage(artifact, content);
234  } catch (TskCoreException ex) {
235  logger.log(Level.SEVERE, "Error sending message to ingest messages inbox", ex); //NON-NLS
236  }
237  }
238  }
239  }
240 
241  ++keywordsProcessed;
242  }
243 
244  /*
245  * Publish an event to notify subscribers of the blackboard posts. The
246  * artifacts are grouped by type, since they may contain both
247  * TSK_KEYWORD_HIT artifacts and TSK_ACCOUNT artifacts (for credit card
248  * account number hits).
249  */
250  if (!hitArtifacts.isEmpty()) {
251  hitArtifacts.stream()
252  // Group artifacts by type
253  .collect(Collectors.groupingBy(BlackboardArtifact::getArtifactTypeID))
254  // For each type send an event
255  .forEach((typeID, artifacts)
256  -> IngestServices.getInstance().fireModuleDataEvent(new ModuleDataEvent(MODULE_NAME, BlackboardArtifact.ARTIFACT_TYPE.fromID(typeID), artifacts)));
257 
258  }
259  }
260 
270  private Collection<KeywordHit> getOneHitPerTextSourceObject(Keyword keyword) {
271  /*
272  * For each Solr document (chunk) for a text source object, return only
273  * a single keyword hit from the first chunk of text (the one with the
274  * lowest chunk id).
275  */
276  HashMap< Long, KeywordHit> hits = new HashMap<>();
277  getResults(keyword).forEach((hit) -> {
278  if (!hits.containsKey(hit.getSolrObjectId())) {
279  hits.put(hit.getSolrObjectId(), hit);
280  } else if (hit.getChunkId() < hits.get(hit.getSolrObjectId()).getChunkId()) {
281  hits.put(hit.getSolrObjectId(), hit);
282  }
283  });
284  return hits.values();
285  }
286 
297  private void writeSingleFileInboxMessage(BlackboardArtifact artifact, Content hitContent) throws TskCoreException {
298  StringBuilder subjectSb = new StringBuilder(1024);
299  if (!query.isLiteral()) {
300  subjectSb.append(NbBundle.getMessage(this.getClass(), "KeywordSearchIngestModule.regExpHitLbl"));
301  } else {
302  subjectSb.append(NbBundle.getMessage(this.getClass(), "KeywordSearchIngestModule.kwHitLbl"));
303  }
304 
305  StringBuilder detailsSb = new StringBuilder(1024);
306  String uniqueKey = null;
307  BlackboardAttribute attr = artifact.getAttribute(new BlackboardAttribute.Type(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD));
308  if (attr != null) {
309  final String keyword = attr.getValueString();
310  subjectSb.append(keyword);
311  uniqueKey = keyword.toLowerCase();
312  detailsSb.append("<table border='0' cellpadding='4' width='280'>"); //NON-NLS
313  detailsSb.append("<tr>"); //NON-NLS
314  detailsSb.append(NbBundle.getMessage(this.getClass(), "KeywordSearchIngestModule.kwHitThLbl"));
315  detailsSb.append("<td>").append(EscapeUtil.escapeHtml(keyword)).append("</td>"); //NON-NLS
316  detailsSb.append("</tr>"); //NON-NLS
317  }
318 
319  //preview
320  attr = artifact.getAttribute(new BlackboardAttribute.Type(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD_PREVIEW));
321  if (attr != null) {
322  detailsSb.append("<tr>"); //NON-NLS
323  detailsSb.append(NbBundle.getMessage(this.getClass(), "KeywordSearchIngestModule.previewThLbl"));
324  detailsSb.append("<td>").append(EscapeUtil.escapeHtml(attr.getValueString())).append("</td>"); //NON-NLS
325  detailsSb.append("</tr>"); //NON-NLS
326  }
327 
328  //file
329  detailsSb.append("<tr>"); //NON-NLS
330  detailsSb.append(NbBundle.getMessage(this.getClass(), "KeywordSearchIngestModule.fileThLbl"));
331  if (hitContent instanceof AbstractFile) {
332  AbstractFile hitFile = (AbstractFile) hitContent;
333  detailsSb.append("<td>").append(hitFile.getParentPath()).append(hitFile.getName()).append("</td>"); //NON-NLS
334  } else {
335  detailsSb.append("<td>").append(hitContent.getName()).append("</td>"); //NON-NLS
336  }
337  detailsSb.append("</tr>"); //NON-NLS
338 
339  //list
340  attr = artifact.getAttribute(new BlackboardAttribute.Type(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_SET_NAME));
341  if (attr != null) {
342  detailsSb.append("<tr>"); //NON-NLS
343  detailsSb.append(NbBundle.getMessage(this.getClass(), "KeywordSearchIngestModule.listThLbl"));
344  detailsSb.append("<td>").append(attr.getValueString()).append("</td>"); //NON-NLS
345  detailsSb.append("</tr>"); //NON-NLS
346  }
347 
348  //regex
349  if (!query.isLiteral()) {
350  attr = artifact.getAttribute(new BlackboardAttribute.Type(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD_REGEXP));
351  if (attr != null) {
352  detailsSb.append("<tr>"); //NON-NLS
353  detailsSb.append(NbBundle.getMessage(this.getClass(), "KeywordSearchIngestModule.regExThLbl"));
354  detailsSb.append("<td>").append(attr.getValueString()).append("</td>"); //NON-NLS
355  detailsSb.append("</tr>"); //NON-NLS
356  }
357  }
358  detailsSb.append("</table>"); //NON-NLS
359 
360  IngestServices.getInstance().postMessage(IngestMessage.createDataMessage(MODULE_NAME, subjectSb.toString(), detailsSb.toString(), uniqueKey, artifact));
361  }
362 }

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.