Autopsy  4.15.0
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 2019 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 import org.sleuthkit.datamodel.CommunicationsUtils;
27 import org.sleuthkit.datamodel.TskCoreException;
28 
33 final public class CorrelationAttributeNormalizer {
34 
35  //common seperators that may be removed for normalizing
36  private static final String SEPERATORS_REGEX = "[\\s-:]";
37 
47  public static String normalize(CorrelationAttributeInstance.Type attributeType, String data) throws CorrelationAttributeNormalizationException {
48 
49  if (attributeType == null) {
50  throw new CorrelationAttributeNormalizationException("Attribute type was null.");
51  }
52  if (data == null) {
53  throw new CorrelationAttributeNormalizationException("Correlation value was null.");
54  }
55 
56  String trimmedData = data.trim();
57 
58  switch (attributeType.getId()) {
60  return normalizeMd5(trimmedData);
62  return normalizeDomain(trimmedData);
64  return normalizeEmail(trimmedData);
66  return normalizePhone(trimmedData);
68  return normalizeUsbId(trimmedData);
70  return verifySsid(trimmedData);
72  return normalizeMac(trimmedData);
74  return normalizeImei(trimmedData);
76  return normalizeImsi(trimmedData);
78  return normalizeIccid(trimmedData);
79 
80  default:
81  try {
82  // If the atttribute is not one of the above
83  // but is one of the other default correlation types, then let the data go as is
85  for (CorrelationAttributeInstance.Type defaultCorrelationType : defaultCorrelationTypes) {
86  if (defaultCorrelationType.getId() == attributeType.getId()) {
87  return trimmedData;
88  }
89  }
90  final String errorMessage = String.format(
91  "Validator function not found for attribute type: %s",
92  attributeType.getDisplayName());
93  throw new CorrelationAttributeNormalizationException(errorMessage);
94  } catch (CentralRepoException ex) {
95  throw new CorrelationAttributeNormalizationException("Failed to get default correlation types.", ex);
96  }
97  }
98  }
99 
109  public static String normalize(int attributeTypeId, String data) throws CorrelationAttributeNormalizationException {
110  try {
112  Optional<CorrelationAttributeInstance.Type> typeOption = defaultTypes.stream().filter(attributeType -> attributeType.getId() == attributeTypeId).findAny();
113 
114  if (typeOption.isPresent()) {
115  CorrelationAttributeInstance.Type type = typeOption.get();
116  return CorrelationAttributeNormalizer.normalize(type, data);
117  } else {
118  throw new CorrelationAttributeNormalizationException(String.format("Given attributeTypeId did not correspond to any known Attribute: %s", attributeTypeId));
119  }
120  } catch (CentralRepoException ex) {
122  }
123  }
124 
128  private static String normalizeMd5(String data) throws CorrelationAttributeNormalizationException {
129  final String validMd5Regex = "^[a-f0-9]{32}$";
130  final String dataLowered = data.toLowerCase();
131  if (dataLowered.matches(validMd5Regex)) {
132  return dataLowered;
133  } else {
134  throw new CorrelationAttributeNormalizationException(String.format("Data purporting to be an MD5 was found not to comform to expected format: %s", data));
135  }
136  }
137 
142  private static String normalizeDomain(String data) throws CorrelationAttributeNormalizationException {
143  DomainValidator validator = DomainValidator.getInstance(true);
144  if (validator.isValid(data)) {
145  return data.toLowerCase();
146  } else {
147  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])$";
148  if (data.matches(validIpAddressRegex)) {
149  return data;
150  } else {
151  throw new CorrelationAttributeNormalizationException(String.format("Data was expected to be a valid domain: %s", data));
152  }
153  }
154  }
155 
159  private static String normalizeEmail(String data) throws CorrelationAttributeNormalizationException {
160  try {
161  return CommunicationsUtils.normalizeEmailAddress(data);
162  }
163  catch(TskCoreException ex) {
164  throw new CorrelationAttributeNormalizationException(String.format("Data was expected to be a valid email address: %s", data), ex);
165  }
166  }
167 
171  private static String normalizePhone(String data) throws CorrelationAttributeNormalizationException {
172  try {
173  return CommunicationsUtils.normalizePhoneNum(data);
174  }
175  catch(TskCoreException ex) {
176  throw new CorrelationAttributeNormalizationException(String.format("Data was expected to be a valid phone number: %s", data));
177  }
178  }
179 
183  private static String normalizeUsbId(String data) throws CorrelationAttributeNormalizationException {
184  //TODO replace with correct usb id validation at a later date
185  return data;
186  }
187 
201  private static String verifySsid(String data) throws CorrelationAttributeNormalizationException {
202  if (data.length() <= 32) {
203  return data;
204  } else {
205  throw new CorrelationAttributeNormalizationException("Name provided was longer than the maximum valid SSID (32 characters). Name: " + data);
206  }
207  }
208 
231  private static String normalizeIccid(String data) throws CorrelationAttributeNormalizationException {
232  final String validIccidRegex = "^89[f0-9]{17,22}$";
233  final String iccidWithoutSeperators = data.toLowerCase().replaceAll(SEPERATORS_REGEX, "");
234  if (iccidWithoutSeperators.matches(validIccidRegex)) {
235  return iccidWithoutSeperators;
236  } else {
237  throw new CorrelationAttributeNormalizationException("Data provided was not a valid ICCID. : " + data);
238  }
239  }
240 
258  private static String normalizeImsi(String data) throws CorrelationAttributeNormalizationException {
259  final String validImsiRegex = "^[0-9]{14,15}$";
260  final String imsiWithoutSeperators = data.replaceAll(SEPERATORS_REGEX, "");
261  if (imsiWithoutSeperators.matches(validImsiRegex)) {
262  return imsiWithoutSeperators;
263  } else {
264  throw new CorrelationAttributeNormalizationException("Data provided was not a valid Imsi. : " + data);
265  }
266  }
267 
282  private static String normalizeMac(String data) throws CorrelationAttributeNormalizationException {
283  final String validMacRegex = "^([a-f0-9]{12}|[a-f0-9]{16})$";
284  final String macWithoutSeperators = data.toLowerCase().replaceAll(SEPERATORS_REGEX, "");
285  if (macWithoutSeperators.matches(validMacRegex)) {
286  return macWithoutSeperators;
287  } else {
288  throw new CorrelationAttributeNormalizationException("Data provided was not a valid Imsi. : " + data);
289  }
290  }
291 
311  private static String normalizeImei(String data) throws CorrelationAttributeNormalizationException {
312  final String validImeiRegex = "^[0-9]{14,16}$";
313  final String imeiWithoutSeperators = data.replaceAll(SEPERATORS_REGEX, "");
314  if (imeiWithoutSeperators.matches(validImeiRegex)) {
315  return imeiWithoutSeperators;
316  } else {
317  throw new CorrelationAttributeNormalizationException("Data provided was not a valid Imsi. : " + data);
318  }
319  }
320 
325  //Empty constructor
326  }
327 }
static String normalize(CorrelationAttributeInstance.Type attributeType, String data)

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.