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

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