Autopsy  4.12.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
ContactAnalyzer.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2014-2019 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.modules.iOS;
20 
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.sql.Connection;
27 import java.sql.DriverManager;
28 import java.sql.ResultSet;
29 import java.sql.SQLException;
30 import java.sql.Statement;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.List;
34 import java.util.logging.Level;
35 import org.openide.util.NbBundle.Messages;
42 import org.sleuthkit.datamodel.AbstractFile;
43 import org.sleuthkit.datamodel.Blackboard;
44 import org.sleuthkit.datamodel.BlackboardArtifact;
45 import org.sleuthkit.datamodel.BlackboardAttribute;
46 import org.sleuthkit.datamodel.ReadContentInputStream;
47 import org.sleuthkit.datamodel.ReadContentInputStream.ReadContentInputStreamException;
48 import org.sleuthkit.datamodel.SleuthkitCase;
49 import org.sleuthkit.datamodel.TskCoreException;
50 
54 final class ContactAnalyzer {
55 
56  private Connection connection = null;
57  private String dbPath = "";
58  private long fileId = 0;
59  private java.io.File jFile = null;
60  private final String moduleName = iOSModuleFactory.getModuleName();
61  private static final Logger logger = Logger.getLogger(ContactAnalyzer.class.getName());
62  private Blackboard blackboard;
63 
69  public void findContacts(IngestJobContext context) {
70  Case openCase;
71  try {
72  openCase = Case.getCurrentCaseThrows();
73  } catch (NoCurrentCaseException ex) {
74  logger.log(Level.SEVERE, "Exception while getting open case.", ex); //NON-NLS
75  return;
76  }
77 
78  blackboard = openCase.getSleuthkitCase().getBlackboard();
79  List<AbstractFile> absFiles;
80  try {
81  SleuthkitCase skCase = openCase.getSleuthkitCase();
82  absFiles = skCase.findAllFilesWhere("LOWER(name) LIKE LOWER('%call_history%') "); //NON-NLS //get exact file names
83  if (absFiles.isEmpty()) {
84  return;
85  }
86  for (AbstractFile file : absFiles) {
87  try {
88  jFile = new java.io.File(openCase.getTempDirectory(), file.getName().replaceAll("[<>%|\"/:*\\\\]", ""));
89  dbPath = jFile.toString(); //path of file as string
90  fileId = file.getId();
91  ContentUtils.writeToFile(file, jFile, context::dataSourceIngestIsCancelled);
92  } catch (ReadContentInputStreamException ex) {
93  logger.log(Level.WARNING, String.format("Error reading content from file '%s' (id=%d).", file.getName(), fileId), ex); //NON-NLS
94  } catch (Exception ex) {
95  logger.log(Level.SEVERE, String.format("Error writing content from file '%s' (id=%d) to '%s'.", file.getName(), fileId, dbPath), ex); //NON-NLS
96  }
97  }
98  } catch (TskCoreException e) {
99  logger.log(Level.SEVERE, "Error finding Contacts", e); //NON-NLS
100  }
101  }
102 
110  @Messages({"ContactAnalyzer.indexError.message=Failed to index contact artifact for keyword search."})
111  private void findContactsInDB(String DatabasePath, long fileId) {
112  if (DatabasePath == null || DatabasePath.isEmpty()) {
113  return;
114  }
115 
116  Case currentCase;
117  try {
118  currentCase = Case.getCurrentCaseThrows();
119  } catch (NoCurrentCaseException ex) {
120  logger.log(Level.SEVERE, "Exception while getting open case.", ex); //NON-NLS
121  return;
122  }
123 
124  Statement statement = null;
125  try {
126  Class.forName("org.sqlite.JDBC"); //NON-NLS //load JDBC driver
127  connection = DriverManager.getConnection("jdbc:sqlite:" + DatabasePath); //NON-NLS
128  statement = connection.createStatement();
129  } catch (ClassNotFoundException | SQLException e) {
130  logger.log(Level.SEVERE, "Error opening database", e); //NON-NLS
131  }
132 
133  SleuthkitCase skCase = currentCase.getSleuthkitCase();
134  try {
135  AbstractFile file = skCase.getAbstractFileById(fileId);
136  if (file == null) {
137  logger.log(Level.SEVERE, "Error getting abstract file {0}", fileId); //NON-NLS
138  return;
139  }
140 
141  ResultSet resultSet = null;
142  try {
143  // get display_name, mimetype(email or phone number) and data1 (phonenumber or email address depending on mimetype)
144  //sorted by name, so phonenumber/email would be consecutive for a person if they exist.
145  resultSet = statement.executeQuery(
146  "SELECT mimetype,data1, name_raw_contact.display_name AS display_name \n" //NON-NLS
147  + "FROM raw_contacts JOIN contacts ON (raw_contacts.contact_id=contacts._id) \n" //NON-NLS
148  + "JOIN raw_contacts AS name_raw_contact ON(name_raw_contact_id=name_raw_contact._id) " //NON-NLS
149  + "LEFT OUTER JOIN data ON (data.raw_contact_id=raw_contacts._id) \n" //NON-NLS
150  + "LEFT OUTER JOIN mimetypes ON (data.mimetype_id=mimetypes._id) \n" //NON-NLS
151  + "WHERE mimetype = 'vnd.android.cursor.item/phone_v2' OR mimetype = 'vnd.android.cursor.item/email_v2'\n" //NON-NLS
152  + "ORDER BY name_raw_contact.display_name ASC;"); //NON-NLS
153 
154  BlackboardArtifact bba;
155  bba = file.newArtifact(BlackboardArtifact.ARTIFACT_TYPE.TSK_CONTACT);
156  Collection<BlackboardAttribute> attributes = new ArrayList<>();
157  String name;
158  String oldName = "";
159  String mimetype; // either phone or email
160  String data1; // the phone number or email
161  while (resultSet.next()) {
162  name = resultSet.getString("display_name"); //NON-NLS
163  data1 = resultSet.getString("data1"); //NON-NLS
164  mimetype = resultSet.getString("mimetype"); //NON-NLS
165  if (name.equals(oldName) == false) {
166  bba = file.newArtifact(BlackboardArtifact.ARTIFACT_TYPE.TSK_CONTACT);
167  attributes = new ArrayList<>();
168  attributes.add(new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_NAME, moduleName, name));
169  }
170  if (mimetype.equals("vnd.android.cursor.item/phone_v2")) { //NON-NLS
171  attributes.add(new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PHONE_NUMBER, moduleName, data1));
172  } else {
173  attributes.add(new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_EMAIL, moduleName, data1));
174  }
175 
176  // TODO: If this code comes back to life, add code to create the account
177  // and relationship between the phone numbers & emails. Also
178  // investigate if the mimetype "vnd.android.cursor.item/phone_v2"
179  // makes sense in an ios word
180 
181  oldName = name;
182 
183  bba.addAttributes(attributes);
184  try {
185  // index the artifact for keyword search
186  blackboard.postArtifact(bba, moduleName);
187  } catch (Blackboard.BlackboardException ex) {
188  logger.log(Level.SEVERE, "Unable to index blackboard artifact " + bba.getArtifactID(), ex); //NON-NLS
189  MessageNotifyUtil.Notify.error(
190  Bundle.ContactAnalyzer_indexError_message(), bba.getDisplayName());
191  }
192  }
193 
194  } catch (Exception e) {
195  logger.log(Level.SEVERE, "Error parsing Contacts to Blackboard", e); //NON-NLS
196  } finally {
197  try {
198  resultSet.close();
199  statement.close();
200  connection.close();
201  } catch (Exception e) {
202  logger.log(Level.SEVERE, "Error closing database", e); //NON-NLS
203  }
204  }
205  } catch (Exception e) {
206  logger.log(Level.SEVERE, "Error parsing Contacts to Blackboard", e); //NON-NLS
207  }
208 
209  }
210 
211  public static void copyFileUsingStream(AbstractFile file, File jFile) throws IOException {
212  InputStream is = new ReadContentInputStream(file);
213  OutputStream os = new FileOutputStream(jFile);
214  byte[] buffer = new byte[8192];
215  int length;
216  try {
217  while ((length = is.read(buffer)) != -1) {
218  os.write(buffer, 0, length);
219  os.flush();
220 
221  }
222 
223  } finally {
224  is.close();
225  os.close();
226  }
227  }
228 
229  public static void copyFileUsingStreams(AbstractFile file, File jFile) {
230  InputStream istream;
231  OutputStream ostream = null;
232  int c;
233  final int EOF = -1;
234  istream = new ReadContentInputStream(file);
235  try {
236  ostream = new FileOutputStream(jFile);
237  while ((c = istream.read()) != EOF) {
238  ostream.write(c);
239  }
240  } catch (IOException e) {
241  logger.log(Level.WARNING, "Error copying file", e);
242  } finally {
243  try {
244  istream.close();
245  ostream.close();
246  } catch (IOException e) {
247  logger.log(Level.WARNING, "File did not close", e);
248  }
249  }
250  }
251 }

Copyright © 2012-2018 Basis Technology. Generated on: Wed Sep 18 2019
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.