-
Notifications
You must be signed in to change notification settings - Fork 593
/
SleuthkitJNI.java
1052 lines (924 loc) · 35.1 KB
/
SleuthkitJNI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Sleuth Kit Data Model
*
* Copyright 2011-2016 Basis Technology Corp.
* Contact: carrier <at> sleuthkit <dot> org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.sleuthkit.datamodel;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Path;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.sleuthkit.datamodel.TskData.TSK_FS_ATTR_TYPE_ENUM;
/**
* A utility class that provides a interface to the SleuthKit via JNI. Supports
* case management, add image process, reading data off content objects Setting
* up Hash database parameters and updating / reading values
*
* Caches image and filesystem handles and reuses them for the duration of the
* application
*/
public class SleuthkitJNI {
/*
* Loads the SleuthKit libraries.
*/
static {
LibraryUtils.loadSleuthkitJNI();
}
/*
* A monitor used to guard access to cached SleuthKit image and file system
* handles.
*/
private static final Object cacheLock = new Object();
/**
* Contructor for the utility class that provides a interface to the
* SleuthKit via JNI.
*/
private SleuthkitJNI() {
}
/**
* Encapsulates a handle to a SleuthKit case database with support for
* adding images to the database.
*/
public static class CaseDbHandle {
/*
* A pointer to a TskCaseDb object.
*/
private final long caseDbPointer;
/*
* A SleuthKit image handle cache implemented as a mappng of
* concatenated image file paths to image handles.
*/
private static final Map<String, Long> imageHandleCache = new HashMap<String, Long>();
/*
* A SleuthKit file system handles cache implemented as a mapping of
* image handles to image offset and file system handle pairs.
*/
private static final Map<Long, Map<Long, Long>> fsHandleCache = new HashMap<Long, Map<Long, Long>>();
/**
* Constructs an object that encapsulates a handle to a SleuthKit case
* database with support for adding images to the database.
*
* @param caseDbPointer A pointer to a TskCaseDb object.
*/
private CaseDbHandle(long caseDbPointer) {
this.caseDbPointer = caseDbPointer;
}
/**
* Closes the case database and any open image and file system handles.
*
* @throws TskCoreException if there is a problem competing the
* operation.
*/
void free() throws TskCoreException {
synchronized (cacheLock) {
/*
* Close any cached file system handles.
*/
for (Map<Long, Long> imageToFsMap : fsHandleCache.values()) {
for (Long fsHandle : imageToFsMap.values()) {
closeFsNat(fsHandle);
}
}
/*
* Close any cached image handles.
*/
for (Long imageHandle : imageHandleCache.values()) {
closeImgNat(imageHandle);
}
fsHandleCache.clear();
imageHandleCache.clear();
}
SleuthkitJNI.closeCaseDbNat(caseDbPointer);
}
/**
* Adds an image to the case database. For finer-grained control of the
* process of adding the image, call CaseDbHandle.initAddImageProcess
* instead.
*
* @param deviceObjId The object id of the device associated with
* the image.
* @param imageFilePaths The image file paths.
* @param timeZone The time zone for the image.
* @param addFileSystems Pass true to attempt to add file systems
* within the image to the case database.
* @param addUnallocSpace Pass true to create virtual files for
* unallocated space. Ignored if addFileSystems
* is false.
* @param skipFatFsOrphans Pass true to skip processing of orphan files
* for FAT file systems. Ignored if
* addFileSystems is false.
*
* @return The object id of the image.
*
* @throws TskCoreException if there is an error adding the image to
* case database.
*/
long addImage(long deviceObjId, List<String> imageFilePaths, String timeZone, boolean addFileSystems, boolean addUnallocSpace, boolean skipFatFsOrphans) throws TskCoreException {
try {
long tskAutoDbPointer = initializeAddImgNat(caseDbPointer, timezoneLongToShort(timeZone), addFileSystems, addUnallocSpace, skipFatFsOrphans);
runAddImgNat(tskAutoDbPointer, UUID.randomUUID().toString(), imageFilePaths.toArray(new String[0]), imageFilePaths.size(), timeZone);
return commitAddImgNat(tskAutoDbPointer);
} catch (TskDataException ex) {
throw new TskCoreException("Error adding image to case database", ex);
}
}
/**
* Initializes a multi-step process for adding an image to the case
* database.
*
* @param timeZone The time zone of the image.
* @param addUnallocSpace Pass true to create virtual files for
* unallocated space.
* @param skipFatFsOrphans Pass true to skip processing of orphan files
* for FAT file systems.
*
* @return An object that can be used to exercise fine-grained control
* of the process of adding the image to the case database.
*/
AddImageProcess initAddImageProcess(String timeZone, boolean addUnallocSpace, boolean skipFatFsOrphans) {
return new AddImageProcess(timeZone, addUnallocSpace, skipFatFsOrphans);
}
/**
* Encapsulates a multi-step process to add an image to the case
* database.
*/
public class AddImageProcess {
private final String timeZone;
private final boolean addUnallocSpace;
private final boolean skipFatFsOrphans;
private volatile long tskAutoDbPointer;
/**
* Consdtructs an object that encapsulates a multi-step process to
* add an image to the case database.
*
* @param timeZone The time zone of the image.
* @param addUnallocSpace Pass true to create virtual files for
* unallocated space.
* @param skipFatFsOrphans Pass true to skip processing of orphan
* files for FAT file systems.
*/
private AddImageProcess(String timeZone, boolean addUnallocSpace, boolean skipFatFsOrphans) {
this.timeZone = timeZone;
this.addUnallocSpace = addUnallocSpace;
this.skipFatFsOrphans = skipFatFsOrphans;
tskAutoDbPointer = 0;
}
/**
* Starts the process of adding an image to the case database.
* Either AddImageProcess.commit or AddImageProcess.revert MUST be
* called after calling AddImageProcess.run.
*
* @param deviceId An ASCII-printable identifier for the
* device associated with the image that
* should be unique across multiple cases
* (e.g., a UUID).
* @param imageFilePaths Full path(s) to the image file(s).
*
* @throws TskCoreException if a critical error occurs within the
* SleuthKit.
* @throws TskDataException if a non-critical error occurs within
* the SleuthKit (should be OK to continue
* the process)
*/
public void run(String deviceId, String[] imageFilePaths) throws TskCoreException, TskDataException {
if (0 != tskAutoDbPointer) {
throw new TskCoreException("Add image process already started");
}
synchronized (this) {
tskAutoDbPointer = initAddImgNat(caseDbPointer, timezoneLongToShort(timeZone), addUnallocSpace, skipFatFsOrphans);
}
if (0 == tskAutoDbPointer) {
throw new TskCoreException("initAddImgNat returned a NULL TskAutoDb pointer");
}
runAddImgNat(tskAutoDbPointer, deviceId, imageFilePaths, imageFilePaths.length, timeZone);
}
/**
* Stops the process of adding the image to the case database that
* was started by calling AddImageProcess.run.
* AddImageProcess.revert should be called after calling
* AddImageProcess.stop.
*
* @throws TskCoreException if a critical error occurs within the
* SleuthKit.
*/
public void stop() throws TskCoreException {
if (tskAutoDbPointer == 0) {
throw new TskCoreException("AddImgProcess::stop: AutoDB pointer is NULL");
}
stopAddImgNat(tskAutoDbPointer);
}
/**
* Rolls back the process of adding an image to the case database
* that was started by calling AddImageProcess.run.
*
* @throws TskCoreException if a critical error occurs within the
* SleuthKit.
*/
public synchronized void revert() throws TskCoreException {
if (tskAutoDbPointer == 0) {
throw new TskCoreException("AddImgProcess::revert: AutoDB pointer is NULL");
}
revertAddImgNat(tskAutoDbPointer);
// the native code deleted the object
tskAutoDbPointer = 0;
}
/**
* Completes the process of adding an image to the case database
* that was started by calling AddImageProcess.run.
*
* @return The object id of the image that was added.
*
* @throws TskCoreException if a critical error occurs within the
* SleuthKit.
*/
public synchronized long commit() throws TskCoreException {
if (tskAutoDbPointer == 0) {
throw new TskCoreException("AddImgProcess::commit: AutoDB pointer is NULL");
}
long id = commitAddImgNat(tskAutoDbPointer);
// the native code deleted the object
tskAutoDbPointer = 0;
return id;
}
/**
* Gets the file system directory currently being processed by the
* SleuthKit.
*
* @return The directory
*/
public synchronized String currentDirectory() {
return tskAutoDbPointer == 0 ? "NO_INFO" : getCurDirNat(tskAutoDbPointer); //NON-NLS
}
/**
* Starts the process of adding an image to the case database.
* Either commit() or revert() MUST be called after calling run().
*
* @param imageFilePaths Full path(s) to the image file(s).
*
* @throws TskCoreException if a critical error occurs within the
* SleuthKit.
* @throws TskDataException if a non-critical error occurs within
* the SleuthKit (should be OK to continue
* the process)
*
* @deprecated Use run(String dataSourceId, String[] imageFilePaths)
* instead
*/
@Deprecated
public void run(String[] imageFilePaths) throws TskCoreException, TskDataException {
run(null, imageFilePaths);
}
}
}
/**
* Creates a new case database. Must call .free() on CaseDbHandle instance
* when done.
*
* @param path Location to create the database at.
*
* @return Handle for a new TskCaseDb instance.
*
* @throws TskCoreException exception thrown if critical error occurs within
* TSK
*/
static CaseDbHandle newCaseDb(String path) throws TskCoreException {
return new CaseDbHandle(newCaseDbNat(path));
}
/**
* Creates a new case database. Must call .free() on CaseDbHandle instance
* when done.
*
* @param databaseName the name of the database to create
* @param info the connection info class for the database to create
*
* @return Handle for a new TskCaseDb instance.
*
* @throws TskCoreException exception thrown if critical error occurs within
* TSK
*/
static CaseDbHandle newCaseDb(String databaseName, CaseDbConnectionInfo info) throws TskCoreException {
return new CaseDbHandle(newCaseDbMultiNat(info.getHost(), info.getPort(), info.getUserName(), info.getPassword(), info.getDbType().ordinal(), databaseName));
}
/**
* Opens an existing case database. Must call .free() on CaseDbHandle
* instance when done.
*
* @param path Location of the existing database.
*
* @return Handle for a new TskCaseDb instance.
*
* @throws TskCoreException exception thrown if critical error occurs within
* TSK
*/
static CaseDbHandle openCaseDb(String path) throws TskCoreException {
return new CaseDbHandle(openCaseDbNat(path));
}
/**
* Opens an existing case database. Must call .free() on CaseDbHandle
* instance when done.
*
* @param databaseName the name of the database to open
* @param info the connection info class for the database to open
*
* @return Handle for a new TskCaseDb instance.
*
* @throws TskCoreException exception thrown if critical error occurs within
* TSK
*/
static CaseDbHandle openCaseDb(String databaseName, CaseDbConnectionInfo info) throws TskCoreException {
return new CaseDbHandle(openCaseDbMultiNat(info.getHost(), info.getPort(), info.getUserName(), info.getPassword(), info.getDbType().ordinal(), databaseName));
}
/**
* get the Sleuth Kit version string
*
* @return the version string
*/
public static String getVersion() {
return getVersionNat();
}
/**
* Enable verbose logging and redirect stderr to the given log file.
*/
public static void startVerboseLogging(String logPath) {
startVerboseLoggingNat(logPath);
}
/**
* open the image and return the image info pointer
*
* @param imageFiles the paths to the images
*
* @return the image info pointer
*
* @throws TskCoreException exception thrown if critical error occurs within
* TSK
*/
public static long openImage(String[] imageFiles) throws TskCoreException {
long imageHandle;
StringBuilder keyBuilder = new StringBuilder();
for (int i = 0; i < imageFiles.length; ++i) {
keyBuilder.append(imageFiles[i]);
}
final String imageKey = keyBuilder.toString();
synchronized (cacheLock) {
if (CaseDbHandle.imageHandleCache.containsKey(imageKey)) //get from cache
{
imageHandle = CaseDbHandle.imageHandleCache.get(imageKey);
} else {
//open new handle and cache it
imageHandle = openImgNat(imageFiles, imageFiles.length);
CaseDbHandle.fsHandleCache.put(imageHandle, new HashMap<Long, Long>());
CaseDbHandle.imageHandleCache.put(imageKey, imageHandle);
}
}
return imageHandle;
}
/**
* Get volume system Handle
*
* @param imgHandle a handle to previously opened image
* @param vsOffset byte offset in the image to the volume system (usually
* 0)
*
* @return pointer to a vsHandle structure in the sleuthkit
*
* @throws TskCoreException exception thrown if critical error occurs within
* TSK
*/
public static long openVs(long imgHandle, long vsOffset) throws TskCoreException {
return openVsNat(imgHandle, vsOffset);
}
//get pointers
/**
* Get volume Handle
*
* @param vsHandle pointer to the volume system structure in the sleuthkit
* @param volId id of the volume
*
* @return pointer to a volHandle structure in the sleuthkit
*
* @throws TskCoreException exception thrown if critical error occurs within
* TSK
*/
public static long openVsPart(long vsHandle, long volId) throws TskCoreException {
//returned long is ptr to vs Handle object in tsk
return openVolNat(vsHandle, volId);
}
/**
* Get file system Handle Opened handle is cached (transparently) so it does
* not need be reopened next time for the duration of the application
*
* @param imgHandle pointer to imgHandle in sleuthkit
* @param fsOffset byte offset to the file system
*
* @return pointer to a fsHandle structure in the sleuthkit
*
* @throws TskCoreException exception thrown if critical error occurs within
* TSK
*/
public static long openFs(long imgHandle, long fsOffset) throws TskCoreException {
long fsHandle;
synchronized (cacheLock) {
final Map<Long, Long> imgOffSetToFsHandle = CaseDbHandle.fsHandleCache.get(imgHandle);
if (imgOffSetToFsHandle.containsKey(fsOffset)) {
//return cached
fsHandle = imgOffSetToFsHandle.get(fsOffset);
} else {
fsHandle = openFsNat(imgHandle, fsOffset);
//cache it
imgOffSetToFsHandle.put(fsOffset, fsHandle);
}
}
return fsHandle;
}
/**
* Get file Handle
*
* @param fsHandle fsHandle pointer in the sleuthkit
* @param fileId id of the file
* @param attrType file attribute type to open
* @param attrId file attribute id to open
*
* @return pointer to a file structure in the sleuthkit
*
* @throws TskCoreException exception thrown if critical error occurs within
* TSK
*/
public static long openFile(long fsHandle, long fileId, TSK_FS_ATTR_TYPE_ENUM attrType, int attrId) throws TskCoreException {
/*
* NOTE: previously attrId used to be stored in AbstractFile as (signed)
* short even though it is stored as uint16 in TSK. In extremely rare
* occurances attrId can be larger than what a signed short can hold
* (2^15). Changes were made to AbstractFile to store attrId as integer.
* However, a depricated method still exists in AbstractFile to get
* attrId as short. In that method we convert attribute ids that are
* larger than 32K to a negative number. Therefore if encountered, we
* need to convert negative attribute id to uint16 which is what TSK is
* using to store attribute id.
*/
return openFileNat(fsHandle, fileId, attrType.getValue(), convertSignedToUnsigned(attrId));
}
/**
* Converts signed integer to an unsigned integer.
*
* @param val value to be converter
*
* @return unsigned integer value
*/
private static int convertSignedToUnsigned(int val) {
if (val >= 0) {
return val;
}
return val & 0xffff; // convert negative value to positive value
}
//do reads
/**
* reads data from an image
*
* @param imgHandle
* @param readBuffer buffer to read to
* @param offset byte offset in the image to start at
* @param len amount of data to read
*
* @return the number of characters read, or -1 if the end of the stream has
* been reached
*
* @throws TskCoreException exception thrown if critical error occurs within
* TSK
*/
public static int readImg(long imgHandle, byte[] readBuffer, long offset, long len) throws TskCoreException {
//returned byte[] is the data buffer
return readImgNat(imgHandle, readBuffer, offset, len);
}
/**
* reads data from an volume system
*
* @param vsHandle pointer to a volume system structure in the sleuthkit
* @param readBuffer buffer to read to
* @param offset sector offset in the image to start at
* @param len amount of data to read
*
* @return the number of characters read, or -1 if the end of the stream has
* been reached
*
* @throws TskCoreException exception thrown if critical error occurs within
* TSK
*/
public static int readVs(long vsHandle, byte[] readBuffer, long offset, long len) throws TskCoreException {
return readVsNat(vsHandle, readBuffer, offset, len);
}
/**
* reads data from an volume
*
* @param volHandle pointer to a volume structure in the sleuthkit
* @param readBuffer buffer to read to
* @param offset byte offset in the image to start at
* @param len amount of data to read
*
* @return the number of characters read, or -1 if the end of the stream has
* been reached
*
* @throws TskCoreException exception thrown if critical error occurs within
* TSK
*/
public static int readVsPart(long volHandle, byte[] readBuffer, long offset, long len) throws TskCoreException {
//returned byte[] is the data buffer
return readVolNat(volHandle, readBuffer, offset, len);
}
/**
* reads data from an file system
*
* @param fsHandle pointer to a file system structure in the sleuthkit
* @param readBuffer buffer to read to
* @param offset byte offset in the image to start at
* @param len amount of data to read
*
* @return the number of characters read, or -1 if the end of the stream has
* been reached
*
* @throws TskCoreException exception thrown if critical error occurs within
* TSK
*/
public static int readFs(long fsHandle, byte[] readBuffer, long offset, long len) throws TskCoreException {
//returned byte[] is the data buffer
return readFsNat(fsHandle, readBuffer, offset, len);
}
/**
* reads data from an file
*
* @param fileHandle pointer to a file structure in the sleuthkit
* @param readBuffer pre-allocated buffer to read to
* @param offset byte offset in the image to start at
* @param len amount of data to read
*
* @return the number of characters read, or -1 if the end of the stream has
* been reached
*
* @throws TskCoreException exception thrown if critical error occurs within
* TSK
*/
public static int readFile(long fileHandle, byte[] readBuffer, long offset, long len) throws TskCoreException {
return readFileNat(fileHandle, readBuffer, offset, len);
}
/**
* Get human readable (some what) details about a file. This is the same as
* the 'istat' TSK tool
*
* @param fileHandle pointer to file structure in the sleuthkit
*
* @return text
*
* @throws TskCoreException if errors occurred
*/
public static List<String> getFileMetaDataText(long fileHandle) throws TskCoreException {
try {
java.io.File tmp = java.io.File.createTempFile("tsk", ".txt");
saveFileMetaDataTextNat(fileHandle, tmp.getAbsolutePath());
FileReader fr = new FileReader(tmp.getAbsolutePath());
BufferedReader textReader = new BufferedReader(fr);
List<String> lines = new ArrayList<String>();
while (true) {
String line = textReader.readLine();
if (line == null) {
break;
}
lines.add(line);
}
textReader.close();
fr.close();
tmp.delete();
return lines;
} catch (IOException ex) {
throw new TskCoreException("Error reading istat output: " + ex.getLocalizedMessage());
}
}
//free pointers
/**
* frees the imgHandle pointer currently does not close the image, until the
* application terminates (image handle is cached)
*
* @param imgHandle to close the image
*/
public static void closeImg(long imgHandle) {
//@@@ TODO close the image handle when Case is closed instead
//currently the image handle is not being freed, it's cached for duration of the application
//closeImgNat(imgHandle);
}
/**
* frees the vsHandle pointer
*
* @param vsHandle pointer to volume system structure in sleuthkit
*/
public static void closeVs(long vsHandle) {
closeVsNat(vsHandle);
}
/**
* frees the fsHandle pointer Currently does not do anything - preserves the
* cached object for the duration of the application
*
* @param fsHandle pointer to file system structure in sleuthkit
*/
public static void closeFs(long fsHandle) {
//@@@ TODO close the fs handle when Case is closed instead
//currently the fs handle is not being freed, it's cached for duration of the application
//closeFsNat(fsHandle);
}
/**
* frees the fileHandle pointer
*
* @param fileHandle pointer to file structure in sleuthkit
*/
public static void closeFile(long fileHandle) {
closeFileNat(fileHandle);
}
/**
* Create an index for a hash database.
*
* @param dbHandle A hash database handle.
*
* @throws TskCoreException if a critical error occurs within TSK core
*/
public static void createLookupIndexForHashDatabase(int dbHandle) throws TskCoreException {
hashDbCreateIndexNat(dbHandle);
}
/**
* Check if an index exists for a hash database.
*
* @param dbHandle A hash database handle.
*
* @return true if index exists
*
* @throws TskCoreException if a critical error occurs within TSK core
*/
public static boolean hashDatabaseHasLookupIndex(int dbHandle) throws TskCoreException {
return hashDbIndexExistsNat(dbHandle);
}
/**
* hashDatabaseCanBeReindexed
*
* @param dbHandle previously opened hash db handle
*
* @return Does this database have a source database that is different than
* the index?
*
* @throws TskCoreException if a critical error occurs within TSK core
*/
public static boolean hashDatabaseCanBeReindexed(int dbHandle) throws TskCoreException {
return hashDbIsReindexableNat(dbHandle);
}
/**
* getHashDatabasePath
*
* @param dbHandle previously opened hash db handle
*
* @return Hash db file path
*
* @throws TskCoreException if a critical error occurs within TSK core
*/
public static String getHashDatabasePath(int dbHandle) throws TskCoreException {
return hashDbPathNat(dbHandle);
}
/**
* getHashDatabaseIndexPath
*
* @param dbHandle previously opened hash db handle
*
* @return Index file path
*
* @throws TskCoreException if a critical error occurs within TSK core
*/
public static String getHashDatabaseIndexPath(int dbHandle) throws TskCoreException {
return hashDbIndexPathNat(dbHandle);
}
public static int openHashDatabase(String path) throws TskCoreException {
return hashDbOpenNat(path);
}
/**
* Creates a hash database. Will be of the default TSK hash database type.
*
* @param path The path to the database
*
* @return a handle for that database
*
* @throws TskCoreException if a critical error occurs within TSK core
*/
public static int createHashDatabase(String path) throws TskCoreException {
return hashDbNewNat(path);
}
/**
* Close the currently open lookup databases. Resets the handle counting.
*
* @throws TskCoreException exception thrown if critical error occurs within
* TSK
*/
public static void closeAllHashDatabases() throws TskCoreException {
hashDbCloseAll();
}
/**
* Close a particular open lookup database. Existing handles are not
* affected.
*
* @throws TskCoreException exception thrown if critical error occurs within
* TSK
*/
public static void closeHashDatabase(int dbHandle) throws TskCoreException {
hashDbClose(dbHandle);
}
/**
* Get the name of the database
*
* @param dbHandle previously opened hash db handle
*
* @throws TskCoreException if a critical error occurs within TSK core
*/
public static String getHashDatabaseDisplayName(int dbHandle) throws TskCoreException {
return hashDbGetDisplayName(dbHandle);
}
/**
* Lookup the given hash value and get basic answer
*
* @param hash Hash value to search for
* @param dbHandle Handle of database to lookup in.
*
* @return True if hash was found in database.
*
* @throws TskCoreException
*/
public static boolean lookupInHashDatabase(String hash, int dbHandle) throws TskCoreException {
return hashDbLookup(hash, dbHandle);
}
/**
* Lookup hash value in DB and return details on results (more time
* consuming than basic lookup)
*
* @param hash Hash value to search for
* @param dbHandle Handle of database to lookup in.
*
* @return Details on hash if it was in DB or null if it was not found.
*
* @throws TskCoreException
*/
public static HashHitInfo lookupInHashDatabaseVerbose(String hash, int dbHandle) throws TskCoreException {
return hashDbLookupVerbose(hash, dbHandle);
}
/**
* Adds a hash value to a hash database.
*
* @param filename Name of file (can be null)
* @param md5 Text of MD5 hash (can be null)
* @param sha1 Text of SHA1 hash (can be null)
* @param sha256 Text of SHA256 hash (can be null)
* @param comment A comment (can be null)
* @param dbHandle Handle to DB
*
* @throws TskCoreException
*/
public static void addToHashDatabase(String filename, String md5, String sha1, String sha256, String comment, int dbHandle) throws TskCoreException {
hashDbAddEntryNat(filename, md5, sha1, sha256, comment, dbHandle);
}
public static void addToHashDatabase(List<HashEntry> hashes, int dbHandle) throws TskCoreException {
hashDbBeginTransactionNat(dbHandle);
try {
for (HashEntry entry : hashes) {
hashDbAddEntryNat(entry.getFileName(), entry.getMd5Hash(), entry.getSha1Hash(), entry.getSha256Hash(), entry.getComment(), dbHandle);
}
hashDbCommitTransactionNat(dbHandle);
} catch (TskCoreException ex) {
try {
hashDbRollbackTransactionNat(dbHandle);
} catch (TskCoreException ex2) {
ex2.initCause(ex);
throw ex2;
}
throw ex;
}
}
public static boolean isUpdateableHashDatabase(int dbHandle) throws TskCoreException {
return hashDbIsUpdateableNat(dbHandle);
}
public static boolean hashDatabaseIsIndexOnly(int dbHandle) throws TskCoreException {
return hashDbIsIdxOnlyNat(dbHandle);
}
/**
* Convert this timezone from long to short form Convert timezoneLongForm
* passed in from long to short form
*
* @param timezoneLongForm the long form (e.g., America/New_York)
*
* @return the short form (e.g., EST5EDT) string representation, or an empty
* string if empty long form was passed in
*/
private static String timezoneLongToShort(String timezoneLongForm) {
if (timezoneLongForm == null || timezoneLongForm.isEmpty()) {
return "";
}
String timezoneShortForm;
TimeZone zone = TimeZone.getTimeZone(timezoneLongForm);
int offset = zone.getRawOffset() / 1000;
int hour = offset / 3600;
int min = (offset % 3600) / 60;
DateFormat dfm = new SimpleDateFormat("z");
dfm.setTimeZone(zone);
boolean hasDaylight = zone.useDaylightTime();
String first = dfm.format(new GregorianCalendar(2010, 1, 1).getTime()).substring(0, 3); // make it only 3 letters code
String second = dfm.format(new GregorianCalendar(2011, 6, 6).getTime()).substring(0, 3); // make it only 3 letters code
int mid = hour * -1;
timezoneShortForm = first + Integer.toString(mid);
if (min != 0) {
timezoneShortForm = timezoneShortForm + ":" + (min < 10 ? "0" : "") + Integer.toString(min);
}
if (hasDaylight) {
timezoneShortForm += second;
}
return timezoneShortForm;
}
/**
* Get size of a device (physical, logical device, image) pointed to by
* devPath
*
* @param devPath device path pointing to the device
*
* @return size of the device in bytes
*
* @throws TskCoreException exception thrown if the device size could not be
* queried
*/
public static long findDeviceSize(String devPath) throws TskCoreException {
return findDeviceSizeNat(devPath);
}
public static boolean isImageSupported(String imagePath){
return isImageSupportedNat(imagePath);
}
private static native String getVersionNat();
private static native void startVerboseLoggingNat(String logPath);
private static native long newCaseDbNat(String dbPath) throws TskCoreException;
private static native long newCaseDbMultiNat(String hostNameOrIP, String portNumber, String userName, String password, int dbTypeOrdinal, String databaseName);
private static native long openCaseDbMultiNat(String hostNameOrIP, String portNumber, String userName, String password, int dbTypeOrdinal, String databaseName);
private static native long openCaseDbNat(String path) throws TskCoreException;
private static native void closeCaseDbNat(long db) throws TskCoreException;
private static native int hashDbOpenNat(String hashDbPath) throws TskCoreException;
private static native int hashDbNewNat(String hashDbPath) throws TskCoreException;
private static native int hashDbBeginTransactionNat(int dbHandle) throws TskCoreException;
private static native int hashDbCommitTransactionNat(int dbHandle) throws TskCoreException;
private static native int hashDbRollbackTransactionNat(int dbHandle) throws TskCoreException;
private static native int hashDbAddEntryNat(String filename, String hashMd5, String hashSha1, String hashSha256, String comment, int dbHandle) throws TskCoreException;
private static native boolean hashDbIsUpdateableNat(int dbHandle);
private static native boolean hashDbIsReindexableNat(int dbHandle);
private static native String hashDbPathNat(int dbHandle);
private static native String hashDbIndexPathNat(int dbHandle);
private static native String hashDbGetDisplayName(int dbHandle) throws TskCoreException;
private static native void hashDbCloseAll() throws TskCoreException;
private static native void hashDbClose(int dbHandle) throws TskCoreException;
private static native void hashDbCreateIndexNat(int dbHandle) throws TskCoreException;
private static native boolean hashDbIndexExistsNat(int dbHandle) throws TskCoreException;
private static native boolean hashDbIsIdxOnlyNat(int dbHandle) throws TskCoreException;
private static native boolean hashDbLookup(String hash, int dbHandle) throws TskCoreException;