19package org.sleuthkit.autopsy.commonpropertiessearch;
21import com.google.common.collect.Iterables;
22import java.sql.ResultSet;
23import java.sql.SQLException;
24import java.util.ArrayList;
25import java.util.Arrays;
26import java.util.Collections;
27import java.util.HashMap;
28import java.util.HashSet;
32import java.util.TreeMap;
33import java.util.logging.Level;
34import java.util.stream.Collectors;
35import org.sleuthkit.autopsy.casemodule.Case;
36import org.sleuthkit.autopsy.centralrepository.datamodel.CorrelationAttributeInstance;
37import org.sleuthkit.autopsy.centralrepository.datamodel.CorrelationAttributeInstance.Type;
38import org.sleuthkit.autopsy.centralrepository.datamodel.CorrelationAttributeNormalizationException;
39import org.sleuthkit.autopsy.centralrepository.datamodel.CorrelationCase;
40import org.sleuthkit.autopsy.centralrepository.datamodel.CorrelationDataSource;
41import org.sleuthkit.autopsy.centralrepository.datamodel.CentralRepoException;
42import org.sleuthkit.autopsy.centralrepository.datamodel.InstanceTableCallback;
43import org.sleuthkit.autopsy.commonpropertiessearch.AbstractCommonAttributeInstance.NODE_TYPE;
44import org.sleuthkit.autopsy.coreutils.Logger;
45import org.sleuthkit.datamodel.CaseDbAccessManager;
46import org.sleuthkit.datamodel.TskData;
47import org.sleuthkit.datamodel.HashUtility;
48import org.sleuthkit.datamodel.TskCoreException;
49import org.sleuthkit.autopsy.centralrepository.datamodel.CentralRepository;
55final 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) {
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();
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();
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();
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();
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<>();
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;
304 value.addMetadataToList(commonAttributeValue);
311 LOGGER.log(Level.WARNING,
"Error getting artifact instances from database.", ex);
315 Map<Integer, CommonAttributeValueList> getInstanceCollatedCommonFiles() {
336 this.caseID = caseId;
343 List<Integer> targetCases =
new ArrayList<>();
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) {
374 if (!dataSourceToFile.containsKey(dataSourceNameKey)) {
379 searchResult.setCurrentAttributeInst(instance);
381 commonAttributeValue.addInstance(searchResult);
382 valueList.addMetadataToList(commonAttributeValue);
383 dataSourceToFile.put(dataSourceNameKey, valueList);
389 LOGGER.log(Level.WARNING,
"Error getting artifact instances from database.", ex);
393 Map<String, Map<String, CommonAttributeValueList>> getInstanceCollatedCommonFiles() {
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;
Long getDataSourceObjectID()
void process(ResultSet resultSet)
void process(ResultSet resultSet)
InterCaseByCaseCallback(int caseId, int targetCase)
InterCaseByCaseCallback(int caseId)
static final int VALUE_BATCH_SIZE
final Map< String, Map< String, CommonAttributeValueList > > caseCollatedDataSourceCollections
void process(ResultSet resultSet)
InterCaseByCountCallback(int caseId, int targetCase)
final TreeMap< Integer, CommonAttributeValueList > instanceCollatedCommonFiles
InterCaseByCountCallback(int caseId)
List< CorrelationAttributeInstance > getArtifactInstancesByTypeValues(CorrelationAttributeInstance.Type aType, List< String > values)
static CentralRepository getInstance()
CorrelationDataSource getDataSourceById(CorrelationCase correlationCase, int dataSourceId)
CorrelationCase getCaseById(int caseId)
List< CorrelationAttributeInstance > getArtifactInstancesByTypeValuesAndCases(CorrelationAttributeInstance.Type aType, List< String > values, List< Integer > caseIds)
CorrelationAttributeInstance getCorrelationAttributeInstance(CorrelationAttributeInstance.Type type, CorrelationCase correlationCase, CorrelationDataSource correlationDataSource, String value, String filePath)
static int getCaseId(ResultSet resultSet)
static int getDataSourceId(ResultSet resultSet)
static String getValue(ResultSet resultSet)
static String getFilePath(ResultSet resultSet)
static long getFileObjectId(ResultSet resultSet)