Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
StandardInterestingFilesSetsLoader.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2020 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.modules.interestingitems;
20 
21 import java.io.File;
22 import java.io.FilenameFilter;
23 import java.io.IOException;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.function.Function;
27 import java.util.logging.Level;
28 import java.util.stream.Collectors;
29 import org.openide.modules.InstalledFileLocator;
30 import org.openide.modules.OnStart;
31 import org.openide.util.NbBundle.Messages;
35 
40 @OnStart
41 public class StandardInterestingFilesSetsLoader implements Runnable {
42 
43  private static final Logger LOGGER = Logger.getLogger(StandardInterestingFilesSetsLoader.class.getName());
44 
45  private static final String CONFIG_DIR = "InterestingFileSetRules";
46 
47  private static final FilenameFilter DEFAULT_XML_FILTER = new FilenameFilter() {
48  @Override
49  public boolean accept(File dir, String name) {
50  return name.endsWith(".xml");
51  }
52  };
53 
54  @Override
55  @Messages({
56  "StandardInterestingFilesSetsLoader_cannotLoadStandard=Unable to properly read standard interesting files sets.",
57  "StandardInterestingFilesSetsLoader_cannotLoadUserConfigured=Unable to properly read user-configured interesting files sets.",
58  "StandardInterestingFilesSetsLoader_cannotUpdateInterestingFilesSets=Unable to write updated configuration for interesting files sets to config directory."
59  })
60  public void run() {
61  upgradeConfig();
62 
63  Map<String, FilesSet> standardInterestingFileSets = null;
64  try {
65  standardInterestingFileSets = readStandardFileXML();
67  handleError(Bundle.StandardInterestingFilesSetsLoader_cannotLoadStandard(), ex);
68  return;
69  }
70 
71  // Call FilesSetManager.getInterestingFilesSets() to get a Map<String, FilesSet> of the existing rule sets.
72  Map<String, FilesSet> userConfiguredSettings = null;
73  try {
74  userConfiguredSettings = FilesSetsManager.getInstance().getInterestingFilesSets();
76  LOGGER.log(Level.SEVERE, "Unable to properly read user-configured interesting files sets.", ex);
77  handleError(Bundle.StandardInterestingFilesSetsLoader_cannotLoadStandard(), ex);
78  return;
79  }
80 
81  // Add each FilesSet read from the standard rules set XML files that is missing from the Map to the Map.
82  copyOnNewer(standardInterestingFileSets, userConfiguredSettings, true);
83 
84  try {
85  // Call FilesSetManager.setInterestingFilesSets with the updated Map.
86  FilesSetsManager.getInstance().setInterestingFilesSets(userConfiguredSettings);
88  handleError(Bundle.StandardInterestingFilesSetsLoader_cannotUpdateInterestingFilesSets(), ex);
89  }
90  }
91 
95  private void upgradeConfig() {
96  try {
97  FilesSetsManager.getInstance().upgradeConfig();
98  } catch (IOException ex) {
99  LOGGER.log(Level.WARNING, "There was an error while upgrading config paths.", ex);
100  }
101  }
102 
109  private static void handleError(String message, Exception ex) {
110  LOGGER.log(Level.SEVERE, message, ex);
113  }
114  }
115 
123  private static Map<String, FilesSet> readStandardFileXML() throws FilesSetsManager.FilesSetsManagerException {
124  Map<String, FilesSet> standardInterestingFileSets = new HashMap<>();
125 
126  File configFolder = InstalledFileLocator.getDefault().locate(
127  CONFIG_DIR, StandardInterestingFilesSetsLoader.class.getPackage().getName(), false);
128 
129  if (configFolder == null || !configFolder.exists() || !configFolder.isDirectory()) {
130  throw new FilesSetsManager.FilesSetsManagerException("No standard interesting files set folder exists.");
131  }
132 
133  File[] standardFileSets = configFolder.listFiles(DEFAULT_XML_FILTER);
134 
135  for (File standardFileSetsFile : standardFileSets) { //NON-NLS
136  try {
137  Map<String, FilesSet> thisFilesSet = InterestingItemsFilesSetSettings.readDefinitionsXML(standardFileSetsFile);
138 
139  // ensure that read resources are standard sets
140  thisFilesSet = thisFilesSet.values()
141  .stream()
142  .map((filesSet) -> getAsStandardFilesSet(filesSet, true))
143  .collect(Collectors.toMap(FilesSet::getName, Function.identity()));
144 
145  copyOnNewer(thisFilesSet, standardInterestingFileSets);
147  LOGGER.log(Level.WARNING, String.format("There was a problem importing the standard interesting file set at: %s.",
148  standardFileSetsFile.getAbsoluteFile()), ex);
149  }
150  }
151  return standardInterestingFileSets;
152  }
153 
164  static FilesSet getAsStandardFilesSet(FilesSet origFilesSet, boolean standardFilesSet) {
165  return new FilesSet(
166  origFilesSet.getName(),
167  origFilesSet.getDescription(),
168  origFilesSet.ignoresKnownFiles(),
169  origFilesSet.ingoresUnallocatedSpace(),
170  origFilesSet.getRules(),
171  standardFilesSet,
172  origFilesSet.getVersionNumber()
173  );
174  }
175 
184  private static void copyOnNewer(Map<String, FilesSet> src, Map<String, FilesSet> dest) {
185  copyOnNewer(src, dest, false);
186  }
187 
199  private static void copyOnNewer(Map<String, FilesSet> src, Map<String, FilesSet> dest, boolean appendCustom) {
200  for (Map.Entry<String, FilesSet> srcEntry : src.entrySet()) {
201  String key = srcEntry.getKey();
202  FilesSet srcFileSet = srcEntry.getValue();
203  FilesSet destFileSet = dest.get(key);
204  if (destFileSet != null) {
205  // If and only if there is a naming conflict with a user-defined rule set, append “(Custom)”
206  // to the user-defined rule set and add it back to the Map.
207  if (appendCustom && srcFileSet.isStandardSet() != destFileSet.isStandardSet()) {
208  if (srcFileSet.isStandardSet()) {
209  addCustomFile(dest, destFileSet);
210  dest.put(key, srcFileSet);
211  } else {
212  addCustomFile(dest, srcFileSet);
213  }
214  continue;
215  }
216 
217  // Replace each FilesSet read from the standard rules set XML files that has a newer version
218  // number than the corresponding FilesSet in the Map with the updated FilesSet.
219  if (destFileSet.getVersionNumber() >= srcEntry.getValue().getVersionNumber()) {
220  continue;
221  }
222  }
223 
224  dest.put(srcEntry.getKey(), srcEntry.getValue());
225  }
226  }
227 
236  private static void addCustomFile(Map<String, FilesSet> dest, FilesSet srcFilesSet) {
237  if (srcFilesSet.isStandardSet()) {
238  LOGGER.log(Level.SEVERE, "An attempt to create a custom file that was a standard set.");
239  return;
240  }
241 
242  FilesSet srcToAdd = srcFilesSet;
243 
244  do {
245  srcToAdd = getAsCustomFileSet(srcToAdd);
246  } while (dest.containsKey(srcToAdd.getName()));
247 
248  dest.put(srcToAdd.getName(), srcToAdd);
249  }
250 
259  @Messages({
260  "# {0} - filesSetName",
261  "StandardInterestingFileSetsLoader.customSuffixed={0} (Custom)"
262  })
263  static FilesSet getAsCustomFileSet(FilesSet srcFilesSet) {
264  String customKey = Bundle.StandardInterestingFileSetsLoader_customSuffixed(srcFilesSet.getName());
265  return new FilesSet(
266  customKey,
267  srcFilesSet.getDescription(),
268  srcFilesSet.ignoresKnownFiles(),
269  srcFilesSet.ingoresUnallocatedSpace(),
270  srcFilesSet.getRules(),
271  false,
272  srcFilesSet.getVersionNumber()
273  );
274  }
275 }
static void copyOnNewer(Map< String, FilesSet > src, Map< String, FilesSet > dest)
static void copyOnNewer(Map< String, FilesSet > src, Map< String, FilesSet > dest, boolean appendCustom)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

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