23 package org.sleuthkit.autopsy.recentactivity;
 
   25 import java.io.BufferedReader;
 
   26 import org.openide.util.NbBundle;
 
   30 import java.io.FileInputStream;
 
   31 import java.io.FileNotFoundException;
 
   32 import java.io.IOException;
 
   33 import java.io.InputStreamReader;
 
   34 import java.nio.file.Paths;
 
   35 import java.text.ParseException;
 
   36 import java.text.SimpleDateFormat;
 
   37 import java.util.ArrayList;
 
   38 import java.util.List;
 
   39 import java.util.logging.Level;
 
   41 import java.util.Collection;
 
   42 import java.util.Scanner;
 
   43 import java.util.stream.Collectors;
 
   44 import org.openide.modules.InstalledFileLocator;
 
   45 import org.openide.util.NbBundle.Messages;
 
   66 class ExtractIE 
extends Extract {
 
   68     private static final Logger logger = Logger.getLogger(ExtractIE.class.getName());
 
   69     private String PASCO_LIB_PATH;
 
   70     private final String JAVA_PATH;
 
   71     private static final String RESOURCE_URL_PREFIX = 
"res://";
 
   72     private static final SimpleDateFormat dateFormatter = 
new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
 
   73     private Content dataSource;
 
   74     private final IngestJobContext context;
 
   77         "Progress_Message_IE_History=IE History",
 
   78         "Progress_Message_IE_Bookmarks=IE Bookmarks",
 
   79         "Progress_Message_IE_Cookies=IE Cookies",
 
   80         "Progress_Message_IE_Downloads=IE Downloads",
 
   81         "Progress_Message_IE_FormHistory=IE Form History",
 
   82         "Progress_Message_IE_AutoFill=IE Auto Fill",
 
   83         "Progress_Message_IE_Logins=IE Logins",})
 
   85     ExtractIE(IngestJobContext context) {
 
   86         super(NbBundle.getMessage(ExtractIE.class, 
"ExtractIE.moduleName.text"), context);
 
   87         JAVA_PATH = PlatformUtil.getJavaPath();
 
   88         this.context = context;
 
   92     public void process(Content dataSource, DataSourceIngestModuleProgress progressBar) {
 
   93         String moduleTempDir = RAImageIngestModule.getRATempPath(getCurrentCase(), 
"IE", context.getJobId());
 
   94         String moduleTempResultsDir = Paths.get(moduleTempDir, 
"results").toString();
 
   96         this.dataSource = dataSource;
 
   99         progressBar.progress(Bundle.Progress_Message_IE_Bookmarks());
 
  102         if (context.dataSourceIngestIsCancelled()) {
 
  106         progressBar.progress(Bundle.Progress_Message_IE_Cookies());
 
  109         if (context.dataSourceIngestIsCancelled()) {
 
  113         progressBar.progress(Bundle.Progress_Message_IE_History());
 
  114         this.getHistory(moduleTempDir, moduleTempResultsDir);
 
  120     private void getBookmark() {
 
  122         List<AbstractFile> favoritesFiles;
 
  124             favoritesFiles = fileManager.
findFiles(dataSource, 
"%.url", 
"Favorites"); 
 
  125         } 
catch (TskCoreException ex) {
 
  126             logger.log(Level.WARNING, 
"Error fetching 'url' files for Internet Explorer bookmarks.", ex); 
 
  127             this.addErrorMessage(
 
  128                     NbBundle.getMessage(
this.getClass(), 
"ExtractIE.getBookmark.errMsg.errGettingBookmarks",
 
  129                             this.getDisplayName()));
 
  133         if (favoritesFiles.isEmpty()) {
 
  134             logger.log(Level.INFO, 
"Didn't find any IE bookmark files."); 
 
  139         Collection<BlackboardArtifact> bbartifacts = 
new ArrayList<>();
 
  140         for (AbstractFile fav : favoritesFiles) {
 
  141             if (fav.getSize() == 0) {
 
  145             if (context.dataSourceIngestIsCancelled()) {
 
  149             String url = getURLFromIEBookmarkFile(fav);
 
  151             String name = fav.getName();
 
  152             Long datetime = fav.getCrtime();
 
  153             String Tempdate = datetime.toString();
 
  154             datetime = Long.valueOf(Tempdate);
 
  155             String domain = extractDomain(url);
 
  158                 Collection<BlackboardAttribute> bbattributes = createBookmarkAttributes(
 
  162                         NbBundle.getMessage(
this.getClass(), 
"ExtractIE.moduleName.text"), 
 
  165                 bbartifacts.add(createArtifactWithAttributes(BlackboardArtifact.Type.TSK_WEB_BOOKMARK, fav, bbattributes));
 
  166             } 
catch (TskCoreException ex) {
 
  167                 logger.log(Level.SEVERE, String.format(
"Failed to create %s for file %d", ARTIFACT_TYPE.TSK_WEB_BOOKMARK.getDisplayName(), fav.getId()), ex);
 
  171         if (!context.dataSourceIngestIsCancelled()) {
 
  172             postArtifacts(bbartifacts);
 
  176     private String getURLFromIEBookmarkFile(AbstractFile fav) {
 
  177         BufferedReader reader = 
new BufferedReader(
new InputStreamReader(
new ReadContentInputStream(fav)));
 
  178         String line, url = 
"";
 
  180             line = reader.readLine();
 
  181             while (null != line) {
 
  184                 if (line.startsWith(
"URL")) { 
 
  185                     url = line.substring(line.indexOf(
"=") + 1);
 
  188                 line = reader.readLine();
 
  190         } 
catch (IOException ex) {
 
  191             logger.log(Level.WARNING, 
"Failed to read from content: " + fav.getName(), ex); 
 
  192             this.addErrorMessage(
 
  193                     NbBundle.getMessage(
this.getClass(), 
"ExtractIE.getURLFromIEBmkFile.errMsg", this.getDisplayName(),
 
  195         } 
catch (IndexOutOfBoundsException ex) {
 
  196             logger.log(Level.WARNING, 
"Failed while getting URL of IE bookmark. Unexpected format of the bookmark file: " + fav.getName(), ex); 
 
  197             this.addErrorMessage(
 
  198                     NbBundle.getMessage(
this.getClass(), 
"ExtractIE.getURLFromIEBmkFile.errMsg2", this.getDisplayName(),
 
  203             } 
catch (IOException ex) {
 
  204                 logger.log(Level.WARNING, 
"Failed to close reader.", ex); 
 
  214     private void getCookie() {
 
  216         List<AbstractFile> cookiesFiles;
 
  218             cookiesFiles = fileManager.
findFiles(dataSource, 
"%.txt", 
"Cookies"); 
 
  219         } 
catch (TskCoreException ex) {
 
  220             logger.log(Level.WARNING, 
"Error getting cookie files for IE"); 
 
  221             this.addErrorMessage(
 
  222                     NbBundle.getMessage(
this.getClass(), 
"ExtractIE.getCookie.errMsg.errGettingFile", this.getDisplayName()));
 
  226         if (cookiesFiles.isEmpty()) {
 
  227             logger.log(Level.INFO, 
"Didn't find any IE cookies files."); 
 
  232         Collection<BlackboardArtifact> bbartifacts = 
new ArrayList<>();
 
  233         for (AbstractFile cookiesFile : cookiesFiles) {
 
  234             if (context.dataSourceIngestIsCancelled()) {
 
  237             if (cookiesFile.getSize() == 0) {
 
  241             byte[] t = 
new byte[(int) cookiesFile.getSize()];
 
  243                 final int bytesRead = cookiesFile.read(t, 0, cookiesFile.getSize());
 
  244             } 
catch (TskCoreException ex) {
 
  245                 logger.log(Level.WARNING, 
"Error reading bytes of Internet Explorer cookie.", ex); 
 
  246                 this.addErrorMessage(
 
  247                         NbBundle.getMessage(
this.getClass(), 
"ExtractIE.getCookie.errMsg.errReadingIECookie",
 
  248                                 this.getDisplayName(), cookiesFile.getName()));
 
  251             String cookieString = 
new String(t);
 
  252             String[] values = cookieString.split(
"\n");
 
  253             String url = values.length > 2 ? values[2] : 
"";
 
  254             String value = values.length > 1 ? values[1] : 
"";
 
  255             String name = values.length > 0 ? values[0] : 
"";
 
  256             Long datetime = cookiesFile.getCrtime();
 
  257             String tempDate = datetime.toString();
 
  258             datetime = Long.valueOf(tempDate);
 
  259             String domain = extractDomain(url);
 
  261             Collection<BlackboardAttribute> bbattributes = 
new ArrayList<>();
 
  262             bbattributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_URL,
 
  263                     RecentActivityExtracterModuleFactory.getModuleName(), url));
 
  264             bbattributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_DATETIME_CREATED,
 
  265                     RecentActivityExtracterModuleFactory.getModuleName(), datetime));
 
  266             bbattributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_NAME,
 
  267                     RecentActivityExtracterModuleFactory.getModuleName(), (name != null) ? name : 
""));
 
  268             bbattributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_VALUE,
 
  269                     RecentActivityExtracterModuleFactory.getModuleName(), value));
 
  270             bbattributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_PROG_NAME,
 
  271                     RecentActivityExtracterModuleFactory.getModuleName(),
 
  272                     NbBundle.getMessage(this.getClass(), 
"ExtractIE.moduleName.text")));
 
  273             if (domain != null && domain.isEmpty() == 
false) {
 
  274                 bbattributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_DOMAIN,
 
  275                         RecentActivityExtracterModuleFactory.getModuleName(), domain));
 
  279                 bbartifacts.add(createArtifactWithAttributes(BlackboardArtifact.Type.TSK_WEB_COOKIE, cookiesFile, bbattributes));
 
  280             } 
catch (TskCoreException ex) {
 
  281                 logger.log(Level.SEVERE, String.format(
"Failed to create %s for file %d", BlackboardArtifact.Type.TSK_WEB_COOKIE.getDisplayName(), cookiesFile.getId()), ex);
 
  285         if (!context.dataSourceIngestIsCancelled()) {
 
  286             postArtifacts(bbartifacts);
 
  297     private void getHistory(String moduleTempDir, String moduleTempResultsDir) {
 
  298         logger.log(Level.INFO, 
"Pasco results path: {0}", moduleTempResultsDir); 
 
  299         boolean foundHistory = 
false;
 
  301         final File pascoRoot = InstalledFileLocator.getDefault().locate(
"pasco2", ExtractIE.class.getPackage().getName(), 
false); 
 
  302         if (pascoRoot == null) {
 
  303             this.addErrorMessage(
 
  304                     NbBundle.getMessage(
this.getClass(), 
"ExtractIE.getHistory.errMsg.unableToGetHist", this.getDisplayName()));
 
  305             logger.log(Level.SEVERE, 
"Error finding pasco program "); 
 
  309         final String pascoHome = pascoRoot.getAbsolutePath();
 
  310         logger.log(Level.INFO, 
"Pasco2 home: {0}", pascoHome); 
 
  312         PASCO_LIB_PATH = pascoHome + File.separator + 
"pasco2.jar" + File.pathSeparator 
 
  313                 + pascoHome + File.separator + 
"*";
 
  315         File resultsDir = 
new File(moduleTempResultsDir);
 
  319         FileManager fileManager = currentCase.getServices().getFileManager();
 
  320         List<AbstractFile> indexFiles;
 
  322             indexFiles = fileManager.findFiles(dataSource, 
"index.dat"); 
 
  323         } 
catch (TskCoreException ex) {
 
  324             this.addErrorMessage(NbBundle.getMessage(
this.getClass(), 
"ExtractIE.getHistory.errMsg.errGettingHistFiles",
 
  325                     this.getDisplayName()));
 
  326             logger.log(Level.WARNING, 
"Error fetching 'index.data' files for Internet Explorer history."); 
 
  330         if (indexFiles.isEmpty()) {
 
  331             String msg = NbBundle.getMessage(this.getClass(), 
"ExtractIE.getHistory.errMsg.noHistFiles");
 
  332             logger.log(Level.INFO, msg);
 
  337         Collection<BlackboardArtifact> bbartifacts = 
new ArrayList<>();
 
  339         String indexFileName;
 
  340         for (AbstractFile indexFile : indexFiles) {
 
  347             indexFileName = 
"index" + Integer.toString((
int) indexFile.getId()) + 
".dat"; 
 
  349             temps = moduleTempDir + File.separator + indexFileName; 
 
  350             File datFile = 
new File(temps);
 
  351             if (context.dataSourceIngestIsCancelled()) {
 
  355                 ContentUtils.writeToFile(indexFile, datFile, context::dataSourceIngestIsCancelled);
 
  356             } 
catch (IOException e) {
 
  357                 logger.log(Level.WARNING, 
"Error while trying to write index.dat file " + datFile.getAbsolutePath(), e); 
 
  358                 this.addErrorMessage(
 
  359                         NbBundle.getMessage(
this.getClass(), 
"ExtractIE.getHistory.errMsg.errWriteFile", this.getDisplayName(),
 
  360                                 datFile.getAbsolutePath()));
 
  364             String filename = 
"pasco2Result." + indexFile.getId() + 
".txt"; 
 
  365             boolean bPascProcSuccess = executePasco(temps, filename, moduleTempResultsDir);
 
  366             if (context.dataSourceIngestIsCancelled()) {
 
  372             if (bPascProcSuccess) {
 
  374                 bbartifacts.addAll(parsePascoOutput(indexFile, filename, moduleTempResultsDir).stream()
 
  375                         .filter(bbart -> bbart.getArtifactTypeID() == ARTIFACT_TYPE.TSK_WEB_HISTORY.getTypeID())
 
  376                         .collect(Collectors.toList()));
 
  377                 if (context.dataSourceIngestIsCancelled()) {
 
  385                 logger.log(Level.WARNING, 
"pasco execution failed on: {0}", filename); 
 
  386                 this.addErrorMessage(
 
  387                         NbBundle.getMessage(
this.getClass(), 
"ExtractIE.getHistory.errMsg.errProcHist", this.getDisplayName()));
 
  391         if (!context.dataSourceIngestIsCancelled()) {
 
  392             postArtifacts(bbartifacts);
 
  406         "# {0} - sub module name",
 
  407         "ExtractIE_executePasco_errMsg_errorRunningPasco={0}: Error analyzing Internet Explorer web history",})
 
  408     private boolean executePasco(String indexFilePath, String outputFileName, String moduleTempResultsDir) {
 
  409         boolean success = 
true;
 
  411             final String outputFileFullPath = moduleTempResultsDir + File.separator + outputFileName;
 
  412             final String errFileFullPath = moduleTempResultsDir + File.separator + outputFileName + 
".err"; 
 
  413             logger.log(Level.INFO, 
"Writing pasco results to: {0}", outputFileFullPath); 
 
  414             List<String> commandLine = 
new ArrayList<>();
 
  415             commandLine.add(JAVA_PATH);
 
  416             commandLine.add(
"--add-exports=java.xml/com.sun.org.apache.xalan.internal.xsltc.dom=ALL-UNNAMED");
 
  417             commandLine.add(
"-cp"); 
 
  418             commandLine.add(PASCO_LIB_PATH);
 
  419             commandLine.add(
"isi.pasco2.Main"); 
 
  420             commandLine.add(
"-T"); 
 
  421             commandLine.add(
"history");  
 
  422             commandLine.add(indexFilePath);
 
  423             ProcessBuilder processBuilder = 
new ProcessBuilder(commandLine);
 
  424             processBuilder.redirectOutput(
new File(outputFileFullPath));
 
  425             processBuilder.redirectError(
new File(errFileFullPath));
 
  435             ExecUtil.execute(processBuilder, 
new DataSourceIngestModuleProcessTerminator(context, 
true));
 
  437         } 
catch (IOException ex) {
 
  438             logger.log(Level.SEVERE, 
"Error executing Pasco to process Internet Explorer web history", ex); 
 
  439             addErrorMessage(Bundle.ExtractIE_executePasco_errMsg_errorRunningPasco(getDisplayName()));
 
  455     private Collection<BlackboardArtifact> parsePascoOutput(AbstractFile origFile, String pascoOutputFileName, String moduleTempResultsDir) {
 
  457         Collection<BlackboardArtifact> bbartifacts = 
new ArrayList<>();
 
  458         String fnAbs = moduleTempResultsDir + File.separator + pascoOutputFileName;
 
  460         File file = 
new File(fnAbs);
 
  461         if (file.exists() == 
false) {
 
  462             this.addErrorMessage(
 
  463                     NbBundle.getMessage(
this.getClass(), 
"ExtractIE.parsePascoOutput.errMsg.notFound", this.getDisplayName(),
 
  465             logger.log(Level.WARNING, 
"Pasco Output not found: {0}", file.getPath()); 
 
  471         if (file.length() == 0) {
 
  477             fileScanner = 
new Scanner(
new FileInputStream(file.toString()));
 
  478         } 
catch (FileNotFoundException ex) {
 
  479             this.addErrorMessage(
 
  480                     NbBundle.getMessage(
this.getClass(), 
"ExtractIE.parsePascoOutput.errMsg.errParsing", this.getDisplayName(),
 
  482             logger.log(Level.WARNING, 
"Unable to find the Pasco file at " + file.getPath(), ex); 
 
  485         while (fileScanner.hasNext()) {
 
  487             if (context.dataSourceIngestIsCancelled()) {
 
  491             String line = fileScanner.nextLine();
 
  492             if (!line.startsWith(
"URL")) {   
 
  496             String[] lineBuff = line.split(
"\\t"); 
 
  498             if (lineBuff.length < 4) {
 
  499                 logger.log(Level.INFO, 
"Found unrecognized IE history format."); 
 
  503             String actime = lineBuff[3];
 
  504             Long ftime = (long) 0;
 
  506             String realurl = null;
 
  513             if (lineBuff[1].contains(
"@")) {
 
  514                 String url[] = lineBuff[1].split(
"@", 2);
 
  519                 domain = extractDomain(url[0]);
 
  521                 if (domain != null && domain.isEmpty() == 
false) {
 
  525                     realurl = lineBuff[1].trim();
 
  532                     user = user.replace(
"Visited:", 
""); 
 
  533                     user = user.replace(
":Host:", 
""); 
 
  534                     user = user.replaceAll(
"(:)(.*?)(:)", 
"");
 
  537                     realurl = realurl.replace(
"Visited:", 
""); 
 
  538                     realurl = realurl.replaceAll(
":(.*?):", 
"");
 
  539                     realurl = realurl.replace(
":Host:", 
""); 
 
  540                     realurl = realurl.trim();
 
  541                     domain = extractDomain(realurl);
 
  547                 realurl = lineBuff[1].trim();
 
  548                 domain = extractDomain(realurl);
 
  551             if (!actime.isEmpty()) {
 
  553                     Long epochtime = dateFormatter.parse(actime).getTime();
 
  554                     ftime = epochtime / 1000;
 
  555                 } 
catch (ParseException e) {
 
  556                     this.addErrorMessage(
 
  557                             NbBundle.getMessage(
this.getClass(), 
"ExtractIE.parsePascoOutput.errMsg.errParsingEntry",
 
  558                                     this.getDisplayName()));
 
  559                     logger.log(Level.WARNING, String.format(
"Error parsing Pasco results, may have partial processing of corrupt file (id=%d)", origFile.getId()), e); 
 
  564                 Collection<BlackboardAttribute> bbattributes = createHistoryAttributes(
 
  569                         NbBundle.getMessage(
this.getClass(), 
"ExtractIE.moduleName.text"), 
 
  573                 bbartifacts.add(createArtifactWithAttributes(BlackboardArtifact.Type.TSK_WEB_HISTORY, origFile, bbattributes));
 
  574             } 
catch (TskCoreException ex) {
 
  575                 logger.log(Level.SEVERE, String.format(
"Failed to create %s for file %d", BlackboardArtifact.Type.TSK_WEB_HISTORY.getDisplayName(), origFile.getId()), ex);
 
  590     private String extractDomain(String url) {
 
  591         if (url == null || url.isEmpty()) {
 
  595         if (url.toLowerCase().startsWith(RESOURCE_URL_PREFIX)) {
 
  602         return NetworkUtils.extractDomain(url);
 
List< AbstractFile > findFiles(String fileName)