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;
 
   38 import org.apache.commons.lang.StringUtils;
 
   57 abstract class Extract {
 
   59     protected final Case currentCase;
 
   60     protected final SleuthkitCase tskCase;
 
   61     private static final Logger logger = Logger.getLogger(Extract.class.getName());
 
   62     private final ArrayList<String> errorMessages = 
new ArrayList<>();
 
   63     private final String displayName;
 
   64     protected boolean dataFound = 
false;
 
   65     private final IngestJobContext context;
 
   74     Extract(String displayName, IngestJobContext context) {
 
   75         this.displayName = displayName;
 
   76         this.context = context;
 
   77         currentCase = Case.getCurrentCase();
 
   78         tskCase = currentCase.getSleuthkitCase();
 
   88     void startUp() throws IngestModuleException {
 
   99     abstract void process(Content dataSource, DataSourceIngestModuleProgress progressBar);
 
  113     List<String> getErrorMessages() {
 
  114         return Collections.unmodifiableList(errorMessages);
 
  123     protected void addErrorMessage(String message) {
 
  124         errorMessages.add(message);
 
  139     BlackboardArtifact createArtifactWithAttributes(BlackboardArtifact.Type type, Content content, Collection<BlackboardAttribute> attributes) 
throws TskCoreException {
 
  140         if (type.getCategory() == BlackboardArtifact.Category.DATA_ARTIFACT) {
 
  141             return content.newDataArtifact(type, attributes);
 
  142         } 
else if (type.getCategory() == BlackboardArtifact.Category.ANALYSIS_RESULT) {
 
  143             return content.newAnalysisResult(type, Score.SCORE_UNKNOWN, null, null, null, attributes).getAnalysisResult();
 
  145             throw new TskCoreException(
"Unknown category type: " + type.getCategory().getDisplayName());
 
  160     BlackboardArtifact createAssociatedArtifact(Content content, BlackboardArtifact artifact) 
throws TskCoreException {
 
  161         BlackboardAttribute attribute = 
new BlackboardAttribute(BlackboardAttribute.Type.TSK_ASSOCIATED_ARTIFACT, getRAModuleName(), artifact.getArtifactID());
 
  162         return createArtifactWithAttributes(BlackboardArtifact.Type.TSK_ASSOCIATED_OBJECT, content, Collections.singletonList(attribute));
 
  170     void postArtifact(BlackboardArtifact artifact) {
 
  171         if (artifact != null && !context.dataArtifactIngestIsCancelled()) {
 
  172             postArtifacts(Collections.singleton(artifact));
 
  181     void postArtifacts(Collection<BlackboardArtifact> artifacts) {
 
  182         if (artifacts != null && !artifacts.isEmpty() && !context.dataArtifactIngestIsCancelled()) {
 
  184                 tskCase.getBlackboard().postArtifacts(artifacts, RecentActivityExtracterModuleFactory.getModuleName(), context.getJobId());
 
  185             } 
catch (Blackboard.BlackboardException ex) {
 
  186                 logger.log(Level.SEVERE, 
"Failed to post artifacts", ex); 
 
  206     protected List<HashMap<String, Object>> querySQLiteDb(String path, String query) {
 
  208         List<HashMap<String, Object>> list;
 
  209         String connectionString = 
"jdbc:sqlite:" + path; 
 
  210         SQLiteDBConnect dbConnection = null;
 
  212             dbConnection = 
new SQLiteDBConnect(
"org.sqlite.JDBC", connectionString); 
 
  213             resultSet = dbConnection.executeQry(query);
 
  214             list = resultSetToArrayList(resultSet);
 
  215         } 
catch (SQLException ex) {
 
  216             logger.log(Level.WARNING, 
"Error while trying to read into a sqlite db." + connectionString, ex); 
 
  217             return Collections.<HashMap<String, Object>>emptyList();
 
  219             if (dbConnection != null) {
 
  220                 dbConnection.closeConnection();
 
  235     private List<HashMap<String, Object>> resultSetToArrayList(ResultSet rs) 
throws SQLException {
 
  236         ResultSetMetaData md = rs.getMetaData();
 
  237         int columns = md.getColumnCount();
 
  238         List<HashMap<String, Object>> results = 
new ArrayList<>(50);
 
  240             HashMap<String, Object> row = 
new HashMap<>(columns);
 
  241             for (
int i = 1; i <= columns; ++i) {
 
  242                 if (rs.getObject(i) == null) {
 
  243                     row.put(md.getColumnName(i), 
"");
 
  245                     row.put(md.getColumnName(i), rs.getObject(i));
 
  258     protected String getDisplayName() {
 
  267     protected String getRAModuleName() {
 
  268         return RecentActivityExtracterModuleFactory.getModuleName();
 
  277     public boolean foundData() {
 
  287     protected void setFoundData(
boolean foundData) {
 
  288         dataFound = foundData;
 
  296     protected Case getCurrentCase() {
 
  297         return this.currentCase;
 
  317     protected Collection<BlackboardAttribute> createHistoryAttributes(String url, Long accessTime,
 
  318             String referrer, String title, String programName, String domain, String user) 
throws TskCoreException {
 
  320         Collection<BlackboardAttribute> bbattributes = 
new ArrayList<>();
 
  321         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_URL,
 
  322                 RecentActivityExtracterModuleFactory.getModuleName(), url)); 
 
  324         if (accessTime != null) {
 
  325             bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_ACCESSED,
 
  326                     RecentActivityExtracterModuleFactory.getModuleName(), 
 
  330         if (StringUtils.isNotBlank(referrer)) {
 
  331             bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_REFERRER,
 
  332                     RecentActivityExtracterModuleFactory.getModuleName(),
 
  336         if (StringUtils.isNotBlank(title)) {
 
  337             bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_TITLE,
 
  338                     RecentActivityExtracterModuleFactory.getModuleName(),
 
  342         if (StringUtils.isNotBlank(programName)) {
 
  343             bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PROG_NAME,
 
  344                     RecentActivityExtracterModuleFactory.getModuleName(),
 
  349         if (StringUtils.isNotBlank(url)) {
 
  350             bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DOMAIN,
 
  351                     RecentActivityExtracterModuleFactory.getModuleName(),
 
  355         if (StringUtils.isNotBlank(user)) {
 
  356             bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_USER_NAME,
 
  357                     RecentActivityExtracterModuleFactory.getModuleName(),
 
  376     protected Collection<BlackboardAttribute> createCookieAttributes(String url,
 
  377             Long creationTime, Long accessTime, Long endTime, String name, String value, String programName, String domain) {
 
  379         Collection<BlackboardAttribute> bbattributes = 
new ArrayList<>();
 
  380         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_URL,
 
  381                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  382                 (url != null) ? url : 
"")); 
 
  384         if (creationTime != null && creationTime != 0) {
 
  385             bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_CREATED,
 
  386                     RecentActivityExtracterModuleFactory.getModuleName(), creationTime));
 
  389         if (accessTime != null && accessTime != 0) {
 
  390             bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_ACCESSED,
 
  391                     RecentActivityExtracterModuleFactory.getModuleName(), accessTime));
 
  394         if (endTime != null && endTime != 0) {
 
  395             bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_END,
 
  396                     RecentActivityExtracterModuleFactory.getModuleName(), endTime));
 
  399         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_NAME,
 
  400                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  401                 (name != null) ? name : 
"")); 
 
  403         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_VALUE,
 
  404                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  405                 (value != null) ? value : 
"")); 
 
  407         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PROG_NAME,
 
  408                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  409                 (programName != null) ? programName : 
"")); 
 
  411         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DOMAIN,
 
  412                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  413                 (domain != null) ? domain : 
"")); 
 
  430     protected Collection<BlackboardAttribute> createBookmarkAttributes(String url, String title, Long creationTime, String programName, String domain) {
 
  431         Collection<BlackboardAttribute> bbattributes = 
new ArrayList<>();
 
  433         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_URL,
 
  434                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  435                 (url != null) ? url : 
"")); 
 
  437         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_TITLE,
 
  438                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  439                 (title != null) ? title : 
"")); 
 
  441         if (creationTime != null) {
 
  442             bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_CREATED,
 
  443                     RecentActivityExtracterModuleFactory.getModuleName(), creationTime));
 
  446         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PROG_NAME,
 
  447                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  448                 (programName != null) ? programName : 
"")); 
 
  450         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DOMAIN,
 
  451                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  452                 (domain != null) ? domain : 
"")); 
 
  468     protected Collection<BlackboardAttribute> createDownloadAttributes(String path, Long pathID, String url, Long accessTime, String domain, String programName) {
 
  469         Collection<BlackboardAttribute> bbattributes = 
new ArrayList<>();
 
  471         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PATH,
 
  472                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  473                 (path != null) ? path : 
"")); 
 
  475         if (pathID != null && pathID != -1) {
 
  476             bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PATH_ID,
 
  477                     RecentActivityExtracterModuleFactory.getModuleName(),
 
  481         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_URL,
 
  482                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  483                 (url != null) ? url : 
"")); 
 
  485         if (accessTime != null) {
 
  486             bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_ACCESSED,
 
  487                     RecentActivityExtracterModuleFactory.getModuleName(), accessTime));
 
  490         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DOMAIN,
 
  491                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  492                 (domain != null) ? domain : 
"")); 
 
  494         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PROG_NAME,
 
  495                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  496                 (programName != null) ? programName : 
"")); 
 
  513     protected File createTemporaryFile(AbstractFile file) 
throws IOException {
 
  514         Path tempFilePath = Paths.get(RAImageIngestModule.getRATempPath(getCurrentCase(), getDisplayName(), context.getJobId()), file.getName() + file.getId() + file.getNameExtension());
 
  515         java.io.File tempFile = tempFilePath.toFile();
 
  516         ContentUtils.writeToFile(file, tempFile, context::dataSourceIngestIsCancelled);