19 package org.sleuthkit.autopsy.recentactivity;
21 import com.dd.plist.NSArray;
22 import com.dd.plist.NSDate;
23 import com.dd.plist.NSDictionary;
24 import com.dd.plist.NSObject;
25 import com.dd.plist.NSString;
26 import com.dd.plist.PropertyListFormatException;
27 import com.dd.plist.PropertyListParser;
29 import java.io.IOException;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.text.ParseException;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.HashMap;
36 import java.util.Iterator;
37 import java.util.List;
38 import java.util.logging.Level;
39 import javax.xml.parsers.ParserConfigurationException;
40 import org.apache.commons.io.FilenameUtils;
41 import org.openide.util.NbBundle.Messages;
52 import static org.
sleuthkit.datamodel.BlackboardArtifact.ARTIFACT_TYPE.TSK_WEB_BOOKMARK;
53 import static org.
sleuthkit.datamodel.BlackboardArtifact.ARTIFACT_TYPE.TSK_WEB_COOKIE;
54 import static org.
sleuthkit.datamodel.BlackboardArtifact.ARTIFACT_TYPE.TSK_WEB_DOWNLOAD;
55 import static org.
sleuthkit.datamodel.BlackboardArtifact.ARTIFACT_TYPE.TSK_WEB_HISTORY;
58 import org.xml.sax.SAXException;
64 final class ExtractSafari
extends Extract {
66 private final IngestServices services = IngestServices.getInstance();
69 private static final String HISTORY_QUERY =
"SELECT url, title, visit_time + 978307200 as time FROM 'history_items' JOIN history_visits ON history_item = history_items.id;";
71 private static final String HISTORY_FILE_NAME =
"History.db";
72 private static final String BOOKMARK_FILE_NAME =
"Bookmarks.plist";
73 private static final String DOWNLOAD_FILE_NAME =
"Downloads.plist";
74 private static final String COOKIE_FILE_NAME =
"Cookies.binarycookies";
75 private static final String COOKIE_FOLDER =
"Cookies";
76 private static final String SAFARI_FOLDER =
"Safari";
78 private static final String HEAD_URL =
"url";
79 private static final String HEAD_TITLE =
"title";
80 private static final String HEAD_TIME =
"time";
82 private static final String PLIST_KEY_CHILDREN =
"Children";
83 private static final String PLIST_KEY_URL =
"URLString";
84 private static final String PLIST_KEY_URI =
"URIDictionary";
85 private static final String PLIST_KEY_TITLE =
"title";
86 private static final String PLIST_KEY_DOWNLOAD_URL =
"DownloadEntryURL";
87 private static final String PLIST_KEY_DOWNLOAD_DATE =
"DownloadEntryDateAddedKey";
88 private static final String PLIST_KEY_DOWNLOAD_PATH =
"DownloadEntryPath";
89 private static final String PLIST_KEY_DOWNLOAD_HISTORY =
"DownloadHistory";
91 private static final Logger LOG = Logger.getLogger(ExtractSafari.class.getName());
94 "ExtractSafari_Module_Name=Safari",
95 "ExtractSafari_Error_Getting_History=An error occurred while processing Safari history files.",
96 "ExtractSafari_Error_Parsing_Bookmark=An error occured while processing Safari Bookmark files",
97 "ExtractSafari_Error_Parsing_Cookies=An error occured while processing Safari Cookies files",
98 "Progress_Message_Safari_History=Safari History",
99 "Progress_Message_Safari_Bookmarks=Safari Bookmarks",
100 "Progress_Message_Safari_Cookies=Safari Cookies",
101 "Progress_Message_Safari_Downloads=Safari Downloads",
105 protected String getName() {
106 return Bundle.ExtractSafari_Module_Name();
110 void process(Content dataSource, IngestJobContext context, DataSourceIngestModuleProgress progressBar) {
113 progressBar.progress(Bundle.Progress_Message_Safari_Cookies());
115 processHistoryDB(dataSource, context);
117 }
catch (IOException | TskCoreException ex) {
118 this.addErrorMessage(Bundle.ExtractSafari_Error_Getting_History());
119 LOG.log(Level.SEVERE,
"Exception thrown while processing history file.", ex);
122 if (context.dataSourceIngestIsCancelled()) {
126 progressBar.progress(Bundle.Progress_Message_Safari_Bookmarks());
128 processBookmarkPList(dataSource, context);
129 }
catch (IOException | TskCoreException | SAXException | PropertyListFormatException | ParseException | ParserConfigurationException ex) {
130 this.addErrorMessage(Bundle.ExtractSafari_Error_Parsing_Bookmark());
131 LOG.log(Level.SEVERE,
"Exception thrown while parsing Safari Bookmarks file.", ex);
134 if (context.dataSourceIngestIsCancelled()) {
138 progressBar.progress(Bundle.Progress_Message_Safari_Downloads());
140 processDownloadsPList(dataSource, context);
141 }
catch (IOException | TskCoreException | SAXException | PropertyListFormatException | ParseException | ParserConfigurationException ex) {
142 this.addErrorMessage(Bundle.ExtractSafari_Error_Parsing_Bookmark());
143 LOG.log(Level.SEVERE,
"Exception thrown while parsing Safari Download.plist file.", ex);
146 if (context.dataSourceIngestIsCancelled()) {
150 progressBar.progress(Bundle.Progress_Message_Safari_Cookies());
152 processBinaryCookieFile(dataSource, context);
153 }
catch (TskCoreException ex) {
154 this.addErrorMessage(Bundle.ExtractSafari_Error_Parsing_Cookies());
155 LOG.log(Level.SEVERE,
"Exception thrown while processing Safari cookies file.", ex);
166 private void processHistoryDB(Content dataSource, IngestJobContext context)
throws TskCoreException, IOException {
167 FileManager fileManager = getCurrentCase().getServices().getFileManager();
169 List<AbstractFile> historyFiles = fileManager.findFiles(dataSource, HISTORY_FILE_NAME, SAFARI_FOLDER);
171 if (historyFiles == null || historyFiles.isEmpty()) {
177 for (AbstractFile historyFile : historyFiles) {
178 if (context.dataSourceIngestIsCancelled()) {
182 getHistory(context, historyFile);
197 private void processBookmarkPList(Content dataSource, IngestJobContext context)
throws TskCoreException, IOException, SAXException, PropertyListFormatException, ParseException, ParserConfigurationException {
198 FileManager fileManager = getCurrentCase().getServices().getFileManager();
200 List<AbstractFile> files = fileManager.findFiles(dataSource, BOOKMARK_FILE_NAME, SAFARI_FOLDER);
202 if (files == null || files.isEmpty()) {
208 for (AbstractFile file : files) {
209 if (context.dataSourceIngestIsCancelled()) {
213 getBookmarks(context, file);
229 private void processDownloadsPList(Content dataSource, IngestJobContext context)
throws TskCoreException, IOException, SAXException, PropertyListFormatException, ParseException, ParserConfigurationException {
230 FileManager fileManager = getCurrentCase().getServices().getFileManager();
232 List<AbstractFile> files = fileManager.findFiles(dataSource, DOWNLOAD_FILE_NAME, SAFARI_FOLDER);
234 if (files == null || files.isEmpty()) {
240 for (AbstractFile file : files) {
241 if (context.dataSourceIngestIsCancelled()) {
245 getDownloads(dataSource, context, file);
256 private void processBinaryCookieFile(Content dataSource, IngestJobContext context)
throws TskCoreException {
257 FileManager fileManager = getCurrentCase().getServices().getFileManager();
259 List<AbstractFile> files = fileManager.findFiles(dataSource, COOKIE_FILE_NAME, COOKIE_FOLDER);
261 if (files == null || files.isEmpty()) {
267 for (AbstractFile file : files) {
268 if (context.dataSourceIngestIsCancelled()) {
272 getCookies(context, file);
273 }
catch (IOException ex) {
274 LOG.log(Level.WARNING, String.format(
"Failed to get cookies from file %s", Paths.get(file.getUniquePath(), file.getName()).toString()), ex);
287 private void getHistory(IngestJobContext context, AbstractFile historyFile)
throws TskCoreException, IOException {
288 if (historyFile.getSize() == 0) {
292 File tempHistoryFile = createTemporaryFile(context, historyFile, context.getJobId());
295 ContentUtils.writeToFile(historyFile, tempHistoryFile, context::dataSourceIngestIsCancelled);
296 }
catch (IOException ex) {
297 throw new IOException(
"Error writingToFile: " + historyFile, ex);
301 if(!context.dataSourceIngestIsCancelled()) {
302 postArtifacts(getHistoryArtifacts(historyFile, tempHistoryFile.toPath(), context));
305 tempHistoryFile.delete();
322 private void getBookmarks(IngestJobContext context, AbstractFile file)
throws TskCoreException, IOException, SAXException, PropertyListFormatException, ParseException, ParserConfigurationException {
323 if (file.getSize() == 0) {
327 File tempFile = createTemporaryFile(context, file, context.getJobId());
330 if(!context.dataSourceIngestIsCancelled()) {
331 postArtifacts(getBookmarkArtifacts(file, tempFile, context));
352 private void getDownloads(Content dataSource, IngestJobContext context, AbstractFile file)
throws TskCoreException, IOException, SAXException, PropertyListFormatException, ParseException, ParserConfigurationException {
353 if (file.getSize() == 0) {
357 File tempFile = createTemporaryFile(context, file, context.getJobId());
360 if(!context.dataSourceIngestIsCancelled()) {
361 postArtifacts(getDownloadArtifacts(dataSource, file, tempFile));
364 if (tempFile != null) {
380 private void getCookies(IngestJobContext context, AbstractFile file)
throws TskCoreException, IOException {
381 if (file.getSize() == 0) {
385 File tempFile = null;
388 tempFile = createTemporaryFile(context, file, context.getJobId());
390 if(!context.dataSourceIngestIsCancelled()) {
391 postArtifacts(getCookieArtifacts(file, tempFile, context));
395 if (tempFile != null) {
411 private Collection<BlackboardArtifact> getHistoryArtifacts(AbstractFile origFile, Path tempFilePath, IngestJobContext context)
throws TskCoreException {
412 List<HashMap<String, Object>> historyList = this.dbConnect(tempFilePath.toString(), HISTORY_QUERY);
414 if (historyList == null || historyList.isEmpty()) {
418 Collection<BlackboardArtifact> bbartifacts =
new ArrayList<>();
419 for (HashMap<String, Object> row : historyList) {
420 if (context.dataSourceIngestIsCancelled()) {
424 String url = row.get(HEAD_URL).toString();
425 String title = row.get(HEAD_TITLE).toString();
426 Long time = (Double.valueOf(row.get(HEAD_TIME).toString())).longValue();
429 createArtifactWithAttributes(
432 createHistoryAttribute(url, time, null, title,
433 this.getName(), NetworkUtils.extractDomain(url), null)));
452 private Collection<BlackboardArtifact> getBookmarkArtifacts(AbstractFile origFile, File tempFile, IngestJobContext context)
throws IOException, PropertyListFormatException, ParseException, ParserConfigurationException, SAXException, TskCoreException {
453 Collection<BlackboardArtifact> bbartifacts =
new ArrayList<>();
456 NSDictionary root = (NSDictionary) PropertyListParser.parse(tempFile);
458 parseBookmarkDictionary(bbartifacts, origFile, root, context);
459 }
catch (PropertyListFormatException ex) {
460 PropertyListFormatException plfe =
new PropertyListFormatException(origFile.getName() +
": " + ex.getMessage());
461 plfe.setStackTrace(ex.getStackTrace());
463 }
catch (ParseException ex) {
464 ParseException pe =
new ParseException(origFile.getName() +
": " + ex.getMessage(), ex.getErrorOffset());
465 pe.setStackTrace(ex.getStackTrace());
467 }
catch (ParserConfigurationException ex) {
468 ParserConfigurationException pce =
new ParserConfigurationException(origFile.getName() +
": " + ex.getMessage());
469 pce.setStackTrace(ex.getStackTrace());
471 }
catch (SAXException ex) {
472 SAXException se =
new SAXException(origFile.getName() +
": " + ex.getMessage());
473 se.setStackTrace(ex.getStackTrace());
493 private Collection<BlackboardArtifact> getDownloadArtifacts(Content dataSource, AbstractFile origFile, File tempFile)
throws IOException, PropertyListFormatException, ParseException, ParserConfigurationException, SAXException, TskCoreException {
494 Collection<BlackboardArtifact> bbartifacts = null;
498 NSDictionary root = (NSDictionary)PropertyListParser.parse(tempFile);
503 NSArray nsArray = (NSArray)root.get(PLIST_KEY_DOWNLOAD_HISTORY);
508 NSObject[] objectArray = nsArray.getArray();
509 bbartifacts =
new ArrayList<>();
511 for(NSObject obj: objectArray){
512 if(obj instanceof NSDictionary){
513 bbartifacts.addAll(parseDownloadDictionary(dataSource, origFile, (NSDictionary)obj));
519 }
catch (PropertyListFormatException ex) {
520 PropertyListFormatException plfe =
new PropertyListFormatException(origFile.getName() +
": " + ex.getMessage());
521 plfe.setStackTrace(ex.getStackTrace());
523 }
catch (ParseException ex) {
524 ParseException pe =
new ParseException(origFile.getName() +
": " + ex.getMessage(), ex.getErrorOffset());
525 pe.setStackTrace(ex.getStackTrace());
527 }
catch (ParserConfigurationException ex) {
528 ParserConfigurationException pce =
new ParserConfigurationException(origFile.getName() +
": " + ex.getMessage());
529 pce.setStackTrace(ex.getStackTrace());
531 }
catch (SAXException ex) {
532 SAXException se =
new SAXException(origFile.getName() +
": " + ex.getMessage());
533 se.setStackTrace(ex.getStackTrace());
550 private Collection<BlackboardArtifact> getCookieArtifacts(AbstractFile origFile, File tempFile, IngestJobContext context)
throws TskCoreException, IOException {
551 Collection<BlackboardArtifact> bbartifacts = null;
552 BinaryCookieReader reader = BinaryCookieReader.initalizeReader(tempFile);
554 if (reader != null) {
555 bbartifacts =
new ArrayList<>();
557 Iterator<Cookie> iter = reader.iterator();
558 while (iter.hasNext()) {
559 if (context.dataSourceIngestIsCancelled()) {
563 Cookie cookie = iter.next();
566 createArtifactWithAttributes(
569 createCookieAttributes(
571 cookie.getCreationDate(),
573 cookie.getExpirationDate(),
574 cookie.getName(), cookie.getValue(),
576 NetworkUtils.extractDomain(cookie.getURL()))));
592 private void parseBookmarkDictionary(Collection<BlackboardArtifact> bbartifacts, AbstractFile origFile, NSDictionary root, IngestJobContext context)
throws TskCoreException {
594 if (context.dataSourceIngestIsCancelled()) {
598 if (root.containsKey(PLIST_KEY_CHILDREN)) {
599 NSArray children = (NSArray) root.objectForKey(PLIST_KEY_CHILDREN);
601 if (children != null) {
602 for (NSObject obj : children.getArray()) {
603 parseBookmarkDictionary(bbartifacts, origFile, (NSDictionary) obj, context);
606 }
else if (root.containsKey(PLIST_KEY_URL)) {
610 NSString nsstr = (NSString) root.objectForKey(PLIST_KEY_URL);
612 url = nsstr.toString();
615 NSDictionary dic = (NSDictionary) root.get(PLIST_KEY_URI);
617 nsstr = (NSString) root.objectForKey(PLIST_KEY_TITLE);
620 title = ((NSString) dic.get(PLIST_KEY_TITLE)).toString();
623 if (url != null || title != null) {
624 bbartifacts.add(createArtifactWithAttributes(TSK_WEB_BOOKMARK, origFile,
625 createBookmarkAttributes(url,
629 NetworkUtils.extractDomain(url))));
643 private Collection<BlackboardArtifact> parseDownloadDictionary(Content dataSource, AbstractFile origFile, NSDictionary entry)
throws TskCoreException {
644 Collection<BlackboardArtifact> bbartifacts =
new ArrayList<>();
650 NSString nsstring = (NSString) entry.get(PLIST_KEY_DOWNLOAD_URL);
651 if (nsstring != null) {
652 url = nsstring.toString();
655 nsstring = (NSString) entry.get(PLIST_KEY_DOWNLOAD_PATH);
656 if (nsstring != null) {
657 path = nsstring.toString();
658 pathID = Util.findID(dataSource, path);
661 NSDate date = (NSDate) entry.get(PLIST_KEY_DOWNLOAD_DATE);
663 time = date.getDate().getTime();
666 BlackboardArtifact webDownloadArtifact = createArtifactWithAttributes(TSK_WEB_DOWNLOAD, origFile, createDownloadAttributes(path, pathID, url, time, NetworkUtils.extractDomain(url), getName()));
667 bbartifacts.add(webDownloadArtifact);
670 for (AbstractFile downloadedFile : currentCase.getSleuthkitCase().getFileManager().findFilesExactNameExactPath(dataSource,
671 FilenameUtils.getName(path), FilenameUtils.getPath(path))) {
672 bbartifacts.add(createAssociatedArtifact(downloadedFile, webDownloadArtifact));