19 package org.sleuthkit.autopsy.commonpropertiessearch;
21 import com.google.common.collect.Iterables;
22 import java.sql.ResultSet;
23 import java.sql.SQLException;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.HashSet;
30 import java.util.List;
32 import java.util.TreeMap;
33 import java.util.logging.Level;
34 import java.util.stream.Collectors;
55 final class InterCaseSearchResultsProcessor {
57 private static final Logger LOGGER = Logger.getLogger(CommonAttributePanel.class.getName());
58 private static final String INTER_CASE_WHERE_CLAUSE =
"case_id=%s AND (known_status !=%s OR known_status IS NULL)";
62 private final Type correlationType;
71 InterCaseSearchResultsProcessor(CorrelationAttributeInstance.Type theType) {
72 this.correlationType = theType;
82 CorrelationAttributeInstance findSingleCorrelationAttribute(
int attrbuteId) {
85 InterCaseCommonAttributeRowCallback instancetableCallback =
new InterCaseCommonAttributeRowCallback();
86 CentralRepository dbManager = CentralRepository.getInstance();
87 dbManager.processInstanceTableWhere(correlationType, String.format(
"id = %s", attrbuteId), instancetableCallback);
89 return instancetableCallback.getCorrelationAttribute();
91 }
catch (CentralRepoException ex) {
92 LOGGER.log(Level.SEVERE,
"Error accessing EamDb processing InstanceTable row.", ex);
109 private String getFileQuery(Set<String> mimeTypesToFilterOn)
throws CentralRepoException {
111 query =
"md5 AS value FROM tsk_files WHERE known!=" + TskData.FileKnown.KNOWN.getFileKnownValue() +
" AND md5 IS NOT NULL";
112 if (!mimeTypesToFilterOn.isEmpty()) {
113 query = query +
" AND mime_type IS NOT NULL AND mime_type IN ('" + String.join(
"', '", mimeTypesToFilterOn) +
"')";
129 Map<String, Map<String, CommonAttributeValueList>> findInterCaseValuesByCase(Case currentCase, Set<String> mimeTypesToFilterOn) {
132 CentralRepository dbManager = CentralRepository.getInstance();
133 int caseId = dbManager.getCase(currentCase).getID();
134 InterCaseByCaseCallback instancetableCallback =
new InterCaseByCaseCallback(caseId);
135 if (correlationType.getId() == CorrelationAttributeInstance.FILES_TYPE_ID) {
136 currentCase.getSleuthkitCase().getCaseDbAccessManager().select(getFileQuery(mimeTypesToFilterOn), instancetableCallback);
138 dbManager.processInstanceTableWhere(correlationType, String.format(INTER_CASE_WHERE_CLAUSE, caseId,
139 TskData.FileKnown.KNOWN.getFileKnownValue()),
140 instancetableCallback);
142 return instancetableCallback.getInstanceCollatedCommonFiles();
144 }
catch (CentralRepoException | TskCoreException ex) {
145 LOGGER.log(Level.SEVERE,
"Error accessing EamDb processing CaseInstancesTable.", ex);
147 return new HashMap<>();
159 Map<Integer, CommonAttributeValueList> findInterCaseValuesByCount(Case currentCase, Set<String> mimeTypesToFilterOn) {
162 CentralRepository dbManager = CentralRepository.getInstance();
164 int caseId = dbManager.getCase(currentCase).getID();
165 InterCaseByCountCallback instancetableCallback =
new InterCaseByCountCallback(caseId);
166 if (correlationType.getId() == CorrelationAttributeInstance.FILES_TYPE_ID) {
167 currentCase.getSleuthkitCase().getCaseDbAccessManager().select(getFileQuery(mimeTypesToFilterOn), instancetableCallback);
169 dbManager.processInstanceTableWhere(correlationType, String.format(INTER_CASE_WHERE_CLAUSE, caseId,
170 TskData.FileKnown.KNOWN.getFileKnownValue()),
171 instancetableCallback);
173 return instancetableCallback.getInstanceCollatedCommonFiles();
175 }
catch (CentralRepoException | TskCoreException ex) {
176 LOGGER.log(Level.SEVERE,
"Error accessing EamDb processing CaseInstancesTable.", ex);
178 return new TreeMap<>();
193 Map<Integer, CommonAttributeValueList> findSingleInterCaseValuesByCount(Case currentCase, Set<String> mimeTypesToFilterOn, CorrelationCase singleCase) {
195 CentralRepository dbManager = CentralRepository.getInstance();
196 int caseId = dbManager.getCase(currentCase).getID();
197 int targetCaseId = singleCase.getID();
198 InterCaseByCountCallback instancetableCallback =
new InterCaseByCountCallback(caseId, targetCaseId);
199 if (correlationType.getId() == CorrelationAttributeInstance.FILES_TYPE_ID) {
200 currentCase.getSleuthkitCase().getCaseDbAccessManager().select(getFileQuery(mimeTypesToFilterOn), instancetableCallback);
202 dbManager.processInstanceTableWhere(correlationType, String.format(INTER_CASE_WHERE_CLAUSE, caseId,
203 TskData.FileKnown.KNOWN.getFileKnownValue()),
204 instancetableCallback);
206 return instancetableCallback.getInstanceCollatedCommonFiles();
207 }
catch (CentralRepoException | TskCoreException ex) {
208 LOGGER.log(Level.SEVERE,
"Error accessing EamDb processing CaseInstancesTable.", ex);
210 return new TreeMap<>();
226 Map<String, Map<String, CommonAttributeValueList>> findSingleInterCaseValuesByCase(Case currentCase, Set<String> mimeTypesToFilterOn, CorrelationCase singleCase) {
229 CentralRepository dbManager = CentralRepository.getInstance();
230 int caseId = dbManager.getCase(currentCase).getID();
231 int targetCaseId = singleCase.getID();
232 InterCaseByCaseCallback instancetableCallback =
new InterCaseByCaseCallback(caseId, targetCaseId);
233 if (correlationType.getId() == CorrelationAttributeInstance.FILES_TYPE_ID) {
234 currentCase.getSleuthkitCase().getCaseDbAccessManager().select(getFileQuery(mimeTypesToFilterOn), instancetableCallback);
236 dbManager.processInstanceTableWhere(correlationType, String.format(INTER_CASE_WHERE_CLAUSE, caseId,
237 TskData.FileKnown.KNOWN.getFileKnownValue()),
238 instancetableCallback);
240 return instancetableCallback.getInstanceCollatedCommonFiles();
241 }
catch (CentralRepoException | TskCoreException ex) {
242 LOGGER.log(Level.SEVERE,
"Error accessing EamDb processing CaseInstancesTable.", ex);
244 return new HashMap<>();
262 this.caseID = caseId;
269 Set<String> values =
new HashSet<>();
270 List<Integer> targetCases =
new ArrayList<>();
271 if (targetCase != 0) {
272 targetCases.add(caseID);
273 targetCases.add(targetCase);
275 while (resultSet.next()) {
277 if (corValue == null || HashUtility.isNoDataMd5(corValue)) {
280 values.add(corValue);
282 for (String corValue : values) {
283 List<CorrelationAttributeInstance> instances;
284 if (targetCases.isEmpty()) {
289 int size = instances.stream().map(instance -> instance.getCorrelationDataSource().getID()).collect(Collectors.toSet()).size();
292 boolean anotherCase =
false;
295 searchResult.setCurrentAttributeInst(instance);
296 commonAttributeValue.addInstance(searchResult);
297 anotherCase = anotherCase || instance.getCorrelationCase().getID() !=
caseID;
300 if (instanceCollatedCommonFiles.containsKey(size)) {
301 instanceCollatedCommonFiles.get(size).addMetadataToList(commonAttributeValue);
304 value.addMetadataToList(commonAttributeValue);
305 instanceCollatedCommonFiles.put(size, value);
311 LOGGER.log(Level.WARNING,
"Error getting artifact instances from database.", ex);
315 Map<Integer, CommonAttributeValueList> getInstanceCollatedCommonFiles() {
316 return Collections.unmodifiableSortedMap(instanceCollatedCommonFiles);
336 this.caseID = caseId;
343 List<Integer> targetCases =
new ArrayList<>();
344 if (targetCase != 0) {
345 targetCases.add(caseID);
346 targetCases.add(targetCase);
348 Set<String> values =
new HashSet<>();
349 while (resultSet.next()) {
351 if (corValue == null || HashUtility.isNoDataMd5(corValue)) {
354 values.add(corValue);
356 for (List<String> valuesChunk : Iterables.partition(values, VALUE_BATCH_SIZE)) {
357 List<CorrelationAttributeInstance> instances;
358 if (targetCases.isEmpty()) {
363 if (instances.size() > 1) {
370 if (!caseCollatedDataSourceCollections.containsKey(caseName)) {
371 caseCollatedDataSourceCollections.put(caseName,
new HashMap<>());
373 Map<String, CommonAttributeValueList> dataSourceToFile = caseCollatedDataSourceCollections.get(caseName);
374 if (!dataSourceToFile.containsKey(dataSourceNameKey)) {
379 searchResult.setCurrentAttributeInst(instance);
381 commonAttributeValue.addInstance(searchResult);
382 valueList.addMetadataToList(commonAttributeValue);
383 dataSourceToFile.put(dataSourceNameKey, valueList);
384 caseCollatedDataSourceCollections.put(caseName, dataSourceToFile);
389 LOGGER.log(Level.WARNING,
"Error getting artifact instances from database.", ex);
393 Map<String, Map<String, CommonAttributeValueList>> getInstanceCollatedCommonFiles() {
394 return Collections.unmodifiableMap(caseCollatedDataSourceCollections);
411 while (resultSet.next()) {
416 if (fileObjectId != 0) {
418 correlationCase, dataSource, fileObjectId);
427 LOGGER.log(Level.INFO,
"Unable to get CorrelationAttributeInstance.", ex);
432 LOGGER.log(Level.WARNING,
"Error getting single correlation artifact instance from database.", ex);
437 return correlationAttributeInstance;
void process(ResultSet resultSet)
void process(ResultSet resultSet)
CorrelationCase getCaseById(int caseId)
static long getFileObjectId(ResultSet resultSet)
final Map< String, Map< String, CommonAttributeValueList > > caseCollatedDataSourceCollections
static String getFilePath(ResultSet resultSet)
InterCaseByCountCallback(int caseId, int targetCase)
InterCaseByCaseCallback(int caseId)
static final int VALUE_BATCH_SIZE
InterCaseByCountCallback(int caseId)
List< CorrelationAttributeInstance > getArtifactInstancesByTypeValues(CorrelationAttributeInstance.Type aType, List< String > values)
static int getDataSourceId(ResultSet resultSet)
CorrelationAttributeInstance getCorrelationAttributeInstance(CorrelationAttributeInstance.Type type, CorrelationCase correlationCase, CorrelationDataSource correlationDataSource, String value, String filePath)
Long getDataSourceObjectID()
List< CorrelationAttributeInstance > getArtifactInstancesByTypeValuesAndCases(CorrelationAttributeInstance.Type aType, List< String > values, List< Integer > caseIds)
final TreeMap< Integer, CommonAttributeValueList > instanceCollatedCommonFiles
InterCaseByCaseCallback(int caseId, int targetCase)
void process(ResultSet resultSet)
static CentralRepository getInstance()
static int getCaseId(ResultSet resultSet)
static String getValue(ResultSet resultSet)
CorrelationDataSource getDataSourceById(CorrelationCase correlationCase, int dataSourceId)