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;
58 final class ExtractSru
extends Extract {
60 private static final Logger logger = Logger.getLogger(ExtractSru.class.getName());
62 private static final String APPLICATION_USAGE_SOURCE_NAME =
"System Resource Usage - Application Usage";
63 private static final String NETWORK_USAGE_SOURCE_NAME =
"System Resource Usage - Network Usage";
64 private static final String SRU_TOOL_FOLDER =
"markmckinnon";
65 private static final String SRU_TOOL_NAME_WINDOWS_32 =
"Export_Srudb_32.exe";
66 private static final String SRU_TOOL_NAME_WINDOWS_64 =
"Export_Srudb_64.exe";
67 private static final String SRU_TOOL_NAME_LINUX =
"Export_Srudb_Linux.exe";
68 private static final String SRU_TOOL_NAME_MAC =
"Export_srudb_macos";
69 private static final String SRU_OUTPUT_FILE_NAME =
"Output.txt";
70 private static final String SRU_ERROR_FILE_NAME =
"Error.txt";
72 private static final Map<String, AbstractFile> applicationFilesFound =
new HashMap<>();
73 private final IngestJobContext context;
76 "ExtractSru_module_name=System Resource Usage Analyzer"
78 ExtractSru(IngestJobContext context) {
79 super(Bundle.ExtractSru_module_name(), context);
80 this.context = context;
84 "ExtractSru_error_finding_export_srudb_program=Error finding export_srudb program",
85 "ExtractSru_process_error_executing_export_srudb_program=Error running export_srudb program"
89 void process(Content dataSource, DataSourceIngestModuleProgress progressBar) {
91 String modOutPath = Case.getCurrentCase().getModuleDirectory() + File.separator +
"sru";
92 File dir =
new File(modOutPath);
93 if (dir.exists() ==
false) {
97 String tempDirPath = RAImageIngestModule.getRATempPath(Case.getCurrentCase(),
"sru", context.getJobId());
98 String softwareHiveFileName = getSoftwareHiveFile(dataSource, tempDirPath);
100 if (softwareHiveFileName == null) {
104 AbstractFile sruAbstractFile = getSruFile(dataSource, tempDirPath);
106 if (sruAbstractFile == null) {
110 final String sruDumper = getPathForSruDumper();
111 if (sruDumper == null) {
112 this.addErrorMessage(Bundle.ExtractSru_error_finding_export_srudb_program());
113 logger.log(Level.SEVERE,
"Error finding export_srudb program");
117 if (context.dataSourceIngestIsCancelled()) {
122 String modOutFile = modOutPath + File.separator + sruAbstractFile.getId() +
"_srudb.db3";
123 String sruFileName = tempDirPath + File.separator + sruAbstractFile.getId() +
"_" + sruAbstractFile.getName();
125 extractSruFiles(sruDumper, sruFileName, modOutFile, tempDirPath, softwareHiveFileName);
127 findSruExecutedFiles(modOutFile, dataSource);
129 createNetUsageArtifacts(modOutFile, sruAbstractFile);
130 createAppUsageArtifacts(modOutFile, sruAbstractFile);
131 }
catch (IOException ex) {
132 logger.log(Level.SEVERE,
"Error processing SRUDB.dat file", ex);
133 this.addErrorMessage(Bundle.ExtractSru_process_error_executing_export_srudb_program());
138 "ExtractSru_process_errormsg_find_software_hive=Unable to find SOFTWARE HIVE file",
139 "ExtractSru_process_errormsg_write_software_hive=Unable to write SOFTWARE HIVE file"
150 String getSoftwareHiveFile(Content dataSource, String tempDirPath) {
151 FileManager fileManager = Case.getCurrentCase().getServices().getFileManager();
153 List<AbstractFile> softwareHiveFiles;
156 softwareHiveFiles = fileManager.findFiles(dataSource,
"SOFTWARE");
157 }
catch (TskCoreException ex) {
158 this.addErrorMessage(Bundle.ExtractSru_process_errormsg_find_software_hive());
159 logger.log(Level.WARNING,
"Unable to find SOFTWARE HIVE file.", ex);
163 String softwareHiveFileName = null;
165 for (AbstractFile softwareFile : softwareHiveFiles) {
167 if (softwareFile.getParentPath().endsWith(
"/config/")) {
168 softwareHiveFileName = tempDirPath + File.separator + softwareFile.getId() +
"_" + softwareFile.getName();
171 ContentUtils.writeToFile(softwareFile,
new File(softwareHiveFileName));
172 }
catch (IOException ex) {
173 this.addErrorMessage(Bundle.ExtractSru_process_errormsg_find_software_hive());
174 logger.log(Level.WARNING, String.format(
"Unable to write %s to temp directory. File name: %s", softwareFile.getName(), softwareFile), ex);
179 return softwareHiveFileName;
183 "ExtractSru_process_errormsg_find_srudb_dat=Unable to find srudb.dat file",
184 "ExtractSru_process_errormsg_write_srudb_dat=Unable to write srudb.dat file"
194 AbstractFile getSruFile(Content dataSource, String tempDirPath) {
195 FileManager fileManager = Case.getCurrentCase().getServices().getFileManager();
197 List<AbstractFile> sruFiles;
200 sruFiles = fileManager.findFiles(dataSource,
"SRUDB.DAT");
201 }
catch (TskCoreException ex) {
202 this.addErrorMessage(Bundle.ExtractSru_process_errormsg_find_srudb_dat());
203 logger.log(Level.WARNING,
"Unable to find SRUDB.DAT file.", ex);
207 AbstractFile sruAbstractFile = null;
209 for (AbstractFile sruFile : sruFiles) {
211 String sruFileName = tempDirPath + File.separator + sruFile.getId() +
"_" + sruFile.getName();
212 sruAbstractFile = sruFile;
215 ContentUtils.writeToFile(sruFile,
new File(sruFileName));
216 }
catch (IOException ex) {
217 this.addErrorMessage(Bundle.ExtractSru_process_errormsg_write_srudb_dat());
218 logger.log(Level.WARNING, String.format(
"Unable to write %s to temp directory. File name: %s", sruFile.getName(), sruFile), ex);
223 return sruAbstractFile;
236 void extractSruFiles(String sruExePath, String sruFile, String tempOutFile, String tempOutPath, String softwareHiveFile)
throws IOException {
237 final Path outputFilePath = Paths.get(tempOutPath, SRU_OUTPUT_FILE_NAME);
238 final Path errFilePath = Paths.get(tempOutPath, SRU_ERROR_FILE_NAME);
240 List<String> commandLine =
new ArrayList<>();
241 commandLine.add(sruExePath);
242 commandLine.add(sruFile);
243 commandLine.add(softwareHiveFile);
244 commandLine.add(tempOutFile);
246 ProcessBuilder processBuilder =
new ProcessBuilder(commandLine);
247 processBuilder.redirectOutput(outputFilePath.toFile());
248 processBuilder.redirectError(errFilePath.toFile());
250 ExecUtil.execute(processBuilder,
new DataSourceIngestModuleProcessTerminator(context,
true));
253 private String getPathForSruDumper() {
255 if (PlatformUtil.isWindowsOS()) {
256 if (PlatformUtil.is64BitOS()) {
257 path = Paths.get(SRU_TOOL_FOLDER, SRU_TOOL_NAME_WINDOWS_64);
259 path = Paths.get(SRU_TOOL_FOLDER, SRU_TOOL_NAME_WINDOWS_32);
262 if (
"Linux".equals(PlatformUtil.getOSName())) {
263 path = Paths.get(SRU_TOOL_FOLDER, SRU_TOOL_NAME_LINUX);
265 path = Paths.get(SRU_TOOL_FOLDER, SRU_TOOL_NAME_MAC);
268 File sruToolFile = InstalledFileLocator.getDefault().locate(path.toString(),
269 ExtractSru.class.getPackage().getName(),
false);
270 if (sruToolFile != null) {
271 return sruToolFile.getAbsolutePath();
277 private void findSruExecutedFiles(String sruDb, Content dataSource) {
281 String sqlStatement =
"SELECT DISTINCT SUBSTR(LTRIM(IdBlob, '\\Device\\HarddiskVolume'), INSTR(LTRIM(IdBlob, '\\Device\\HarddiskVolume'), '\\')) "
282 +
" application_name, idBlob source_name FROM SruDbIdMapTable WHERE idType = 0 AND idBlob NOT LIKE '!!%'";
284 try (SQLiteDBConnect tempdbconnect =
new SQLiteDBConnect(
"org.sqlite.JDBC",
"jdbc:sqlite:" + sruDb);
285 ResultSet resultSet = tempdbconnect.executeQry(sqlStatement)) {
287 while (resultSet.next()) {
289 if (context.dataSourceIngestIsCancelled()) {
290 logger.log(Level.INFO,
"Cancelled SRU Artifact Creation.");
294 String applicationName = resultSet.getString(
"application_name");
295 String sourceName = resultSet.getString(
"source_name");
297 String normalizePathName = FilenameUtils.normalize(applicationName,
true);
298 String fileName = FilenameUtils.getName(normalizePathName);
299 String filePath = FilenameUtils.getPath(normalizePathName);
300 if (fileName.contains(
" [")) {
301 fileName = fileName.substring(0, fileName.indexOf(
" ["));
303 List<AbstractFile> sourceFiles;
305 sourceFiles = fileManager.
findFiles(dataSource, fileName, filePath);
306 for (AbstractFile sourceFile : sourceFiles) {
307 if (sourceFile.getParentPath().endsWith(filePath)) {
308 applicationFilesFound.put(sourceName.toLowerCase(), sourceFile);
312 }
catch (TskCoreException ex) {
313 logger.log(Level.WARNING, String.format(
"Error finding actual file %s. file may not exist", normalizePathName));
316 }
catch (SQLException ex) {
317 logger.log(Level.WARNING,
"Error while trying to read into a sqlite db.", ex);
322 private void createNetUsageArtifacts(String sruDb, AbstractFile sruAbstractFile) {
323 List<BlackboardArtifact> bba =
new ArrayList<>();
325 String sqlStatement =
"SELECT STRFTIME('%s', timestamp) ExecutionTime, a.application_name, b.Application_Name formatted_application_name, User_Name, "
326 +
" bytesSent, BytesRecvd FROM network_Usage a, SruDbIdMapTable, exe_to_app b "
327 +
" where appId = IdIndex and IdType = 0 and a.application_name = b.source_name order by ExecutionTime;";
329 try (SQLiteDBConnect tempdbconnect =
new SQLiteDBConnect(
"org.sqlite.JDBC",
"jdbc:sqlite:" + sruDb);
330 ResultSet resultSet = tempdbconnect.executeQry(sqlStatement)) {
332 while (resultSet.next()) {
334 if (context.dataSourceIngestIsCancelled()) {
335 logger.log(Level.INFO,
"Cancelled SRU Net Usage Artifact Creation.");
339 String applicationName = resultSet.getString(
"Application_Name");
340 String formattedApplicationName = resultSet.getString(
"formatted_Application_name");
341 Long executionTime = Long.valueOf(resultSet.getInt(
"ExecutionTime"));
342 Long bytesSent = Long.valueOf(resultSet.getInt(
"bytesSent"));
343 Long bytesRecvd = Long.valueOf(resultSet.getInt(
"BytesRecvd"));
344 String userName = resultSet.getString(
"User_Name");
346 Collection<BlackboardAttribute> bbattributes = Arrays.asList(
347 new BlackboardAttribute(
348 BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PROG_NAME, getDisplayName(),
349 formattedApplicationName),
350 new BlackboardAttribute(
351 BlackboardAttribute.ATTRIBUTE_TYPE.TSK_USER_NAME, getDisplayName(),
353 new BlackboardAttribute(
354 BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME, getDisplayName(),
356 new BlackboardAttribute(
357 BlackboardAttribute.ATTRIBUTE_TYPE.TSK_BYTES_SENT, getDisplayName(), bytesSent),
358 new BlackboardAttribute(
359 BlackboardAttribute.ATTRIBUTE_TYPE.TSK_BYTES_RECEIVED, getDisplayName(), bytesRecvd),
360 new BlackboardAttribute(
361 BlackboardAttribute.ATTRIBUTE_TYPE.TSK_COMMENT, getDisplayName(), NETWORK_USAGE_SOURCE_NAME));
364 BlackboardArtifact bbart = createArtifactWithAttributes(BlackboardArtifact.Type.TSK_PROG_RUN, sruAbstractFile, bbattributes);
366 BlackboardArtifact associateBbArtifact = createAssociatedArtifact(applicationName.toLowerCase(), bbart);
367 if (associateBbArtifact != null) {
368 bba.add(associateBbArtifact);
370 }
catch (TskCoreException ex) {
371 logger.log(Level.SEVERE,
"Exception Adding Artifact.", ex);
375 }
catch (SQLException ex) {
376 logger.log(Level.SEVERE,
"Error while trying to read into a sqlite db.", ex);
379 if (!context.dataSourceIngestIsCancelled()) {
384 private void createAppUsageArtifacts(String sruDb, AbstractFile sruAbstractFile) {
385 List<BlackboardArtifact> bba =
new ArrayList<>();
387 String sqlStatement =
"SELECT STRFTIME('%s', timestamp) ExecutionTime, a.application_name, b.Application_Name formatted_application_name, User_Name "
388 +
" FROM Application_Resource_Usage a, SruDbIdMapTable, exe_to_app b WHERE "
389 +
" idType = 0 and idIndex = appId and a.application_name = b.source_name order by ExecutionTime;";
391 try (SQLiteDBConnect tempdbconnect =
new SQLiteDBConnect(
"org.sqlite.JDBC",
"jdbc:sqlite:" + sruDb);
392 ResultSet resultSet = tempdbconnect.executeQry(sqlStatement)) {
394 while (resultSet.next()) {
396 if (context.dataSourceIngestIsCancelled()) {
397 logger.log(Level.INFO,
"Cancelled SRU Net Usage Artifact Creation.");
401 String applicationName = resultSet.getString(
"Application_Name");
402 String formattedApplicationName = resultSet.getString(
"formatted_application_name");
403 Long executionTime = Long.valueOf(resultSet.getInt(
"ExecutionTime"));
404 String userName = resultSet.getString(
"User_Name");
406 Collection<BlackboardAttribute> bbattributes = Arrays.asList(
407 new BlackboardAttribute(
408 BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PROG_NAME, getDisplayName(),
409 formattedApplicationName),
410 new BlackboardAttribute(
411 BlackboardAttribute.ATTRIBUTE_TYPE.TSK_USER_NAME, getDisplayName(),
413 new BlackboardAttribute(
414 BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME, getDisplayName(),
416 new BlackboardAttribute(
417 BlackboardAttribute.ATTRIBUTE_TYPE.TSK_COMMENT, getDisplayName(), APPLICATION_USAGE_SOURCE_NAME));
420 BlackboardArtifact bbart = createArtifactWithAttributes(BlackboardArtifact.Type.TSK_PROG_RUN, sruAbstractFile, bbattributes);
422 BlackboardArtifact associateBbArtifact = createAssociatedArtifact(applicationName.toLowerCase(), bbart);
423 if (associateBbArtifact != null) {
424 bba.add(associateBbArtifact);
426 }
catch (TskCoreException ex) {
427 logger.log(Level.SEVERE,
"Exception Adding Artifact.", ex);
431 }
catch (SQLException ex) {
432 logger.log(Level.SEVERE,
"Error while trying to read into a sqlite db.", ex);
435 if (!context.dataSourceIngestIsCancelled()) {
451 private BlackboardArtifact createAssociatedArtifact(String filePathName, BlackboardArtifact bba)
throws TskCoreException {
452 if (applicationFilesFound.containsKey(filePathName)) {
453 AbstractFile sourceFile = applicationFilesFound.get(filePathName);
454 return createAssociatedArtifact(sourceFile, bba);
List< AbstractFile > findFiles(String fileName)