Autopsy  4.11.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.net.MalformedURLException;
30 import java.net.URL;
31 import java.nio.MappedByteBuffer;
32 import java.nio.channels.FileChannel;
33 import java.nio.charset.Charset;
34 import java.sql.ResultSet;
35 import java.text.SimpleDateFormat;
36 import java.util.Date;
37 import java.util.List;
38 import java.util.StringTokenizer;
39 import java.util.logging.Level;
41 import java.util.regex.Matcher;
42 import java.util.regex.Pattern;
46 import org.sleuthkit.datamodel.AbstractFile;
47 import org.sleuthkit.datamodel.Content;
48 import org.sleuthkit.datamodel.TskCoreException;
49 
54 class Util {
55 
56  private static Logger logger = Logger.getLogger(Util.class.getName());
57 
58  private Util() {
59  }
60 
61  public static boolean pathexists(String path) {
62  File file = new File(path);
63  boolean exists = file.exists();
64  return exists;
65  }
66 
67  public static String utcConvert(String utc) {
68  SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy HH:mm");
69  String tempconvert = formatter.format(new Date(Long.parseLong(utc)));
70  return tempconvert;
71  }
72 
73  public static String readFile(String path) throws IOException {
74  FileInputStream stream = new FileInputStream(new File(path));
75  try {
76  FileChannel fc = stream.getChannel();
77  MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
78  /*
79  * Instead of using default, pass in a decoder.
80  */
81  return Charset.defaultCharset().decode(bb).toString();
82  } finally {
83  stream.close();
84  }
85  }
86 
87  public static String getFileName(String value) {
88  String filename = "";
89  String filematch = "^([a-zA-Z]\\:)(\\\\[^\\\\/:*?<>\"|]*(?<!\\[ \\]))*(\\.[a-zA-Z]{2,6})$"; //NON-NLS
90 
91  Pattern p = Pattern.compile(filematch, Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.COMMENTS);
92  Matcher m = p.matcher(value);
93  if (m.find()) {
94  filename = m.group(1);
95 
96  }
97  int lastPos = value.lastIndexOf('\\');
98  filename = (lastPos < 0) ? value : value.substring(lastPos + 1);
99  return filename.toString();
100  }
101 
102  public static String getPath(String txt) {
103  String path = "";
104 
105  //String drive ="([a-z]:\\\\(?:[-\\w\\.\\d]+\\\\)*(?:[-\\w\\.\\d]+)?)"; // Windows drive
106  String drive = "([a-z]:\\\\\\S.+)"; //NON-NLS
107  Pattern p = Pattern.compile(drive, Pattern.CASE_INSENSITIVE | Pattern.COMMENTS);
108  Matcher m = p.matcher(txt);
109  if (m.find()) {
110  path = m.group(1);
111 
112  } else {
113 
114  String network = "(\\\\(?:\\\\[^:\\s?*\"<>|]+)+)"; // Windows network NON-NLS
115 
116  Pattern p2 = Pattern.compile(network, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
117  Matcher m2 = p2.matcher(txt);
118  if (m2.find()) {
119  path = m2.group(1);
120  }
121  }
122  return path;
123  }
124 
125  public static long findID(Content dataSource, String path) {
126  String parent_path = path.replace('\\', '/'); // fix Chrome paths
127  if (parent_path.length() > 2 && parent_path.charAt(1) == ':') {
128  parent_path = parent_path.substring(2); // remove drive letter (e.g., 'C:')
129  }
130  int index = parent_path.lastIndexOf('/');
131  String name = parent_path.substring(++index);
132  parent_path = parent_path.substring(0, index);
133  List<AbstractFile> files = null;
134  try {
135  FileManager fileManager = Case.getCurrentCaseThrows().getServices().getFileManager();
136  files = fileManager.findFiles(dataSource, name, parent_path);
137  } catch (TskCoreException | NoCurrentCaseException ex) {
138  logger.log(Level.WARNING, "Error fetching 'index.data' files for Internet Explorer history."); //NON-NLS
139  }
140 
141  if (files == null || files.isEmpty()) {
142  return -1;
143  }
144  return files.get(0).getId();
145  }
146 
147  public static boolean checkColumn(String column, String tablename, String connection) {
148  String query = "PRAGMA table_info(" + tablename + ")"; //NON-NLS
149  boolean found = false;
150  ResultSet temprs;
151  SQLiteDBConnect tempdbconnect = null;
152  try {
153  tempdbconnect = new SQLiteDBConnect("org.sqlite.JDBC", "jdbc:sqlite:" + connection); //NON-NLS
154  temprs = tempdbconnect.executeQry(query);
155  while (temprs.next()) {
156  if (temprs.getString("name") == null ? column == null : temprs.getString("name").equals(column)) { //NON-NLS
157  found = true;
158  }
159  }
160  } catch (Exception ex) {
161  logger.log(Level.WARNING, "Error while trying to get columns from sqlite db." + connection, ex); //NON-NLS
162  }
163  finally{
164  if (tempdbconnect != null) {
165  tempdbconnect.closeConnection();
166  }
167  }
168  return found;
169  }
170 
171  public static ResultSet runQuery(String query, String connection) {
172  ResultSet results = null;
173  try {
174  SQLiteDBConnect tempdbconnect = new SQLiteDBConnect("org.sqlite.JDBC", "jdbc:sqlite:" + connection); //NON-NLS
175  results = tempdbconnect.executeQry(query);
176  tempdbconnect.closeConnection();
177  } catch (Exception ex) {
178  logger.log(Level.WARNING, "Error while trying to run sql query: " + query + " : " + connection, ex); //NON-NLS
179  }
180  return results;
181  }
182 }

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