19 package org.sleuthkit.autopsy.datasourcesummary.datamodel;
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.Date;
29 import java.util.HashSet;
30 import java.util.List;
32 import java.util.function.Function;
33 import java.util.stream.Collectors;
34 import org.apache.commons.lang.StringUtils;
37 import org.
sleuthkit.datamodel.BlackboardArtifact.ARTIFACT_TYPE;
49 ARTIFACT_TYPE.TSK_PROG_RUN.getTypeID()
83 if (pathList.size() < 2) {
87 String rootParent = pathList.get(0).toUpperCase();
88 if (
"PROGRAM FILES".equals(rootParent) ||
"PROGRAM FILES (X86)".equals(rootParent)) {
89 return pathList.get(1);
96 for (String pathEl : pathList) {
97 String uppered = pathEl.toUpperCase();
98 if (
"APPLICATION DATA".equals(uppered) ||
"APPDATA".equals(uppered)) {
124 String innerQueryName =
"inner_attribute_" + queryName;
126 return "\n" + joinType +
" JOIN (\n"
128 +
" " + innerQueryName +
".artifact_id,\n"
129 +
" " + innerQueryName +
"." + attributeColumn +
" AS " + keyName +
"\n"
130 +
" FROM blackboard_attributes " + innerQueryName +
"\n"
131 +
" WHERE " + innerQueryName +
".attribute_type_id = " + attrType.getTypeID() +
" -- " + attrType.name() +
"\n"
132 +
") " + queryName +
" ON " + queryName +
".artifact_id = " + bbaName +
".artifact_id\n";
143 return key + QUERY_SUFFIX +
"." + key;
155 if (clauses.isEmpty()) {
159 List<String> parenthesized = clauses.stream()
160 .map(c ->
"(" + c +
")")
161 .collect(Collectors.toList());
163 return "\nWHERE " + String.join(
"\n AND ", parenthesized) +
"\n";
175 private static String
getLikeClause(String column, String likeString,
boolean isLike) {
176 return column + (isLike ?
"" :
" NOT") +
" LIKE '" + likeString +
"'";
210 if (dataSource == null || count <= 0) {
211 return Collections.emptyList();
215 final String ntosBootIdentifier =
"NTOSBOOT";
217 final String windowsDir =
"/WINDOWS%";
219 final String nameParam =
"name";
220 final String pathParam =
"path";
221 final String runCountParam =
"run_count";
222 final String lastRunParam =
"last_run";
224 String bbaQuery =
"bba";
226 final String query =
"SELECT\n"
227 +
" " +
getFullKey(nameParam) +
" AS " + nameParam +
",\n"
228 +
" " +
getFullKey(pathParam) +
" AS " + pathParam +
",\n"
229 +
" MAX(" +
getFullKey(runCountParam) +
") AS " + runCountParam +
",\n"
230 +
" MAX(" +
getFullKey(lastRunParam) +
") AS " + lastRunParam +
"\n"
231 +
"FROM blackboard_artifacts " + bbaQuery +
"\n"
237 bbaQuery +
".artifact_type_id = " + BlackboardArtifact.ARTIFACT_TYPE.TSK_PROG_RUN.getTypeID(),
238 bbaQuery +
".data_source_obj_id = " + dataSource.getId(),
246 +
" MAX(" +
getFullKey(runCountParam) +
") DESC,\n"
247 +
" MAX(" +
getFullKey(lastRunParam) +
") DESC,\n"
250 DataSourceInfoUtilities.ResultSetHandler<List<TopProgramsResult>> handler = (resultSet) -> {
251 List<TopProgramsResult> progResults =
new ArrayList<>();
253 boolean quitAtCount =
false;
255 while (resultSet.next() && (!quitAtCount || progResults.size() < count)) {
256 long lastRunEpoch = resultSet.getLong(lastRunParam);
257 Date lastRun = (resultSet.wasNull()) ? null :
new Date(lastRunEpoch * 1000);
259 Long runCount = resultSet.getLong(runCountParam);
260 if (resultSet.wasNull()) {
264 if (lastRun != null || runCount != null) {
269 resultSet.getString(nameParam),
270 resultSet.getString(pathParam),
278 try (SleuthkitCase.CaseDbQuery dbQuery = provider.
get().executeQuery(query);
279 ResultSet resultSet = dbQuery.getResultSet()) {
281 return handler.process(resultSet);
294 if (strPath == null) {
298 List<String> pathEls =
new ArrayList<>(Arrays.asList(applicationName));
300 File file =
new File(strPath);
301 while (file != null && StringUtils.isNotBlank(file.getName())) {
302 pathEls.add(file.getName());
303 file = file.getParentFile();
306 Collections.reverse(pathEls);
308 for (Function<List<String>, String> matchEntry : SHORT_FOLDER_MATCHERS) {
309 String result = matchEntry.apply(pathEls);
310 if (StringUtils.isNotBlank(result)) {
335 TopProgramsResult(String programName, String programPath, Long runTimes, Date lastRun) {
static String getAttributeJoin(JoinType joinType, AttributeColumn attributeColumn, BlackboardAttribute.ATTRIBUTE_TYPE attrType, String keyName, String bbaName)
Set< Integer > getArtifactTypeIdsForRefresh()
SleuthkitCaseProvider DEFAULT
static String getFullKey(String key)
List< TopProgramsResult > getTopPrograms(DataSource dataSource, int count)
static final Set< Integer > ARTIFACT_UPDATE_TYPE_IDS
static String getWhereString(List< String > clauses)
static String getLikeClause(String column, String likeString, boolean isLike)
static final List< Function< List< String >, String > > SHORT_FOLDER_MATCHERS
static final String QUERY_SUFFIX
String getShortFolderName(String strPath, String applicationName)
final SleuthkitCaseProvider provider
TopProgramsSummary(SleuthkitCaseProvider provider)