19 package org.sleuthkit.autopsy.modules.interestingitems;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.Serializable;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.HashMap;
31 import java.util.List;
33 import java.util.logging.Level;
34 import java.util.regex.Pattern;
35 import java.util.regex.PatternSyntaxException;
36 import javax.xml.parsers.DocumentBuilder;
37 import javax.xml.parsers.DocumentBuilderFactory;
38 import javax.xml.parsers.ParserConfigurationException;
39 import org.apache.commons.lang.StringUtils;
40 import org.openide.util.io.NbObjectInputStream;
41 import org.openide.util.io.NbObjectOutputStream;
52 import org.w3c.dom.Document;
53 import org.w3c.dom.Element;
54 import org.w3c.dom.NodeList;
55 import java.util.Comparator;
56 import java.util.function.Function;
57 import java.util.stream.Collectors;
59 class InterestingItemsFilesSetSettings
implements Serializable {
61 private static final long serialVersionUID = 1L;
64 private static final String FILE_SETS_ROOT_TAG =
"INTERESTING_FILE_SETS";
65 private static final String DESC_ATTR =
"description";
66 private static final String IGNORE_KNOWN_FILES_ATTR =
"ignoreKnown";
67 private static final String PATH_REGEX_ATTR =
"pathRegex";
68 private static final String TYPE_FILTER_VALUE_ALL =
"all";
69 private static final String TYPE_FILTER_VALUE_FILES_AND_DIRS =
"files_and_dirs";
70 private static final String IGNORE_UNALLOCATED_SPACE =
"ingoreUnallocated";
71 private static final String PATH_FILTER_ATTR =
"pathFilter";
72 private static final String TYPE_FILTER_VALUE_DIRS =
"dir";
73 private static final String REGEX_ATTR =
"regex";
74 private static final List<String> illegalFileNameChars = FilesSetsManager.getIllegalFileNameChars();
75 private static final String FILE_SET_TAG =
"INTERESTING_FILE_SET";
76 private static final String NAME_RULE_TAG =
"NAME";
77 private static final String NAME_ATTR =
"name";
78 private static final String DAYS_INCLUDED_ATTR =
"daysIncluded";
79 private static final String MIME_ATTR =
"mimeType";
80 private static final String FS_COMPARATOR_ATTR =
"comparatorSymbol";
81 private static final String FS_SIZE_ATTR =
"sizeValue";
82 private static final String FS_UNITS_ATTR =
"sizeUnits";
83 private static final String TYPE_FILTER_VALUE_FILES =
"file";
84 private static final String XML_ENCODING =
"UTF-8";
85 private static final Logger logger = Logger.getLogger(InterestingItemsFilesSetSettings.class.getName());
86 private static final String TYPE_FILTER_ATTR =
"typeFilter";
87 private static final String EXTENSION_RULE_TAG =
"EXTENSION";
88 private static final String STANDARD_SET =
"standardSet";
89 private static final String VERSION_NUMBER =
"versionNumber";
91 private Map<String, FilesSet> filesSets;
93 InterestingItemsFilesSetSettings(Map<String, FilesSet> filesSets) {
94 this.filesSets = filesSets;
100 Map<String, FilesSet> getFilesSets() {
111 private static String readRuleName(Element elem) {
113 String ruleName = elem.getAttribute(NAME_ATTR);
125 private static Map<String, FilesSet> readSerializedDefinitions(String serialFileName)
throws FilesSetsManager.FilesSetsManagerException {
126 Path filePath = Paths.get(PlatformUtil.getUserConfigDirectory(), serialFileName);
127 File fileSetFile = filePath.toFile();
128 String filePathStr = filePath.toString();
129 if (fileSetFile.exists()) {
131 try (
final NbObjectInputStream in =
new NbObjectInputStream(
new FileInputStream(filePathStr))) {
132 InterestingItemsFilesSetSettings filesSetsSettings = (InterestingItemsFilesSetSettings) in.readObject();
133 return filesSetsSettings.getFilesSets();
135 }
catch (IOException | ClassNotFoundException ex) {
136 throw new FilesSetsManager.FilesSetsManagerException(String.format(
"Failed to read settings from %s", filePathStr), ex);
139 return new HashMap<>();
154 private static ParentPathCondition readPathCondition(Element ruleElement)
throws FilesSetsManager.FilesSetsManagerException {
157 ParentPathCondition pathCondition = null;
158 if (!ruleElement.getAttribute(PATH_FILTER_ATTR).isEmpty() || !ruleElement.getAttribute(PATH_REGEX_ATTR).isEmpty()) {
159 String path = ruleElement.getAttribute(PATH_FILTER_ATTR);
160 String pathRegex = ruleElement.getAttribute(PATH_REGEX_ATTR);
161 if (!pathRegex.isEmpty() && path.isEmpty()) {
163 Pattern pattern = Pattern.compile(pathRegex);
164 pathCondition =
new ParentPathCondition(pattern);
165 }
catch (PatternSyntaxException ex) {
166 logger.log(Level.SEVERE,
"Error compiling " + PATH_REGEX_ATTR +
" regex, ignoring malformed path condition definition", ex);
167 throw new FilesSetsManager.FilesSetsManagerException(String.format(
"error compiling %s regex", PATH_REGEX_ATTR), ex);
169 }
else if (!path.isEmpty() && pathRegex.isEmpty()) {
170 pathCondition =
new ParentPathCondition(path);
172 if (pathCondition == null) {
174 throw new FilesSetsManager.FilesSetsManagerException(String.format(
"Error creating path condition for rule %s", readRuleName(ruleElement)));
177 return pathCondition;
191 private static DateCondition readDateCondition(Element ruleElement)
throws FilesSetsManager.FilesSetsManagerException {
194 DateCondition dateCondition = null;
195 if (!ruleElement.getAttribute(DAYS_INCLUDED_ATTR).isEmpty()) {
196 String daysIncluded = ruleElement.getAttribute(DAYS_INCLUDED_ATTR);
197 if (!daysIncluded.isEmpty()) {
199 dateCondition =
new DateCondition(Integer.parseInt(daysIncluded));
200 }
catch (NumberFormatException ex) {
201 logger.log(Level.SEVERE,
"Error creating condition for " + daysIncluded +
", ignoring malformed date condition definition", ex);
202 throw new FilesSetsManager.FilesSetsManagerException(String.format(
"error compiling %s regex", DAYS_INCLUDED_ATTR), ex);
206 return dateCondition;
216 private static Pattern compileRegex(String regex) {
218 return Pattern.compile(regex);
219 }
catch (PatternSyntaxException ex) {
220 logger.log(Level.SEVERE,
"Error compiling rule regex: " + ex.getMessage(), ex);
237 private static FilesSet.Rule readRule(Element elem)
throws FilesSetsManager.FilesSetsManagerException {
238 String ruleName = readRuleName(elem);
239 FileNameCondition nameCondition = readNameCondition(elem);
240 MetaTypeCondition metaCondition = readMetaTypeCondition(elem);
241 ParentPathCondition pathCondition = readPathCondition(elem);
242 MimeTypeCondition mimeCondition = readMimeCondition(elem);
243 FileSizeCondition sizeCondition = readSizeCondition(elem);
244 DateCondition dateCondition = readDateCondition(elem);
245 if (metaCondition == null || (nameCondition == null && pathCondition == null && mimeCondition == null && sizeCondition == null && dateCondition == null)) {
246 logger.log(Level.WARNING,
"Error Reading Rule, " + ruleName +
" was either missing a meta condition or contained only a meta condition. No rule was imported.");
247 throw new FilesSetsManager.FilesSetsManagerException(String.format(
"Invalid Rule in FilesSet xml, missing necessary conditions for %s", ruleName));
249 return new FilesSet.Rule(ruleName, nameCondition, metaCondition, pathCondition, mimeCondition, sizeCondition, dateCondition);
263 private static FileNameCondition readNameCondition(Element elem)
throws FilesSetsManager.FilesSetsManagerException {
264 FileNameCondition nameCondition = null;
265 String content = elem.getTextContent();
266 String regex = elem.getAttribute(REGEX_ATTR);
267 if (content != null && !content.isEmpty()) {
268 if ((!regex.isEmpty() && regex.equalsIgnoreCase(
"true")) || content.contains(
"*")) {
269 Pattern pattern = compileRegex(content);
270 if (pattern != null) {
271 if (elem.getTagName().equals(NAME_RULE_TAG)) {
272 nameCondition =
new FilesSet.Rule.FullNameCondition(pattern);
273 }
else if (elem.getTagName().equals(EXTENSION_RULE_TAG)) {
274 nameCondition =
new FilesSet.Rule.ExtensionCondition(pattern);
276 throw new FilesSetsManager.FilesSetsManagerException(String.format(
"Name condition has invalid tag name of %s for rule %s", elem.getTagName(), readRuleName(elem)));
279 logger.log(Level.SEVERE,
"Error compiling " + elem.getTagName() +
" regex, ignoring malformed '{0}' rule definition", readRuleName(elem));
280 throw new FilesSetsManager.FilesSetsManagerException(String.format(
"error compiling %s regex in rule %s", REGEX_ATTR, readRuleName(elem)));
283 for (String illegalChar : illegalFileNameChars) {
284 if (content.contains(illegalChar)) {
285 logger.log(Level.SEVERE, elem.getTagName() +
" content has illegal chars, ignoring malformed '{0}' rule definition",
new Object[]{elem.getTagName(), readRuleName(elem)});
286 throw new FilesSetsManager.FilesSetsManagerException(String.format(
"File name has illegal character of %s in rule %s", illegalChar, readRuleName(elem)));
289 if (elem.getTagName().equals(NAME_RULE_TAG)) {
290 nameCondition =
new FilesSet.Rule.FullNameCondition(content);
291 }
else if (elem.getTagName().equals(EXTENSION_RULE_TAG)) {
292 nameCondition =
new FilesSet.Rule.ExtensionCondition(Arrays.asList(content.split(
",")));
296 return nameCondition;
307 private static MimeTypeCondition readMimeCondition(Element elem) {
308 MimeTypeCondition mimeCondition = null;
309 if (!elem.getAttribute(MIME_ATTR).isEmpty()) {
310 mimeCondition =
new MimeTypeCondition(elem.getAttribute(MIME_ATTR));
315 return mimeCondition;
329 private static FileSizeCondition readSizeCondition(Element elem)
throws FilesSetsManager.FilesSetsManagerException {
330 FileSizeCondition sizeCondition = null;
331 if (!elem.getAttribute(FS_COMPARATOR_ATTR).isEmpty() && !elem.getAttribute(FS_SIZE_ATTR).isEmpty() && !elem.getAttribute(FS_UNITS_ATTR).isEmpty()) {
333 FileSizeCondition.COMPARATOR comparator = FileSizeCondition.COMPARATOR.fromSymbol(elem.getAttribute(FS_COMPARATOR_ATTR));
334 FileSizeCondition.SIZE_UNIT sizeUnit = FileSizeCondition.SIZE_UNIT.fromName(elem.getAttribute(FS_UNITS_ATTR));
335 int size = Integer.parseInt(elem.getAttribute(FS_SIZE_ATTR));
336 sizeCondition =
new FileSizeCondition(comparator, sizeUnit, size);
337 }
catch (NumberFormatException nfEx) {
338 logger.log(Level.SEVERE,
"Value in file size attribute was not an integer, unable to create FileSizeCondition for rule: " + readRuleName(elem), nfEx);
339 throw new FilesSetsManager.FilesSetsManagerException(String.format(
"Non integer size in FilesSet XML for rule %s", readRuleName(elem)), nfEx);
340 }
catch (IllegalArgumentException iaEx) {
341 logger.log(Level.SEVERE,
"Invalid Comparator symbol or Size Unit set in FilesSet xml, unable to create FileSizeCondition for rule: " + readRuleName(elem), iaEx);
342 throw new FilesSetsManager.FilesSetsManagerException(String.format(
"Invalid Comparator or Size unit in FilesSet XML for rule %s", readRuleName(elem)), iaEx);
345 else if (!elem.getAttribute(FS_COMPARATOR_ATTR).isEmpty() || !elem.getAttribute(FS_SIZE_ATTR).isEmpty() || !elem.getAttribute(FS_UNITS_ATTR).isEmpty()) {
346 logger.log(Level.SEVERE,
"Invalid Comparator symbol or Size Unit set in FilesSet xml, unable to create FileSizeCondition for rule: " + readRuleName(elem));
347 throw new FilesSetsManager.FilesSetsManagerException(String.format(
"XML malformed missing at least one fileSize attribute for rule %s", readRuleName(elem)));
349 return sizeCondition;
362 private static void readFilesSet(Element setElem, Map<String, FilesSet> filesSets, String filePath)
throws FilesSetsManager.FilesSetsManagerException {
364 String setName = setElem.getAttribute(NAME_ATTR);
365 if (setName.isEmpty()) {
366 logger.log(Level.SEVERE,
"Found {0} element without required {1} attribute, ignoring malformed file set definition in FilesSet definition file at {2}",
new Object[]{FILE_SET_TAG, NAME_ATTR, filePath});
369 if (filesSets.containsKey(setName)) {
370 logger.log(Level.SEVERE,
"Found duplicate definition of set named {0} in FilesSet definition file at {1}, discarding duplicate set",
new Object[]{setName, filePath});
374 String description = setElem.getAttribute(DESC_ATTR);
377 String ignoreKnown = setElem.getAttribute(IGNORE_KNOWN_FILES_ATTR);
378 boolean ignoreKnownFiles =
false;
379 if (!ignoreKnown.isEmpty()) {
380 ignoreKnownFiles = Boolean.parseBoolean(ignoreKnown);
384 String ignoreUnallocated = setElem.getAttribute(IGNORE_UNALLOCATED_SPACE);
385 boolean ignoreUnallocatedSpace =
false;
386 if (!ignoreUnallocated.isEmpty()) {
387 ignoreUnallocatedSpace = Boolean.parseBoolean(ignoreUnallocated);
390 String isStandardSetString = setElem.getAttribute(STANDARD_SET);
391 boolean isStandardSet =
false;
392 if (StringUtils.isNotBlank(isStandardSetString)) {
393 isStandardSet = Boolean.parseBoolean(isStandardSetString);
396 String versionNumberString = setElem.getAttribute(VERSION_NUMBER);
397 int versionNumber = 0;
398 if (StringUtils.isNotBlank(versionNumberString)) {
400 versionNumber = Integer.parseInt(versionNumberString);
401 }
catch (NumberFormatException ex) {
402 logger.log(Level.WARNING,
403 String.format(
"Unable to parse version number for files set named: %s with provided input: '%s'", setName, versionNumberString),
409 Map<String, FilesSet.Rule> rules =
new HashMap<>();
410 NodeList allRuleElems = setElem.getChildNodes();
411 for (
int j = 0; j < allRuleElems.getLength(); ++j) {
412 if (allRuleElems.item(j) instanceof Element) {
413 Element elem = (Element) allRuleElems.item(j);
414 FilesSet.Rule rule = readRule(elem);
416 if (!rules.containsKey(rule.getUuid())) {
417 rules.put(rule.getUuid(), rule);
419 logger.log(Level.SEVERE,
"Found duplicate rule {0} for set named {1} in FilesSet definition file at {2}, discarding malformed set",
new Object[]{rule.getUuid(), setName, filePath});
423 logger.log(Level.SEVERE,
"Found malformed rule for set named {0} in FilesSet definition file at {1}, discarding malformed set",
new Object[]{setName, filePath});
431 FilesSet set =
new FilesSet(setName, description, ignoreKnownFiles, ignoreUnallocatedSpace, rules, isStandardSet, versionNumber);
432 filesSets.put(set.getName(), set);
450 static Map<String, FilesSet> readDefinitionsFile(String fileName, String legacyFileName)
throws FilesSetsManager.FilesSetsManagerException {
451 Map<String, FilesSet> filesSets = readSerializedDefinitions(fileName);
452 if (!filesSets.isEmpty()) {
456 if (!legacyFileName.isEmpty()) {
457 return readDefinitionsXML(Paths.get(PlatformUtil.getUserConfigDirectory(), legacyFileName).toFile());
475 static Map<String, FilesSet> readDefinitionsXML(File xmlFile)
throws FilesSetsManager.FilesSetsManagerException {
476 if (!xmlFile.exists()) {
477 return new HashMap<>();
480 if (!xmlFile.canRead()) {
481 logger.log(Level.SEVERE,
"FilesSet definition file at {0} exists, but cannot be read", xmlFile.getPath());
482 return new HashMap<>();
485 Document doc = XMLUtil.loadDoc(InterestingItemsFilesSetSettings.class, xmlFile.getPath());
486 return readDefinitionsXML(doc, xmlFile.getPath());
502 static Map<String, FilesSet> readDefinitionsXML(Document doc, String resourceName)
throws FilesSetsManager.FilesSetsManagerException {
504 Map<String, FilesSet> filesSets =
new HashMap<>();
507 logger.log(Level.SEVERE,
"FilesSet definition file at {0}", resourceName);
511 Element root = doc.getDocumentElement();
513 logger.log(Level.SEVERE,
"Failed to get root {0} element tag of FilesSet definition file at {1}",
514 new Object[]{FILE_SETS_ROOT_TAG, resourceName});
518 NodeList setElems = root.getElementsByTagName(FILE_SET_TAG);
519 for (
int i = 0; i < setElems.getLength(); ++i) {
520 readFilesSet((Element) setElems.item(i), filesSets, resourceName);
535 static boolean writeDefinitionsFile(String fileName, Map<String, FilesSet> interestingFilesSets)
throws FilesSetsManager.FilesSetsManagerException {
536 try (
final NbObjectOutputStream out =
new NbObjectOutputStream(
new FileOutputStream(Paths.get(PlatformUtil.getUserConfigDirectory(), fileName).toString()))) {
537 out.writeObject(
new InterestingItemsFilesSetSettings(interestingFilesSets));
538 }
catch (IOException ex) {
539 throw new FilesSetsManager.FilesSetsManagerException(String.format(
"Failed to write settings to %s", fileName), ex);
551 private static <T> List<T> sortOnField(Collection<T> itemsToSort,
final Function<T, String> getName) {
552 Comparator<T> comparator = (a,b) -> {
553 String aName = getName.apply(a);
554 String bName = getName.apply(b);
563 return aName.compareToIgnoreCase(bName);
566 return itemsToSort.stream()
568 .collect(Collectors.toList());
581 static boolean exportXmlDefinitionsFile(File xmlFile, List<FilesSet> interestingFilesSets) {
582 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
585 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
586 Document doc = docBuilder.newDocument();
587 Element rootElement = doc.createElement(FILE_SETS_ROOT_TAG);
588 doc.appendChild(rootElement);
591 List<FilesSet> sortedFilesSets = sortOnField(
592 interestingFilesSets,
593 filesSet -> filesSet == null ? null : filesSet.getName());
595 for (FilesSet set : sortedFilesSets) {
597 Element setElement = doc.createElement(FILE_SET_TAG);
598 setElement.setAttribute(NAME_ATTR, set.getName());
599 setElement.setAttribute(DESC_ATTR, set.getDescription());
600 setElement.setAttribute(IGNORE_KNOWN_FILES_ATTR, Boolean.toString(set.ignoresKnownFiles()));
601 setElement.setAttribute(STANDARD_SET, Boolean.toString(set.isStandardSet()));
602 setElement.setAttribute(VERSION_NUMBER, Integer.toString(set.getVersionNumber()));
606 List<FilesSet.Rule> sortedRules = sortOnField(
607 set.getRules().values(),
608 rule -> rule == null ? null : rule.getName());
610 for (FilesSet.Rule rule : sortedRules) {
615 FileNameCondition nameCondition = rule.getFileNameCondition();
620 if (nameCondition instanceof FilesSet.Rule.FullNameCondition) {
621 ruleElement = doc.createElement(NAME_RULE_TAG);
623 ruleElement = doc.createElement(EXTENSION_RULE_TAG);
626 ruleElement.setAttribute(NAME_ATTR, rule.getName());
627 if (nameCondition != null) {
629 ruleElement.setAttribute(REGEX_ATTR, Boolean.toString(nameCondition.isRegex()));
631 ruleElement.setTextContent(nameCondition.getTextToMatch());
634 MetaTypeCondition typeCondition = rule.getMetaTypeCondition();
635 switch (typeCondition.getMetaType()) {
637 ruleElement.setAttribute(TYPE_FILTER_ATTR, TYPE_FILTER_VALUE_FILES);
640 ruleElement.setAttribute(TYPE_FILTER_ATTR, TYPE_FILTER_VALUE_DIRS);
643 ruleElement.setAttribute(TYPE_FILTER_ATTR, TYPE_FILTER_VALUE_ALL);
647 ParentPathCondition pathCondition = rule.getPathCondition();
648 if (pathCondition != null) {
649 if (pathCondition.isRegex()) {
650 ruleElement.setAttribute(PATH_REGEX_ATTR, pathCondition.getTextToMatch());
652 ruleElement.setAttribute(PATH_FILTER_ATTR, pathCondition.getTextToMatch());
656 MimeTypeCondition mimeCondition = rule.getMimeTypeCondition();
657 if (mimeCondition != null) {
658 ruleElement.setAttribute(MIME_ATTR, mimeCondition.getMimeType());
661 FileSizeCondition sizeCondition = rule.getFileSizeCondition();
662 if (sizeCondition != null) {
663 ruleElement.setAttribute(FS_COMPARATOR_ATTR, sizeCondition.getComparator().getSymbol());
664 ruleElement.setAttribute(FS_SIZE_ATTR, Integer.toString(sizeCondition.getSizeValue()));
665 ruleElement.setAttribute(FS_UNITS_ATTR, sizeCondition.getUnit().getName());
669 DateCondition dateCondition = rule.getDateCondition();
670 if (dateCondition != null) {
671 ruleElement.setAttribute(DAYS_INCLUDED_ATTR, Integer.toString(dateCondition.getDaysIncluded()));
674 setElement.appendChild(ruleElement);
676 rootElement.appendChild(setElement);
680 return XMLUtil.saveDoc(InterestingItemsFilesSetSettings.class, xmlFile.getPath(), XML_ENCODING, doc);
681 }
catch (ParserConfigurationException ex) {
682 logger.log(Level.SEVERE,
"Error writing interesting files definition file to " + xmlFile.getPath(), ex);
698 private static MetaTypeCondition readMetaTypeCondition(Element ruleElement)
throws FilesSetsManager.FilesSetsManagerException {
699 MetaTypeCondition metaCondition = null;
702 if (!ruleElement.getAttribute(TYPE_FILTER_ATTR).isEmpty()) {
703 String conditionAttribute = ruleElement.getAttribute(TYPE_FILTER_ATTR);
704 if (!conditionAttribute.isEmpty()) {
705 switch (conditionAttribute) {
706 case TYPE_FILTER_VALUE_FILES:
707 metaCondition =
new MetaTypeCondition(MetaTypeCondition.Type.FILES);
709 case TYPE_FILTER_VALUE_DIRS:
710 metaCondition =
new MetaTypeCondition(MetaTypeCondition.Type.DIRECTORIES);
712 case TYPE_FILTER_VALUE_ALL:
713 case TYPE_FILTER_VALUE_FILES_AND_DIRS:
714 metaCondition =
new MetaTypeCondition(MetaTypeCondition.Type.ALL);
717 logger.log(Level.SEVERE,
"Found {0} " + TYPE_FILTER_ATTR +
" attribute with unrecognized value ''{0}'', ignoring malformed rule definition", conditionAttribute);
719 throw new FilesSetsManager.FilesSetsManagerException(String.format(
"Malformed XML for Metatype condition, %s, in rule %s", conditionAttribute, readRuleName(ruleElement)));
723 if (metaCondition == null) {
726 metaCondition =
new MetaTypeCondition(MetaTypeCondition.Type.FILES);
728 return metaCondition;