Autopsy  4.16.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
OutputGenerator.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2019-2019 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  */
19 package org.sleuthkit.autopsy.commandlineingest;
20 
21 import com.fasterxml.jackson.core.JsonEncoding;
22 import com.fasterxml.jackson.core.JsonFactory;
23 import com.fasterxml.jackson.core.JsonGenerator;
24 import com.fasterxml.jackson.core.util.DefaultIndenter;
25 import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
26 import java.io.File;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import java.sql.ResultSet;
31 import java.sql.SQLException;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.logging.Level;
40 import org.sleuthkit.datamodel.AbstractFile;
41 import org.sleuthkit.datamodel.Content;
42 import org.sleuthkit.datamodel.Image;
43 import org.sleuthkit.datamodel.SleuthkitCase;
44 import org.sleuthkit.datamodel.TskCoreException;
45 
49 class OutputGenerator {
50 
51  private static final Logger logger = Logger.getLogger(OutputGenerator.class.getName());
52 
53  private OutputGenerator() {
54  }
55 
64  static void saveCreateCaseOutput(Case caseForJob, String outputDirPath, String baseCaseName) {
65  JsonFactory jsonGeneratorFactory = new JsonFactory();
66  String reportOutputPath = outputDirPath + File.separator + "createCase_" + TimeStampUtils.createTimeStamp() + ".json";
67  java.io.File reportFile = Paths.get(reportOutputPath).toFile();
68  try {
69  Files.createDirectories(Paths.get(reportFile.getParent()));
70  } catch (IOException ex) {
71  logger.log(Level.SEVERE, "Unable to create output file " + reportFile.toString() + " for 'Create Case' command", ex); //NON-NLS
72  System.err.println("Unable to create output file " + reportFile.toString() + " for 'Create Case' command"); //NON-NLS
73  return;
74  }
75 
76  JsonGenerator jsonGenerator = null;
77  try {
78  jsonGenerator = jsonGeneratorFactory.createGenerator(reportFile, JsonEncoding.UTF8);
79  // instert \n after each field for more readable formatting
80  jsonGenerator.setPrettyPrinter(new DefaultPrettyPrinter().withObjectIndenter(new DefaultIndenter(" ", "\n")));
81 
82  // save command output
83  jsonGenerator.writeStartObject();
84  jsonGenerator.writeStringField("@caseName", baseCaseName);
85  jsonGenerator.writeStringField("@caseDir", caseForJob.getCaseDirectory());
86  jsonGenerator.writeEndObject();
87  } catch (IOException ex) {
88  logger.log(Level.SEVERE, "Failed to create JSON output for 'Create Case' command", ex); //NON-NLS
89  System.err.println("Failed to create JSON output for 'Create Case' command"); //NON-NLS
90  } finally {
91  if (jsonGenerator != null) {
92  try {
93  jsonGenerator.close();
94  } catch (IOException ex) {
95  logger.log(Level.WARNING, "Failed to close JSON output file for 'Create Case' command", ex); //NON-NLS
96  System.err.println("Failed to close JSON output file for 'Create Case' command"); //NON-NLS
97  }
98  }
99  }
100  }
101 
110  static void saveAddDataSourceOutput(Case caseForJob, AutoIngestDataSource dataSource, String outputDirPath) {
111 
112  List<Content> contentObjects = dataSource.getContent();
113  if (contentObjects == null || contentObjects.isEmpty()) {
114  logger.log(Level.SEVERE, "No content objects for 'Add Data Source' command"); //NON-NLS
115  System.err.println("No content objects for 'Add Data Source' command"); //NON-NLS
116  return;
117  }
118 
119  JsonFactory jsonGeneratorFactory = new JsonFactory();
120  String reportOutputPath = outputDirPath + File.separator + "addDataSource_" + TimeStampUtils.createTimeStamp() + ".json";
121  java.io.File reportFile = Paths.get(reportOutputPath).toFile();
122  try {
123  Files.createDirectories(Paths.get(reportFile.getParent()));
124  } catch (IOException ex) {
125  logger.log(Level.SEVERE, "Unable to create output file " + reportFile.toString() + " for 'Add Data Source' command", ex); //NON-NLS
126  System.err.println("Unable to create output file " + reportFile.toString() + " for 'Add Data Source' command"); //NON-NLS
127  return;
128  }
129 
130  JsonGenerator jsonGenerator = null;
131  try {
132  jsonGenerator = jsonGeneratorFactory.createGenerator(reportFile, JsonEncoding.UTF8);
133  // instert \n after each field for more readable formatting
134  jsonGenerator.setPrettyPrinter(new DefaultPrettyPrinter().withObjectIndenter(new DefaultIndenter(" ", "\n")));
135 
136  // save command output
137  for (Content content : contentObjects) {
138  String dataSourceName;
139  if (content.getDataSource() instanceof Image) {
140  // image data source. Need to get display name
141  dataSourceName = getImageDisplayName(caseForJob, content.getId());
142  if (dataSourceName == null) {
143  // some image data sources do not have "display_name" set, use data source name instead
144  dataSourceName = content.getName();
145  }
146  } else {
147  // logical data source. The only way I found that works for all types of logical
148  // data sources is to get AbstractFile from database and use it's name.
149  // Content.getName() works for most but not all scenarios.
150  AbstractFile file = caseForJob.getSleuthkitCase().getAbstractFileById(content.getId());
151  dataSourceName = file.getName();
152  }
153 
154  // save the JSON output
155  saveDataSourceInfoToFile(jsonGenerator, dataSourceName, content.getId());
156  }
157  } catch (IOException ex) {
158  logger.log(Level.SEVERE, "Failed to create JSON output for 'Add Data Source' command", ex); //NON-NLS
159  System.err.println("Failed to create JSON output for 'Add Data Source' command"); //NON-NLS
160  } catch (TskCoreException ex) {
161  logger.log(Level.SEVERE, "Failed to get data source info for 'Add Data Source' command output", ex); //NON-NLS
162  System.err.println("Failed to get data source info for 'Add Data Source' command output"); //NON-NLS
163  } catch (SQLException ex) {
164  logger.log(Level.SEVERE, "Failed to get data source display name for 'Add Data Source' command output", ex); //NON-NLS
165  System.err.println("Failed to get data source display name for 'Add Data Source' command output"); //NON-NLS
166  } finally {
167  if (jsonGenerator != null) {
168  try {
169  jsonGenerator.close();
170  } catch (IOException ex) {
171  logger.log(Level.WARNING, "Failed to close JSON output file for 'Add Data Source' command", ex); //NON-NLS
172  System.err.println("Failed to close JSON output file for 'Add Data Source' command"); //NON-NLS
173  }
174  }
175  }
176  }
177 
186  static void listAllDataSources(Case caseForJob, String outputDirPath) {
187  JsonFactory jsonGeneratorFactory = new JsonFactory();
188  String reportOutputPath = outputDirPath + File.separator + "listAllDataSources_" + TimeStampUtils.createTimeStamp() + ".json";
189  java.io.File reportFile = Paths.get(reportOutputPath).toFile();
190  try {
191  Files.createDirectories(Paths.get(reportFile.getParent()));
192  } catch (IOException ex) {
193  logger.log(Level.SEVERE, "Unable to create output file " + reportFile.toString() + " for 'List All Data Sources' command", ex); //NON-NLS
194  System.err.println("Unable to create output file " + reportFile.toString() + " for 'List All Data Sources' command"); //NON-NLS
195  return;
196  }
197 
198  JsonGenerator jsonGenerator = null;
199  try {
200  jsonGenerator = jsonGeneratorFactory.createGenerator(reportFile, JsonEncoding.UTF8);
201  // instert \n after each field for more readable formatting
202  jsonGenerator.setPrettyPrinter(new DefaultPrettyPrinter().withObjectIndenter(new DefaultIndenter(" ", "\n")));
203 
204  // list all image data sources
205  Map<Long, String> imageDataSources = DataSourceLoader.getImageDataSources(caseForJob.getSleuthkitCase());
206  for (Map.Entry<Long, String> entry : imageDataSources.entrySet()) {
207  String dataSourceName;
208  Long dataSourceObjOd = entry.getKey();
209  dataSourceName = getImageDisplayName(caseForJob, dataSourceObjOd);
210  if (dataSourceName == null) {
211  // some image data sources do not have "display_name" set, use data source name instead
212  dataSourceName = entry.getValue();
213  }
214  saveDataSourceInfoToFile(jsonGenerator, dataSourceName, dataSourceObjOd);
215  }
216 
217  // list all logical data sources
218  Map<Long, String> logicalDataSources = DataSourceLoader.getLogicalDataSources(caseForJob.getSleuthkitCase());
219  for (Map.Entry<Long, String> entry : logicalDataSources.entrySet()) {
220  String dataSourceName = entry.getValue();
221  Long dataSourceObjOd = entry.getKey();
222  saveDataSourceInfoToFile(jsonGenerator, dataSourceName, dataSourceObjOd);
223  }
224  } catch (IOException ex) {
225  logger.log(Level.SEVERE, "Failed to create JSON output for 'List All Data Sources' command", ex); //NON-NLS
226  System.err.println("Failed to create JSON output for 'List All Data Sources' command"); //NON-NLS
227  } catch (TskCoreException ex) {
228  logger.log(Level.SEVERE, "Failed to get data source info for 'List All Data Sources' command output", ex); //NON-NLS
229  System.err.println("Failed to get data source info for 'List All Data Sources' command output"); //NON-NLS
230  } catch (SQLException ex) {
231  logger.log(Level.SEVERE, "Failed to get data source display name for 'List All Data Sources' command output", ex); //NON-NLS
232  System.err.println("Failed to get data source display name for 'List All Data Sources' command output"); //NON-NLS
233  } finally {
234  if (jsonGenerator != null) {
235  try {
236  jsonGenerator.close();
237  } catch (IOException ex) {
238  logger.log(Level.WARNING, "Failed to close JSON output file for 'List All Data Sources' command", ex); //NON-NLS
239  System.err.println("Failed to close JSON output file for 'List All Data Sources' command"); //NON-NLS
240  }
241  }
242  }
243  }
244 
253  private static void saveDataSourceInfoToFile(JsonGenerator jsonGenerator, String dataSourceName, long dataSourceObjId) throws IOException {
254  jsonGenerator.writeStartObject();
255  jsonGenerator.writeStringField("@dataSourceName", dataSourceName);
256  jsonGenerator.writeStringField("@dataSourceObjectId", String.valueOf(dataSourceObjId));
257  jsonGenerator.writeEndObject();
258  }
259 
269  private static String getImageDisplayName(Case caseForJob, Long dataSourceId) throws TskCoreException, SQLException {
270  String getImageDataSourceQuery = "select display_name from tsk_image_info where obj_id = " + dataSourceId;
271  try (SleuthkitCase.CaseDbQuery queryResult = caseForJob.getSleuthkitCase().executeQuery(getImageDataSourceQuery)) {
272  ResultSet resultSet = queryResult.getResultSet();
273  // check if we got a result
274  while (resultSet.next()) {
275  // we got a result so the data source was an image data source
276  return resultSet.getString(1);
277  }
278  }
279 
280  return null;
281  }
282 }

Copyright © 2012-2020 Basis Technology. Generated on: Tue Sep 22 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.