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

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