Autopsy  4.12.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
Util.java
Go to the documentation of this file.
1  /*
2  *
3  * Autopsy Forensic Browser
4  *
5  * Copyright 2012-2018 Basis Technology Corp.
6  *
7  * Copyright 2012 42six Solutions.
8  * Contact: aebadirad <at> 42six <dot> com
9  * Project Contact/Architect: carrier <at> sleuthkit <dot> org
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 package org.sleuthkit.autopsy.recentactivity;
24 
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.nio.MappedByteBuffer;
30 import java.nio.channels.FileChannel;
31 import java.nio.charset.Charset;
32 import java.sql.ResultSet;
33 import java.text.SimpleDateFormat;
34 import java.util.Date;
35 import java.util.List;
36 import java.util.logging.Level;
38 import java.util.regex.Matcher;
39 import java.util.regex.Pattern;
43 import org.sleuthkit.datamodel.AbstractFile;
44 import org.sleuthkit.datamodel.Content;
45 import org.sleuthkit.datamodel.TskCoreException;
46 
51 class Util {
52 
53  private static Logger logger = Logger.getLogger(Util.class.getName());
54 
55  private Util() {
56  }
57 
58  public static boolean pathexists(String path) {
59  File file = new File(path);
60  boolean exists = file.exists();
61  return exists;
62  }
63 
64  public static String utcConvert(String utc) {
65  SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy HH:mm");
66  String tempconvert = formatter.format(new Date(Long.parseLong(utc)));
67  return tempconvert;
68  }
69 
70  public static String readFile(String path) throws IOException {
71  FileInputStream stream = new FileInputStream(new File(path));
72  try {
73  FileChannel fc = stream.getChannel();
74  MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
75  /*
76  * Instead of using default, pass in a decoder.
77  */
78  return Charset.defaultCharset().decode(bb).toString();
79  } finally {
80  stream.close();
81  }
82  }
83 
84  public static String getFileName(String value) {
85  String filename = "";
86  String filematch = "^([a-zA-Z]\\:)(\\\\[^\\\\/:*?<>\"|]*(?<!\\[ \\]))*(\\.[a-zA-Z]{2,6})$"; //NON-NLS
87 
88  Pattern p = Pattern.compile(filematch, Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.COMMENTS);
89  Matcher m = p.matcher(value);
90  if (m.find()) {
91  filename = m.group(1);
92 
93  }
94  int lastPos = value.lastIndexOf('\\');
95  filename = (lastPos < 0) ? value : value.substring(lastPos + 1);
96  return filename.toString();
97  }
98 
99  public static String getPath(String txt) {
100  String path = "";
101 
102  //String drive ="([a-z]:\\\\(?:[-\\w\\.\\d]+\\\\)*(?:[-\\w\\.\\d]+)?)"; // Windows drive
103  String drive = "([a-z]:\\\\\\S.+)"; //NON-NLS
104  Pattern p = Pattern.compile(drive, Pattern.CASE_INSENSITIVE | Pattern.COMMENTS);
105  Matcher m = p.matcher(txt);
106  if (m.find()) {
107  path = m.group(1);
108 
109  } else {
110 
111  String network = "(\\\\(?:\\\\[^:\\s?*\"<>|]+)+)"; // Windows network NON-NLS
112 
113  Pattern p2 = Pattern.compile(network, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
114  Matcher m2 = p2.matcher(txt);
115  if (m2.find()) {
116  path = m2.group(1);
117  }
118  }
119  return path;
120  }
121 
122  public static long findID(Content dataSource, String path) {
123  String parent_path = path.replace('\\', '/'); // fix Chrome paths
124  if (parent_path.length() > 2 && parent_path.charAt(1) == ':') {
125  parent_path = parent_path.substring(2); // remove drive letter (e.g., 'C:')
126  }
127  int index = parent_path.lastIndexOf('/');
128  String name = parent_path.substring(++index);
129  parent_path = parent_path.substring(0, index);
130  List<AbstractFile> files = null;
131  try {
132  FileManager fileManager = Case.getCurrentCaseThrows().getServices().getFileManager();
133  files = fileManager.findFiles(dataSource, name, parent_path);
134  } catch (TskCoreException | NoCurrentCaseException ex) {
135  logger.log(Level.WARNING, "Error fetching 'index.data' files for Internet Explorer history."); //NON-NLS
136  }
137 
138  if (files == null || files.isEmpty()) {
139  return -1;
140  }
141  return files.get(0).getId();
142  }
143 
144  public static boolean checkColumn(String column, String tablename, String connection) {
145  String query = "PRAGMA table_info(" + tablename + ")"; //NON-NLS
146  boolean found = false;
147  ResultSet temprs;
148  SQLiteDBConnect tempdbconnect = null;
149  try {
150  tempdbconnect = new SQLiteDBConnect("org.sqlite.JDBC", "jdbc:sqlite:" + connection); //NON-NLS
151  temprs = tempdbconnect.executeQry(query);
152  while (temprs.next()) {
153  if (temprs.getString("name") == null ? column == null : temprs.getString("name").equals(column)) { //NON-NLS
154  found = true;
155  }
156  }
157  } catch (Exception ex) {
158  logger.log(Level.WARNING, "Error while trying to get columns from sqlite db." + connection, ex); //NON-NLS
159  }
160  finally{
161  if (tempdbconnect != null) {
162  tempdbconnect.closeConnection();
163  }
164  }
165  return found;
166  }
167 
168  public static ResultSet runQuery(String query, String connection) {
169  ResultSet results = null;
170  try {
171  SQLiteDBConnect tempdbconnect = new SQLiteDBConnect("org.sqlite.JDBC", "jdbc:sqlite:" + connection); //NON-NLS
172  results = tempdbconnect.executeQry(query);
173  tempdbconnect.closeConnection();
174  } catch (Exception ex) {
175  logger.log(Level.WARNING, "Error while trying to run sql query: " + query + " : " + connection, ex); //NON-NLS
176  }
177  return results;
178  }
179 }

Copyright © 2012-2018 Basis Technology. Generated on: Wed Sep 18 2019
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.