23 package org.sleuthkit.autopsy.recentactivity;
26 import java.io.IOException;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29 import java.sql.ResultSet;
30 import java.sql.ResultSetMetaData;
31 import java.sql.SQLException;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.Collections;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.logging.Level;
48 import org.
sleuthkit.datamodel.BlackboardArtifact.Category;
55 abstract class Extract {
57 protected final Case currentCase;
58 protected final SleuthkitCase tskCase;
59 private static final Logger logger = Logger.getLogger(Extract.class.getName());
60 private final ArrayList<String> errorMessages =
new ArrayList<>();
61 private final String displayName;
62 protected boolean dataFound =
false;
63 private final IngestJobContext context;
72 Extract(String displayName, IngestJobContext context) {
73 this.displayName = displayName;
74 this.context = context;
75 currentCase = Case.getCurrentCase();
76 tskCase = currentCase.getSleuthkitCase();
86 void startUp() throws IngestModuleException {
97 abstract void process(Content dataSource, DataSourceIngestModuleProgress progressBar);
111 List<String> getErrorMessages() {
112 return Collections.unmodifiableList(errorMessages);
121 protected void addErrorMessage(String message) {
122 errorMessages.add(message);
137 BlackboardArtifact createArtifactWithAttributes(BlackboardArtifact.Type type, Content content, Collection<BlackboardAttribute> attributes)
throws TskCoreException {
138 if (type.getCategory() == BlackboardArtifact.Category.DATA_ARTIFACT) {
139 return content.newDataArtifact(type, attributes);
140 }
else if (type.getCategory() == BlackboardArtifact.Category.ANALYSIS_RESULT) {
141 return content.newAnalysisResult(type, Score.SCORE_UNKNOWN, null, null, null, attributes).getAnalysisResult();
143 throw new TskCoreException(
"Unknown category type: " + type.getCategory().getDisplayName());
158 BlackboardArtifact createAssociatedArtifact(Content content, BlackboardArtifact artifact)
throws TskCoreException {
159 BlackboardAttribute attribute =
new BlackboardAttribute(BlackboardAttribute.Type.TSK_ASSOCIATED_ARTIFACT, getRAModuleName(), artifact.getArtifactID());
160 return createArtifactWithAttributes(BlackboardArtifact.Type.TSK_ASSOCIATED_OBJECT, content, Collections.singletonList(attribute));
168 void postArtifact(BlackboardArtifact artifact) {
169 if (artifact != null && !context.dataArtifactIngestIsCancelled()) {
170 postArtifacts(Collections.singleton(artifact));
179 void postArtifacts(Collection<BlackboardArtifact> artifacts) {
180 if (artifacts != null && !artifacts.isEmpty() && !context.dataArtifactIngestIsCancelled()) {
182 tskCase.getBlackboard().postArtifacts(artifacts, RecentActivityExtracterModuleFactory.getModuleName(), context.getJobId());
183 }
catch (Blackboard.BlackboardException ex) {
184 logger.log(Level.SEVERE,
"Failed to post artifacts", ex);
204 protected List<HashMap<String, Object>> querySQLiteDb(String path, String query) {
206 List<HashMap<String, Object>> list;
207 String connectionString =
"jdbc:sqlite:" + path;
208 SQLiteDBConnect dbConnection = null;
210 dbConnection =
new SQLiteDBConnect(
"org.sqlite.JDBC", connectionString);
211 resultSet = dbConnection.executeQry(query);
212 list = resultSetToArrayList(resultSet);
213 }
catch (SQLException ex) {
214 logger.log(Level.WARNING,
"Error while trying to read into a sqlite db." + connectionString, ex);
215 return Collections.<HashMap<String, Object>>emptyList();
217 if (dbConnection != null) {
218 dbConnection.closeConnection();
233 private List<HashMap<String, Object>> resultSetToArrayList(ResultSet rs)
throws SQLException {
234 ResultSetMetaData md = rs.getMetaData();
235 int columns = md.getColumnCount();
236 List<HashMap<String, Object>> results =
new ArrayList<>(50);
238 HashMap<String, Object> row =
new HashMap<>(columns);
239 for (
int i = 1; i <= columns; ++i) {
240 if (rs.getObject(i) == null) {
241 row.put(md.getColumnName(i),
"");
243 row.put(md.getColumnName(i), rs.getObject(i));
256 protected String getDisplayName() {
265 protected String getRAModuleName() {
266 return RecentActivityExtracterModuleFactory.getModuleName();
275 public boolean foundData() {
285 protected void setFoundData(
boolean foundData) {
286 dataFound = foundData;
294 protected Case getCurrentCase() {
295 return this.currentCase;
315 protected Collection<BlackboardAttribute> createHistoryAttributes(String url, Long accessTime,
316 String referrer, String title, String programName, String domain, String user)
throws TskCoreException {
318 Collection<BlackboardAttribute> bbattributes =
new ArrayList<>();
319 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_URL,
320 RecentActivityExtracterModuleFactory.getModuleName(),
321 (url != null) ? url :
""));
323 if (accessTime != null) {
324 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_ACCESSED,
325 RecentActivityExtracterModuleFactory.getModuleName(), accessTime));
328 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_REFERRER,
329 RecentActivityExtracterModuleFactory.getModuleName(),
330 (referrer != null) ? referrer :
""));
332 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_TITLE,
333 RecentActivityExtracterModuleFactory.getModuleName(),
334 (title != null) ? title :
""));
336 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PROG_NAME,
337 RecentActivityExtracterModuleFactory.getModuleName(),
338 (programName != null) ? programName :
""));
340 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DOMAIN,
341 RecentActivityExtracterModuleFactory.getModuleName(),
342 (domain != null) ? domain :
""));
344 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_USER_NAME,
345 RecentActivityExtracterModuleFactory.getModuleName(),
346 (user != null) ? user :
""));
363 protected Collection<BlackboardAttribute> createCookieAttributes(String url,
364 Long creationTime, Long accessTime, Long endTime, String name, String value, String programName, String domain) {
366 Collection<BlackboardAttribute> bbattributes =
new ArrayList<>();
367 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_URL,
368 RecentActivityExtracterModuleFactory.getModuleName(),
369 (url != null) ? url :
""));
371 if (creationTime != null && creationTime != 0) {
372 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_CREATED,
373 RecentActivityExtracterModuleFactory.getModuleName(), creationTime));
376 if (accessTime != null && accessTime != 0) {
377 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_ACCESSED,
378 RecentActivityExtracterModuleFactory.getModuleName(), accessTime));
381 if (endTime != null && endTime != 0) {
382 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_END,
383 RecentActivityExtracterModuleFactory.getModuleName(), endTime));
386 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_NAME,
387 RecentActivityExtracterModuleFactory.getModuleName(),
388 (name != null) ? name :
""));
390 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_VALUE,
391 RecentActivityExtracterModuleFactory.getModuleName(),
392 (value != null) ? value :
""));
394 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PROG_NAME,
395 RecentActivityExtracterModuleFactory.getModuleName(),
396 (programName != null) ? programName :
""));
398 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DOMAIN,
399 RecentActivityExtracterModuleFactory.getModuleName(),
400 (domain != null) ? domain :
""));
417 protected Collection<BlackboardAttribute> createBookmarkAttributes(String url, String title, Long creationTime, String programName, String domain) {
418 Collection<BlackboardAttribute> bbattributes =
new ArrayList<>();
420 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_URL,
421 RecentActivityExtracterModuleFactory.getModuleName(),
422 (url != null) ? url :
""));
424 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_TITLE,
425 RecentActivityExtracterModuleFactory.getModuleName(),
426 (title != null) ? title :
""));
428 if (creationTime != null) {
429 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_CREATED,
430 RecentActivityExtracterModuleFactory.getModuleName(), creationTime));
433 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PROG_NAME,
434 RecentActivityExtracterModuleFactory.getModuleName(),
435 (programName != null) ? programName :
""));
437 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DOMAIN,
438 RecentActivityExtracterModuleFactory.getModuleName(),
439 (domain != null) ? domain :
""));
455 protected Collection<BlackboardAttribute> createDownloadAttributes(String path, Long pathID, String url, Long accessTime, String domain, String programName) {
456 Collection<BlackboardAttribute> bbattributes =
new ArrayList<>();
458 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PATH,
459 RecentActivityExtracterModuleFactory.getModuleName(),
460 (path != null) ? path :
""));
462 if (pathID != null && pathID != -1) {
463 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PATH_ID,
464 RecentActivityExtracterModuleFactory.getModuleName(),
468 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_URL,
469 RecentActivityExtracterModuleFactory.getModuleName(),
470 (url != null) ? url :
""));
472 if (accessTime != null) {
473 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_ACCESSED,
474 RecentActivityExtracterModuleFactory.getModuleName(), accessTime));
477 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DOMAIN,
478 RecentActivityExtracterModuleFactory.getModuleName(),
479 (domain != null) ? domain :
""));
481 bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PROG_NAME,
482 RecentActivityExtracterModuleFactory.getModuleName(),
483 (programName != null) ? programName :
""));
500 protected File createTemporaryFile(AbstractFile file)
throws IOException {
501 Path tempFilePath = Paths.get(RAImageIngestModule.getRATempPath(getCurrentCase(), getDisplayName(), context.getJobId()), file.getName() + file.getId() + file.getNameExtension());
502 java.io.File tempFile = tempFilePath.toFile();
503 ContentUtils.writeToFile(file, tempFile, context::dataSourceIngestIsCancelled);