Autopsy  4.8.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 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 
30 final public class CorrelationAttributeNormalizer {
31 
36 
46  public static String normalize(CorrelationAttributeInstance.Type attributeType, String data) throws CorrelationAttributeNormalizationException {
47 
48  if(attributeType == null){
49  throw new CorrelationAttributeNormalizationException("Attribute type was null.");
50  }
51  if(data == null){
52  throw new CorrelationAttributeNormalizationException("Data was null.");
53  }
54 
55  switch(attributeType.getId()){
57  return normalizeMd5(data);
59  return normalizeDomain(data);
61  return normalizeEmail(data);
63  return normalizePhone(data);
65  return normalizeUsbId(data);
66  default:
67  final String errorMessage = String.format(
68  "Validator function not found for attribute type: %s",
69  attributeType.getDisplayName());
70  throw new CorrelationAttributeNormalizationException(errorMessage);
71  }
72  }
73 
83  public static String normalize(int attributeTypeId, String data) throws CorrelationAttributeNormalizationException {
84  try {
86  Optional<CorrelationAttributeInstance.Type> typeOption = defaultTypes.stream().filter(attributeType -> attributeType.getId() == attributeTypeId).findAny();
87 
88  if(typeOption.isPresent()){
89  CorrelationAttributeInstance.Type type = typeOption.get();
90  return CorrelationAttributeNormalizer.normalize(type, data);
91  } else {
92  throw new CorrelationAttributeNormalizationException(String.format("Given attributeTypeId did not correspond to any known Attribute: %s", attributeTypeId));
93  }
94  } catch (EamDbException ex) {
96  }
97  }
98 
102  private static String normalizeMd5(String data) throws CorrelationAttributeNormalizationException {
103  final String validMd5Regex = "^[a-f0-9]{32}$";
104  final String dataLowered = data.toLowerCase();
105  if(dataLowered.matches(validMd5Regex)){
106  return dataLowered;
107  } else {
108  throw new CorrelationAttributeNormalizationException(String.format("Data purporting to be an MD5 was found not to comform to expected format: %s", data));
109  }
110  }
111 
115  private static String normalizeDomain(String data) throws CorrelationAttributeNormalizationException {
116  DomainValidator validator = DomainValidator.getInstance(true);
117  if(validator.isValid(data)){
118  return data.toLowerCase();
119  } else {
120  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])$";
121  if(data.matches(validIpAddressRegex)){
122  return data;
123  } else {
124  throw new CorrelationAttributeNormalizationException(String.format("Data was expected to be a valid domain: %s", data));
125  }
126  }
127  }
128 
132  private static String normalizeEmail(String data) throws CorrelationAttributeNormalizationException {
133  EmailValidator validator = EmailValidator.getInstance(true, true);
134  if(validator.isValid(data)){
135  return data.toLowerCase();
136  } else {
137  throw new CorrelationAttributeNormalizationException(String.format("Data was expected to be a valid email address: %s", data));
138  }
139  }
140 
144  private static String normalizePhone(String data) throws CorrelationAttributeNormalizationException {
145  if(data.matches("\\+?[0-9()\\-\\s]+")){
146  String phoneNumber = data.replaceAll("[^0-9\\+]", "");
147  return phoneNumber;
148  } else {
149  throw new CorrelationAttributeNormalizationException(String.format("Data was expected to be a valid phone number: %s", data));
150  }
151  }
152 
156  private static String normalizeUsbId(String data) throws CorrelationAttributeNormalizationException {
157  //TODO replace with correct usb id validation at a later date
158  return data;
159  }
160 }
static String normalize(CorrelationAttributeInstance.Type attributeType, String data)

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