Autopsy  4.9.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
CorrelationAttributeNormalizer.java
Go to the documentation of this file.
1 /*
2  *
3  * Autopsy Forensic Browser
4  *
5  * Copyright 2018 Basis Technology Corp.
6  * Contact: carrier <at> sleuthkit <dot> org
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 package org.sleuthkit.autopsy.centralrepository.datamodel;
21 
22 import java.util.List;
23 import java.util.Optional;
24 import org.apache.commons.validator.routines.DomainValidator;
25 import org.apache.commons.validator.routines.EmailValidator;
26 
31 final public class CorrelationAttributeNormalizer {
32 
33  //common seperators that may be removed for normalizing
34  private static final String SEPERATORS_REGEX = "[\\s-:]";
35 
40  }
41 
51  public static String normalize(CorrelationAttributeInstance.Type attributeType, String data) throws CorrelationAttributeNormalizationException {
52 
53  if (attributeType == null) {
54  throw new CorrelationAttributeNormalizationException("Attribute type was null.");
55  }
56  if (data == null) {
57  throw new CorrelationAttributeNormalizationException("Data was null.");
58  }
59 
60  String trimmedData = data.trim();
61 
62  switch (attributeType.getId()) {
64  return normalizeMd5(trimmedData);
66  return normalizeDomain(trimmedData);
68  return normalizeEmail(trimmedData);
70  return normalizePhone(trimmedData);
72  return normalizeUsbId(trimmedData);
74  return verifySsid(trimmedData);
76  return normalizeMac(trimmedData);
78  return normalizeImei(trimmedData);
80  return normalizeImsi(trimmedData);
82  return normalizeIccid(trimmedData);
83 
84  default:
85  final String errorMessage = String.format(
86  "Validator function not found for attribute type: %s",
87  attributeType.getDisplayName());
88  throw new CorrelationAttributeNormalizationException(errorMessage);
89  }
90  }
91 
101  public static String normalize(int attributeTypeId, String data) throws CorrelationAttributeNormalizationException {
102  try {
104  Optional<CorrelationAttributeInstance.Type> typeOption = defaultTypes.stream().filter(attributeType -> attributeType.getId() == attributeTypeId).findAny();
105 
106  if (typeOption.isPresent()) {
107  CorrelationAttributeInstance.Type type = typeOption.get();
108  return CorrelationAttributeNormalizer.normalize(type, data);
109  } else {
110  throw new CorrelationAttributeNormalizationException(String.format("Given attributeTypeId did not correspond to any known Attribute: %s", attributeTypeId));
111  }
112  } catch (EamDbException ex) {
114  }
115  }
116 
120  private static String normalizeMd5(String data) throws CorrelationAttributeNormalizationException {
121  final String validMd5Regex = "^[a-f0-9]{32}$";
122  final String dataLowered = data.toLowerCase();
123  if (dataLowered.matches(validMd5Regex)) {
124  return dataLowered;
125  } else {
126  throw new CorrelationAttributeNormalizationException(String.format("Data purporting to be an MD5 was found not to comform to expected format: %s", data));
127  }
128  }
129 
134  private static String normalizeDomain(String data) throws CorrelationAttributeNormalizationException {
135  DomainValidator validator = DomainValidator.getInstance(true);
136  if (validator.isValid(data)) {
137  return data.toLowerCase();
138  } else {
139  final String validIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
140  if (data.matches(validIpAddressRegex)) {
141  return data;
142  } else {
143  throw new CorrelationAttributeNormalizationException(String.format("Data was expected to be a valid domain: %s", data));
144  }
145  }
146  }
147 
152  private static String normalizeEmail(String data) throws CorrelationAttributeNormalizationException {
153  EmailValidator validator = EmailValidator.getInstance(true, true);
154  if (validator.isValid(data)) {
155  return data.toLowerCase();
156  } else {
157  throw new CorrelationAttributeNormalizationException(String.format("Data was expected to be a valid email address: %s", data));
158  }
159  }
160 
164  private static String normalizePhone(String data) throws CorrelationAttributeNormalizationException {
165  if (data.matches("\\+?[0-9()\\-\\s]+")) {
166  String phoneNumber = data.replaceAll("[^0-9\\+]", "");
167  return phoneNumber;
168  } else {
169  throw new CorrelationAttributeNormalizationException(String.format("Data was expected to be a valid phone number: %s", data));
170  }
171  }
172 
176  private static String normalizeUsbId(String data) throws CorrelationAttributeNormalizationException {
177  //TODO replace with correct usb id validation at a later date
178  return data;
179  }
180 
194  private static String verifySsid(String data) throws CorrelationAttributeNormalizationException {
195  if (data.length() <= 32) {
196  return data;
197  } else {
198  throw new CorrelationAttributeNormalizationException("Name provided was longer than the maximum valid SSID (32 characters). Name: " + data);
199  }
200  }
201 
224  private static String normalizeIccid(String data) throws CorrelationAttributeNormalizationException {
225  final String validIccidRegex = "^89[f0-9]{17,22}$";
226  final String iccidWithoutSeperators = data.toLowerCase().replaceAll(SEPERATORS_REGEX, "");
227  if (iccidWithoutSeperators.matches(validIccidRegex)) {
228  return iccidWithoutSeperators;
229  } else {
230  throw new CorrelationAttributeNormalizationException("Data provided was not a valid ICCID. : " + data);
231  }
232  }
233 
251  private static String normalizeImsi(String data) throws CorrelationAttributeNormalizationException {
252  final String validImsiRegex = "^[0-9]{14,15}$";
253  final String imsiWithoutSeperators = data.replaceAll(SEPERATORS_REGEX, "");
254  if (imsiWithoutSeperators.matches(validImsiRegex)) {
255  return imsiWithoutSeperators;
256  } else {
257  throw new CorrelationAttributeNormalizationException("Data provided was not a valid Imsi. : " + data);
258  }
259  }
260 
275  private static String normalizeMac(String data) throws CorrelationAttributeNormalizationException {
276  final String validMacRegex = "^([a-f0-9]{12}|[a-f0-9]{16})$";
277  final String macWithoutSeperators = data.toLowerCase().replaceAll(SEPERATORS_REGEX, "");
278  if (macWithoutSeperators.matches(validMacRegex)) {
279  return macWithoutSeperators;
280  } else {
281  throw new CorrelationAttributeNormalizationException("Data provided was not a valid Imsi. : " + data);
282  }
283  }
284 
304  private static String normalizeImei(String data) throws CorrelationAttributeNormalizationException {
305  final String validImeiRegex = "^[0-9]{14,16}$";
306  final String imeiWithoutSeperators = data.replaceAll(SEPERATORS_REGEX, "");
307  if (imeiWithoutSeperators.matches(validImeiRegex)) {
308  return imeiWithoutSeperators;
309  } else {
310  throw new CorrelationAttributeNormalizationException("Data provided was not a valid Imsi. : " + data);
311  }
312  }
313 }
static String normalize(CorrelationAttributeInstance.Type attributeType, String data)

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