Autopsy  4.15.0
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.util.HashMap;
24 import java.util.Map;
25 import java.util.function.Function;
26 import java.util.logging.Level;
27 import java.util.stream.Collectors;
28 import org.openide.modules.InstalledFileLocator;
29 import org.openide.modules.OnStart;
30 import org.openide.util.NbBundle.Messages;
32 
37 @OnStart
38 public class StandardInterestingFilesSetsLoader implements Runnable {
39 
40  private static final Logger LOGGER = Logger.getLogger(StandardInterestingFilesSetsLoader.class.getName());
41 
42  private static final String CONFIG_DIR = "InterestingFileSetRules";
43 
44  private static final FilenameFilter DEFAULT_XML_FILTER = new FilenameFilter() {
45  @Override
46  public boolean accept(File dir, String name) {
47  return name.endsWith(".xml");
48  }
49  };
50 
51  @Override
52  public void run() {
53  Map<String, FilesSet> standardInterestingFileSets = readStandardFileXML();
54 
55  // Call FilesSetManager.getInterestingFilesSets() to get a Map<String, FilesSet> of the existing rule sets.
56  Map<String, FilesSet> userConfiguredSettings = null;
57  try {
58  userConfiguredSettings = FilesSetsManager.getInstance().getInterestingFilesSets();
60  LOGGER.log(Level.SEVERE, "Unable to properly read user-configured interesting files sets.", ex);
61  }
62 
63  if (userConfiguredSettings == null) {
64  userConfiguredSettings = new HashMap<>();
65  }
66 
67  // Add each FilesSet read from the standard rules set XML files that is missing from the Map to the Map.
68  copyOnNewer(standardInterestingFileSets, userConfiguredSettings, true);
69 
70  try {
71  // Call FilesSetManager.setInterestingFilesSets with the updated Map.
72  FilesSetsManager.getInstance().setInterestingFilesSets(userConfiguredSettings);
74  LOGGER.log(Level.SEVERE, "Unable to write updated configuration for interesting files sets to config directory.", ex);
75  }
76  }
77 
85  private static Map<String, FilesSet> readStandardFileXML() {
86  Map<String, FilesSet> standardInterestingFileSets = new HashMap<>();
87 
88  File[] standardFileSets = InstalledFileLocator.getDefault()
89  .locate(CONFIG_DIR, StandardInterestingFilesSetsLoader.class.getPackage().getName(), false)
90  .listFiles(DEFAULT_XML_FILTER);
91 
92  for (File standardFileSetsFile : standardFileSets) { //NON-NLS
93  try {
94  Map<String, FilesSet> thisFilesSet = InterestingItemsFilesSetSettings.readDefinitionsXML(standardFileSetsFile);
95 
96  // ensure that read resources are standard sets
97  thisFilesSet = thisFilesSet.values()
98  .stream()
99  .map((filesSet) -> getAsStandardFilesSet(filesSet, true))
100  .collect(Collectors.toMap(FilesSet::getName, Function.identity()));
101 
102  copyOnNewer(thisFilesSet, standardInterestingFileSets);
104  LOGGER.log(Level.WARNING, String.format("There was a problem importing the standard interesting file set at: %s.",
105  standardFileSetsFile.getAbsoluteFile()), ex);
106  }
107  }
108  return standardInterestingFileSets;
109  }
110 
121  static FilesSet getAsStandardFilesSet(FilesSet origFilesSet, boolean standardFilesSet) {
122  return new FilesSet(
123  origFilesSet.getName(),
124  origFilesSet.getDescription(),
125  origFilesSet.ignoresKnownFiles(),
126  origFilesSet.ingoresUnallocatedSpace(),
127  origFilesSet.getRules(),
128  standardFilesSet,
129  origFilesSet.getVersionNumber()
130  );
131  }
132 
141  private static void copyOnNewer(Map<String, FilesSet> src, Map<String, FilesSet> dest) {
142  copyOnNewer(src, dest, false);
143  }
144 
156  private static void copyOnNewer(Map<String, FilesSet> src, Map<String, FilesSet> dest, boolean appendCustom) {
157  for (Map.Entry<String, FilesSet> srcEntry : src.entrySet()) {
158  String key = srcEntry.getKey();
159  FilesSet srcFileSet = srcEntry.getValue();
160  FilesSet destFileSet = dest.get(key);
161  if (destFileSet != null) {
162  // If and only if there is a naming conflict with a user-defined rule set, append “(Custom)”
163  // to the user-defined rule set and add it back to the Map.
164  if (appendCustom && srcFileSet.isStandardSet() != destFileSet.isStandardSet()) {
165  if (srcFileSet.isStandardSet()) {
166  addCustomFile(dest, destFileSet);
167  dest.put(key, srcFileSet);
168  } else {
169  addCustomFile(dest, srcFileSet);
170  }
171  continue;
172  }
173 
174  // Replace each FilesSet read from the standard rules set XML files that has a newer version
175  // number than the corresponding FilesSet in the Map with the updated FilesSet.
176  if (destFileSet.getVersionNumber() >= srcEntry.getValue().getVersionNumber()) {
177  continue;
178  }
179  }
180 
181  dest.put(srcEntry.getKey(), srcEntry.getValue());
182  }
183  }
184 
193  private static void addCustomFile(Map<String, FilesSet> dest, FilesSet srcFilesSet) {
194  if (srcFilesSet.isStandardSet()) {
195  LOGGER.log(Level.SEVERE, "An attempt to create a custom file that was a standard set.");
196  return;
197  }
198 
199  FilesSet srcToAdd = srcFilesSet;
200 
201  do {
202  srcToAdd = getAsCustomFileSet(srcToAdd);
203  } while (dest.containsKey(srcToAdd.getName()));
204 
205  dest.put(srcToAdd.getName(), srcToAdd);
206  }
207 
216  @Messages({
217  "# {0} - filesSetName",
218  "StandardInterestingFileSetsLoader.customSuffixed={0} (Custom)"
219  })
220  static FilesSet getAsCustomFileSet(FilesSet srcFilesSet) {
221  String customKey = Bundle.StandardInterestingFileSetsLoader_customSuffixed(srcFilesSet.getName());
222  return new FilesSet(
223  customKey,
224  srcFilesSet.getDescription(),
225  srcFilesSet.ignoresKnownFiles(),
226  srcFilesSet.ingoresUnallocatedSpace(),
227  srcFilesSet.getRules(),
228  false,
229  srcFilesSet.getVersionNumber()
230  );
231  }
232 }
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-2020 Basis Technology. Generated on: Mon Jul 6 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.