Sleuth Kit Java Bindings (JNI)  4.2
Java bindings for using The Sleuth Kit
SleuthkitJNI.java
Go to the documentation of this file.
1 /*
2  * Sleuth Kit Data Model
3  *
4  * Copyright 2011 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.datamodel;
20 
21 import java.io.BufferedReader;
22 import java.io.FileReader;
23 import java.io.IOException;
24 import java.text.DateFormat;
25 import java.text.SimpleDateFormat;
26 import java.util.ArrayList;
27 import java.util.GregorianCalendar;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.TimeZone;
33 
42 public class SleuthkitJNI {
43 
44  private static final int MAX_DATABASES = 256;
45 
46  //Native methods
47  private static native String getVersionNat();
48 
49  private static native void startVerboseLoggingNat(String logPath);
50 
51  //database
52  private static native long newCaseDbNat(String dbPath) throws TskCoreException;
53 
54  private static native long openCaseDbNat(String path) throws TskCoreException;
55 
56  private static native void closeCaseDbNat(long db) throws TskCoreException;
57 
58  private static native int hashDbOpenNat(String hashDbPath) throws TskCoreException;
59 
60  private static native int hashDbNewNat(String hashDbPath) throws TskCoreException;
61 
62  private static native int hashDbBeginTransactionNat(int dbHandle) throws TskCoreException;
63 
64  private static native int hashDbCommitTransactionNat(int dbHandle) throws TskCoreException;
65 
66  private static native int hashDbRollbackTransactionNat(int dbHandle) throws TskCoreException;
67 
68  private static native int hashDbAddEntryNat(String filename, String hashMd5, String hashSha1, String hashSha256, String comment, int dbHandle) throws TskCoreException;
69 
70  private static native boolean hashDbIsUpdateableNat(int dbHandle);
71 
72  private static native boolean hashDbIsReindexableNat(int dbHandle);
73 
74  private static native String hashDbPathNat(int dbHandle);
75 
76  private static native String hashDbIndexPathNat(int dbHandle);
77 
78  private static native String hashDbGetDisplayName(int dbHandle) throws TskCoreException;
79 
80  private static native void hashDbCloseAll() throws TskCoreException;
81 
82  private static native void hashDbClose(int dbHandle) throws TskCoreException;
83 
84  //hash-lookup database functions
85  private static native void hashDbCreateIndexNat(int dbHandle) throws TskCoreException;
86 
87  private static native boolean hashDbIndexExistsNat(int dbHandle) throws TskCoreException;
88 
89  private static native boolean hashDbIsIdxOnlyNat(int dbHandle) throws TskCoreException;
90 
91  private static native boolean hashDbLookup(String hash, int dbHandle) throws TskCoreException;
92 
93  private static native HashHitInfo hashDbLookupVerbose(String hash, int dbHandle) throws TskCoreException;
94 
95  //load image
96  private static native long initAddImgNat(long db, String timezone, boolean processUnallocSpace, boolean noFatFsOrphans) throws TskCoreException;
97 
98  private static native void runAddImgNat(long process, String[] imgPath, int splits, String timezone) throws TskCoreException, TskDataException; // if runAddImg finishes without being stopped, revertAddImg or commitAddImg MUST be called
99 
100  private static native void stopAddImgNat(long process) throws TskCoreException;
101 
102  private static native void revertAddImgNat(long process) throws TskCoreException;
103 
104  private static native long commitAddImgNat(long process) throws TskCoreException;
105 
106  //open functions
107  private static native long openImgNat(String[] imgPath, int splits) throws TskCoreException;
108 
109  private static native long openVsNat(long imgHandle, long vsOffset) throws TskCoreException;
110 
111  private static native long openVolNat(long vsHandle, long volId) throws TskCoreException;
112 
113  private static native long openFsNat(long imgHandle, long fsId) throws TskCoreException;
114 
115  private static native long openFileNat(long fsHandle, long fileId, int attrType, int attrId) throws TskCoreException;
116 
117  //read functions
118  private static native int readImgNat(long imgHandle, byte[] readBuffer, long offset, long len) throws TskCoreException;
119 
120  private static native int readVsNat(long vsHandle, byte[] readBuffer, long offset, long len) throws TskCoreException;
121 
122  private static native int readVolNat(long volHandle, byte[] readBuffer, long offset, long len) throws TskCoreException;
123 
124  private static native int readFsNat(long fsHandle, byte[] readBuffer, long offset, long len) throws TskCoreException;
125 
126  private static native int readFileNat(long fileHandle, byte[] readBuffer, long offset, long len) throws TskCoreException;
127 
128  private static native int saveFileMetaDataTextNat(long fileHandle, String fileName) throws TskCoreException;
129 
130  //close functions
131  private static native void closeImgNat(long imgHandle);
132 
133  private static native void closeVsNat(long vsHandle);
134 
135  private static native void closeFsNat(long fsHandle);
136 
137  private static native void closeFileNat(long fileHandle);
138 
139  //util functions
140  private static native long findDeviceSizeNat(String devicePath) throws TskCoreException;
141 
142  private static native String getCurDirNat(long process);
143 
144  //Linked library loading
145  static {
147  }
148 
149  private SleuthkitJNI() {
150 
151  }
152 
156  public static class CaseDbHandle {
157 
158  private long caseDbPointer;
159  //map concat. image paths to cached image handle
160  private static final Map<String, Long> imageHandleCache = new HashMap<String, Long>();
161  //map image and offsets to cached fs handle
162  private static final Map<Long, Map<Long, Long>> fsHandleCache = new HashMap<Long, Map<Long, Long>>();
163 
164  private CaseDbHandle(long pointer) {
165  this.caseDbPointer = pointer;
166  }
167 
174  void free() throws TskCoreException {
175  SleuthkitJNI.closeCaseDbNat(caseDbPointer);
176  }
177 
192  AddImageProcess initAddImageProcess(String timezone, boolean processUnallocSpace, boolean noFatFsOrphans) {
193  return new AddImageProcess(timezone, processUnallocSpace, noFatFsOrphans);
194  }
195 
201  public class AddImageProcess {
202 
203  private String timezone;
204  private boolean processUnallocSpace;
205  private boolean noFatFsOrphans;
206  private volatile long autoDbPointer;
207 
208  private AddImageProcess(String timezone, boolean processUnallocSpace, boolean noFatFsOrphans) {
209  this.timezone = timezone;
210  this.processUnallocSpace = processUnallocSpace;
211  this.noFatFsOrphans = noFatFsOrphans;
212  autoDbPointer = 0;
213  }
214 
225  public void run(String[] imgPath) throws TskCoreException, TskDataException {
226  if (autoDbPointer != 0) {
227  throw new TskCoreException("AddImgProcess:run: AutoDB pointer is already set");
228  }
229 
230  synchronized (this) {
231  autoDbPointer = initAddImgNat(caseDbPointer, timezoneLongToShort(timezone), processUnallocSpace, noFatFsOrphans);
232  }
233  if (autoDbPointer == 0) {
234  //additional check in case initAddImgNat didn't throw exception
235  throw new TskCoreException("AddImgProcess::run: AutoDB pointer is NULL after initAddImgNat");
236  }
237  runAddImgNat(autoDbPointer, imgPath, imgPath.length, timezone);
238  }
239 
248  public void stop() throws TskCoreException {
249  if (autoDbPointer == 0) {
250  throw new TskCoreException("AddImgProcess::stop: AutoDB pointer is NULL");
251  }
252 
253  stopAddImgNat(autoDbPointer);
254  }
255 
264  public synchronized void revert() throws TskCoreException {
265  if (autoDbPointer == 0) {
266  throw new TskCoreException("AddImgProcess::revert: AutoDB pointer is NULL");
267  }
268 
269  revertAddImgNat(autoDbPointer);
270  // the native code deleted the object
271  autoDbPointer = 0;
272  }
273 
285  public synchronized long commit() throws TskCoreException {
286  if (autoDbPointer == 0) {
287  throw new TskCoreException("AddImgProcess::commit: AutoDB pointer is NULL");
288  }
289 
290  long id = commitAddImgNat(autoDbPointer);
291  // the native code deleted the object
292  autoDbPointer = 0;
293  return id;
294  }
295 
302  public synchronized String currentDirectory() {
303  return autoDbPointer == 0 ? "NO_INFO" : getCurDirNat(autoDbPointer); //NON-NLS
304  }
305  }
306  }
307 
317  static CaseDbHandle newCaseDb(String path) throws TskCoreException {
318  return new CaseDbHandle(newCaseDbNat(path));
319  }
320 
330  static CaseDbHandle openCaseDb(String path) throws TskCoreException {
331  return new CaseDbHandle(openCaseDbNat(path));
332  }
333 
339  public static String getVersion() {
340  return getVersionNat();
341  }
342 
346  public static void startVerboseLogging(String logPath) {
347  startVerboseLoggingNat(logPath);
348  }
349 
358  public synchronized static long openImage(String[] imageFiles) throws TskCoreException {
359  long imageHandle = 0;
360 
361  StringBuilder keyBuilder = new StringBuilder();
362  for (int i = 0; i < imageFiles.length; ++i) {
363  keyBuilder.append(imageFiles[i]);
364  }
365  final String imageKey = keyBuilder.toString();
366 
367  if (CaseDbHandle.imageHandleCache.containsKey(imageKey)) //get from cache
368  {
369  imageHandle = CaseDbHandle.imageHandleCache.get(imageKey);
370  } else {
371  //open new handle and cache it
372  imageHandle = openImgNat(imageFiles, imageFiles.length);
373  CaseDbHandle.fsHandleCache.put(imageHandle, new HashMap<Long, Long>());
374  CaseDbHandle.imageHandleCache.put(imageKey, imageHandle);
375  }
376 
377  return imageHandle;
378 
379  }
380 
390  public static long openVs(long imgHandle, long vsOffset) throws TskCoreException {
391  return openVsNat(imgHandle, vsOffset);
392  }
393 
394  //get pointers
404  public static long openVsPart(long vsHandle, long volId) throws TskCoreException {
405  //returned long is ptr to vs Handle object in tsk
406  return openVolNat(vsHandle, volId);
407  }
408 
419  public synchronized static long openFs(long imgHandle, long fsOffset) throws TskCoreException {
420  long fsHandle = 0;
421  final Map<Long, Long> imgOffSetToFsHandle = CaseDbHandle.fsHandleCache.get(imgHandle);
422  if (imgOffSetToFsHandle.containsKey(fsOffset)) {
423  //return cached
424  fsHandle = imgOffSetToFsHandle.get(fsOffset);
425  } else {
426  fsHandle = openFsNat(imgHandle, fsOffset);
427  //cache it
428  imgOffSetToFsHandle.put(fsOffset, fsHandle);
429  }
430  return fsHandle;
431  }
432 
444  public static long openFile(long fsHandle, long fileId, TSK_FS_ATTR_TYPE_ENUM attrType, int attrId) throws TskCoreException {
445  return openFileNat(fsHandle, fileId, attrType.getValue(), attrId);
446  }
447 
448  //do reads
461  public static int readImg(long imgHandle, byte[] readBuffer, long offset, long len) throws TskCoreException {
462  //returned byte[] is the data buffer
463  return readImgNat(imgHandle, readBuffer, offset, len);
464  }
465 
478  public static int readVs(long vsHandle, byte[] readBuffer, long offset, long len) throws TskCoreException {
479  return readVsNat(vsHandle, readBuffer, offset, len);
480  }
481 
494  public static int readVsPart(long volHandle, byte[] readBuffer, long offset, long len) throws TskCoreException {
495  //returned byte[] is the data buffer
496  return readVolNat(volHandle, readBuffer, offset, len);
497  }
498 
511  public static int readFs(long fsHandle, byte[] readBuffer, long offset, long len) throws TskCoreException {
512  //returned byte[] is the data buffer
513  return readFsNat(fsHandle, readBuffer, offset, len);
514  }
515 
528  public static int readFile(long fileHandle, byte[] readBuffer, long offset, long len) throws TskCoreException {
529  return readFileNat(fileHandle, readBuffer, offset, len);
530  }
531 
540  public static List<String> getFileMetaDataText(long fileHandle) throws TskCoreException {
541  try {
542  java.io.File tmp = java.io.File.createTempFile("tsk", ".txt");
543 
544  saveFileMetaDataTextNat(fileHandle, tmp.getAbsolutePath());
545 
546  FileReader fr = new FileReader(tmp.getAbsolutePath());
547  BufferedReader textReader = new BufferedReader(fr);
548 
549  List<String> lines = new ArrayList<String>();
550  while (true) {
551  String line = textReader.readLine();
552  if (line == null) {
553  break;
554  }
555  lines.add(line);
556  }
557  textReader.close();
558  fr.close();
559  tmp.delete();
560  return lines;
561  } catch (IOException ex) {
562  throw new TskCoreException("Error reading istat output: " + ex.getLocalizedMessage());
563  }
564  }
565 
566  //free pointers
573  public static void closeImg(long imgHandle) {
574  //@@@ TODO close the image handle when Case is closed instead
575  //currently the image handle is not being freed, it's cached for duration of the application
576  //closeImgNat(imgHandle);
577  }
578 
584  public static void closeVs(long vsHandle) {
585  closeVsNat(vsHandle);
586  }
587 
594  public static void closeFs(long fsHandle) {
595  //@@@ TODO close the fs handle when Case is closed instead
596  //currently the fs handle is not being freed, it's cached for duration of the application
597  //closeFsNat(fsHandle);
598  }
599 
605  public static void closeFile(long fileHandle) {
606  closeFileNat(fileHandle);
607  }
608 
618  public static void createLookupIndexForHashDatabase(int dbHandle) throws TskCoreException {
619  hashDbCreateIndexNat(dbHandle);
620  }
621 
629  public static boolean hashDatabaseHasLookupIndex(int dbHandle) throws TskCoreException {
630  return hashDbIndexExistsNat(dbHandle);
631  }
632 
641  public static boolean hashDatabaseCanBeReindexed(int dbHandle) throws TskCoreException {
642  return hashDbIsReindexableNat(dbHandle);
643  }
644 
652  public static String getHashDatabasePath(int dbHandle) throws TskCoreException {
653  return hashDbPathNat(dbHandle);
654  }
655 
663  public static String getHashDatabaseIndexPath(int dbHandle) throws TskCoreException {
664  return hashDbIndexPathNat(dbHandle);
665  }
666 
667  public static int openHashDatabase(String path) throws TskCoreException {
668  return hashDbOpenNat(path);
669  }
670 
678  public static int createHashDatabase(String path) throws TskCoreException {
679  return hashDbNewNat(path);
680  }
681 
688  public static void closeAllHashDatabases() throws TskCoreException {
689  hashDbCloseAll();
690  }
691 
699  public static void closeHashDatabase(int dbHandle) throws TskCoreException {
700  hashDbClose(dbHandle);
701  }
702 
709  public static String getHashDatabaseDisplayName(int dbHandle) throws TskCoreException {
710  return hashDbGetDisplayName(dbHandle);
711  }
712 
721  public static boolean lookupInHashDatabase(String hash, int dbHandle) throws TskCoreException {
722  return hashDbLookup(hash, dbHandle);
723  }
724 
734  public static HashHitInfo lookupInHashDatabaseVerbose(String hash, int dbHandle) throws TskCoreException {
735  return hashDbLookupVerbose(hash, dbHandle);
736  }
737 
748  public static void addToHashDatabase(String filename, String md5, String sha1, String sha256, String comment, int dbHandle) throws TskCoreException {
749  hashDbAddEntryNat(filename, md5, sha1, sha256, comment, dbHandle);
750  }
751 
752  public static void addToHashDatabase(List<HashEntry> hashes, int dbHandle) throws TskCoreException {
753  hashDbBeginTransactionNat(dbHandle);
754  try {
755  for (HashEntry entry : hashes) {
756  hashDbAddEntryNat(entry.getFileName(), entry.getMd5Hash(), entry.getSha1Hash(), entry.getSha256Hash(), entry.getComment(), dbHandle);
757  }
758  hashDbCommitTransactionNat(dbHandle);
759  } catch (TskCoreException ex) {
760  try {
762  } catch (TskCoreException ex2) {
763  ex2.initCause(ex);
764  throw ex2;
765  }
766  throw ex;
767  }
768  }
769 
770  public static boolean isUpdateableHashDatabase(int dbHandle) throws TskCoreException {
771  return hashDbIsUpdateableNat(dbHandle);
772  }
773 
774  public static boolean hashDatabaseIsIndexOnly(int dbHandle) throws TskCoreException {
775  return hashDbIsIdxOnlyNat(dbHandle);
776  }
777 
786  private static String timezoneLongToShort(String timezoneLongForm) {
787  if (timezoneLongForm == null || timezoneLongForm.isEmpty()) {
788  return "";
789  }
790 
791  String timezoneShortForm = "";
792  TimeZone zone = TimeZone.getTimeZone(timezoneLongForm);
793  int offset = zone.getRawOffset() / 1000;
794  int hour = offset / 3600;
795  int min = (offset % 3600) / 60;
796  DateFormat dfm = new SimpleDateFormat("z");
797  dfm.setTimeZone(zone);
798  boolean hasDaylight = zone.useDaylightTime();
799  String first = dfm.format(new GregorianCalendar(2010, 1, 1).getTime()).substring(0, 3); // make it only 3 letters code
800  String second = dfm.format(new GregorianCalendar(2011, 6, 6).getTime()).substring(0, 3); // make it only 3 letters code
801  int mid = hour * -1;
802  timezoneShortForm = first + Integer.toString(mid);
803  if (min != 0) {
804  timezoneShortForm = timezoneShortForm + ":" + (min < 10 ? "0" : "") + Integer.toString(min);
805  }
806  if (hasDaylight) {
807  timezoneShortForm = timezoneShortForm + second;
808  }
809  return timezoneShortForm;
810  }
811 
821  public static long findDeviceSize(String devPath) throws TskCoreException {
822  return findDeviceSizeNat(devPath);
823  }
824 }
static native boolean hashDbIsUpdateableNat(int dbHandle)
static int readImg(long imgHandle, byte[] readBuffer, long offset, long len)
static native long commitAddImgNat(long process)
static native int readFileNat(long fileHandle, byte[] readBuffer, long offset, long len)
static String getHashDatabaseIndexPath(int dbHandle)
static native void closeVsNat(long vsHandle)
static int readVs(long vsHandle, byte[] readBuffer, long offset, long len)
static void createLookupIndexForHashDatabase(int dbHandle)
static native void startVerboseLoggingNat(String logPath)
static native long initAddImgNat(long db, String timezone, boolean processUnallocSpace, boolean noFatFsOrphans)
static void addToHashDatabase(String filename, String md5, String sha1, String sha256, String comment, int dbHandle)
static native int readImgNat(long imgHandle, byte[] readBuffer, long offset, long len)
static int createHashDatabase(String path)
static native String getCurDirNat(long process)
static void closeFs(long fsHandle)
static native int hashDbRollbackTransactionNat(int dbHandle)
static long openFile(long fsHandle, long fileId, TSK_FS_ATTR_TYPE_ENUM attrType, int attrId)
static int readFile(long fileHandle, byte[] readBuffer, long offset, long len)
static native int hashDbAddEntryNat(String filename, String hashMd5, String hashSha1, String hashSha256, String comment, int dbHandle)
static native int readVsNat(long vsHandle, byte[] readBuffer, long offset, long len)
static native void revertAddImgNat(long process)
static native void runAddImgNat(long process, String[] imgPath, int splits, String timezone)
static HashHitInfo lookupInHashDatabaseVerbose(String hash, int dbHandle)
static native String getVersionNat()
static native boolean hashDbIndexExistsNat(int dbHandle)
static native long findDeviceSizeNat(String devicePath)
static long openVs(long imgHandle, long vsOffset)
static native int saveFileMetaDataTextNat(long fileHandle, String fileName)
static native boolean hashDbIsIdxOnlyNat(int dbHandle)
static native long openVolNat(long vsHandle, long volId)
static native void closeFsNat(long fsHandle)
static native void hashDbClose(int dbHandle)
static native int readFsNat(long fsHandle, byte[] readBuffer, long offset, long len)
static boolean hashDatabaseIsIndexOnly(int dbHandle)
static int readVsPart(long volHandle, byte[] readBuffer, long offset, long len)
static native HashHitInfo hashDbLookupVerbose(String hash, int dbHandle)
static native int hashDbCommitTransactionNat(int dbHandle)
static final Map< Long, Map< Long, Long > > fsHandleCache
static synchronized long openFs(long imgHandle, long fsOffset)
static native int hashDbOpenNat(String hashDbPath)
static void closeVs(long vsHandle)
static native long newCaseDbNat(String dbPath)
static native long openImgNat(String[] imgPath, int splits)
Definition: HashEntry.java:25
static long findDeviceSize(String devPath)
static String getHashDatabaseDisplayName(int dbHandle)
static native long openFileNat(long fsHandle, long fileId, int attrType, int attrId)
static native void closeFileNat(long fileHandle)
static List< String > getFileMetaDataText(long fileHandle)
static void closeImg(long imgHandle)
static native long openVsNat(long imgHandle, long vsOffset)
static native boolean hashDbLookup(String hash, int dbHandle)
static final Map< String, Long > imageHandleCache
static native int readVolNat(long volHandle, byte[] readBuffer, long offset, long len)
static String timezoneLongToShort(String timezoneLongForm)
static native void hashDbCloseAll()
static int openHashDatabase(String path)
static native int hashDbBeginTransactionNat(int dbHandle)
static void closeFile(long fileHandle)
static boolean lookupInHashDatabase(String hash, int dbHandle)
static boolean hashDatabaseHasLookupIndex(int dbHandle)
AddImageProcess(String timezone, boolean processUnallocSpace, boolean noFatFsOrphans)
static synchronized long openImage(String[] imageFiles)
static native long openFsNat(long imgHandle, long fsId)
static native void closeCaseDbNat(long db)
static long openVsPart(long vsHandle, long volId)
static boolean isUpdateableHashDatabase(int dbHandle)
static native int hashDbNewNat(String hashDbPath)
static void addToHashDatabase(List< HashEntry > hashes, int dbHandle)
static native void stopAddImgNat(long process)
static native void closeImgNat(long imgHandle)
static boolean hashDatabaseCanBeReindexed(int dbHandle)
static native String hashDbGetDisplayName(int dbHandle)
static native String hashDbIndexPathNat(int dbHandle)
static void startVerboseLogging(String logPath)
static native void hashDbCreateIndexNat(int dbHandle)
static native String hashDbPathNat(int dbHandle)
static int readFs(long fsHandle, byte[] readBuffer, long offset, long len)
static String getHashDatabasePath(int dbHandle)
static void closeHashDatabase(int dbHandle)
static native long openCaseDbNat(String path)
static native boolean hashDbIsReindexableNat(int dbHandle)

Copyright © 2011-2015 Brian Carrier. (carrier -at- sleuthkit -dot- org)
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.