Autopsy 4.22.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
CommandLineManager.java
Go to the documentation of this file.
1/*
2 * Autopsy Forensic Browser
3 *
4 * Copyright 2020-2022 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 */
19package org.sleuthkit.autopsy.commandlineingest;
20
21import java.io.File;
22import java.io.FilenameFilter;
23import java.nio.file.Path;
24import java.nio.file.Paths;
25import java.util.logging.Level;
26import org.sleuthkit.autopsy.casemodule.Case;
27import org.sleuthkit.autopsy.casemodule.CaseActionException;
28import org.sleuthkit.autopsy.casemodule.CaseDetails;
29import org.sleuthkit.autopsy.casemodule.CaseMetadata;
30import static org.sleuthkit.autopsy.casemodule.CaseMetadata.getFileExtension;
31import org.sleuthkit.autopsy.coreutils.Logger;
32import org.sleuthkit.autopsy.coreutils.TimeStampUtils;
33
37class CommandLineManager {
38
39 private static final String LOG_DIR_NAME = "Command Output";
40 private static final Logger LOGGER = Logger.getLogger(CommandLineOpenCaseManager.class.getName());
41
50 Case openCase(String casePath) throws CaseActionException {
51
52 String metadataFilePath;
53 if (casePath.endsWith(".aut") && (new File(casePath)).isFile()) {
54 LOGGER.log(Level.INFO, "Opening case {0}", casePath);
55 metadataFilePath = casePath;
56 } else {
57 LOGGER.log(Level.INFO, "Opening case in directory {0}", casePath);
58 metadataFilePath = findAutFile(casePath);
59 }
60 Case.openAsCurrentCase(metadataFilePath);
61
62 Case newCase = Case.getCurrentCase();
63 LOGGER.log(Level.INFO, "Opened case {0}", newCase.getName());
64
65 return newCase;
66 }
67
77 private String findAutFile(String caseDirectory) throws CaseActionException {
78 File caseFolder = Paths.get(caseDirectory).toFile();
79 if (caseFolder.exists()) {
80 /*
81 * Search for '*.aut' files.
82 */
83 File[] fileArray = caseFolder.listFiles();
84 if (fileArray == null) {
85 throw new CaseActionException("No files found in case directory");
86 }
87 String autFilePath = null;
88 for (File file : fileArray) {
89 String name = file.getName().toLowerCase();
90 if (autFilePath == null && name.endsWith(getFileExtension())) {
91 return file.getAbsolutePath();
92 }
93 }
94 throw new CaseActionException("No .aut files found in case directory");
95 }
96 throw new CaseActionException("Case directory was not found");
97 }
98
110 Case createCase(String baseCaseName, String rootOutputDirectory, Case.CaseType caseType) throws CaseActionException {
111
112 LOGGER.log(Level.INFO, "Creating new case {0} in directory {1}", new Object[]{baseCaseName, rootOutputDirectory});
113 Path caseDirectoryPath = findCaseDirectory(Paths.get(rootOutputDirectory), baseCaseName);
114 if (null != caseDirectoryPath) {
115 // found an existing case directory for same case name. the input case name must be unique. Exit.
116 LOGGER.log(Level.SEVERE, "Case {0} already exists. Case name must be unique. Exiting", baseCaseName);
117 throw new CaseActionException("Case " + baseCaseName + " already exists. Case name must be unique. Exiting");
118 } else {
119 caseDirectoryPath = createCaseFolderPath(Paths.get(rootOutputDirectory), baseCaseName);
120
121 // Create the case directory
122 Case.createCaseDirectory(caseDirectoryPath.toString(), caseType);
123
124 CaseDetails caseDetails = new CaseDetails(baseCaseName);
125 Case.createAsCurrentCase(caseType, caseDirectoryPath.toString(), caseDetails);
126 }
127
128 Case caseForJob = Case.getCurrentCase();
129 LOGGER.log(Level.INFO, "Created case {0}", caseForJob.getName());
130 return caseForJob;
131 }
132
142 private Path createCaseFolderPath(Path caseFoldersPath, String caseName) {
143 String folderName = caseName + "_" + TimeStampUtils.createTimeStamp();
144 return Paths.get(caseFoldersPath.toString(), folderName);
145 }
146
156 Case openExistingCase(String caseName, String rootOutputDirectory) throws CaseActionException {
157 LOGGER.log(Level.INFO, "Opening case {0} in directory {1}", new Object[]{caseName, rootOutputDirectory});
158 Path caseDirectoryPath = findCaseDirectory(Paths.get(rootOutputDirectory), caseName);
159 if (null != caseDirectoryPath) {
160 // found an existing case directory for same case name.
161 Path metadataFilePath = caseDirectoryPath.resolve(caseName + CaseMetadata.getFileExtension());
162 Case.openAsCurrentCase(metadataFilePath.toString());
163 } else {
164 // did not find existing case directory for same case name. Exit.
165 LOGGER.log(Level.SEVERE, "Case {0} doesn't exist. Exiting", caseName);
166 throw new CaseActionException("Case " + caseName + " doesn't exist. Exiting");
167 }
168
169 Case caseForJob = Case.getCurrentCase();
170 LOGGER.log(Level.INFO, "Opened case {0}", caseForJob.getName());
171 return caseForJob;
172 }
173
184 private Path findCaseDirectory(Path folderToSearch, String caseName) {
185 File searchFolder = new File(folderToSearch.toString());
186 if (!searchFolder.isDirectory()) {
187 return null;
188 }
189 Path caseFolderPath = null;
190 String[] candidateFolders = searchFolder.list(new CaseFolderFilter(caseName));
191 long mostRecentModified = 0;
192 for (String candidateFolder : candidateFolders) {
193 File file = new File(candidateFolder);
194 if (file.lastModified() >= mostRecentModified) {
195 mostRecentModified = file.lastModified();
196 caseFolderPath = Paths.get(folderToSearch.toString(), file.getPath());
197 }
198 }
199 return caseFolderPath;
200 }
201
209 String getOutputDirPath(Case caseForJob) {
210 return caseForJob.getCaseDirectory() + File.separator + LOG_DIR_NAME;
211 }
212
213 private static class CaseFolderFilter implements FilenameFilter {
214
215 private final String caseName;
216 private final static String CASE_METADATA_EXT = CaseMetadata.getFileExtension();
217
218 CaseFolderFilter(String caseName) {
219 this.caseName = caseName;
220 }
221
222 @Override
223 public boolean accept(File folder, String fileName) {
224 File file = new File(folder, fileName);
225 if (fileName.length() > TimeStampUtils.getTimeStampLength() && file.isDirectory()) {
226 if (TimeStampUtils.endsWithTimeStamp(fileName)) {
227 if (null != caseName) {
228 String fileNamePrefix = fileName.substring(0, fileName.length() - TimeStampUtils.getTimeStampLength());
229 if (fileNamePrefix.equals(caseName)) {
230 return hasCaseMetadataFile(file);
231 }
232 } else {
233 return hasCaseMetadataFile(file);
234 }
235 }
236 }
237 return false;
238 }
239
248 private static boolean hasCaseMetadataFile(File folder) {
249 for (File file : folder.listFiles()) {
250 if (file.getName().toLowerCase().endsWith(CASE_METADATA_EXT) && file.isFile()) {
251 return true;
252 }
253 }
254 return false;
255 }
256 }
257
258}
static boolean endsWithTimeStamp(String inputString)

Copyright © 2012-2024 Sleuth Kit Labs. Generated on:
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.