Autopsy  4.18.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
SaveTaggedHashesToHashDb.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2021 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.report.modules.taggedhashes;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.logging.Level;
25 import javax.swing.JPanel;
26 import org.openide.util.NbBundle.Messages;
27 import org.openide.util.lookup.ServiceProvider;
37 import org.sleuthkit.datamodel.AbstractFile;
38 import org.sleuthkit.datamodel.Content;
39 import org.sleuthkit.datamodel.ContentTag;
40 import org.sleuthkit.datamodel.TagName;
41 import org.sleuthkit.datamodel.TskCoreException;
42 
47 @ServiceProvider(service = GeneralReportModule.class)
49 
50  private static final Logger logger = Logger.getLogger(SaveTaggedHashesToHashDb.class.getName());
51  private SaveTaggedHashesToHashDbConfigPanel configPanel;
52 
54  }
55 
56  @Override
57  public String getName() {
58  return "Save Tagged Hashes";
59  }
60 
61  @Override
62  public String getDescription() {
63  return "Adds hashes of tagged files to a hash set.";
64  }
65 
66  @Override
67  public String getRelativeFilePath() {
68  return null;
69  }
70 
76  @Override
78  return new HashesReportModuleSettings();
79  }
80 
86  @Override
88  initializePanel();
89  return configPanel.getConfiguration();
90  }
91 
97  @Override
98  public void setConfiguration(ReportModuleSettings settings) {
99  initializePanel();
100  if (settings == null || settings instanceof NoReportModuleSettings) {
101  configPanel.setConfiguration((HashesReportModuleSettings) getDefaultConfiguration());
102  return;
103  }
104 
105  if (settings instanceof HashesReportModuleSettings) {
106  configPanel.setConfiguration((HashesReportModuleSettings) settings);
107  return;
108  }
109 
110  throw new IllegalArgumentException("Expected settings argument to be an instance of HashesReportModuleSettings");
111  }
112 
113  @Messages({
114  "AddTaggedHashesToHashDb.error.noHashSetsSelected=No hash set selected for export.",
115  "AddTaggedHashesToHashDb.error.unableToOpenCase=Exception while getting open case.",
116  "AddTaggedHashesToHashDb.error.noTagsSelected=No tags selected for export."
117  })
118  @Override
119  public void generateReport(GeneralReportSettings settings, ReportProgressPanel progressPanel) {
120  Case openCase;
121  try {
122  openCase = Case.getCurrentCaseThrows();
123  } catch (NoCurrentCaseException ex) {
124  Logger.getLogger(SaveTaggedHashesToHashDb.class.getName()).log(Level.SEVERE, "Exception while getting open case.", ex);
125  progressPanel.complete(ReportProgressPanel.ReportStatus.ERROR, Bundle.AddTaggedHashesToHashDb_error_unableToOpenCase());
126  return;
127  }
128  progressPanel.setIndeterminate(true);
129  progressPanel.start();
130  progressPanel.updateStatusLabel("Adding hashes...");
131 
132  HashDb hashSet = configPanel.getSelectedHashDatabase();
133  if (hashSet == null) {
134  logger.log(Level.WARNING, "No hash set selected for export."); //NON-NLS
135  progressPanel.setIndeterminate(false);
136  progressPanel.complete(ReportProgressPanel.ReportStatus.ERROR, Bundle.AddTaggedHashesToHashDb_error_noHashSetsSelected());
137  return;
138  }
139 
140  progressPanel.updateStatusLabel("Adding hashes to " + hashSet.getHashSetName() + " hash set...");
141 
142  TagsManager tagsManager = openCase.getServices().getTagsManager();
143  List<TagName> tagNames = configPanel.getSelectedTagNames();
144  if (tagNames.isEmpty()) {
145  logger.log(Level.WARNING, "No tags selected for export."); //NON-NLS
146  progressPanel.setIndeterminate(false);
147  progressPanel.complete(ReportProgressPanel.ReportStatus.ERROR, Bundle.AddTaggedHashesToHashDb_error_noTagsSelected());
148  return;
149  }
150 
151  ArrayList<String> failedExports = new ArrayList<>();
152  int notAddedCount = 0;
153  int addedCount = 0;
154  for (TagName tagName : tagNames) {
155  if (progressPanel.getStatus() == ReportProgressPanel.ReportStatus.CANCELED) {
156  break;
157  }
158 
159  progressPanel.updateStatusLabel("Adding " + tagName.getDisplayName() + " hashes to " + hashSet.getHashSetName() + " hash set...");
160  List<ContentTag> tags = new ArrayList<>();
161  try {
162  tags.addAll(tagsManager.getContentTagsByTagName(tagName));
163 
164  } catch (TskCoreException ex) {
165  Logger.getLogger(SaveTaggedHashesToHashDb.class.getName()).log(Level.SEVERE, "Error adding to hash set", ex);
166  progressPanel.updateStatusLabel("Error getting selected tags for case.");
167  }
168  for (ContentTag tag : tags) {
169  // TODO: Currently only AbstractFiles have md5 hashes. Here only files matter.
170  Content content = tag.getContent();
171  if (content instanceof AbstractFile) {
172  if (null != ((AbstractFile) content).getMd5Hash()) {
173  //if there is a failure to add the file for a reason other than missing an md5 keep going but take note
174  try {
175  hashSet.addHashes(content, openCase.getDisplayName());
176  addedCount++;
177  } catch (TskCoreException ex) {
178  Logger.getLogger(SaveTaggedHashesToHashDb.class.getName()).log(Level.SEVERE, "Error adding hash for obj_id = " + content.getId() + " to hash set " + hashSet.getHashSetName(), ex);
179  failedExports.add(content.getName());
180  }
181  } else {
182  notAddedCount++;
183  }
184  }
185  }
186  }
187  progressPanel.setIndeterminate(false);
188  //if the failed exports indicate the report had an issue note the files and indicate an error otherwise if there was no error indicate it is complete
189  if (!failedExports.isEmpty()) {
190  StringBuilder errorMessage = new StringBuilder("Failed to export hashes for the following file" + (failedExports.size() > 1 ? " and " + (failedExports.size() - 1) + " others: " : ": "));
191  errorMessage.append(failedExports.get(0));
192  progressPanel.complete(ReportProgressPanel.ReportStatus.ERROR, errorMessage.toString());
193  } else if (progressPanel.getStatus() != ReportProgressPanel.ReportStatus.ERROR) {
194  progressPanel.complete(ReportProgressPanel.ReportStatus.COMPLETE, addedCount + " file(s) added to hash set. " + notAddedCount + " file(s) without a hash skipped.");
195  }
196  }
197 
198  @Override
199  public JPanel getConfigurationPanel() {
200  initializePanel();
201  return configPanel;
202  }
203 
204  private void initializePanel() {
205  if (configPanel == null) {
206  configPanel = new SaveTaggedHashesToHashDbConfigPanel();
207  }
208  }
209 }
void generateReport(GeneralReportSettings settings, ReportProgressPanel progressPanel)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
List< ContentTag > getContentTagsByTagName(TagName tagName)

Copyright © 2012-2021 Basis Technology. Generated on: Thu Jul 8 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.