Autopsy  4.1
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 2014-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.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.netbeans.api.progress.ProgressHandle;
31 import org.netbeans.api.progress.aggregate.ProgressContributor;
32 import org.openide.util.NbBundle;
38 import org.sleuthkit.datamodel.AbstractFile;
39 import org.sleuthkit.datamodel.BlackboardArtifact;
40 import org.sleuthkit.datamodel.BlackboardAttribute;
41 import org.sleuthkit.datamodel.Content;
42 
48 class QueryResults {
49 
50  private static final Logger logger = Logger.getLogger(QueryResults.class.getName());
51  private static final String MODULE_NAME = KeywordSearchModuleFactory.getModuleName();
55  private final KeywordSearchQuery keywordSearchQuery;
56 
60  private final Map<Keyword, List<KeywordHit>> results = new HashMap<>();
61 
65  // TODO: This is redundant. The keyword list is in the query.
66  private final KeywordList keywordList;
67 
68  QueryResults(KeywordSearchQuery query, KeywordList keywordList) {
69  this.keywordSearchQuery = query;
70  this.keywordList = keywordList;
71  }
72 
73  void addResult(Keyword keyword, List<KeywordHit> hits) {
74  results.put(keyword, hits);
75  }
76 
77  // TODO: This is redundant. The keyword list is in the query.
78  KeywordList getKeywordList() {
79  return keywordList;
80  }
81 
82  KeywordSearchQuery getQuery() {
83  return keywordSearchQuery;
84  }
85 
86  List<KeywordHit> getResults(Keyword keyword) {
87  return results.get(keyword);
88  }
89 
90  Set<Keyword> getKeywords() {
91  return results.keySet();
92  }
93 
109  Collection<BlackboardArtifact> writeAllHitsToBlackBoard(ProgressHandle progress, ProgressContributor subProgress, SwingWorker<Object, Void> worker, boolean notifyInbox) {
110  final Collection<BlackboardArtifact> newArtifacts = new ArrayList<>();
111  if (progress != null) {
112  progress.start(getKeywords().size());
113  }
114  int unitProgress = 0;
115 
116  for (final Keyword keyword : getKeywords()) {
117  if (worker.isCancelled()) {
118  logger.log(Level.INFO, "Cancel detected, bailing before new keyword processed: {0}", keyword.getSearchTerm()); //NON-NLS
119  break;
120  }
121 
122  // Update progress object(s), if any
123  if (progress != null) {
124  progress.progress(keyword.toString(), unitProgress);
125  }
126  if (subProgress != null) {
127  String hitDisplayStr = keyword.getSearchTerm();
128  if (hitDisplayStr.length() > 50) {
129  hitDisplayStr = hitDisplayStr.substring(0, 49) + "...";
130  }
131  subProgress.progress(keywordList.getName() + ": " + hitDisplayStr, unitProgress);
132  }
133 
134  for (KeywordHit hit : getOneHitPerObject(keyword)) {
135  String termString = keyword.getSearchTerm();
136  final String snippetQuery = KeywordSearchUtil.escapeLuceneQuery(termString);
137  String snippet;
138  try {
139  snippet = LuceneQuery.querySnippet(snippetQuery, hit.getSolrObjectId(), hit.getChunkId(), !keywordSearchQuery.isLiteral(), true);
140  } catch (NoOpenCoreException e) {
141  logger.log(Level.WARNING, "Error querying snippet: " + snippetQuery, e); //NON-NLS
142  //no reason to continue
143  break;
144  } catch (Exception e) {
145  logger.log(Level.WARNING, "Error querying snippet: " + snippetQuery, e); //NON-NLS
146  continue;
147  }
148  if (snippet != null) {
149  KeywordCachedArtifact writeResult = keywordSearchQuery.writeSingleFileHitsToBlackBoard(termString, hit, snippet, keywordList.getName());
150  if (writeResult != null) {
151  newArtifacts.add(writeResult.getArtifact());
152  if (notifyInbox) {
153  writeSingleFileInboxMessage(writeResult, hit.getContent());
154  }
155  } else {
156  logger.log(Level.WARNING, "BB artifact for keyword hit not written, file: {0}, hit: {1}", new Object[]{hit.getContent(), keyword.toString()}); //NON-NLS
157  }
158  }
159  }
160  ++unitProgress;
161  }
162 
163  // Update artifact browser
164  if (!newArtifacts.isEmpty()) {
165  newArtifacts.stream()
166  //group artifacts by type
167  .collect(Collectors.groupingBy(BlackboardArtifact::getArtifactTypeID))
168  //for each type send an event
169  .forEach((typeID, artifacts) ->
170  IngestServices.getInstance().fireModuleDataEvent(new ModuleDataEvent(MODULE_NAME, BlackboardArtifact.ARTIFACT_TYPE.fromID(typeID), artifacts)));
171 
172  }
173 
174  return newArtifacts;
175  }
176 
185  private Collection<KeywordHit> getOneHitPerObject(Keyword keyword) {
186 
187  HashMap<Long, KeywordHit> hits = new HashMap<>();
188 
189  // create a list of KeywordHits. KeywordHits with lowest chunkID is added the the list.
190  for (KeywordHit hit : getResults(keyword)) {
191  if (!hits.containsKey(hit.getSolrObjectId())) {
192  hits.put(hit.getSolrObjectId(), hit);
193  } else if (hit.getChunkId() < hits.get(hit.getSolrObjectId()).getChunkId()) {
194  hits.put(hit.getSolrObjectId(), hit);
195  }
196  }
197  return hits.values();
198  }
199 
206  private void writeSingleFileInboxMessage(KeywordCachedArtifact written, Content hitContent) {
207  StringBuilder subjectSb = new StringBuilder();
208  StringBuilder detailsSb = new StringBuilder();
209 
210  if (!keywordSearchQuery.isLiteral()) {
211  subjectSb.append(NbBundle.getMessage(this.getClass(), "KeywordSearchIngestModule.regExpHitLbl"));
212  } else {
213  subjectSb.append(NbBundle.getMessage(this.getClass(), "KeywordSearchIngestModule.kwHitLbl"));
214  }
215  String uniqueKey = null;
216  BlackboardAttribute attr = written.getAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD.getTypeID());
217  if (attr != null) {
218  final String keyword = attr.getValueString();
219  subjectSb.append(keyword);
220  uniqueKey = keyword.toLowerCase();
221  }
222 
223  //details
224  detailsSb.append("<table border='0' cellpadding='4' width='280'>"); //NON-NLS
225  //hit
226  detailsSb.append("<tr>"); //NON-NLS
227  detailsSb.append(NbBundle.getMessage(this.getClass(), "KeywordSearchIngestModule.kwHitThLbl"));
228  detailsSb.append("<td>").append(EscapeUtil.escapeHtml(attr.getValueString())).append("</td>"); //NON-NLS
229  detailsSb.append("</tr>"); //NON-NLS
230 
231  //preview
232  attr = written.getAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD_PREVIEW.getTypeID());
233  if (attr != null) {
234  detailsSb.append("<tr>"); //NON-NLS
235  detailsSb.append(NbBundle.getMessage(this.getClass(), "KeywordSearchIngestModule.previewThLbl"));
236  detailsSb.append("<td>").append(EscapeUtil.escapeHtml(attr.getValueString())).append("</td>"); //NON-NLS
237  detailsSb.append("</tr>"); //NON-NLS
238  }
239 
240  //file
241  detailsSb.append("<tr>"); //NON-NLS
242  detailsSb.append(NbBundle.getMessage(this.getClass(), "KeywordSearchIngestModule.fileThLbl"));
243  if (hitContent instanceof AbstractFile) {
244  AbstractFile hitFile = (AbstractFile) hitContent;
245  detailsSb.append("<td>").append(hitFile.getParentPath()).append(hitFile.getName()).append("</td>"); //NON-NLS
246  } else {
247  detailsSb.append("<td>").append(hitContent.getName()).append("</td>"); //NON-NLS
248  }
249  detailsSb.append("</tr>"); //NON-NLS
250 
251  //list
252  attr = written.getAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_SET_NAME.getTypeID());
253  if (attr != null) {
254  detailsSb.append("<tr>"); //NON-NLS
255  detailsSb.append(NbBundle.getMessage(this.getClass(), "KeywordSearchIngestModule.listThLbl"));
256  detailsSb.append("<td>").append(attr.getValueString()).append("</td>"); //NON-NLS
257  detailsSb.append("</tr>"); //NON-NLS
258  }
259  //regex
260  if (!keywordSearchQuery.isLiteral()) {
261  attr = written.getAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_KEYWORD_REGEXP.getTypeID());
262  if (attr != null) {
263  detailsSb.append("<tr>"); //NON-NLS
264  detailsSb.append(NbBundle.getMessage(this.getClass(), "KeywordSearchIngestModule.regExThLbl"));
265  detailsSb.append("<td>").append(attr.getValueString()).append("</td>"); //NON-NLS
266  detailsSb.append("</tr>"); //NON-NLS
267  }
268  }
269  detailsSb.append("</table>"); //NON-NLS
270 
271  IngestServices.getInstance().postMessage(IngestMessage.createDataMessage(MODULE_NAME, subjectSb.toString(), detailsSb.toString(), uniqueKey, written.getArtifact()));
272  }
273 
274 }

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.