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.openide.util.NbBundle;
 
   39 import org.openide.util.NbBundle.Messages;
 
   59 abstract class Extract {
 
   61     protected Case currentCase;
 
   62     protected SleuthkitCase tskCase;
 
   63     private final Logger logger = Logger.getLogger(this.getClass().getName());
 
   64     private final ArrayList<String> errorMessages = 
new ArrayList<>();
 
   65     String moduleName = 
"";
 
   66     boolean dataFound = 
false;
 
   71     final void init() throws IngestModuleException {
 
   73             currentCase = Case.getCurrentCaseThrows();
 
   74             tskCase = currentCase.getSleuthkitCase();
 
   75         } 
catch (NoCurrentCaseException ex) {
 
   76             throw new IngestModuleException(Bundle.Extract_indexError_message(), ex);
 
   86     void configExtractor() throws IngestModuleException  {        
 
   89     abstract void process(Content dataSource, IngestJobContext context, DataSourceIngestModuleProgress progressBar);
 
   99     List<String> getErrorMessages() {
 
  100         return errorMessages;
 
  108     protected void addErrorMessage(String message) {
 
  109         errorMessages.add(message);
 
  124     protected BlackboardArtifact addArtifact(BlackboardArtifact.ARTIFACT_TYPE type, Content content, Collection<BlackboardAttribute> bbattributes) {
 
  126             BlackboardArtifact bbart = content.newArtifact(type);
 
  127             bbart.addAttributes(bbattributes);
 
  129             this.indexArtifact(bbart);
 
  131         } 
catch (TskException ex) {
 
  132             logger.log(Level.SEVERE, 
"Error while trying to add an artifact", ex); 
 
  142     @Messages({
"Extract.indexError.message=Failed to index artifact for keyword search.",
 
  143                "Extract.noOpenCase.errMsg=No open case available."})
 
  144     void indexArtifact(BlackboardArtifact bbart) {
 
  146             Blackboard blackboard = Case.getCurrentCaseThrows().getServices().getBlackboard();
 
  148             blackboard.indexArtifact(bbart);
 
  149         } 
catch (Blackboard.BlackboardException ex) {
 
  150             logger.log(Level.SEVERE, 
"Unable to index blackboard artifact " + bbart.getDisplayName(), ex); 
 
  151             MessageNotifyUtil.Notify.error(Bundle.Extract_indexError_message(), bbart.getDisplayName());
 
  152         } 
catch (NoCurrentCaseException ex) {
 
  153             logger.log(Level.SEVERE, 
"Exception while getting open case.", ex); 
 
  154             MessageNotifyUtil.Notify.error(Bundle.Extract_noOpenCase_errMsg(), bbart.getDisplayName());
 
  169     protected List<HashMap<String, Object>> dbConnect(String path, String query) {
 
  171         List<HashMap<String, Object>> list;
 
  172         String connectionString = 
"jdbc:sqlite:" + path; 
 
  174             SQLiteDBConnect tempdbconnect = 
new SQLiteDBConnect(
"org.sqlite.JDBC", connectionString); 
 
  175             temprs = tempdbconnect.executeQry(query);
 
  176             list = this.resultSetToArrayList(temprs);
 
  177             tempdbconnect.closeConnection();
 
  178         } 
catch (SQLException ex) {
 
  179             logger.log(Level.SEVERE, 
"Error while trying to read into a sqlite db." + connectionString, ex); 
 
  180             errorMessages.add(NbBundle.getMessage(
this.getClass(), 
"Extract.dbConn.errMsg.failedToQueryDb", getName()));
 
  181             return Collections.<HashMap<String, Object>>emptyList();
 
  193     private List<HashMap<String, Object>> resultSetToArrayList(ResultSet rs) 
throws SQLException {
 
  194         ResultSetMetaData md = rs.getMetaData();
 
  195         int columns = md.getColumnCount();
 
  196         List<HashMap<String, Object>> list = 
new ArrayList<>(50);
 
  198             HashMap<String, Object> row = 
new HashMap<>(columns);
 
  199             for (
int i = 1; i <= columns; ++i) {
 
  200                 if (rs.getObject(i) == null) {
 
  201                     row.put(md.getColumnName(i), 
"");
 
  203                     row.put(md.getColumnName(i), rs.getObject(i));
 
  217     protected String getName() {
 
  225     public boolean foundData() {
 
  233     protected void setFoundData(
boolean foundData){
 
  234         dataFound = foundData;
 
  241     protected Case getCurrentCase(){
 
  242         return this.currentCase;
 
  258     protected Collection<BlackboardAttribute> createHistoryAttribute(String url, Long accessTime,
 
  259             String referrer, String title, String programName, String domain, String user) 
throws TskCoreException {
 
  261         Collection<BlackboardAttribute> bbattributes = 
new ArrayList<>();
 
  262         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_URL,
 
  263                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  264                 (url != null) ? url : 
"")); 
 
  266         if (accessTime != null) {
 
  267             bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_ACCESSED,
 
  268                     RecentActivityExtracterModuleFactory.getModuleName(), accessTime));
 
  271         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_REFERRER,
 
  272                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  273                 (referrer != null) ? referrer : 
"")); 
 
  275         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_TITLE,
 
  276                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  277                 (title != null) ? title : 
"")); 
 
  279         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PROG_NAME,
 
  280                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  281                 (programName != null) ? programName : 
"")); 
 
  283         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DOMAIN,
 
  284                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  285                 (domain != null) ? domain : 
"")); 
 
  287         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_USER_NAME,
 
  288                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  289                 (user != null) ? user : 
"")); 
 
  305     protected Collection<BlackboardAttribute> createCookieAttributes(String url,
 
  306             Long creationTime, String name, String value, String programName, String domain) {
 
  308         Collection<BlackboardAttribute> bbattributes = 
new ArrayList<>();
 
  309         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_URL,
 
  310                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  311                 (url != null) ? url : 
"")); 
 
  313         if (creationTime != null) {
 
  314             bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME,
 
  315                     RecentActivityExtracterModuleFactory.getModuleName(), creationTime));
 
  318         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_NAME,
 
  319                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  320                 (name != null) ? name : 
"")); 
 
  322         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_VALUE,
 
  323                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  324                 (value != null) ? value : 
