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

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