Autopsy  4.6.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-2018 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;
43 import org.sleuthkit.datamodel.AbstractFile;
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 ResultSet resultSet = null;
58  private Statement statement = null;
59  private String dbPath = "";
60  private long fileId = 0;
61  private java.io.File jFile = null;
62  private final String moduleName = iOSModuleFactory.getModuleName();
63  private static final Logger logger = Logger.getLogger(ContactAnalyzer.class.getName());
64  private Blackboard blackboard;
65 
71  public void findContacts(IngestJobContext context) {
72  Case openCase;
73  try {
74  openCase = Case.getOpenCase();
75  } catch (NoCurrentCaseException ex) {
76  logger.log(Level.SEVERE, "Exception while getting open case.", ex); //NON-NLS
77  return;
78  }
79 
80  blackboard = openCase.getServices().getBlackboard();
81  List<AbstractFile> absFiles;
82  try {
83  SleuthkitCase skCase = openCase.getSleuthkitCase();
84  absFiles = skCase.findAllFilesWhere("LOWER(name) LIKE LOWER('%call_history%') "); //NON-NLS //get exact file names
85  if (absFiles.isEmpty()) {
86  return;
87  }
88  for (AbstractFile file : absFiles) {
89  try {
90  jFile = new java.io.File(openCase.getTempDirectory(), file.getName().replaceAll("[<>%|\"/:*\\\\]", ""));
91  dbPath = jFile.toString(); //path of file as string
92  fileId = file.getId();
93  ContentUtils.writeToFile(file, jFile, context::dataSourceIngestIsCancelled);
94  } catch (ReadContentInputStreamException ex) {
95  logger.log(Level.WARNING, String.format("Error reading content from file '%s' (id=%d).", file.getName(), fileId), ex); //NON-NLS
96  } catch (Exception ex) {
97  logger.log(Level.SEVERE, String.format("Error writing content from file '%s' (id=%d) to '%s'.", file.getName(), fileId, dbPath), ex); //NON-NLS
98  }
99  }
100  } catch (TskCoreException e) {
101  logger.log(Level.SEVERE, "Error finding Contacts", e); //NON-NLS
102  }
103  }
104 
112  @Messages({"ContactAnalyzer.indexError.message=Failed to index contact artifact for keyword search."})
113  private void findContactsInDB(String DatabasePath, long fileId) {
114  if (DatabasePath == null || DatabasePath.isEmpty()) {
115  return;
116  }
117  Case currentCase;
118  try {
119  currentCase = Case.getOpenCase();
120  } catch (NoCurrentCaseException ex) {
121  logger.log(Level.SEVERE, "Exception while getting open case.", ex); //NON-NLS
122  return;
123  }
124  try {
125  Class.forName("org.sqlite.JDBC"); //NON-NLS //load JDBC driver
126  connection = DriverManager.getConnection("jdbc:sqlite:" + DatabasePath); //NON-NLS
127  statement = connection.createStatement();
128  } catch (ClassNotFoundException | SQLException e) {
129  logger.log(Level.SEVERE, "Error opening database", e); //NON-NLS
130  }
131 
132  SleuthkitCase skCase = currentCase.getSleuthkitCase();
133  try {
134  AbstractFile file = skCase.getAbstractFileById(fileId);
135  if (file == null) {
136  logger.log(Level.SEVERE, "Error getting abstract file {0}", fileId); //NON-NLS
137  return;
138  }
139 
140  try {
141  // get display_name, mimetype(email or phone number) and data1 (phonenumber or email address depending on mimetype)
142  //sorted by name, so phonenumber/email would be consecutive for a person if they exist.
143  resultSet = statement.executeQuery(
144  "SELECT mimetype,data1, name_raw_contact.display_name AS display_name \n" //NON-NLS
145  + "FROM raw_contacts JOIN contacts ON (raw_contacts.contact_id=contacts._id) \n" //NON-NLS
146  + "JOIN raw_contacts AS name_raw_contact ON(name_raw_contact_id=name_raw_contact._id) " //NON-NLS
147  + "LEFT OUTER JOIN data ON (data.raw_contact_id=raw_contacts._id) \n" //NON-NLS
148  + "LEFT OUTER JOIN mimetypes ON (data.mimetype_id=mimetypes._id) \n" //NON-NLS
149  + "WHERE mimetype = 'vnd.android.cursor.item/phone_v2' OR mimetype = 'vnd.android.cursor.item/email_v2'\n" //NON-NLS
150  + "ORDER BY name_raw_contact.display_name ASC;"); //NON-NLS
151 
152  BlackboardArtifact bba;
153  bba = file.newArtifact(BlackboardArtifact.ARTIFACT_TYPE.TSK_CONTACT);
154  Collection<BlackboardAttribute> attributes = new ArrayList<>();
155  String name;
156  String oldName = "";
157  String mimetype; // either phone or email
158  String data1; // the phone number or email
159  while (resultSet.next()) {
160  name = resultSet.getString("display_name"); //NON-NLS
161  data1 = resultSet.getString("data1"); //NON-NLS
162  mimetype = resultSet.getString("mimetype"); //NON-NLS
163  if (name.equals(oldName) == false) {
164  attributes.add(new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_NAME, moduleName, name));
165  }
166  if (mimetype.equals("vnd.android.cursor.item/phone_v2")) { //NON-NLS
167  attributes.add(new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PHONE_NUMBER, moduleName, data1));
168  } else {
169  attributes.add(new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_EMAIL, moduleName, data1));
170  }
171  oldName = name;
172 
173  bba.addAttributes(attributes);
174  try {
175  // index the artifact for keyword search
176  blackboard.indexArtifact(bba);
177  } catch (Blackboard.BlackboardException ex) {
178  logger.log(Level.SEVERE, "Unable to index blackboard artifact " + bba.getArtifactID(), ex); //NON-NLS
179  MessageNotifyUtil.Notify.error(
180  Bundle.ContactAnalyzer_indexError_message(), bba.getDisplayName());
181  }
182  }
183 
184  } catch (Exception e) {
185  logger.log(Level.SEVERE, "Error parsing Contacts to Blackboard", e); //NON-NLS
186  } finally {
187  try {
188  resultSet.close();
189  statement.close();
190  connection.close();
191  } catch (Exception e) {
192  logger.log(Level.SEVERE, "Error closing database", e); //NON-NLS
193  }
194  }
195  } catch (Exception e) {
196  logger.log(Level.SEVERE, "Error parsing Contacts to Blackboard", e); //NON-NLS
197  }
198 
199  }
200 
201  public static void copyFileUsingStream(AbstractFile file, File jFile) throws IOException {
202  InputStream is = new ReadContentInputStream(file);
203  OutputStream os = new FileOutputStream(jFile);
204  byte[] buffer = new byte[8192];
205  int length;
206  try {
207  while ((length = is.read(buffer)) != -1) {
208  os.write(buffer, 0, length);
209  System.out.println(length);
210  os.flush();
211 
212  }
213 
214  } finally {
215  is.close();
216  os.close();
217  }
218  }
219 
220  public static void copyFileUsingStreams(AbstractFile file, File jFile) {
221  InputStream istream;
222  OutputStream ostream = null;
223  int c;
224  final int EOF = -1;
225  istream = new ReadContentInputStream(file);
226  try {
227  ostream = new FileOutputStream(jFile);
228  while ((c = istream.read()) != EOF) {
229  ostream.write(c);
230  }
231  } catch (IOException e) {
232  System.out.println("Error: " + e.getMessage()); //NON-NLS
233  } finally {
234  try {
235  istream.close();
236  ostream.close();
237  } catch (IOException e) {
238  System.out.println("File did not close"); //NON-NLS
239  }
240  }
241  }
242 }

Copyright © 2012-2016 Basis Technology. Generated on: Mon May 7 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.