"")); 
 
  326         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PROG_NAME,
 
  327                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  328                 (programName != null) ? programName : 
"")); 
 
  330         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DOMAIN,
 
  331                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  332                 (domain != null) ? domain : 
"")); 
 
  347     protected Collection<BlackboardAttribute> createBookmarkAttributes(String url, String title, Long creationTime, String programName, String domain) {
 
  348         Collection<BlackboardAttribute> bbattributes = 
new ArrayList<>();
 
  350         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_URL,
 
  351                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  352                 (url != null) ? url : 
"")); 
 
  354         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_TITLE,
 
  355                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  356                 (title != null) ? title : 
"")); 
 
  358         if (creationTime != null) {
 
  359             bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_CREATED,
 
  360                     RecentActivityExtracterModuleFactory.getModuleName(), creationTime));
 
  363         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PROG_NAME,
 
  364                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  365                 (programName != null) ? programName : 
"")); 
 
  367         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DOMAIN,
 
  368                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  369                 (domain != null) ? domain : 
"")); 
 
  384     protected Collection<BlackboardAttribute> createDownloadAttributes(String path, Long pathID, String url, Long accessTime, String domain, String programName) {
 
  385         Collection<BlackboardAttribute> bbattributes = 
new ArrayList<>();
 
  387         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PATH,
 
  388                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  389                 (path != null) ? path : 
"")); 
 
  391         if (pathID != null && pathID != -1) {
 
  392             bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PATH_ID,
 
  393                     RecentActivityExtracterModuleFactory.getModuleName(),
 
  397         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_URL,
 
  398                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  399                 (url != null) ? url : 
"")); 
 
  401         if (accessTime != null) {
 
  402             bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_ACCESSED,
 
  403                     RecentActivityExtracterModuleFactory.getModuleName(), accessTime));
 
  406         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DOMAIN,
 
  407                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  408                 (domain != null) ? domain : 
"")); 
 
  410         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PROG_NAME,
 
  411                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  412                 (programName != null) ? programName : 
"")); 
 
  423     protected Collection<BlackboardAttribute> createDownloadSourceAttributes(String url) {
 
  424         Collection<BlackboardAttribute> bbattributes = 
new ArrayList<>();
 
  426         bbattributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_URL,
 
  427                 RecentActivityExtracterModuleFactory.getModuleName(),
 
  428                 (url != null) ? url : 
"")); 
 
  442     protected File createTemporaryFile(IngestJobContext context, AbstractFile file) 
throws IOException{
 
  443         Path tempFilePath = Paths.get(RAImageIngestModule.getRATempPath(
 
  444                 getCurrentCase(), getName()), file.getName() + file.getId() + file.getNameExtension());
 
  445         java.io.File tempFile = tempFilePath.toFile();
 
  448             ContentUtils.writeToFile(file, tempFile, context::dataSourceIngestIsCancelled);
 
  449         } 
catch (IOException ex) {
 
  450             throw new IOException(
"Error writingToFile: " + file, ex);