30 package org.sleuthkit.autopsy.examples;
33 import java.io.FileOutputStream;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.logging.Level;
38 import javax.xml.parsers.DocumentBuilder;
39 import javax.xml.parsers.DocumentBuilderFactory;
40 import javax.xml.parsers.ParserConfigurationException;
41 import javax.xml.transform.Transformer;
42 import javax.xml.transform.TransformerConfigurationException;
43 import javax.xml.transform.TransformerException;
44 import javax.xml.transform.TransformerFactory;
45 import javax.xml.transform.dom.DOMSource;
46 import javax.xml.transform.stream.StreamResult;
61 import org.
sleuthkit.datamodel.BlackboardArtifact.ARTIFACT_TYPE;
62 import org.
sleuthkit.datamodel.BlackboardAttribute.ATTRIBUTE_TYPE;
65 import org.w3c.dom.Document;
66 import org.w3c.dom.Element;
88 File outputDir =
new File(outputDirPath);
89 if (outputDir.exists() ==
false) {
112 String resultsFilePath = outputDirPath + File.separator + String.format(
"job_%d_results.xml", context.
getJobId());
113 boolean haveRealExecutable =
false;
114 if (haveRealExecutable) {
115 if (dataSource instanceof Image) {
116 Image image = (Image) dataSource;
117 String dataSourcePath = image.getPaths()[0];
118 List<String> commandLine =
new ArrayList<>();
119 commandLine.add(
"some.exe");
120 commandLine.add(dataSourcePath);
121 commandLine.add(resultsFilePath);
122 ProcessBuilder processBuilder =
new ProcessBuilder(commandLine);
143 }
catch (ParserConfigurationException | TransformerException | IOException ex) {
145 logger.log(Level.SEVERE,
"Failed to simulate analysis and results import", ex);
152 private void generateSimulatedResults(String resultsFilePath)
throws ParserConfigurationException, IOException, TransformerConfigurationException, TransformerException {
159 List<String> filePaths =
new ArrayList<>();
160 String fileContents =
"This is a simulated derived file.";
161 for (
int i = 0; i < 2; ++i) {
162 String fileName = String.format(
"job_%d_derived_file_%d.txt", context.
getJobId(), i);
163 filePaths.add(
generateFile(fileName, fileContents.getBytes()));
165 this.derivedFileInCaseDatabase = this.fileInCaseDatabase +
"/" + fileName;
172 List<String> filePaths =
new ArrayList<>();
173 String fileContents =
"This is a simulated report.";
174 for (
int i = 0; i < 2; ++i) {
175 String fileName = String.format(
"job_%d_report_%d.txt", context.
getJobId(), i);
176 filePaths.add(
generateFile(fileName, fileContents.getBytes()));
181 private String
generateFile(String fileName, byte[] fileContents)
throws IOException {
182 String filePath = outputDirPath + File.separator + fileName;
183 File file =
new File(filePath);
184 if (!file.exists()) {
185 file.createNewFile();
187 try (FileOutputStream fileStream =
new FileOutputStream(file)) {
188 fileStream.write(fileContents);
194 private void generateSimulatedResultsFile(List<String> derivedFilePaths, List<String> reportPaths, String resultsFilePath)
throws ParserConfigurationException, TransformerConfigurationException, TransformerException {
247 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
248 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
249 Document doc = docBuilder.newDocument();
251 doc.appendChild(rootElement);
255 rootElement.appendChild(derivedFilesListElement);
264 for (
int i = 0; i < derivedFilePaths.size(); ++i) {
265 String filePath = derivedFilePaths.get(i);
267 derivedFilesListElement.appendChild(derivedFileElement);
269 localPathElement.setTextContent(filePath);
270 derivedFileElement.appendChild(localPathElement);
273 parentPathElement.setTextContent(this.fileInCaseDatabase);
275 parentPathElement.setTextContent(this.derivedFileInCaseDatabase);
277 derivedFileElement.appendChild(parentPathElement);
282 rootElement.appendChild(artifactsListElement);
289 artifactsListElement.appendChild(artifactElement);
295 fileElement.setTextContent(this.fileInCaseDatabase);
296 artifactElement.appendChild(fileElement);
303 artifactElement.appendChild(artifactAttrElement);
309 artifactAttributeValueElement.setTextContent(
"SampleInterestingFilesSet");
310 artifactAttrElement.appendChild(artifactAttributeValueElement);
315 artifactAttrSourceElement.setTextContent(moduleName);
316 artifactAttrElement.appendChild(artifactAttrSourceElement);
321 artifactsListElement.appendChild(artifactElement);
325 fileElement.setTextContent(this.derivedFileInCaseDatabase);
326 artifactElement.appendChild(fileElement);
330 for (
int i = 0; i < 5; ++i) {
333 artifactElement.appendChild(artifactAttrElement);
338 artifactAttributeValueElement.setTextContent(
"One");
342 artifactAttributeValueElement.setTextContent(
"2");
346 artifactAttributeValueElement.setTextContent(
"3");
350 artifactAttributeValueElement.setTextContent(
"4.0");
354 artifactAttributeValueElement.setTextContent(
"7023372036854775839");
357 artifactAttrElement.appendChild(artifactAttributeValueElement);
362 rootElement.appendChild(reportsListElement);
371 for (
int i = 0; i < reportPaths.size(); ++i) {
372 String reportPath = reportPaths.get(i);
374 reportsListElement.appendChild(reportElement);
376 reportPathElement.setTextContent(reportPath);
377 reportElement.appendChild(reportPathElement);
379 reportSourceModuleElement.setTextContent(moduleName);
380 reportElement.appendChild(reportSourceModuleElement);
383 reportNameElement.setTextContent(
"Sample Report");
384 reportElement.appendChild(reportNameElement);
388 TransformerFactory transformerFactory = TransformerFactory.newInstance();
389 Transformer transformer = transformerFactory.newTransformer();
390 DOMSource source =
new DOMSource(doc);
391 StreamResult result =
new StreamResult(
new File(resultsFilePath));
392 transformer.transform(source, result);
synchronized long get(long jobId)
static int execute(ProcessBuilder processBuilder)
static IngestMessage createErrorMessage(String source, String subject, String detailsHtml)
List< ErrorInfo > getErrorInfo()
String derivedFileInCaseDatabase
static final String moduleName
void generateSimulatedResultsFile(List< String > derivedFilePaths, List< String > reportPaths, String resultsFilePath)
Logger getLogger(String moduleDisplayName)
synchronized long incrementAndGet(long jobId)
List< String > generateSimulatedDerivedFiles()
static final IngestModuleReferenceCounter refCounter
void postMessage(final IngestMessage message)
String generateFile(String fileName, byte[] fileContents)
void generateSimulatedResults(String resultsFilePath)
void startUp(IngestJobContext context)
String getModuleDirectory()
List< ErrorInfo > importResults(ExternalResults results)
void switchToDeterminate(int workUnits)
ProcessResult process(Content dataSource, DataSourceIngestModuleProgress progressBar)
static Case getCurrentCase()
List< String > generateSimulatedReports()
final String fileInCaseDatabase
void progress(int workUnits)
static synchronized IngestServices getInstance()