19 package org.sleuthkit.autopsy.recentactivity;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
26 import java.sql.ResultSet;
27 import java.sql.SQLException;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Collection;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.logging.Level;
35 import org.apache.commons.io.FilenameUtils;
36 import org.openide.modules.InstalledFileLocator;
37 import org.openide.util.NbBundle.Messages;
51 import static org.
sleuthkit.datamodel.BlackboardArtifact.ARTIFACT_TYPE.TSK_ASSOCIATED_OBJECT;
53 import static org.
sleuthkit.datamodel.BlackboardAttribute.ATTRIBUTE_TYPE.TSK_ASSOCIATED_ARTIFACT;
61 final class ExtractSru
extends Extract {
63 private static final Logger logger = Logger.getLogger(ExtractSru.class.getName());
65 private IngestJobContext context;
67 private static final String APPLICATION_USAGE_SOURCE_NAME =
"System Resource Usage - Application Usage";
68 private static final String NETWORK_USAGE_SOURCE_NAME =
"System Resource Usage - Network Usage";
71 private static final String MODULE_NAME =
"extractSRU";
73 private static final String SRU_TOOL_FOLDER =
"markmckinnon";
74 private static final String SRU_TOOL_NAME_WINDOWS_32 =
"Export_Srudb_32.exe";
75 private static final String SRU_TOOL_NAME_WINDOWS_64 =
"Export_Srudb_64.exe";
76 private static final String SRU_TOOL_NAME_LINUX =
"Export_Srudb_Linux.exe";
77 private static final String SRU_TOOL_NAME_MAC =
"Export_srudb_macos";
78 private static final String SRU_OUTPUT_FILE_NAME =
"Output.txt";
79 private static final String SRU_ERROR_FILE_NAME =
"Error.txt";
81 private static final Map<String, AbstractFile> applicationFilesFound =
new HashMap<>();
84 "ExtractSru_module_name=System Resource Usage Extractor"
87 this.moduleName = Bundle.ExtractSru_module_name();
91 "ExtractSru_error_finding_export_srudb_program=Error finding export_srudb program",
92 "ExtractSru_process_error_executing_export_srudb_program=Error running export_srudb program"
96 void process(Content dataSource, IngestJobContext context, DataSourceIngestModuleProgress progressBar) {
98 this.context = context;
100 String modOutPath = Case.getCurrentCase().getModuleDirectory() + File.separator +
"sru";
101 File dir =
new File(modOutPath);
102 if (dir.exists() ==
false) {
106 String tempDirPath = RAImageIngestModule.getRATempPath(Case.getCurrentCase(),
"sru");
107 String softwareHiveFileName = getSoftwareHiveFile(dataSource, tempDirPath);
109 if (softwareHiveFileName == null) {
113 AbstractFile sruAbstractFile = getSruFile(dataSource, tempDirPath);
115 if (sruAbstractFile == null) {
119 final String sruDumper = getPathForSruDumper();
120 if (sruDumper == null) {
121 this.addErrorMessage(Bundle.ExtractSru_error_finding_export_srudb_program());
122 logger.log(Level.SEVERE,
"Error finding export_srudb program");
126 if (context.dataSourceIngestIsCancelled()) {
131 String modOutFile = modOutPath + File.separator + sruAbstractFile.getId() +
"_srudb.db3";
132 String sruFileName = tempDirPath + File.separator + sruAbstractFile.getId() +
"_" + sruAbstractFile.getName();
134 extractSruFiles(sruDumper, sruFileName, modOutFile, tempDirPath, softwareHiveFileName);
136 findSruExecutedFiles(modOutFile, dataSource);
138 createNetUsageArtifacts(modOutFile, sruAbstractFile);
139 createAppUsageArtifacts(modOutFile, sruAbstractFile);
140 }
catch (IOException ex) {
141 logger.log(Level.SEVERE,
"Error processing SRUDB.dat file", ex);
142 this.addErrorMessage(Bundle.ExtractSru_process_error_executing_export_srudb_program());
147 "ExtractSru_process_errormsg_find_software_hive=Unable to find SOFTWARE HIVE file",
148 "ExtractSru_process_errormsg_write_software_hive=Unable to write SOFTWARE HIVE file"
159 String getSoftwareHiveFile(Content dataSource, String tempDirPath) {
160 FileManager fileManager = Case.getCurrentCase().getServices().getFileManager();
162 List<AbstractFile> softwareHiveFiles;
165 softwareHiveFiles = fileManager.findFiles(dataSource,
"SOFTWARE");
166 }
catch (TskCoreException ex) {
167 this.addErrorMessage(Bundle.ExtractSru_process_errormsg_find_software_hive());
168 logger.log(Level.WARNING,
"Unable to find SOFTWARE HIVE file.", ex);
172 String softwareHiveFileName = null;
174 for (AbstractFile softwareFile : softwareHiveFiles) {
176 if (softwareFile.getParentPath().endsWith(
"/config/")) {
177 softwareHiveFileName = tempDirPath + File.separator + softwareFile.getId() +
"_" + softwareFile.getName();
180 ContentUtils.writeToFile(softwareFile,
new File(softwareHiveFileName));
181 }
catch (IOException ex) {
182 this.addErrorMessage(Bundle.ExtractSru_process_errormsg_find_software_hive());
183 logger.log(Level.WARNING, String.format(
"Unable to write %s to temp directory. File name: %s", softwareFile.getName(), softwareFile), ex);
188 return softwareHiveFileName;
192 "ExtractSru_process_errormsg_find_srudb_dat=Unable to find srudb.dat file",
193 "ExtractSru_process_errormsg_write_srudb_dat=Unable to write srudb.dat file"
203 AbstractFile getSruFile(Content dataSource, String tempDirPath) {
204 FileManager fileManager = Case.getCurrentCase().getServices().getFileManager();
206 List<AbstractFile> sruFiles;
209 sruFiles = fileManager.findFiles(dataSource,
"SRUDB.DAT");
210 }
catch (TskCoreException ex) {
211 this.addErrorMessage(Bundle.ExtractSru_process_errormsg_find_srudb_dat());
212 logger.log(Level.WARNING,
"Unable to find SRUDB.DAT file.", ex);
216 AbstractFile sruAbstractFile = null;
218 for (AbstractFile sruFile : sruFiles) {
220 String sruFileName = tempDirPath + File.separator + sruFile.getId() +
"_" + sruFile.getName();
221 sruAbstractFile = sruFile;
224 ContentUtils.writeToFile(sruFile,
new File(sruFileName));
225 }
catch (IOException ex) {
226 this.addErrorMessage(Bundle.ExtractSru_process_errormsg_write_srudb_dat());
227 logger.log(Level.WARNING, String.format(
"Unable to write %s to temp directory. File name: %s", sruFile.getName(), sruFile), ex);
232 return sruAbstractFile;
245 void extractSruFiles(String sruExePath, String sruFile, String tempOutFile, String tempOutPath, String softwareHiveFile)
throws IOException {
246 final Path outputFilePath = Paths.get(tempOutPath, SRU_OUTPUT_FILE_NAME);
247 final Path errFilePath = Paths.get(tempOutPath, SRU_ERROR_FILE_NAME);
249 List<String> commandLine =
new ArrayList<>();
250 commandLine.add(sruExePath);
251 commandLine.add(sruFile);
252 commandLine.add(softwareHiveFile);
253 commandLine.add(tempOutFile);
255 ProcessBuilder processBuilder =
new ProcessBuilder(commandLine);
256 processBuilder.redirectOutput(outputFilePath.toFile());
257 processBuilder.redirectError(errFilePath.toFile());
259 ExecUtil.execute(processBuilder,
new DataSourceIngestModuleProcessTerminator(context,
true));
262 private String getPathForSruDumper() {
264 if (PlatformUtil.isWindowsOS()) {
265 if (PlatformUtil.is64BitOS()) {
266 path = Paths.get(SRU_TOOL_FOLDER, SRU_TOOL_NAME_WINDOWS_64);
268 path = Paths.get(SRU_TOOL_FOLDER, SRU_TOOL_NAME_WINDOWS_32);
271 if (
"Linux".equals(PlatformUtil.getOSName())) {
272 path = Paths.get(SRU_TOOL_FOLDER, SRU_TOOL_NAME_LINUX);
274 path = Paths.get(SRU_TOOL_FOLDER, SRU_TOOL_NAME_MAC);
277 File sruToolFile = InstalledFileLocator.getDefault().locate(path.toString(),
278 ExtractSru.class.getPackage().getName(),
false);
279 if (sruToolFile != null) {
280 return sruToolFile.getAbsolutePath();
286 private void findSruExecutedFiles(String sruDb, Content dataSource) {
290 String sqlStatement =
"SELECT DISTINCT SUBSTR(LTRIM(IdBlob, '\\Device\\HarddiskVolume'), INSTR(LTRIM(IdBlob, '\\Device\\HarddiskVolume'), '\\')) "
291 +
" application_name, idBlob source_name FROM SruDbIdMapTable WHERE idType = 0 AND idBlob NOT LIKE '!!%'";
293 try (SQLiteDBConnect tempdbconnect =
new SQLiteDBConnect(
"org.sqlite.JDBC",
"jdbc:sqlite:" + sruDb);
294 ResultSet resultSet = tempdbconnect.executeQry(sqlStatement)) {
296 while (resultSet.next()) {
298 if (context.dataSourceIngestIsCancelled()) {
299 logger.log(Level.INFO,
"Cancelled SRU Artifact Creation.");
303 String applicationName = resultSet.getString(
"application_name");
304 String sourceName = resultSet.getString(
"source_name");
306 String normalizePathName = FilenameUtils.normalize(applicationName,
true);
307 String fileName = FilenameUtils.getName(normalizePathName);
308 String filePath = FilenameUtils.getPath(normalizePathName);
309 if (fileName.contains(
" [")) {
310 fileName = fileName.substring(0, fileName.indexOf(
" ["));
312 List<AbstractFile> sourceFiles;
314 sourceFiles = fileManager.
findFiles(dataSource, fileName, filePath);
315 for (AbstractFile sourceFile : sourceFiles) {
316 if (sourceFile.getParentPath().endsWith(filePath)) {
317 applicationFilesFound.put(sourceName.toLowerCase(), sourceFile);
321 }
catch (TskCoreException ex) {
322 logger.log(Level.WARNING, String.format(
"Error finding actual file %s. file may not exist", normalizePathName));
325 }
catch (SQLException ex) {
326 logger.log(Level.WARNING,
"Error while trying to read into a sqlite db.", ex);
331 private void createNetUsageArtifacts(String sruDb, AbstractFile sruAbstractFile) {
332 List<BlackboardArtifact> bba =
new ArrayList<>();
334 String sqlStatement =
"SELECT STRFTIME('%s', timestamp) ExecutionTime, a.application_name, b.Application_Name formatted_application_name, User_Name, "
335 +
" bytesSent, BytesRecvd FROM network_Usage a, SruDbIdMapTable, exe_to_app b "
336 +
" where appId = IdIndex and IdType = 0 and a.application_name = b.source_name order by ExecutionTime;";
338 try (SQLiteDBConnect tempdbconnect =
new SQLiteDBConnect(
"org.sqlite.JDBC",
"jdbc:sqlite:" + sruDb);
339 ResultSet resultSet = tempdbconnect.executeQry(sqlStatement)) {
341 while (resultSet.next()) {
343 if (context.dataSourceIngestIsCancelled()) {
344 logger.log(Level.INFO,
"Cancelled SRU Net Usage Artifact Creation.");
348 String applicationName = resultSet.getString(
"Application_Name");
349 String formattedApplicationName = resultSet.getString(
"formatted_Application_name");
350 Long executionTime = Long.valueOf(resultSet.getInt(
"ExecutionTime"));
351 Long bytesSent = Long.valueOf(resultSet.getInt(
"bytesSent"));
352 Long bytesRecvd = Long.valueOf(resultSet.getInt(
"BytesRecvd"));
353 String userName = resultSet.getString(
"User_Name");
355 Collection<BlackboardAttribute> bbattributes = Arrays.asList(
356 new BlackboardAttribute(
357 BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PROG_NAME, getName(),
358 formattedApplicationName),
359 new BlackboardAttribute(
360 BlackboardAttribute.ATTRIBUTE_TYPE.TSK_USER_NAME, getName(),
362 new BlackboardAttribute(
363 BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME, getName(),
365 new BlackboardAttribute(
366 BlackboardAttribute.ATTRIBUTE_TYPE.TSK_BYTES_SENT, getName(), bytesSent),
367 new BlackboardAttribute(
368 BlackboardAttribute.ATTRIBUTE_TYPE.TSK_BYTES_RECEIVED, getName(), bytesRecvd),
369 new BlackboardAttribute(
370 BlackboardAttribute.ATTRIBUTE_TYPE.TSK_COMMENT, getName(), NETWORK_USAGE_SOURCE_NAME));
373 BlackboardArtifact bbart = sruAbstractFile.newArtifact(BlackboardArtifact.ARTIFACT_TYPE.TSK_PROG_RUN);
374 bbart.addAttributes(bbattributes);
376 BlackboardArtifact associateBbArtifact = createAssociatedArtifact(applicationName.toLowerCase(), bbart);
377 if (associateBbArtifact != null) {
378 bba.add(associateBbArtifact);
380 }
catch (TskCoreException ex) {
381 logger.log(Level.SEVERE,
"Exception Adding Artifact.", ex);
385 }
catch (SQLException ex) {
386 logger.log(Level.SEVERE,
"Error while trying to read into a sqlite db.", ex);
390 blackboard.postArtifacts(bba, MODULE_NAME);
391 }
catch (Blackboard.BlackboardException ex) {
392 logger.log(Level.SEVERE,
"Error Posting Artifact.", ex);
396 private void createAppUsageArtifacts(String sruDb, AbstractFile sruAbstractFile) {
397 List<BlackboardArtifact> bba =
new ArrayList<>();
399 String sqlStatement =
"SELECT STRFTIME('%s', timestamp) ExecutionTime, a.application_name, b.Application_Name formatted_application_name, User_Name "
400 +
" FROM Application_Resource_Usage a, SruDbIdMapTable, exe_to_app b WHERE "
401 +
" idType = 0 and idIndex = appId and a.application_name = b.source_name order by ExecutionTime;";
403 try (SQLiteDBConnect tempdbconnect =
new SQLiteDBConnect(
"org.sqlite.JDBC",
"jdbc:sqlite:" + sruDb);
404 ResultSet resultSet = tempdbconnect.executeQry(sqlStatement)) {
406 while (resultSet.next()) {
408 if (context.dataSourceIngestIsCancelled()) {
409 logger.log(Level.INFO,
"Cancelled SRU Net Usage Artifact Creation.");
413 String applicationName = resultSet.getString(
"Application_Name");
414 String formattedApplicationName = resultSet.getString(
"formatted_application_name");
415 Long executionTime = Long.valueOf(resultSet.getInt(
"ExecutionTime"));
416 String userName = resultSet.getString(
"User_Name");
418 Collection<BlackboardAttribute> bbattributes = Arrays.asList(
419 new BlackboardAttribute(
420 BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PROG_NAME, getName(),
421 formattedApplicationName),
422 new BlackboardAttribute(
423 BlackboardAttribute.ATTRIBUTE_TYPE.TSK_USER_NAME, getName(),
425 new BlackboardAttribute(
426 BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME, getName(),
428 new BlackboardAttribute(
429 BlackboardAttribute.ATTRIBUTE_TYPE.TSK_COMMENT, getName(), APPLICATION_USAGE_SOURCE_NAME));
432 BlackboardArtifact bbart = sruAbstractFile.newArtifact(BlackboardArtifact.ARTIFACT_TYPE.TSK_PROG_RUN);
433 bbart.addAttributes(bbattributes);
435 BlackboardArtifact associateBbArtifact = createAssociatedArtifact(applicationName.toLowerCase(), bbart);
436 if (associateBbArtifact != null) {
437 bba.add(associateBbArtifact);
439 }
catch (TskCoreException ex) {
440 logger.log(Level.SEVERE,
"Exception Adding Artifact.", ex);
444 }
catch (SQLException ex) {
445 logger.log(Level.SEVERE,
"Error while trying to read into a sqlite db.", ex);
449 blackboard.postArtifacts(bba, MODULE_NAME);
450 }
catch (Blackboard.BlackboardException ex) {
451 logger.log(Level.SEVERE,
"Error Posting Artifact.", ex);
465 private BlackboardArtifact createAssociatedArtifact(String filePathName, BlackboardArtifact bba) {
466 if (applicationFilesFound.containsKey(filePathName)) {
467 AbstractFile sourceFile = applicationFilesFound.get(filePathName);
468 Collection<BlackboardAttribute> bbattributes2 =
new ArrayList<>();
469 bbattributes2.addAll(Arrays.asList(
470 new BlackboardAttribute(TSK_ASSOCIATED_ARTIFACT, this.getName(),
471 bba.getArtifactID())));
473 BlackboardArtifact associatedObjectBba = createArtifactWithAttributes(TSK_ASSOCIATED_OBJECT, sourceFile, bbattributes2);
474 if (associatedObjectBba != null) {
475 return associatedObjectBba;
synchronized List< AbstractFile > findFiles(String fileName)