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;
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 try {
152 SQLiteDBConnect tempdbconnect = new SQLiteDBConnect("org.sqlite.JDBC", "jdbc:sqlite:" + connection); //NON-NLS
153 temprs = tempdbconnect.executeQry(query);
154 while (temprs.next()) {
155 if (temprs.getString("name") == null ? column == null : temprs.getString("name").equals(column)) { //NON-NLS
156 found = true;
157 }
158 }
159 } catch (Exception ex) {
160 logger.log(Level.WARNING, "Error while trying to get columns from sqlite db." + connection, ex); //NON-NLS
161 }
162 return found;
163 }
164
165 public static ResultSet runQuery(String query, String connection) {
166 ResultSet results = null;
167 try {
168 SQLiteDBConnect tempdbconnect = new SQLiteDBConnect("org.sqlite.JDBC", "jdbc:sqlite:" + connection); //NON-NLS
169 results = tempdbconnect.executeQry(query);
170 tempdbconnect.closeConnection();
171 } catch (Exception ex) {
172 logger.log(Level.WARNING, "Error while trying to run sql query: " + query + " : " + connection, ex); //NON-NLS
173 }
174 return results;
175 }
176 }