Autopsy  3.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.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.ArrayList;
29 import java.util.List;
30 import java.util.logging.Level;
41 
45 class ContactAnalyzer {
46 
47  private static final String moduleName = AndroidModuleFactory.getModuleName();
48  private static final Logger logger = Logger.getLogger(ContactAnalyzer.class.getName());
49 
50  public static void findContacts(Content dataSource, FileManager fileManager) {
51 
52  List<AbstractFile> absFiles;
53  try {
54  absFiles = fileManager.findFiles(dataSource, "contacts.db"); //NON-NLS
55  absFiles.addAll(fileManager.findFiles(dataSource, "contacts2.db")); //NON-NLS
56  if (absFiles.isEmpty()) {
57  return;
58  }
59  for (AbstractFile AF : absFiles) {
60  try {
61  File jFile = new File(Case.getCurrentCase().getTempDirectory(), AF.getName());
62  ContentUtils.writeToFile(AF, jFile);
63  findContactsInDB(jFile.toString(), AF);
64  } catch (Exception e) {
65  logger.log(Level.SEVERE, "Error parsing Contacts", e); //NON-NLS
66  }
67  }
68  } catch (TskCoreException e) {
69  logger.log(Level.SEVERE, "Error finding Contacts", e); //NON-NLS
70  }
71  }
72 
79  private static void findContactsInDB(String databasePath, AbstractFile f) {
80  Connection connection = null;
81  ResultSet resultSet = null;
82  Statement statement = null;
83 
84  if (databasePath == null || databasePath.isEmpty()) {
85  return;
86  }
87  try {
88  Class.forName("org.sqlite.JDBC"); //NON-NLS //load JDBC driver
89  connection = DriverManager.getConnection("jdbc:sqlite:" + databasePath); //NON-NLS
90  statement = connection.createStatement();
91  } catch (ClassNotFoundException | SQLException e) {
92  logger.log(Level.SEVERE, "Error opening database", e); //NON-NLS
93  return;
94  }
95 
96  try {
97  // get display_name, mimetype(email or phone number) and data1 (phonenumber or email address depending on mimetype)
98  //sorted by name, so phonenumber/email would be consecutive for a person if they exist.
99  // check if contacts.name_raw_contact_id exists. Modify the query accordingly.
100  Boolean column_found = false;
101  DatabaseMetaData metadata = connection.getMetaData();
102  ResultSet columnListResultSet = metadata.getColumns(null, null, "contacts", null); //NON-NLS
103  while (columnListResultSet.next()) {
104  if (columnListResultSet.getString("COLUMN_NAME").equals("name_raw_contact_id")) { //NON-NLS
105  column_found = true;
106  break;
107  }
108  }
109  if (column_found) {
110  resultSet = statement.executeQuery(
111  "SELECT mimetype,data1, name_raw_contact.display_name AS display_name \n" //NON-NLS
112  + "FROM raw_contacts JOIN contacts ON (raw_contacts.contact_id=contacts._id) \n" //NON-NLS
113  + "JOIN raw_contacts AS name_raw_contact ON(name_raw_contact_id=name_raw_contact._id) " //NON-NLS
114  + "LEFT OUTER JOIN data ON (data.raw_contact_id=raw_contacts._id) \n" //NON-NLS
115  + "LEFT OUTER JOIN mimetypes ON (data.mimetype_id=mimetypes._id) \n" //NON-NLS
116  + "WHERE mimetype = 'vnd.android.cursor.item/phone_v2' OR mimetype = 'vnd.android.cursor.item/email_v2'\n" //NON-NLS
117  + "ORDER BY name_raw_contact.display_name ASC;"); //NON-NLS
118  } else {
119  resultSet = statement.executeQuery(
120  "SELECT mimetype,data1, raw_contacts.display_name AS display_name \n" //NON-NLS
121  + "FROM raw_contacts JOIN contacts ON (raw_contacts.contact_id=contacts._id) \n" //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 raw_contacts.display_name ASC;"); //NON-NLS
126  }
127 
128  BlackboardArtifact bba;
129  bba = f.newArtifact(BlackboardArtifact.ARTIFACT_TYPE.TSK_CONTACT);
130  String name;
131  String oldName = "";
132  String mimetype; // either phone or email
133  String data1; // the phone number or email
134  while (resultSet.next()) {
135  name = resultSet.getString("display_name"); //NON-NLS
136  data1 = resultSet.getString("data1"); //NON-NLS
137  mimetype = resultSet.getString("mimetype"); //NON-NLS
138 // System.out.println(resultSet.getString("data1") + resultSet.getString("mimetype") + resultSet.getString("display_name")); //Test code
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.getTypeID(), 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.getTypeID(), moduleName, data1));
145  } else {
146  bba.addAttribute(new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_EMAIL.getTypeID(), moduleName, data1));
147  }
148  oldName = name;
149  }
150 
151  } catch (SQLException e) {
152  logger.log(Level.WARNING, "Unable to execute contacts SQL query against {0} : {1}", new Object[]{databasePath, e}); //NON-NLS
153  } catch (TskCoreException e) {
154  logger.log(Level.SEVERE, "Error posting to blackboard", e); //NON-NLS
155  } finally {
156  try {
157  if (resultSet != null) {
158  resultSet.close();
159  }
160  statement.close();
161  connection.close();
162  } catch (Exception e) {
163  logger.log(Level.SEVERE, "Error closing database", e); //NON-NLS
164  }
165  }
166  }
167 }

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.