19 package org.sleuthkit.autopsy.modules.ileappanalyzer;
21 import java.io.BufferedReader;
23 import java.io.FileNotFoundException;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.io.UncheckedIOException;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.text.ParseException;
30 import java.text.SimpleDateFormat;
31 import java.util.List;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.Date;
35 import java.util.HashMap;
36 import static java.util.Locale.US;
38 import java.util.logging.Level;
39 import java.util.stream.Collectors;
40 import java.util.stream.Stream;
41 import javax.xml.parsers.DocumentBuilder;
42 import javax.xml.parsers.DocumentBuilderFactory;
43 import javax.xml.parsers.ParserConfigurationException;
44 import org.apache.commons.io.FilenameUtils;
45 import org.openide.util.NbBundle;
55 import org.
sleuthkit.datamodel.BlackboardAttribute.ATTRIBUTE_TYPE;
59 import org.w3c.dom.Document;
60 import org.w3c.dom.NamedNodeMap;
61 import org.w3c.dom.NodeList;
62 import org.xml.sax.SAXException;
72 private static final String
XMLFILE =
"ileap-artifact-attribute-reference.xml";
80 this.tsvFiles =
new HashMap<>();
81 this.tsvFileArtifacts =
new HashMap<>();
82 this.tsvFileArtifactComments =
new HashMap<>();
83 this.tsvFileAttributes =
new HashMap<>();
91 "ILeappFileProcessor.error.running.iLeapp=Error running iLeapp, see log file.",
92 "ILeappFileProcessor.error.creating.output.dir=Error creating iLeapp module output directory.",
93 "ILeappFileProcessor.starting.iLeapp=Starting iLeapp",
94 "ILeappFileProcessor.running.iLeapp=Running iLeapp",
95 "ILeappFileProcessor.has.run=iLeapp",
96 "ILeappFileProcessor.iLeapp.cancelled=iLeapp run was canceled",
97 "ILeappFileProcessor.completed=iLeapp Processing Completed",
98 "ILeappFileProcessor.error.reading.iLeapp.directory=Error reading iLeapp Output Directory"})
103 List<String> iLeappTsvOutputFiles =
findTsvFiles(moduleOutputPath);
106 logger.log(Level.SEVERE, String.format(
"Error trying to process iLeapp output files in directory %s. ", moduleOutputPath.toString()), ex);
118 List<String> allTsvFiles =
new ArrayList<>();
119 List<String> foundTsvFiles =
new ArrayList<>();
121 try (Stream<Path> walk = Files.walk(iLeappOutputDir)) {
123 allTsvFiles = walk.map(x -> x.toString())
124 .filter(f -> f.toLowerCase().endsWith(
".tsv")).collect(Collectors.toList());
126 for (String tsvFile : allTsvFiles) {
127 if (tsvFiles.containsKey(FilenameUtils.getName(tsvFile))) {
128 foundTsvFiles.add(tsvFile);
132 }
catch (IOException | UncheckedIOException e) {
133 throw new IngestModuleException(Bundle.ILeappFileProcessor_error_reading_iLeapp_directory() + iLeappOutputDir.toString(), e);
136 return foundTsvFiles;
151 List<BlackboardArtifact> bbartifacts =
new ArrayList<>();
153 for (String iLeappFileName : iLeappFilesToProcess) {
154 String fileName = FilenameUtils.getName(iLeappFileName);
155 File iLeappFile =
new File(iLeappFileName);
156 if (tsvFileAttributes.containsKey(fileName)) {
157 List<List<String>> attrList = tsvFileAttributes.get(fileName);
161 processFile(iLeappFile, attrList, fileName, artifactType, bbartifacts, iLeappImageFile);
163 }
catch (TskCoreException ex) {
165 throw new IngestModuleException(String.format(
"Error getting Blackboard Artifact Type for %s", tsvFileArtifacts.get(fileName)), ex);
171 if (!bbartifacts.isEmpty()) {
172 postArtifacts(bbartifacts);
177 private void processFile(File iLeappFile, List<List<String>> attrList, String fileName, BlackboardArtifact.Type artifactType,
178 List<BlackboardArtifact> bbartifacts, AbstractFile iLeappImageFile)
throws FileNotFoundException, IOException,
IngestModuleException {
179 try (BufferedReader reader =
new BufferedReader(
new FileReader(iLeappFile))) {
180 String line = reader.readLine();
184 line = reader.readLine();
185 while (line != null) {
186 Collection<BlackboardAttribute> bbattributes =
processReadLine(line, columnNumberToProcess, fileName);
187 if (!bbattributes.isEmpty()) {
189 if (bbartifact != null) {
190 bbartifacts.add(bbartifact);
193 line = reader.readLine();
210 String[] columnValues = line.split(
"\\t");
212 Collection<BlackboardAttribute> bbattributes =
new ArrayList<BlackboardAttribute>();
214 for (Map.Entry<Integer, String> columnToProcess : columnNumberToProcess.entrySet()) {
215 Integer columnNumber = columnToProcess.getKey();
216 String attributeName = columnToProcess.getValue();
220 if (attributeType == null) {
223 String attrType = attributeType.getValueType().getLabel().toUpperCase();
224 checkAttributeType(bbattributes, attrType, columnValues, columnNumber, attributeType, fileName);
225 }
catch (TskCoreException ex) {
226 throw new IngestModuleException(String.format(
"Error getting Attribute type for Attribute Name %s", attributeName), ex);
230 if (tsvFileArtifactComments.containsKey(fileName)) {
231 bbattributes.add(
new BlackboardAttribute(ATTRIBUTE_TYPE.TSK_COMMENT, MODULE_NAME, tsvFileArtifactComments.get(fileName)));
238 private void checkAttributeType(Collection<BlackboardAttribute> bbattributes, String attrType, String[] columnValues, Integer columnNumber, BlackboardAttribute.Type attributeType,
240 if (attrType.matches(
"STRING")) {
241 bbattributes.add(
new BlackboardAttribute(attributeType, MODULE_NAME, columnValues[columnNumber]));
242 }
else if (attrType.matches(
"INTEGER")) {
243 bbattributes.add(
new BlackboardAttribute(attributeType, MODULE_NAME, Integer.valueOf(columnValues[columnNumber])));
244 }
else if (attrType.matches(
"LONG")) {
245 bbattributes.add(
new BlackboardAttribute(attributeType, MODULE_NAME, Long.valueOf(columnValues[columnNumber])));
246 }
else if (attrType.matches(
"DOUBLE")) {
247 bbattributes.add(
new BlackboardAttribute(attributeType, MODULE_NAME, Double.valueOf(columnValues[columnNumber])));
248 }
else if (attrType.matches(
"BYTE")) {
249 bbattributes.add(
new BlackboardAttribute(attributeType, MODULE_NAME, Byte.valueOf(columnValues[columnNumber])));
250 }
else if (attrType.matches(
"DATETIME")) {
252 SimpleDateFormat dateFormat =
new SimpleDateFormat(
"yyyy-MM-d HH:mm:ss", US);
253 Long dateLong = Long.valueOf(0);
255 Date newDate = dateFormat.parse(columnValues[columnNumber]);
256 dateLong = newDate.getTime() / 1000;
257 bbattributes.add(
new BlackboardAttribute(attributeType, MODULE_NAME, dateLong));
258 }
catch (ParseException ex) {
261 logger.log(Level.WARNING, String.format(
"Failed to parse date/time %s for attribute type %s in file %s.", columnValues[columnNumber], attributeType.getDisplayName(), fileName));
263 }
else if (attrType.matches(
"JSON")) {
265 bbattributes.add(
new BlackboardAttribute(attributeType, MODULE_NAME, columnValues[columnNumber]));
268 logger.log(Level.WARNING, String.format(
"Attribute Type %s not defined.", attrType));
285 String[] columnNames = line.split(
"\\t");
286 HashMap<Integer, String> columnsToProcess =
new HashMap<>();
288 Integer columnPosition = 0;
289 for (String columnName : columnNames) {
291 String cleanColumnName = columnName.replaceAll(
"[^\\n\\r\\t\\p{Print}]",
"");
292 for (List<String> atList : attrList) {
293 if (atList.contains(cleanColumnName.toLowerCase())) {
294 columnsToProcess.put(columnPosition, atList.get(0));
301 return columnsToProcess;
305 "ILeappFileProcessor.cannot.load.artifact.xml=Cannor load xml artifact file.",
306 "ILeappFileProcessor.cannotBuildXmlParser=Cannot buld an XML parser.",
307 "ILeappFileProcessor_cannotParseXml=Cannot Parse XML file.",
308 "ILeappFileProcessor.postartifacts_error=Error posting Blackboard Artifact",
309 "ILeappFileProcessor.error.creating.new.artifacts=Error creating new artifacts."
319 File f =
new File(path);
320 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
321 DocumentBuilder db = dbf.newDocumentBuilder();
322 xmlinput = db.parse(f);
324 }
catch (IOException e) {
325 throw new IngestModuleException(Bundle.ILeappFileProcessor_cannot_load_artifact_xml() + e.getLocalizedMessage(), e);
326 }
catch (ParserConfigurationException pce) {
327 throw new IngestModuleException(Bundle.ILeappFileProcessor_cannotBuildXmlParser() + pce.getLocalizedMessage(), pce);
328 }
catch (SAXException sxe) {
329 throw new IngestModuleException(Bundle.ILeappFileProcessor_cannotParseXml() + sxe.getLocalizedMessage(), sxe);
340 NodeList nlist = xmlinput.getElementsByTagName(
"FileName");
342 for (
int i = 0; i < nlist.getLength(); i++) {
343 NamedNodeMap nnm = nlist.item(i).getAttributes();
344 tsvFiles.put(nnm.getNamedItem(
"filename").getNodeValue(), nnm.getNamedItem(
"description").getNodeValue());
352 NodeList artifactNlist = xmlinput.getElementsByTagName(
"ArtifactName");
353 for (
int k = 0; k < artifactNlist.getLength(); k++) {
354 NamedNodeMap nnm = artifactNlist.item(k).getAttributes();
355 String artifactName = nnm.getNamedItem(
"artifactname").getNodeValue();
356 String comment = nnm.getNamedItem(
"comment").getNodeValue();
357 String parentName = artifactNlist.item(k).getParentNode().getAttributes().getNamedItem(
"filename").getNodeValue();
359 tsvFileArtifacts.put(parentName, artifactName);
361 if (!comment.toLowerCase().matches(
"null")) {
362 tsvFileArtifactComments.put(parentName, comment);
370 NodeList attributeNlist = xmlinput.getElementsByTagName(
"AttributeName");
371 for (
int k = 0; k < attributeNlist.getLength(); k++) {
372 List<String> attributeList =
new ArrayList<>();
373 NamedNodeMap nnm = attributeNlist.item(k).getAttributes();
374 String attributeName = nnm.getNamedItem(
"attributename").getNodeValue();
375 if (!attributeName.toLowerCase().matches(
"null")) {
376 String columnName = nnm.getNamedItem(
"columnName").getNodeValue();
377 String required = nnm.getNamedItem(
"required").getNodeValue();
378 String parentName = attributeNlist.item(k).getParentNode().getParentNode().getAttributes().getNamedItem(
"filename").getNodeValue();
380 attributeList.add(attributeName.toLowerCase());
381 attributeList.add(columnName.toLowerCase());
382 attributeList.add(required.toLowerCase());
384 if (tsvFileAttributes.containsKey(parentName)) {
385 List<List<String>> attrList = tsvFileAttributes.get(parentName);
386 attrList.add(attributeList);
387 tsvFileAttributes.replace(parentName, attrList);
389 List<List<String>> attrList =
new ArrayList<>();
390 attrList.add(attributeList);
391 tsvFileAttributes.put(parentName, attrList);
412 BlackboardArtifact bbart = abstractFile.newArtifact(type);
413 bbart.addAttributes(bbattributes);
415 }
catch (TskException ex) {
416 logger.log(Level.WARNING, Bundle.ILeappFileProcessor_error_creating_new_artifacts(), ex);
427 void postArtifacts(Collection<BlackboardArtifact> artifacts) {
428 if (artifacts == null || artifacts.isEmpty()) {
433 Case.getCurrentCase().getSleuthkitCase().getBlackboard().postArtifacts(artifacts, MODULE_NAME);
434 }
catch (Blackboard.BlackboardException ex) {
435 logger.log(Level.SEVERE, Bundle.ILeappFileProcessor_postartifacts_error(), ex);
void processFile(File iLeappFile, List< List< String >> attrList, String fileName, BlackboardArtifact.Type artifactType, List< BlackboardArtifact > bbartifacts, AbstractFile iLeappImageFile)
void getArtifactNode(Document xmlinput)
final Map< String, List< List< String > > > tsvFileAttributes
void getFileNode(Document xmlinput)
void getAttributeNodes(Document xmlinput)
final Map< String, String > tsvFileArtifacts
final Map< String, String > tsvFiles
ProcessResult processFiles(Content dataSource, Path moduleOutputPath, AbstractFile iLeappFile)
List< String > findTsvFiles(Path iLeappOutputDir)
static final String XMLFILE
final Map< String, String > tsvFileArtifactComments
static final String MODULE_NAME
SleuthkitCase getSleuthkitCase()
Map< Integer, String > findColumnsToProcess(String line, List< List< String >> attrList)
Collection< BlackboardAttribute > processReadLine(String line, Map< Integer, String > columnNumberToProcess, String fileName)
void processiLeappFiles(List< String > iLeappFilesToProcess, AbstractFile iLeappImageFile)
static Case getCurrentCase()
synchronized static Logger getLogger(String name)
void checkAttributeType(Collection< BlackboardAttribute > bbattributes, String attrType, String[] columnValues, Integer columnNumber, BlackboardAttribute.Type attributeType, String fileName)
static final Logger logger
BlackboardArtifact createArtifactWithAttributes(int type, AbstractFile abstractFile, Collection< BlackboardAttribute > bbattributes)