19 package org.sleuthkit.autopsy.modules.iOS;
 
   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;
 
   47 import org.
sleuthkit.datamodel.ReadContentInputStream.ReadContentInputStreamException;
 
   54 final class ContactAnalyzer {
 
   56     private Connection connection = null;
 
   57     private String dbPath = 
"";
 
   58     private long fileId = 0;
 
   59     private java.io.File jFile = null;
 
   60     private final String moduleName = iOSModuleFactory.getModuleName();
 
   61     private static final Logger logger = Logger.getLogger(ContactAnalyzer.class.getName());
 
   62     private Blackboard blackboard;
 
   69     public void findContacts(IngestJobContext context) {
 
   72             openCase = Case.getCurrentCaseThrows();
 
   73         } 
catch (NoCurrentCaseException ex) {
 
   74             logger.log(Level.SEVERE, 
"Exception while getting open case.", ex); 
 
   78         blackboard = openCase.getServices().getBlackboard();
 
   79         List<AbstractFile> absFiles;
 
   81             SleuthkitCase skCase = openCase.getSleuthkitCase();
 
   82             absFiles = skCase.findAllFilesWhere(
"LOWER(name) LIKE LOWER('%call_history%') "); 
 
   83             if (absFiles.isEmpty()) {
 
   86             for (AbstractFile file : absFiles) {
 
   88                     jFile = 
new java.io.File(openCase.getTempDirectory(), file.getName().replaceAll(
"[<>%|\"/:*\\\\]", 
""));
 
   89                     dbPath = jFile.toString(); 
 
   90                     fileId = file.getId();
 
   91                     ContentUtils.writeToFile(file, jFile, context::dataSourceIngestIsCancelled);
 
   92                 } 
catch (ReadContentInputStreamException ex) {
 
   93                     logger.log(Level.WARNING, String.format(
"Error reading content from file '%s' (id=%d).", file.getName(), fileId), ex); 
 
   94                 } 
catch (Exception ex) {
 
   95                     logger.log(Level.SEVERE, String.format(
"Error writing content from file '%s' (id=%d) to '%s'.", file.getName(), fileId, dbPath), ex); 
 
   98         } 
catch (TskCoreException e) {
 
   99             logger.log(Level.SEVERE, 
"Error finding Contacts", e); 
 
  110     @Messages({
"ContactAnalyzer.indexError.message=Failed to index contact artifact for keyword search."})
 
  111     private void findContactsInDB(String DatabasePath, 
long fileId) {
 
  112         if (DatabasePath == null || DatabasePath.isEmpty()) {
 
  118             currentCase = Case.getCurrentCaseThrows();
 
  119         } 
catch (NoCurrentCaseException ex) {
 
  120             logger.log(Level.SEVERE, 
"Exception while getting open case.", ex); 
 
  124         Statement statement = null;
 
  126             Class.forName(
"org.sqlite.JDBC"); 
 
  127             connection = DriverManager.getConnection(
"jdbc:sqlite:" + DatabasePath); 
 
  128             statement = connection.createStatement();
 
  129         } 
catch (ClassNotFoundException | SQLException e) {
 
  130             logger.log(Level.SEVERE, 
"Error opening database", e); 
 
  133         SleuthkitCase skCase = currentCase.getSleuthkitCase();
 
  135             AbstractFile file = skCase.getAbstractFileById(fileId);
 
  137                 logger.log(Level.SEVERE, 
"Error getting abstract file {0}", fileId); 
 
  141             ResultSet resultSet = null;
 
  145                 resultSet = statement.executeQuery(
 
  146                         "SELECT mimetype,data1, name_raw_contact.display_name AS display_name \n"  
  147                         + 
"FROM raw_contacts JOIN contacts ON (raw_contacts.contact_id=contacts._id) \n"  
  148                         + 
"JOIN raw_contacts AS name_raw_contact ON(name_raw_contact_id=name_raw_contact._id) "  
  149                         + 
"LEFT OUTER JOIN data ON (data.raw_contact_id=raw_contacts._id) \n"  
  150                         + 
"LEFT OUTER JOIN mimetypes ON (data.mimetype_id=mimetypes._id) \n"  
  151                         + 
"WHERE mimetype = 'vnd.android.cursor.item/phone_v2' OR mimetype = 'vnd.android.cursor.item/email_v2'\n"  
  152                         + 
"ORDER BY name_raw_contact.display_name ASC;"); 
 
  154                 BlackboardArtifact bba;
 
  155                 bba = file.newArtifact(BlackboardArtifact.ARTIFACT_TYPE.TSK_CONTACT);
 
  156                 Collection<BlackboardAttribute> attributes = 
new ArrayList<>();
 
  161                 while (resultSet.next()) {
 
  162                     name = resultSet.getString(
"display_name"); 
 
  163                     data1 = resultSet.getString(
"data1"); 
 
  164                     mimetype = resultSet.getString(
"mimetype"); 
 
  165                     if (name.equals(oldName) == 
false) {
 
  166                         attributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_NAME, moduleName, name));
 
  168                     if (mimetype.equals(
"vnd.android.cursor.item/phone_v2")) { 
 
  169                         attributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PHONE_NUMBER, moduleName, data1));
 
  171                         attributes.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_EMAIL, moduleName, data1));
 
  175                     bba.addAttributes(attributes);
 
  178                         blackboard.indexArtifact(bba);
 
  179                     } 
catch (Blackboard.BlackboardException ex) {
 
  180                         logger.log(Level.SEVERE, 
"Unable to index blackboard artifact " + bba.getArtifactID(), ex); 
 
  181                         MessageNotifyUtil.Notify.error(
 
  182                                 Bundle.ContactAnalyzer_indexError_message(), bba.getDisplayName());
 
  186             } 
catch (Exception e) {
 
  187                 logger.log(Level.SEVERE, 
"Error parsing Contacts to Blackboard", e); 
 
  193                 } 
catch (Exception e) {
 
  194                     logger.log(Level.SEVERE, 
"Error closing database", e); 
 
  197         } 
catch (Exception e) {
 
  198             logger.log(Level.SEVERE, 
"Error parsing Contacts to Blackboard", e); 
 
  203     public static void copyFileUsingStream(AbstractFile file, File jFile) 
throws IOException {
 
  204         InputStream is = 
new ReadContentInputStream(file);
 
  205         OutputStream os = 
new FileOutputStream(jFile);
 
  206         byte[] buffer = 
new byte[8192];
 
  209             while ((length = is.read(buffer)) != -1) {
 
  210                 os.write(buffer, 0, length);
 
  211                 System.out.println(length);
 
  222     public static void copyFileUsingStreams(AbstractFile file, File jFile) {
 
  224         OutputStream ostream = null;
 
  227         istream = 
new ReadContentInputStream(file);
 
  229             ostream = 
new FileOutputStream(jFile);
 
  230             while ((c = istream.read()) != EOF) {
 
  233         } 
catch (IOException e) {
 
  234             System.out.println(
"Error: " + e.getMessage()); 
 
  239             } 
catch (IOException e) {
 
  240                 System.out.println(
"File did not close");