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.android;
20 
21 import java.io.File;
22 import java.sql.Connection;
23 import java.sql.DatabaseMetaData;
24 import java.sql.DriverManager;
25 import java.sql.ResultSet;
26 import java.sql.SQLException;
27 import java.sql.Statement;
28 import java.util.List;
29 import java.util.logging.Level;
30 import org.openide.util.NbBundle;
37 import org.sleuthkit.datamodel.AbstractFile;
38 import org.sleuthkit.datamodel.BlackboardArtifact;
39 import org.sleuthkit.datamodel.BlackboardAttribute;
40 import org.sleuthkit.datamodel.Content;
41 import org.sleuthkit.datamodel.TskCoreException;
42 
47 class ContactAnalyzer {
48 
49  private static final String moduleName = AndroidModuleFactory.getModuleName();
50  private static final Logger logger = Logger.getLogger(ContactAnalyzer.class.getName());
51 
52  public static void findContacts(Content dataSource, FileManager fileManager) {
53 
54  List<AbstractFile> absFiles;
55  try {
56  absFiles = fileManager.findFiles(dataSource, "contacts.db"); //NON-NLS
57  absFiles.addAll(fileManager.findFiles(dataSource, "contacts2.db")); //NON-NLS
58  if (absFiles.isEmpty()) {
59  return;
60  }
61  for (AbstractFile AF : absFiles) {
62  try {
63  File jFile = new File(Case.getCurrentCase().getTempDirectory(), AF.getName());
64  ContentUtils.writeToFile(AF, jFile);
65  findContactsInDB(jFile.toString(), AF);
66  } catch (Exception e) {
67  logger.log(Level.SEVERE, "Error parsing Contacts", e); //NON-NLS
68  }
69  }
70  } catch (TskCoreException e) {
71  logger.log(Level.SEVERE, "Error finding Contacts", e); //NON-NLS
72  }
73  }
74 
82  private static void findContactsInDB(String databasePath, AbstractFile f) {
83  Connection connection = null;
84  ResultSet resultSet = null;
85  Statement statement = null;
86  Blackboard blackboard = Case.getCurrentCase().getServices().getBlackboard();
87 
88  if (databasePath == null || databasePath.isEmpty()) {
89  return;
90  }
91  try {
92  Class.forName("org.sqlite.JDBC"); //NON-NLS //load JDBC driver
93  connection = DriverManager.getConnection("jdbc:sqlite:" + databasePath); //NON-NLS
94  statement = connection.createStatement();
95  } catch (ClassNotFoundException | SQLException e) {
96  logger.log(Level.SEVERE, "Error opening database", e); //NON-NLS
97  return;
98  }
99 
100  try {
101  // get display_name, mimetype(email or phone number) and data1 (phonenumber or email address depending on mimetype)
102  //sorted by name, so phonenumber/email would be consecutive for a person if they exist.
103  // check if contacts.name_raw_contact_id exists. Modify the query accordingly.
104  Boolean column_found = false;
105  DatabaseMetaData metadata = connection.getMetaData();
106  ResultSet columnListResultSet = metadata.getColumns(null, null, "contacts", null); //NON-NLS
107  while (columnListResultSet.next()) {
108  if (columnListResultSet.getString("COLUMN_NAME").equals("name_raw_contact_id")) { //NON-NLS
109  column_found = true;
110  break;
111  }
112  }
113  if (column_found) {
114  resultSet = statement.executeQuery(
115  "SELECT mimetype,data1, name_raw_contact.display_name AS display_name \n" //NON-NLS
116  + "FROM raw_contacts JOIN contacts ON (raw_contacts.contact_id=contacts._id) \n" //NON-NLS
117  + "JOIN raw_contacts AS name_raw_contact ON(name_raw_contact_id=name_raw_contact._id) " //NON-NLS
118  + "LEFT OUTER JOIN data ON (data.raw_contact_id=raw_contacts._id) \n" //NON-NLS
119  + "LEFT OUTER JOIN mimetypes ON (data.mimetype_id=mimetypes._id) \n" //NON-NLS
120  + "WHERE mimetype = 'vnd.android.cursor.item/phone_v2' OR mimetype = 'vnd.android.cursor.item/email_v2'\n" //NON-NLS
121  + "ORDER BY name_raw_contact.display_name ASC;"); //NON-NLS
122  } else {
123  resultSet = statement.executeQuery(
124  "SELECT mimetype,data1, raw_contacts.display_name AS display_name \n" //NON-NLS
125  + "FROM raw_contacts JOIN contacts ON (raw_contacts.contact_id=contacts._id) \n" //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 raw_contacts.display_name ASC;"); //NON-NLS
130  }
131 
132  BlackboardArtifact bba;
133  bba = f.newArtifact(BlackboardArtifact.ARTIFACT_TYPE.TSK_CONTACT);
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  bba = f.newArtifact(BlackboardArtifact.ARTIFACT_TYPE.TSK_CONTACT);
144  bba.addAttribute(new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_NAME, moduleName, name));
145  }
146  if (mimetype.equals("vnd.android.cursor.item/phone_v2")) { //NON-NLS
147  bba.addAttribute(new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PHONE_NUMBER, moduleName, data1));
148  } else {
149  bba.addAttribute(new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_EMAIL, moduleName, data1));
150  }
151  oldName = name;
152 
153  try {
154  // index the artifact for keyword search
155  blackboard.indexArtifact(bba);
156  } catch (Blackboard.BlackboardException ex) {
157  logger.log(Level.SEVERE, NbBundle.getMessage(Blackboard.class, "Blackboard.unableToIndexArtifact.error.msg", bba.getDisplayName()), ex); //NON-NLS
158  MessageNotifyUtil.Notify.error(
159  NbBundle.getMessage(Blackboard.class, "Blackboard.unableToIndexArtifact.exception.msg"), bba.getDisplayName());
160  }
161  }
162 
163  } catch (SQLException e) {
164  logger.log(Level.WARNING, "Unable to execute contacts SQL query against {0} : {1}", new Object[]{databasePath, e}); //NON-NLS
165  } catch (TskCoreException e) {
166  logger.log(Level.SEVERE, "Error posting to blackboard", e); //NON-NLS
167  } finally {
168  try {
169  if (resultSet != null) {
170  resultSet.close();
171  }
172  statement.close();
173  connection.close();
174  } catch (Exception e) {
175  logger.log(Level.SEVERE, "Error closing database", e); //NON-NLS
176  }
177  }
178  }
179 }

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.