Autopsy  4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
ExtractIE.java
Go to the documentation of this file.
1  /*
2  *
3  * Autopsy Forensic Browser
4  *
5  * Copyright 2011-2016 Basis Technology Corp.
6  *
7  * Copyright 2012 42six Solutions.
8  * Contact: aebadirad <at> 42six <dot> com
9  * Project Contact/Architect: carrier <at> sleuthkit <dot> org
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 package org.sleuthkit.autopsy.recentactivity;
24 
25 import java.io.BufferedReader;
26 
27 import org.openide.util.NbBundle;
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.io.InputStreamReader;
34 import java.text.ParseException;
35 import java.text.SimpleDateFormat;
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.Set;
39 import java.util.HashSet;
40 import java.util.logging.Level;
42 import java.util.Collection;
43 import java.util.Scanner;
44 import org.openide.modules.InstalledFileLocator;
49 import org.sleuthkit.datamodel.BlackboardArtifact;
50 import org.sleuthkit.datamodel.BlackboardArtifact.ARTIFACT_TYPE;
51 import org.sleuthkit.datamodel.BlackboardAttribute;
52 import org.sleuthkit.datamodel.BlackboardAttribute.ATTRIBUTE_TYPE;
53 import org.sleuthkit.datamodel.Content;
57 import org.sleuthkit.datamodel.*;
58 
63 class ExtractIE extends Extract {
64 
65  private static final Logger logger = Logger.getLogger(ExtractIE.class.getName());
66  private final IngestServices services = IngestServices.getInstance();
67  private final String moduleTempResultsDir;
68  private String PASCO_LIB_PATH;
69  private final String JAVA_PATH;
70  private static final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
71  private Content dataSource;
72  private IngestJobContext context;
73 
74  ExtractIE() {
75  moduleName = NbBundle.getMessage(ExtractIE.class, "ExtractIE.moduleName.text");
76  moduleTempResultsDir = RAImageIngestModule.getRATempPath(Case.getCurrentCase(), "IE") + File.separator + "results"; //NON-NLS
77  JAVA_PATH = PlatformUtil.getJavaPath();
78  }
79 
80  @Override
81  public void process(Content dataSource, IngestJobContext context) {
82  this.dataSource = dataSource;
83  this.context = context;
84  dataFound = false;
85  this.getBookmark();
86  this.getCookie();
87  this.getHistory();
88  }
89 
93  private void getBookmark() {
94  org.sleuthkit.autopsy.casemodule.services.FileManager fileManager = currentCase.getServices().getFileManager();
95  List<AbstractFile> favoritesFiles;
96  try {
97  favoritesFiles = fileManager.findFiles(dataSource, "%.url", "Favorites"); //NON-NLS
98  } catch (TskCoreException ex) {
99  logger.log(Level.WARNING, "Error fetching 'url' files for Internet Explorer bookmarks.", ex); //NON-NLS
100  this.addErrorMessage(
101  NbBundle.getMessage(this.getClass(), "ExtractIE.getBookmark.errMsg.errGettingBookmarks",
102  this.getName()));
103  return;
104  }
105 
106  if (favoritesFiles.isEmpty()) {
107  logger.log(Level.INFO, "Didn't find any IE bookmark files."); //NON-NLS
108  return;
109  }
110 
111  dataFound = true;
112  for (AbstractFile fav : favoritesFiles) {
113  if (fav.getSize() == 0) {
114  continue;
115  }
116 
117  if (context.dataSourceIngestIsCancelled()) {
118  break;
119  }
120 
121  String url = getURLFromIEBookmarkFile(fav);
122 
123  String name = fav.getName();
124  Long datetime = fav.getCrtime();
125  String Tempdate = datetime.toString();
126  datetime = Long.valueOf(Tempdate);
127  String domain = Util.extractDomain(url);
128 
129  Collection<BlackboardAttribute> bbattributes = new ArrayList<>();
130  bbattributes.add(new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_URL,
131  NbBundle.getMessage(this.getClass(),
132  "ExtractIE.parentModuleName.noSpace"), url));
133  bbattributes.add(new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_TITLE,
134  NbBundle.getMessage(this.getClass(),
135  "ExtractIE.parentModuleName.noSpace"), name));
136  bbattributes.add(new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_DATETIME_CREATED,
137  NbBundle.getMessage(this.getClass(),
138  "ExtractIE.parentModuleName.noSpace"), datetime));
139  bbattributes.add(new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_PROG_NAME,
140  NbBundle.getMessage(this.getClass(),
141  "ExtractIE.parentModuleName.noSpace"),
142  NbBundle.getMessage(this.getClass(), "ExtractIE.moduleName.text")));
143  bbattributes.add(new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_DOMAIN,
144  NbBundle.getMessage(this.getClass(),
145  "ExtractIE.parentModuleName.noSpace"), domain));
146  this.addArtifact(ARTIFACT_TYPE.TSK_WEB_BOOKMARK, fav, bbattributes);
147  }
148  services.fireModuleDataEvent(new ModuleDataEvent(
149  NbBundle.getMessage(this.getClass(), "ExtractIE.parentModuleName"), BlackboardArtifact.ARTIFACT_TYPE.TSK_WEB_BOOKMARK));
150  }
151 
152  private String getURLFromIEBookmarkFile(AbstractFile fav) {
153  BufferedReader reader = new BufferedReader(new InputStreamReader(new ReadContentInputStream(fav)));
154  String line, url = "";
155  try {
156  line = reader.readLine();
157  while (null != line) {
158  // The actual shortcut line we are interested in is of the
159  // form URL=http://path/to/website
160  if (line.startsWith("URL")) { //NON-NLS
161  url = line.substring(line.indexOf("=") + 1);
162  break;
163  }
164  line = reader.readLine();
165  }
166  } catch (IOException ex) {
167  logger.log(Level.WARNING, "Failed to read from content: " + fav.getName(), ex); //NON-NLS
168  this.addErrorMessage(
169  NbBundle.getMessage(this.getClass(), "ExtractIE.getURLFromIEBmkFile.errMsg", this.getName(),
170  fav.getName()));
171  } catch (IndexOutOfBoundsException ex) {
172  logger.log(Level.WARNING, "Failed while getting URL of IE bookmark. Unexpected format of the bookmark file: " + fav.getName(), ex); //NON-NLS
173  this.addErrorMessage(
174  NbBundle.getMessage(this.getClass(), "ExtractIE.getURLFromIEBmkFile.errMsg2", this.getName(),
175  fav.getName()));
176  } finally {
177  try {
178  reader.close();
179  } catch (IOException ex) {
180  logger.log(Level.WARNING, "Failed to close reader.", ex); //NON-NLS
181  }
182  }
183 
184  return url;
185  }
186 
190  private void getCookie() {
191  org.sleuthkit.autopsy.casemodule.services.FileManager fileManager = currentCase.getServices().getFileManager();
192  List<AbstractFile> cookiesFiles;
193  try {
194  cookiesFiles = fileManager.findFiles(dataSource, "%.txt", "Cookies"); //NON-NLS
195  } catch (TskCoreException ex) {
196  logger.log(Level.WARNING, "Error getting cookie files for IE"); //NON-NLS
197  this.addErrorMessage(
198  NbBundle.getMessage(this.getClass(), "ExtractIE.getCookie.errMsg.errGettingFile", this.getName()));
199  return;
200  }
201 
202  if (cookiesFiles.isEmpty()) {
203  logger.log(Level.INFO, "Didn't find any IE cookies files."); //NON-NLS
204  return;
205  }
206 
207  dataFound = true;
208  for (AbstractFile cookiesFile : cookiesFiles) {
209  if (context.dataSourceIngestIsCancelled()) {
210  break;
211  }
212  if (cookiesFile.getSize() == 0) {
213  continue;
214  }
215 
216  byte[] t = new byte[(int) cookiesFile.getSize()];
217  try {
218  final int bytesRead = cookiesFile.read(t, 0, cookiesFile.getSize());
219  } catch (TskCoreException ex) {
220  logger.log(Level.WARNING, "Error reading bytes of Internet Explorer cookie.", ex); //NON-NLS
221  this.addErrorMessage(
222  NbBundle.getMessage(this.getClass(), "ExtractIE.getCookie.errMsg.errReadingIECookie",
223  this.getName(), cookiesFile.getName()));
224  continue;
225  }
226  String cookieString = new String(t);
227  String[] values = cookieString.split("\n");
228  String url = values.length > 2 ? values[2] : "";
229  String value = values.length > 1 ? values[1] : "";
230  String name = values.length > 0 ? values[0] : "";
231  Long datetime = cookiesFile.getCrtime();
232  String tempDate = datetime.toString();
233  datetime = Long.valueOf(tempDate);
234  String domain = Util.extractDomain(url);
235 
236  Collection<BlackboardAttribute> bbattributes = new ArrayList<>();
237  bbattributes.add(new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_URL,
238  NbBundle.getMessage(this.getClass(),
239  "ExtractIE.parentModuleName.noSpace"), url));
240  bbattributes.add(new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_DATETIME,
241  NbBundle.getMessage(this.getClass(),
242  "ExtractIE.parentModuleName.noSpace"), datetime));
243  bbattributes.add(new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_NAME,
244  NbBundle.getMessage(this.getClass(),
245  "ExtractIE.parentModuleName.noSpace"), (name != null) ? name : ""));
246  bbattributes.add(new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_VALUE,
247  NbBundle.getMessage(this.getClass(),
248  "ExtractIE.parentModuleName.noSpace"), value));
249  bbattributes.add(new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_PROG_NAME,
250  NbBundle.getMessage(this.getClass(),
251  "ExtractIE.parentModuleName.noSpace"),
252  NbBundle.getMessage(this.getClass(), "ExtractIE.moduleName.text")));
253  bbattributes.add(new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_DOMAIN,
254  NbBundle.getMessage(this.getClass(),
255  "ExtractIE.parentModuleName.noSpace"), domain));
256  this.addArtifact(ARTIFACT_TYPE.TSK_WEB_COOKIE, cookiesFile, bbattributes);
257  }
258  services.fireModuleDataEvent(new ModuleDataEvent(
259  NbBundle.getMessage(this.getClass(), "ExtractIE.parentModuleName"), BlackboardArtifact.ARTIFACT_TYPE.TSK_WEB_COOKIE));
260  }
261 
265  private void getHistory() {
266  logger.log(Level.INFO, "Pasco results path: {0}", moduleTempResultsDir); //NON-NLS
267  boolean foundHistory = false;
268 
269  final File pascoRoot = InstalledFileLocator.getDefault().locate("pasco2", ExtractIE.class.getPackage().getName(), false); //NON-NLS
270  if (pascoRoot == null) {
271  this.addErrorMessage(
272  NbBundle.getMessage(this.getClass(), "ExtractIE.getHistory.errMsg.unableToGetHist", this.getName()));
273  logger.log(Level.SEVERE, "Error finding pasco program "); //NON-NLS
274  return;
275  }
276 
277  final String pascoHome = pascoRoot.getAbsolutePath();
278  logger.log(Level.INFO, "Pasco2 home: {0}", pascoHome); //NON-NLS
279 
280  PASCO_LIB_PATH = pascoHome + File.separator + "pasco2.jar" + File.pathSeparator //NON-NLS
281  + pascoHome + File.separator + "*";
282 
283  File resultsDir = new File(moduleTempResultsDir);
284  resultsDir.mkdirs();
285 
286  // get index.dat files
287  org.sleuthkit.autopsy.casemodule.services.FileManager fileManager = currentCase.getServices().getFileManager();
288  List<AbstractFile> indexFiles;
289  try {
290  indexFiles = fileManager.findFiles(dataSource, "index.dat"); //NON-NLS
291  } catch (TskCoreException ex) {
292  this.addErrorMessage(NbBundle.getMessage(this.getClass(), "ExtractIE.getHistory.errMsg.errGettingHistFiles",
293  this.getName()));
294  logger.log(Level.WARNING, "Error fetching 'index.data' files for Internet Explorer history."); //NON-NLS
295  return;
296  }
297 
298  if (indexFiles.isEmpty()) {
299  String msg = NbBundle.getMessage(this.getClass(), "ExtractIE.getHistory.errMsg.noHistFiles");
300  logger.log(Level.INFO, msg);
301  return;
302  }
303 
304  dataFound = true;
305  String temps;
306  String indexFileName;
307  for (AbstractFile indexFile : indexFiles) {
308  // Since each result represent an index.dat file,
309  // just create these files with the following notation:
310  // index<Number>.dat (i.e. index0.dat, index1.dat,..., indexN.dat)
311  // Write each index.dat file to a temp directory.
312  //BlackboardArtifact bbart = fsc.newArtifact(ARTIFACT_TYPE.TSK_WEB_HISTORY);
313  indexFileName = "index" + Integer.toString((int) indexFile.getId()) + ".dat"; //NON-NLS
314  //indexFileName = "index" + Long.toString(bbart.getArtifactID()) + ".dat";
315  temps = RAImageIngestModule.getRATempPath(currentCase, "IE") + File.separator + indexFileName; //NON-NLS
316  File datFile = new File(temps);
317  if (context.dataSourceIngestIsCancelled()) {
318  break;
319  }
320  try {
321  ContentUtils.writeToFile(indexFile, datFile, context::dataSourceIngestIsCancelled);
322  } catch (IOException e) {
323  logger.log(Level.WARNING, "Error while trying to write index.dat file " + datFile.getAbsolutePath(), e); //NON-NLS
324  this.addErrorMessage(
325  NbBundle.getMessage(this.getClass(), "ExtractIE.getHistory.errMsg.errWriteFile", this.getName(),
326  datFile.getAbsolutePath()));
327  continue;
328  }
329 
330  String filename = "pasco2Result." + indexFile.getId() + ".txt"; //NON-NLS
331  boolean bPascProcSuccess = executePasco(temps, filename);
332  if (context.dataSourceIngestIsCancelled()) {
333  return;
334  }
335 
336  //At this point pasco2 proccessed the index files.
337  //Now fetch the results, parse them and the delete the files.
338  if (bPascProcSuccess) {
339  parsePascoOutput(indexFile, filename);
340  foundHistory = true;
341 
342  //Delete index<n>.dat file since it was succcessfully by Pasco
343  datFile.delete();
344  } else {
345  logger.log(Level.WARNING, "pasco execution failed on: {0}", this.getName()); //NON-NLS
346  this.addErrorMessage(
347  NbBundle.getMessage(this.getClass(), "ExtractIE.getHistory.errMsg.errProcHist", this.getName()));
348  }
349  }
350 
351  if (foundHistory) {
352  services.fireModuleDataEvent(new ModuleDataEvent(
353  NbBundle.getMessage(this.getClass(), "ExtractIE.parentModuleName"), BlackboardArtifact.ARTIFACT_TYPE.TSK_WEB_HISTORY));
354  }
355  }
356 
365  private boolean executePasco(String indexFilePath, String outputFileName) {
366  boolean success = true;
367  try {
368  final String outputFileFullPath = moduleTempResultsDir + File.separator + outputFileName;
369  final String errFileFullPath = moduleTempResultsDir + File.separator + outputFileName + ".err"; //NON-NLS
370  logger.log(Level.INFO, "Writing pasco results to: {0}", outputFileFullPath); //NON-NLS
371  List<String> commandLine = new ArrayList<>();
372  commandLine.add(JAVA_PATH);
373  commandLine.add("-cp"); //NON-NLS
374  commandLine.add(PASCO_LIB_PATH);
375  commandLine.add("isi.pasco2.Main"); //NON-NLS
376  commandLine.add("-T"); //NON-NLS
377  commandLine.add("history"); //NON-NLS
378  commandLine.add(indexFilePath);
379  ProcessBuilder processBuilder = new ProcessBuilder(commandLine);
380  processBuilder.redirectOutput(new File(outputFileFullPath));
381  processBuilder.redirectError(new File(errFileFullPath));
382  /*
383  * NOTE on Pasco return codes: There is no documentation for Pasco.
384  * Looking at the Pasco source code I see that when something goes
385  * wrong Pasco returns a negative number as a return code. However,
386  * we should still attempt to parse the Pasco output even if that
387  * happens. I have seen many situations where Pasco output file
388  * contains a lot of useful data and only the last entry is
389  * corrupted.
390  */
391  ExecUtil.execute(processBuilder, new DataSourceIngestModuleProcessTerminator(context));
392  // @@@ Investigate use of history versus cache as type.
393  } catch (IOException ex) {
394  success = false;
395  logger.log(Level.SEVERE, "Unable to execute Pasco to process Internet Explorer web history.", ex); //NON-NLS
396  }
397  return success;
398  }
399 
407  private void parsePascoOutput(AbstractFile origFile, String pascoOutputFileName) {
408 
409  String fnAbs = moduleTempResultsDir + File.separator + pascoOutputFileName;
410 
411  File file = new File(fnAbs);
412  if (file.exists() == false) {
413  this.addErrorMessage(
414  NbBundle.getMessage(this.getClass(), "ExtractIE.parsePascoOutput.errMsg.notFound", this.getName(),
415  file.getName()));
416  logger.log(Level.WARNING, "Pasco Output not found: {0}", file.getPath()); //NON-NLS
417  return;
418  }
419 
420  // Make sure the file the is not empty or the Scanner will
421  // throw a "No Line found" Exception
422  if (file.length() == 0) {
423  return;
424  }
425 
426  Scanner fileScanner;
427  try {
428  fileScanner = new Scanner(new FileInputStream(file.toString()));
429  } catch (FileNotFoundException ex) {
430  this.addErrorMessage(
431  NbBundle.getMessage(this.getClass(), "ExtractIE.parsePascoOutput.errMsg.errParsing", this.getName(),
432  file.getName()));
433  logger.log(Level.WARNING, "Unable to find the Pasco file at " + file.getPath(), ex); //NON-NLS
434  return;
435  }
436 
437  // Keep a list of reported user accounts to avoid repeats
438  Set<String> reportedUserAccounts = new HashSet<>();
439 
440  while (fileScanner.hasNext()) {
441  String line = fileScanner.nextLine();
442  if (!line.startsWith("URL")) { //NON-NLS
443  continue;
444  }
445 
446  String[] lineBuff = line.split("\\t"); //NON-NLS
447 
448  if (lineBuff.length < 4) {
449  logger.log(Level.INFO, "Found unrecognized IE history format."); //NON-NLS
450  continue;
451  }
452 
453  String actime = lineBuff[3];
454  Long ftime = (long) 0;
455  String user;
456  String realurl;
457  String domain;
458 
459  /*
460  * We've seen two types of lines: URL http://XYZ.com .... URL
461  * Visited: Joe@http://XYZ.com ....
462  */
463  if (lineBuff[1].contains("@")) {
464  String url[] = lineBuff[1].split("@", 2);
465  user = url[0];
466  user = user.replace("Visited:", ""); //NON-NLS
467  user = user.replace(":Host:", ""); //NON-NLS
468  user = user.replaceAll("(:)(.*?)(:)", "");
469  user = user.trim();
470  realurl = url[1];
471  realurl = realurl.replace("Visited:", ""); //NON-NLS
472  realurl = realurl.replaceAll(":(.*?):", "");
473  realurl = realurl.replace(":Host:", ""); //NON-NLS
474  realurl = realurl.trim();
475  } else {
476  user = "";
477  realurl = lineBuff[1].trim();
478  }
479 
480  domain = Util.extractDomain(realurl);
481 
482  if (!actime.isEmpty()) {
483  try {
484  Long epochtime = dateFormatter.parse(actime).getTime();
485  ftime = epochtime / 1000;
486  } catch (ParseException e) {
487  this.addErrorMessage(
488  NbBundle.getMessage(this.getClass(), "ExtractIE.parsePascoOutput.errMsg.errParsingEntry",
489  this.getName()));
490  logger.log(Level.WARNING, String.format("Error parsing Pasco results, may have partial processing of corrupt file (id=%d)", origFile.getId()), e); //NON-NLS
491  }
492  }
493 
494  try {
495  BlackboardArtifact bbart = origFile.newArtifact(ARTIFACT_TYPE.TSK_WEB_HISTORY);
496  Collection<BlackboardAttribute> bbattributes = new ArrayList<>();
497  bbattributes.add(new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_URL,
498  NbBundle.getMessage(this.getClass(),
499  "ExtractIE.parentModuleName.noSpace"), realurl));
500  //bbattributes.add(new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_URL_DECODED.getTypeID(), "RecentActivity", EscapeUtil.decodeURL(realurl)));
501 
502  bbattributes.add(new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_DATETIME_ACCESSED,
503  NbBundle.getMessage(this.getClass(),
504  "ExtractIE.parentModuleName.noSpace"), ftime));
505  bbattributes.add(new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_REFERRER,
506  NbBundle.getMessage(this.getClass(),
507  "ExtractIE.parentModuleName.noSpace"), ""));
508  // @@@ NOte that other browser modules are adding TITLE in hre for the title
509  bbattributes.add(new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_PROG_NAME,
510  NbBundle.getMessage(this.getClass(),
511  "ExtractIE.parentModuleName.noSpace"),
512  NbBundle.getMessage(this.getClass(),
513  "ExtractIE.moduleName.text")));
514  bbattributes.add(new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_DOMAIN,
515  NbBundle.getMessage(this.getClass(),
516  "ExtractIE.parentModuleName.noSpace"), domain));
517  bbattributes.add(new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_USER_NAME,
518  NbBundle.getMessage(this.getClass(),
519  "ExtractIE.parentModuleName.noSpace"), user));
520  bbart.addAttributes(bbattributes);
521 
522  // index the artifact for keyword search
523  this.indexArtifact(bbart);
524 
525  if ((!user.isEmpty()) && (!reportedUserAccounts.contains(user))) {
526  BlackboardArtifact osAttr = origFile.newArtifact(ARTIFACT_TYPE.TSK_OS_ACCOUNT);
527  osAttr.addAttribute(new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_USER_NAME,
528  NbBundle.getMessage(this.getClass(), "ExtractIE.parentModuleName.noSpace"), user));
529 
530  // index the artifact for keyword search
531  this.indexArtifact(osAttr);
532 
533  reportedUserAccounts.add(user);
534  }
535  } catch (TskCoreException ex) {
536  logger.log(Level.SEVERE, "Error writing Internet Explorer web history artifact to the blackboard.", ex); //NON-NLS
537  }
538  }
539  fileScanner.close();
540  }
541 }
synchronized List< AbstractFile > findFiles(String fileName)

Copyright © 2012-2016 Basis Technology. Generated on: Tue Oct 25 2016
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.