19 package org.sleuthkit.autopsy.thunderbirdparser;
21 import ezvcard.Ezvcard;
23 import ezvcard.parameter.EmailType;
24 import ezvcard.parameter.TelephoneType;
25 import ezvcard.property.Email;
26 import ezvcard.property.Organization;
27 import ezvcard.property.Photo;
28 import ezvcard.property.Telephone;
29 import ezvcard.property.Url;
31 import java.io.FileOutputStream;
32 import java.io.IOException;
33 import java.nio.file.Paths;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collection;
37 import java.util.HashMap;
38 import java.util.List;
40 import java.util.logging.Level;
41 import org.apache.commons.lang3.StringUtils;
42 import org.openide.util.NbBundle;
72 final class VcardParser {
73 private static final String VCARD_HEADER =
"BEGIN:VCARD";
74 private static final long MIN_FILE_SIZE = 22;
76 private static final String PHOTO_TYPE_BMP =
"bmp";
77 private static final String PHOTO_TYPE_GIF =
"gif";
78 private static final String PHOTO_TYPE_JPEG =
"jpeg";
79 private static final String PHOTO_TYPE_PNG =
"png";
80 private static final Map<String, String> photoTypeExtensions;
82 photoTypeExtensions =
new HashMap<>();
83 photoTypeExtensions.put(PHOTO_TYPE_BMP,
".bmp");
84 photoTypeExtensions.put(PHOTO_TYPE_GIF,
".gif");
85 photoTypeExtensions.put(PHOTO_TYPE_JPEG,
".jpg");
86 photoTypeExtensions.put(PHOTO_TYPE_PNG,
".png");
89 private static final Logger logger = Logger.getLogger(VcardParser.class.getName());
91 private final IngestServices services = IngestServices.getInstance();
92 private final FileManager fileManager;
93 private final IngestJobContext context;
94 private final Blackboard blackboard;
95 private final Case currentCase;
96 private final SleuthkitCase tskCase;
101 VcardParser(Case currentCase, IngestJobContext context) {
102 this.context = context;
103 this.currentCase = currentCase;
104 tskCase = currentCase.getSleuthkitCase();
105 blackboard = tskCase.getBlackboard();
106 fileManager = currentCase.getServices().getFileManager();
116 static boolean isVcardFile(Content content) {
118 if (content.getSize() > MIN_FILE_SIZE) {
119 byte[] buffer =
new byte[VCARD_HEADER.length()];
120 int byteRead = content.read(buffer, 0, VCARD_HEADER.length());
122 String header =
new String(buffer);
123 return header.equalsIgnoreCase(VCARD_HEADER);
126 }
catch (TskException ex) {
127 logger.log(Level.WARNING, String.format(
"Exception while detecting if the file '%s' (id=%d) is a vCard file.",
128 content.getName(), content.getId()));
145 void parse(AbstractFile abstractFile)
throws IOException, NoCurrentCaseException {
146 for (VCard vcard: Ezvcard.parse(
new ReadContentInputStream(abstractFile)).all()) {
147 addContactArtifact(vcard, abstractFile);
163 @NbBundle.Messages({
"VcardParser.addContactArtifact.indexError=Failed to index the contact artifact for keyword search."})
164 private BlackboardArtifact addContactArtifact(VCard vcard, AbstractFile abstractFile)
throws NoCurrentCaseException {
165 List<BlackboardAttribute> attributes =
new ArrayList<>();
166 List<AccountFileInstance> accountInstances =
new ArrayList<>();
169 if (vcard.getFormattedName() != null) {
170 name = vcard.getFormattedName().getValue();
172 if (vcard.getStructuredName() != null) {
174 for (String prefix:vcard.getStructuredName().getPrefixes()) {
175 name += prefix +
" ";
177 if (vcard.getStructuredName().getGiven() != null) {
178 name += vcard.getStructuredName().getGiven() +
" ";
180 if (vcard.getStructuredName().getFamily() != null) {
181 name += vcard.getStructuredName().getFamily() +
" ";
183 for (String suffix:vcard.getStructuredName().getSuffixes()) {
184 name += suffix +
" ";
186 if (! vcard.getStructuredName().getAdditionalNames().isEmpty()) {
188 for (String addName:vcard.getStructuredName().getAdditionalNames()) {
189 name += addName +
" ";
195 ThunderbirdMboxFileIngestModule.addArtifactAttribute(name, BlackboardAttribute.ATTRIBUTE_TYPE.TSK_NAME, attributes);
197 for (Telephone telephone : vcard.getTelephoneNumbers()) {
198 addPhoneAttributes(telephone, abstractFile, attributes);
199 addPhoneAccountInstances(telephone, abstractFile, accountInstances);
202 for (Email email : vcard.getEmails()) {
203 addEmailAttributes(email, abstractFile, attributes);
204 addEmailAccountInstances(email, abstractFile, accountInstances);
207 for (Url url : vcard.getUrls()) {
208 ThunderbirdMboxFileIngestModule.addArtifactAttribute(url.getValue(), BlackboardAttribute.ATTRIBUTE_TYPE.TSK_URL, attributes);
211 for (Organization organization : vcard.getOrganizations()) {
212 List<String> values = organization.getValues();
213 if (values.isEmpty() ==
false) {
214 ThunderbirdMboxFileIngestModule.addArtifactAttribute(values.get(0), BlackboardAttribute.ATTRIBUTE_TYPE.TSK_ORGANIZATION, attributes);
218 AccountFileInstance deviceAccountInstance = addDeviceAccountInstance(abstractFile);
220 BlackboardArtifact artifact = null;
221 org.
sleuthkit.datamodel.Blackboard tskBlackboard = tskCase.getBlackboard();
224 if (!tskBlackboard.artifactExists(abstractFile, BlackboardArtifact.ARTIFACT_TYPE.TSK_CONTACT, attributes)) {
225 artifact = abstractFile.newArtifact(BlackboardArtifact.ARTIFACT_TYPE.TSK_CONTACT);
226 artifact.addAttributes(attributes);
228 extractPhotos(vcard, abstractFile, artifact);
231 if (deviceAccountInstance != null) {
233 currentCase.getSleuthkitCase().getCommunicationsManager().addRelationships(
234 deviceAccountInstance, accountInstances, artifact, Relationship.Type.CONTACT, abstractFile.getCrtime());
235 }
catch (TskDataException ex) {
236 logger.log(Level.SEVERE, String.format(
"Failed to create phone and e-mail account relationships (fileName='%s'; fileId=%d; accountId=%d).",
237 abstractFile.getName(), abstractFile.getId(), deviceAccountInstance.getAccount().getAccountID()), ex);
243 blackboard.postArtifact(artifact, EmailParserModuleFactory.getModuleName());
244 }
catch (Blackboard.BlackboardException ex) {
245 logger.log(Level.SEVERE,
"Unable to index blackboard artifact " + artifact.getArtifactID(), ex);
246 MessageNotifyUtil.Notify.error(Bundle.VcardParser_addContactArtifact_indexError(), artifact.getDisplayName());
249 }
catch (TskCoreException ex) {
250 logger.log(Level.SEVERE, String.format(
"Failed to create contact artifact for vCard file '%s' (id=%d).",
251 abstractFile.getName(), abstractFile.getId()), ex);
265 private void extractPhotos(VCard vcard, AbstractFile abstractFile, BlackboardArtifact artifact)
throws NoCurrentCaseException {
266 String parentFileName = getUniqueName(abstractFile);
269 String outputPath = getOutputFolderPath(parentFileName);
270 if (
new File(outputPath).exists()) {
271 List<Photo> vcardPhotos = vcard.getPhotos();
272 List<AbstractFile> derivedFilesCreated =
new ArrayList<>();
273 for (
int i=0; i < vcardPhotos.size(); i++) {
274 Photo photo = vcardPhotos.get(i);
276 if (photo.getUrl() != null) {
281 String type = photo.getType();
288 type = type.toLowerCase();
289 if (type.startsWith(
"image/")) {
290 type = type.substring(6);
292 String extension = photoTypeExtensions.get(type);
295 byte[] data = photo.getData();
296 String extractedFileName = String.format(
"photo_%d%s", i, extension == null ?
"" : extension);
297 String extractedFilePath = Paths.get(outputPath, extractedFileName).toString();
299 writeExtractedImage(extractedFilePath, data);
300 derivedFilesCreated.add(fileManager.addDerivedFile(extractedFileName, getFileRelativePath(parentFileName, extractedFileName), data.length,
301 abstractFile.getCtime(), abstractFile.getCrtime(), abstractFile.getAtime(), abstractFile.getAtime(),
302 true, artifact, null, EmailParserModuleFactory.getModuleName(), EmailParserModuleFactory.getModuleVersion(),
"", TskData.EncodingType.NONE));
303 }
catch (IOException | TskCoreException ex) {
304 logger.log(Level.WARNING, String.format(
"Could not write image to '%s' (id=%d).", extractedFilePath, abstractFile.getId()), ex);
307 if (!derivedFilesCreated.isEmpty()) {
308 services.fireModuleContentEvent(
new ModuleContentEvent(abstractFile));
309 context.addFilesToJob(derivedFilesCreated);
313 logger.log(Level.INFO, String.format(
"Skipping photo extraction for file '%s' (id=%d), because it has already been processed.",
314 abstractFile.getName(), abstractFile.getId()));
316 }
catch (SecurityException ex) {
317 logger.log(Level.WARNING, String.format(
"Could not create extraction folder for '%s' (id=%d).", parentFileName, abstractFile.getId()));
328 private void writeExtractedImage(String outputPath, byte[] data)
throws IOException {
329 File outputFile =
new File(outputPath);
330 FileOutputStream outputStream =
new FileOutputStream(outputFile);
331 outputStream.write(data);
342 private String getUniqueName(AbstractFile file) {
343 return file.getName() +
"_" + file.getId();
355 private String getFileRelativePath(String parentFileName, String fileName)
throws NoCurrentCaseException {
357 return Paths.get(getRelModuleOutputPath(), parentFileName, fileName).toString();
370 private String getOutputFolderPath(String parentFileName)
throws NoCurrentCaseException {
371 String outputFolderPath = ThunderbirdMboxFileIngestModule.getModuleOutputPath() + File.separator + parentFileName;
372 File outputFilePath =
new File(outputFolderPath);
373 if (!outputFilePath.exists()) {
374 outputFilePath.mkdirs();
376 return outputFolderPath;
387 private void addPhoneAttributes(Telephone telephone, AbstractFile abstractFile, Collection<BlackboardAttribute> attributes) {
388 String telephoneText = telephone.getText();
390 if (telephoneText == null || telephoneText.isEmpty()) {
391 telephoneText = telephone.getUri().getNumber();
392 if (telephoneText == null || telephoneText.isEmpty()) {
398 List<TelephoneType> telephoneTypes = telephone.getTypes();
399 if (telephoneTypes.isEmpty()) {
400 ThunderbirdMboxFileIngestModule.addArtifactAttribute(telephone.getText(), BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PHONE_NUMBER, attributes);
402 TelephoneType type = telephoneTypes.get(0);
409 List<String> splitTelephoneTypes = Arrays.asList(
410 type.getValue().toUpperCase().replaceAll(
"\\s+",
"").split(
","));
412 if (splitTelephoneTypes.size() > 0) {
413 String splitType = splitTelephoneTypes.get(0);
414 String attributeTypeName =
"TSK_PHONE_NUMBER";
415 if (splitType != null && !splitType.isEmpty()) {
416 attributeTypeName =
"TSK_PHONE_NUMBER_" + splitType;
420 BlackboardAttribute.Type attributeType = tskCase.getAttributeType(attributeTypeName);
421 if (attributeType == null) {
424 attributeType = tskCase.addArtifactAttributeType(attributeTypeName,
425 BlackboardAttribute.TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.STRING,
426 String.format(
"Phone Number (%s)", StringUtils.capitalize(splitType.toLowerCase())));
427 }
catch (TskDataException ex) {
428 attributeType = tskCase.getAttributeType(attributeTypeName);
431 ThunderbirdMboxFileIngestModule.addArtifactAttribute(telephoneText, attributeType, attributes);
432 }
catch (TskCoreException ex) {
433 logger.log(Level.WARNING, String.format(
"Unable to retrieve attribute type '%s' for file '%s' (id=%d).", attributeTypeName, abstractFile.getName(), abstractFile.getId()), ex);
447 private void addEmailAttributes(Email email, AbstractFile abstractFile, Collection<BlackboardAttribute> attributes) {
448 String emailValue = email.getValue();
449 if (emailValue == null || emailValue.isEmpty()) {
454 List<EmailType> emailTypes = email.getTypes();
455 if (emailTypes.isEmpty()) {
456 ThunderbirdMboxFileIngestModule.addArtifactAttribute(email.getValue(), BlackboardAttribute.ATTRIBUTE_TYPE.TSK_EMAIL, attributes);
458 EmailType type = emailTypes.get(0);
464 List<String> splitEmailTypes = Arrays.asList(
465 type.getValue().toUpperCase().replaceAll(
"\\s+",
"").split(
","));
467 if (splitEmailTypes.size() > 0) {
468 String splitType = splitEmailTypes.get(0);
469 String attributeTypeName =
"TSK_EMAIL_" + splitType;
470 if(splitType.isEmpty()) {
471 attributeTypeName =
"TSK_EMAIL";
474 BlackboardAttribute.Type attributeType = tskCase.getAttributeType(attributeTypeName);
475 if (attributeType == null) {
477 attributeType = tskCase.addArtifactAttributeType(attributeTypeName,
478 BlackboardAttribute.TSK_BLACKBOARD_ATTRIBUTE_VALUE_TYPE.STRING,
479 String.format(
"Email (%s)", StringUtils.capitalize(splitType.toLowerCase())));
481 ThunderbirdMboxFileIngestModule.addArtifactAttribute(email.getValue(), attributeType, attributes);
482 }
catch (TskCoreException ex) {
483 logger.log(Level.SEVERE, String.format(
"Unable to retrieve attribute type '%s' for file '%s' (id=%d).", attributeTypeName, abstractFile.getName(), abstractFile.getId()), ex);
484 }
catch (TskDataException ex) {
485 logger.log(Level.SEVERE, String.format(
"Unable to add custom attribute type '%s' for file '%s' (id=%d).", attributeTypeName, abstractFile.getName(), abstractFile.getId()), ex);
500 private void addPhoneAccountInstances(Telephone telephone, AbstractFile abstractFile, Collection<AccountFileInstance> accountInstances) {
501 String telephoneText = telephone.getText();
502 if (telephoneText == null || telephoneText.isEmpty()) {
503 telephoneText = telephone.getUri().getNumber();
504 if (telephoneText == null || telephoneText.isEmpty()) {
512 AccountFileInstance phoneAccountInstance = tskCase.getCommunicationsManager().createAccountFileInstance(Account.Type.PHONE,
513 telephoneText, EmailParserModuleFactory.getModuleName(), abstractFile);
514 accountInstances.add(phoneAccountInstance);
516 catch(TskCoreException ex) {
517 logger.log(Level.WARNING, String.format(
518 "Failed to create account for phone number '%s' (content='%s'; id=%d).",
519 telephoneText, abstractFile.getName(), abstractFile.getId()), ex);
532 private void addEmailAccountInstances(Email email, AbstractFile abstractFile, Collection<AccountFileInstance> accountInstances) {
533 String emailValue = email.getValue();
534 if (emailValue == null || emailValue.isEmpty()) {
540 AccountFileInstance emailAccountInstance = tskCase.getCommunicationsManager().createAccountFileInstance(Account.Type.EMAIL,
541 emailValue, EmailParserModuleFactory.getModuleName(), abstractFile);
542 accountInstances.add(emailAccountInstance);
544 catch(TskCoreException ex) {
545 logger.log(Level.WARNING, String.format(
546 "Failed to create account for e-mail address '%s' (content='%s'; id=%d).",
547 emailValue, abstractFile.getName(), abstractFile.getId()), ex);
558 private AccountFileInstance addDeviceAccountInstance(AbstractFile abstractFile) {
560 AccountFileInstance deviceAccountInstance = null;
561 String deviceId = null;
563 long dataSourceObjId = abstractFile.getDataSourceObjectId();
564 DataSource dataSource = tskCase.getDataSource(dataSourceObjId);
565 deviceId = dataSource.getDeviceId();
566 deviceAccountInstance = tskCase.getCommunicationsManager().createAccountFileInstance(Account.Type.DEVICE,
567 deviceId, EmailParserModuleFactory.getModuleName(), abstractFile);
569 catch (TskCoreException ex) {
570 logger.log(Level.WARNING, String.format(
571 "Failed to create device account for '%s' (content='%s'; id=%d).",
572 deviceId, abstractFile.getName(), abstractFile.getId()), ex);
574 catch (TskDataException ex) {
575 logger.log(Level.WARNING, String.format(
576 "Failed to get the data source from the case database (id=%d).",
577 abstractFile.getId()), ex);
580 return deviceAccountInstance;