Skip to content

Commit b55e2e6

Browse files
author
Karl Mortensen
committed
Scrub SQL, document how to use user-supplied queries safely
1 parent 5db7a98 commit b55e2e6

29 files changed

+1768
-1023
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ genfiles.properties
6565
!/Testing/nbproject/project.properties
6666
*~
6767
/netbeans-plat
68-
/docs/doxygen/doxygen_docs
6968
/docs/doxygen-user/user-docs
7069
/jdiff-javadocs/*
7170
/jdiff-logs/*

Core/src/org/sleuthkit/autopsy/datamodel/AbstractAbstractFileNode.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ static String getContentDisplayName(AbstractFile file) {
227227
}
228228
return name;
229229
}
230-
@SuppressWarnings("deprecation")
230+
231231
private static String getHashSetHitsForFile(AbstractFile content) {
232232
String strList = "";
233233
SleuthkitCase skCase = content.getSleuthkitCase();
@@ -255,7 +255,6 @@ private static String getHashSetHitsForFile(AbstractFile content) {
255255
} catch (TskCoreException | SQLException ex) {
256256
logger.log(Level.WARNING, "Error getting hashset hits: ", ex); //NON-NLS
257257
}
258-
259258
return strList;
260259
}
261260

Core/src/org/sleuthkit/autopsy/datamodel/RecentFilesChildren.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private long getLastTime() {
7777

7878
//TODO add a generic query to SleuthkitCase
7979
private String createMaxQuery(String attr) {
80-
return "SELECT MAX(" + attr + ") from tsk_files WHERE " + attr + " < " + System.currentTimeMillis() / 1000; //NON-NLS
80+
return "SELECT MAX(" + attr + ") FROM tsk_files WHERE " + attr + " < " + System.currentTimeMillis() / 1000; //NON-NLS
8181
}
8282

8383
@SuppressWarnings("deprecation")

Core/src/org/sleuthkit/autopsy/filesearch/FileSearchPanel.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ private void search() {
151151
List<AbstractFile> contentList = null;
152152
try {
153153
SleuthkitCase tskDb = currentCase.getSleuthkitCase();
154-
//ResultSet rs = tempDb.runQuery(this.getQuery("count(*) as TotalMatches"));
155154
contentList = tskDb.findAllFilesWhere(this.getQuery());
156155

157156
} catch (TskCoreException ex) {
@@ -205,11 +204,11 @@ private void search() {
205204
*/
206205
private String getQuery() throws FilterValidationException {
207206

208-
//String query = "select " + tempQuery + " from tsk_files where 1";
207+
//String query = "SELECT " + tempQuery + " FROM tsk_files WHERE 1";
209208
String query = " 1";
210209

211210
for (FileSearchFilter f : this.getEnabledFilters()) {
212-
query += " and (" + f.getPredicate() + ")"; //NON-NLS
211+
query += " AND (" + f.getPredicate() + ")"; //NON-NLS
213212
}
214213

215214
return query;

Core/src/org/sleuthkit/autopsy/modules/android/BrowserLocationAnalyzer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private static void findGeoLocationsInDB(String DatabasePath, AbstractFile f) {
8787

8888
try {
8989
resultSet = statement.executeQuery(
90-
"Select timestamp, latitude, longitude, accuracy FROM CachedPosition;"); //NON-NLS
90+
"SELECT timestamp, latitude, longitude, accuracy FROM CachedPosition;"); //NON-NLS
9191

9292
while (resultSet.next()) {
9393
Long timestamp = Long.valueOf(resultSet.getString("timestamp")) / 1000; //NON-NLS

Core/src/org/sleuthkit/autopsy/modules/android/GoogleMapLocationAnalyzer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private static void findGeoLocationsInDB(String DatabasePath, AbstractFile f) {
8787

8888
try {
8989
resultSet = statement.executeQuery(
90-
"Select time,dest_lat,dest_lng,dest_title,dest_address,source_lat,source_lng FROM destination_history;"); //NON-NLS
90+
"SELECT time,dest_lat,dest_lng,dest_title,dest_address,source_lat,source_lng FROM destination_history;"); //NON-NLS
9191

9292
while (resultSet.next()) {
9393
Long time = Long.valueOf(resultSet.getString("time")) / 1000; //NON-NLS

Core/src/org/sleuthkit/autopsy/modules/android/TangoMessageAnalyzer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private static void findTangoMessagesInDB(String DatabasePath, AbstractFile f) {
8484

8585
try {
8686
resultSet = statement.executeQuery(
87-
"Select conv_id, create_time,direction,payload FROM messages ORDER BY create_time DESC;"); //NON-NLS
87+
"SELECT conv_id, create_time,direction,payload FROM messages ORDER BY create_time DESC;"); //NON-NLS
8888

8989
String conv_id; // seems to wrap around the message found in payload after decoding from base-64
9090
String direction; // 1 incoming, 2 outgoing

Core/src/org/sleuthkit/autopsy/modules/android/TextMessageAnalyzer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private static void findTextsInDB(String DatabasePath, AbstractFile f) {
8484

8585
try {
8686
resultSet = statement.executeQuery(
87-
"Select address,date,read,type,subject,body FROM sms;"); //NON-NLS
87+
"SELECT address,date,read,type,subject,body FROM sms;"); //NON-NLS
8888

8989
String address; // may be phone number, or other addresses
9090

Core/src/org/sleuthkit/autopsy/modules/iOS/ContactAnalyzer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void findContacts() {
5757
try {
5858
SleuthkitCase skCase = Case.getCurrentCase().getSleuthkitCase();
5959
absFiles = skCase.findAllFilesWhere("name LIKE '%call_history%' "); //NON-NLS //get exact file names
60-
if (absFiles.isEmpty()) { //asdfkjasfakljsdfhlaksdjfhasdlkjf
60+
if (absFiles.isEmpty()) {
6161
return;
6262
}
6363
for (AbstractFile AF : absFiles) {

Core/src/org/sleuthkit/autopsy/modules/iOS/TextMessageAnalyzer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private void findTextsInDB(String DatabasePath, long fId) {
8787
AbstractFile f = skCase.getAbstractFileById(fId);
8888
try {
8989
resultSet = statement.executeQuery(
90-
"Select address,date,type,subject,body FROM sms;"); //NON-NLS
90+
"SELECT address,date,type,subject,body FROM sms;"); //NON-NLS
9191

9292
BlackboardArtifact bba;
9393
String address; // may be phone number, or other addresses

Core/src/org/sleuthkit/autopsy/report/ReportBodyFile.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ public static synchronized ReportBodyFile getDefault() {
6767
* @param progressPanel panel to update the report's progress
6868
*/
6969
@Override
70-
@SuppressWarnings("deprecation")
7170
public void generateReport(String path, ReportProgressPanel progressPanel) {
7271
// Start the progress bar and setup the report
7372
progressPanel.setIndeterminate(false);
@@ -85,7 +84,7 @@ public void generateReport(String path, ReportProgressPanel progressPanel) {
8584
+ " AND name != '.' AND name != '..'"; //NON-NLS
8685

8786
progressPanel.updateStatusLabel(NbBundle.getMessage(this.getClass(), "ReportBodyFile.progress.loading"));
88-
List<FsContent> fs = skCase.findFilesWhere(query);
87+
List<AbstractFile> fs = skCase.findAllFilesWhere(query);
8988

9089
// Check if ingest has finished
9190
String ingestwarning = "";
@@ -103,7 +102,7 @@ public void generateReport(String path, ReportProgressPanel progressPanel) {
103102
out.write(ingestwarning);
104103
// Loop files and write info to report
105104
int count = 0;
106-
for (FsContent file : fs) {
105+
for (AbstractFile file : fs) {
107106
if (progressPanel.getStatus() == ReportStatus.CANCELED) {
108107
break;
109108
}
@@ -164,7 +163,7 @@ public void generateReport(String path, ReportProgressPanel progressPanel) {
164163
logger.log(Level.SEVERE, errorMessage, ex);
165164
}
166165
}
167-
progressPanel.complete();
166+
progressPanel.complete(ReportStatus.COMPLETE);
168167
} catch(TskCoreException ex) {
169168
logger.log(Level.WARNING, "Failed to get the unique path.", ex); //NON-NLS
170169
}

Core/src/org/sleuthkit/autopsy/report/ReportGenerator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ private List<AbstractFile> getFiles() {
373373
List<AbstractFile> absFiles;
374374
try {
375375
SleuthkitCase skCase = Case.getCurrentCase().getSleuthkitCase();
376-
absFiles = skCase.findAllFilesWhere("NOT meta_type = " + TskData.TSK_FS_META_TYPE_ENUM.TSK_FS_META_TYPE_DIR.getValue()); //NON-NLS
376+
absFiles = skCase.findAllFilesWhere("meta_type != " + TskData.TSK_FS_META_TYPE_ENUM.TSK_FS_META_TYPE_DIR.getValue()); //NON-NLS
377377
return absFiles;
378378
} catch (TskCoreException ex) {
379379
MessageNotifyUtil.Notify.show(

Core/src/org/sleuthkit/autopsy/timeline/TimeLineController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ synchronized void openTimeLine() {
358358
@SuppressWarnings("deprecation")
359359
private long getCaseLastArtifactID(final SleuthkitCase sleuthkitCase) {
360360
long caseLastArtfId = -1;
361-
String query = "select Max(artifact_id) as max_id from blackboard_artifacts"; // NON-NLS
361+
String query = "SELECT MAX(artifact_id) AS max_id FROM blackboard_artifacts"; // NON-NLS
362362

363363
try (CaseDbQuery dbQuery = sleuthkitCase.executeQuery(query)) {
364364
ResultSet resultSet = dbQuery.getResultSet();

Core/src/org/sleuthkit/autopsy/timeline/events/db/EventDB.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,9 @@ public Interval getSpanningInterval(Collection<Long> eventIDs) {
274274
try (Statement stmt = con.createStatement();
275275
//You can't inject multiple values into one ? paramater in prepared statement,
276276
//so we make new statement each time...
277-
ResultSet rs = stmt.executeQuery("select Min(time), Max(time) from events where event_id in (" + StringUtils.join(eventIDs, ", ") + ")");) { // NON-NLS
277+
ResultSet rs = stmt.executeQuery("SELECT MIN(time), MAX(time) FROM events WHERE event_id IN (" + StringUtils.join(eventIDs, ", ") + ")");) { // NON-NLS
278278
while (rs.next()) {
279-
span = new Interval(rs.getLong("Min(time)"), rs.getLong("Max(time)") + 1, DateTimeZone.UTC); // NON-NLS
279+
span = new Interval(rs.getLong("MIN(time)"), rs.getLong("MAX(time)") + 1, DateTimeZone.UTC); // NON-NLS
280280

281281
}
282282
} catch (SQLException ex) {
@@ -314,7 +314,7 @@ int countAllEvents() {
314314
int result = -1;
315315
dbReadLock();
316316
//TODO convert this to prepared statement -jm
317-
try (ResultSet rs = con.createStatement().executeQuery("select count(*) as count from events")) { // NON-NLS
317+
try (ResultSet rs = con.createStatement().executeQuery("SELECT count(*) AS count FROM events")) { // NON-NLS
318318
while (rs.next()) {
319319
result = rs.getInt("count"); // NON-NLS
320320
break;
@@ -444,7 +444,7 @@ Set<Long> getEventIDs(Long startTime, Long endTime, Filter filter) {
444444
Set<Long> resultIDs = new HashSet<>();
445445

446446
dbReadLock();
447-
final String query = "select event_id from events where time >= " + startTime + " and time <" + endTime + " and " + getSQLWhere(filter); // NON-NLS
447+
final String query = "SELECT event_id FROM events WHERE time >= " + startTime + " AND time <" + endTime + " AND " + getSQLWhere(filter); // NON-NLS
448448
//System.out.println(query);
449449
try (Statement stmt = con.createStatement();
450450
ResultSet rs = stmt.executeQuery(query)) {
@@ -597,11 +597,11 @@ final synchronized void initializeDB() {
597597
"INSERT INTO events (file_id ,artifact_id, time, sub_type, base_type, full_description, med_description, short_description, known_state) " // NON-NLS
598598
+ "VALUES (?,?,?,?,?,?,?,?,?)"); // NON-NLS
599599

600-
getMaxTimeStmt = prepareStatement("select Max(time) as max from events"); // NON-NLS
601-
getMinTimeStmt = prepareStatement("select Min(time) as min from events"); // NON-NLS
602-
getEventByIDStmt = prepareStatement("select * from events where event_id = ?"); // NON-NLS
603-
recordDBInfoStmt = prepareStatement("insert or replace into db_info (key, value) values (?, ?)"); // NON-NLS
604-
getDBInfoStmt = prepareStatement("select value from db_info where key = ?"); // NON-NLS
600+
getMaxTimeStmt = prepareStatement("SELECT MAX(time) AS max FROM events"); // NON-NLS
601+
getMinTimeStmt = prepareStatement("SELECT MIN(time) AS min FROM events"); // NON-NLS
602+
getEventByIDStmt = prepareStatement("SELECT * FROM events WHERE event_id = ?"); // NON-NLS
603+
recordDBInfoStmt = prepareStatement("INSERT OR REPLACE INTO db_info (key, value) VALUES (?, ?)"); // NON-NLS
604+
getDBInfoStmt = prepareStatement("SELECT value FROM db_info WHERE key = ?"); // NON-NLS
605605
} catch (SQLException sQLException) {
606606
LOGGER.log(Level.SEVERE, "failed to prepareStatment", sQLException); // NON-NLS
607607
}
@@ -795,8 +795,8 @@ private Map<EventType, Long> countEvents(Long startTime, Long endTime, Filter fi
795795
final boolean useSubTypes = (zoomLevel == EventTypeZoomLevel.SUB_TYPE);
796796

797797
//get some info about the range of dates requested
798-
final String queryString = "select count(*), " + (useSubTypes ? SUB_TYPE_COLUMN : BASE_TYPE_COLUMN) // NON-NLS
799-
+ " from events where time >= " + startTime + " and time < " + endTime + " and " + getSQLWhere(filter) // NON-NLS
798+
final String queryString = "SELECT count(*), " + (useSubTypes ? SUB_TYPE_COLUMN : BASE_TYPE_COLUMN) // NON-NLS
799+
+ " FROM events WHERE time >= " + startTime + " AND time < " + endTime + " AND " + getSQLWhere(filter) // NON-NLS
800800
+ " GROUP BY " + (useSubTypes ? SUB_TYPE_COLUMN : BASE_TYPE_COLUMN); // NON-NLS
801801

802802
ResultSet rs = null;
@@ -876,10 +876,10 @@ private List<AggregateEvent> getAggregatedEvents(Interval timeRange, Filter filt
876876

877877
//get all agregate events in this time unit
878878
dbReadLock();
879-
String query = "select strftime('" + strfTimeFormat + "',time , 'unixepoch'" + (TimeLineController.getTimeZone().get().equals(TimeZone.getDefault()) ? ", 'localtime'" : "") + ") as interval, group_concat(event_id) as event_ids, Min(time), Max(time), " + descriptionColumn + ", " + (useSubTypes ? SUB_TYPE_COLUMN : BASE_TYPE_COLUMN) // NON-NLS
880-
+ " from events where time >= " + start + " and time < " + end + " and " + getSQLWhere(filter) // NON-NLS
881-
+ " group by interval, " + (useSubTypes ? SUB_TYPE_COLUMN : BASE_TYPE_COLUMN) + " , " + descriptionColumn // NON-NLS
882-
+ " order by Min(time)"; // NON-NLS
879+
String query = "SELECT strftime('" + strfTimeFormat + "',time , 'unixepoch'" + (TimeLineController.getTimeZone().get().equals(TimeZone.getDefault()) ? ", 'localtime'" : "") + ") as interval, group_concat(event_id) as event_ids, Min(time), Max(time), " + descriptionColumn + ", " + (useSubTypes ? SUB_TYPE_COLUMN : BASE_TYPE_COLUMN) // NON-NLS
880+
+ " FROM events where time >= " + start + " AND time < " + end + " AND " + getSQLWhere(filter) // NON-NLS
881+
+ " GROUP BY interval, " + (useSubTypes ? SUB_TYPE_COLUMN : BASE_TYPE_COLUMN) + " , " + descriptionColumn // NON-NLS
882+
+ " ORDER BY MIN(time)"; // NON-NLS
883883
//System.out.println(query);
884884
ResultSet rs = null;
885885
try (Statement stmt = con.createStatement(); // scoop up requested events in groups organized by interval, type, and desription

ImageGallery/src/org/sleuthkit/autopsy/imagegallery/ImageGalleryController.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -682,12 +682,12 @@ public void run() {
682682

683683
try {
684684
//grab all files with supported extension or mime types
685-
final List<AbstractFile> files = getSleuthKitCase().findAllFilesWhere(DRAWABLE_QUERY + " or tsk_files.obj_id in (select tsk_files.obj_id from tsk_files , blackboard_artifacts, blackboard_attributes"
686-
+ " where blackboard_artifacts.obj_id = tsk_files.obj_id"
687-
+ " and blackboard_attributes.artifact_id = blackboard_artifacts.artifact_id"
688-
+ " and blackboard_artifacts.artifact_type_id = " + BlackboardArtifact.ARTIFACT_TYPE.TSK_GEN_INFO.getTypeID()
689-
+ " and blackboard_attributes.attribute_type_id = " + BlackboardAttribute.ATTRIBUTE_TYPE.TSK_FILE_TYPE_SIG.getTypeID()
690-
+ " and blackboard_attributes.value_text in ('" + StringUtils.join(ImageGalleryModule.getSupportedMimes(), "','") + "'))");
685+
final List<AbstractFile> files = getSleuthKitCase().findAllFilesWhere(DRAWABLE_QUERY + " OR tsk_files.obj_id IN (select tsk_files.obj_id FROM tsk_files , blackboard_artifacts, blackboard_attributes"
686+
+ " WHERE blackboard_artifacts.obj_id = tsk_files.obj_id"
687+
+ " AND blackboard_attributes.artifact_id = blackboard_artifacts.artifact_id"
688+
+ " AND blackboard_artifacts.artifact_type_id = " + BlackboardArtifact.ARTIFACT_TYPE.TSK_GEN_INFO.getTypeID()
689+
+ " AND blackboard_attributes.attribute_type_id = " + BlackboardAttribute.ATTRIBUTE_TYPE.TSK_FILE_TYPE_SIG.getTypeID()
690+
+ " AND blackboard_attributes.value_text IN ('" + StringUtils.join(ImageGalleryModule.getSupportedMimes(), "','") + "'))");
691691
progressHandle.switchToDeterminate(files.size());
692692

693693
updateProgress(0.0);
@@ -807,7 +807,7 @@ public void run() {
807807
for (FileSystem fs : image.getFileSystems()) {
808808
fsObjIds.add(fs.getId());
809809
}
810-
fsQuery = "(fs_obj_id = " + StringUtils.join(fsObjIds, " or fs_obj_id = ") + ") ";
810+
fsQuery = "(fs_obj_id = " + StringUtils.join(fsObjIds, " OR fs_obj_id = ") + ") ";
811811
}
812812
// NOTE: Logical files currently (Apr '15) have a null value for fs_obj_id in DB.
813813
// for them, we will not specify a fs_obj_id, which means we will grab files
@@ -816,7 +816,7 @@ public void run() {
816816
fsQuery = "(fs_obj_id IS NULL) ";
817817
}
818818

819-
files = getSleuthKitCase().findAllFilesWhere(fsQuery + " and " + DRAWABLE_QUERY);
819+
files = getSleuthKitCase().findAllFilesWhere(fsQuery + " AND " + DRAWABLE_QUERY);
820820
progressHandle.switchToDeterminate(files.size());
821821

822822
//do in transaction

0 commit comments

Comments
 (0)