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

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.