19 package org.sleuthkit.datamodel;
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;
31 import java.util.TimeZone;
32 import java.util.UUID;
56 private static final Object
cacheLock =
new Object();
86 private static final Map<Long, Map<Long, Long>>
fsHandleCache =
new HashMap<Long, Map<Long, Long>>();
109 for (Map<Long, Long> imageToFsMap : fsHandleCache.values()) {
110 for (Long fsHandle : imageToFsMap.values()) {
118 for (Long imageHandle : imageHandleCache.values()) {
122 fsHandleCache.clear();
123 imageHandleCache.clear();
152 long addImageInfo(
long deviceObjId, List<String> imageFilePaths, String timeZone)
throws TskCoreException {
155 runOpenAndAddImgNat(tskAutoDbPointer, UUID.randomUUID().toString(), imageFilePaths.toArray(
new String[0]), imageFilePaths.size(), timeZone);
157 }
catch (TskDataException ex) {
158 throw new TskCoreException(
"Error adding image to case database", ex);
178 AddImageProcess initAddImageProcess(String timeZone,
boolean addUnallocSpace,
boolean skipFatFsOrphans, String imageWriterPath) {
179 return new AddImageProcess(timeZone, addUnallocSpace, skipFatFsOrphans, imageWriterPath);
208 private AddImageProcess(String timeZone,
boolean addUnallocSpace,
boolean skipFatFsOrphans, String imageWriterPath) {
213 tskAutoDbPointer = 0;
214 this.isCanceled =
false;
235 long imageHandle = 0;
236 synchronized (
this) {
237 if (0 != tskAutoDbPointer) {
238 throw new TskCoreException(
"Add image process already started");
241 imageHandle =
openImage(imageFilePaths,
false);
244 if (0 == tskAutoDbPointer) {
245 throw new TskCoreException(
"initAddImgNat returned a NULL TskAutoDb pointer");
248 if (imageHandle != 0) {
249 runAddImgNat(tskAutoDbPointer, deviceId, imageHandle, timeZone, imageWriterPath);
262 public synchronized void stop() throws TskCoreException {
264 if (tskAutoDbPointer != 0) {
276 public synchronized void revert() throws TskCoreException {
277 if (tskAutoDbPointer == 0) {
278 throw new TskCoreException(
"AddImgProcess::revert: AutoDB pointer is NULL");
283 tskAutoDbPointer = 0;
295 public synchronized long commit() throws TskCoreException {
296 if (tskAutoDbPointer == 0) {
297 throw new TskCoreException(
"AddImgProcess::commit: AutoDB pointer is NULL");
302 tskAutoDbPointer = 0;
313 return tskAutoDbPointer == 0 ?
"" :
getCurDirNat(tskAutoDbPointer);
333 run(null, imageFilePaths);
350 static CaseDbHandle newCaseDb(String path)
throws TskCoreException {
366 static CaseDbHandle newCaseDb(String databaseName, CaseDbConnectionInfo info)
throws TskCoreException {
367 return new CaseDbHandle(
newCaseDbMultiNat(info.getHost(), info.getPort(), info.getUserName(), info.getPassword(), info.getDbType().ordinal(), databaseName));
381 static CaseDbHandle openCaseDb(String path)
throws TskCoreException {
397 static CaseDbHandle openCaseDb(String databaseName, CaseDbConnectionInfo info)
throws TskCoreException {
398 return new CaseDbHandle(
openCaseDbMultiNat(info.getHost(), info.getPort(), info.getUserName(), info.getPassword(), info.getDbType().ordinal(), databaseName));
427 public static long openImage(String[] imageFiles)
throws TskCoreException {
446 private static long openImage(String[] imageFiles,
boolean useCache)
throws TskCoreException {
450 StringBuilder keyBuilder =
new StringBuilder();
451 for (
int i = 0; i < imageFiles.length; ++i) {
452 keyBuilder.append(imageFiles[i]);
454 final String imageKey = keyBuilder.toString();
469 imageHandle =
openImgNat(imageFiles, imageFiles.length);
489 public static long openVs(
long imgHandle,
long vsOffset)
throws TskCoreException {
505 public static long openVsPart(
long vsHandle,
long volId)
throws TskCoreException {
522 public static long openFs(
long imgHandle,
long fsOffset)
throws TskCoreException {
526 if (imgOffSetToFsHandle.containsKey(fsOffset)) {
528 fsHandle = imgOffSetToFsHandle.get(fsOffset);
530 fsHandle =
openFsNat(imgHandle, fsOffset);
532 imgOffSetToFsHandle.put(fsOffset, fsHandle);
596 public static int readImg(
long imgHandle, byte[] readBuffer,
long offset,
long len)
throws TskCoreException {
598 return readImgNat(imgHandle, readBuffer, offset, len);
615 public static int readVs(
long vsHandle, byte[] readBuffer,
long offset,
long len)
throws TskCoreException {
616 return readVsNat(vsHandle, readBuffer, offset, len);
633 public static int readVsPart(
long volHandle, byte[] readBuffer,
long offset,
long len)
throws TskCoreException {
635 return readVolNat(volHandle, readBuffer, offset, len);
652 public static int readFs(
long fsHandle, byte[] readBuffer,
long offset,
long len)
throws TskCoreException {
654 return readFsNat(fsHandle, readBuffer, offset, len);
690 public static int readFile(
long fileHandle, byte[] readBuffer,
long offset,
long len)
throws TskCoreException {
708 public static int readFileSlack(
long fileHandle, byte[] readBuffer,
long offset,
long len)
throws TskCoreException {
724 java.io.File tmp = java.io.File.createTempFile(
"tsk",
".txt");
728 FileReader fr =
new FileReader(tmp.getAbsolutePath());
729 BufferedReader textReader =
new BufferedReader(fr);
731 List<String> lines =
new ArrayList<String>();
733 String line = textReader.readLine();
743 }
catch (IOException ex) {
744 throw new TskCoreException(
"Error reading istat output: " + ex.getLocalizedMessage());
945 public static void addToHashDatabase(String filename, String md5, String sha1, String sha256, String comment,
int dbHandle)
throws TskCoreException {
949 public static void addToHashDatabase(List<HashEntry> hashes,
int dbHandle)
throws TskCoreException {
953 hashDbAddEntryNat(entry.getFileName(), entry.getMd5Hash(), entry.getSha1Hash(), entry.getSha256Hash(), entry.getComment(), dbHandle);
956 }
catch (TskCoreException ex) {
959 }
catch (TskCoreException ex2) {
985 if (timezoneLongForm == null || timezoneLongForm.isEmpty()) {
989 String timezoneShortForm;
990 TimeZone zone = TimeZone.getTimeZone(timezoneLongForm);
991 int offset = zone.getRawOffset() / 1000;
992 int hour = offset / 3600;
993 int min = (offset % 3600) / 60;
994 DateFormat dfm =
new SimpleDateFormat(
"z");
995 dfm.setTimeZone(zone);
996 boolean hasDaylight = zone.useDaylightTime();
997 String first = dfm.format(
new GregorianCalendar(2010, 1, 1).getTime()).substring(0, 3);
998 String second = dfm.format(
new GregorianCalendar(2011, 6, 6).getTime()).substring(0, 3);
1000 timezoneShortForm = first + Integer.toString(mid);
1002 timezoneShortForm = timezoneShortForm +
":" + (min < 10 ?
"0" :
"") + Integer.toString(min);
1005 timezoneShortForm += second;
1007 return timezoneShortForm;
1065 private static native
long newCaseDbNat(String dbPath)
throws TskCoreException;
1067 private static native
long newCaseDbMultiNat(String hostNameOrIP, String portNumber, String userName, String password,
int dbTypeOrdinal, String databaseName);
1069 private static native
long openCaseDbMultiNat(String hostNameOrIP, String portNumber, String userName, String password,
int dbTypeOrdinal, String databaseName);
1071 private static native
long openCaseDbNat(String path)
throws TskCoreException;
1073 private static native
void closeCaseDbNat(
long db)
throws TskCoreException;
1075 private static native
int hashDbOpenNat(String hashDbPath)
throws TskCoreException;
1077 private static native
int hashDbNewNat(String hashDbPath)
throws TskCoreException;
1085 private static native
int hashDbAddEntryNat(String filename, String hashMd5, String hashSha1, String hashSha256, String comment,
int dbHandle)
throws TskCoreException;
1097 private static native
void hashDbCloseAll() throws TskCoreException;
1099 private static native
void hashDbClose(
int dbHandle) throws TskCoreException;
1105 private static native
boolean hashDbIsIdxOnlyNat(
int dbHandle) throws TskCoreException;
1107 private static native
boolean hashDbLookup(String hash,
int dbHandle) throws TskCoreException;
1111 private static native
long initAddImgNat(
long db, String timezone,
boolean addUnallocSpace,
boolean skipFatFsOrphans) throws TskCoreException;
1113 private static native
long initializeAddImgNat(
long db, String timezone,
boolean addFileSystems,
boolean addUnallocSpace,
boolean skipFatFsOrphans) throws TskCoreException;
1117 private static native
void runAddImgNat(
long process, String deviceId,
long a_img_info, String timeZone, String imageWriterPath) throws TskCoreException, TskDataException;
1119 private static native
void stopAddImgNat(
long process) throws TskCoreException;
1121 private static native
void revertAddImgNat(
long process) throws TskCoreException;
1123 private static native
long commitAddImgNat(
long process) throws TskCoreException;
1125 private static native
long openImgNat(String[] imgPath,
int splits) throws TskCoreException;
1127 private static native
long openVsNat(
long imgHandle,
long vsOffset) throws TskCoreException;
1129 private static native
long openVolNat(
long vsHandle,
long volId) throws TskCoreException;
1131 private static native
long openFsNat(
long imgHandle,
long fsId) throws TskCoreException;
1133 private static native
long openFileNat(
long fsHandle,
long fileId,
int attrType,
int attrId) throws TskCoreException;
1135 private static native
int readImgNat(
long imgHandle, byte[] readBuffer,
long offset,
long len) throws TskCoreException;
1137 private static native
int readVsNat(
long vsHandle, byte[] readBuffer,
long offset,
long len) throws TskCoreException;
1139 private static native
int readVolNat(
long volHandle, byte[] readBuffer,
long offset,
long len) throws TskCoreException;
1141 private static native
int readFsNat(
long fsHandle, byte[] readBuffer,
long offset,
long len) throws TskCoreException;
1143 private static native
int readFileNat(
long fileHandle, byte[] readBuffer,
long offset,
int offset_type,
long len) throws TskCoreException;
1147 private static native
void closeImgNat(
long imgHandle);
1149 private static native
void closeVsNat(
long vsHandle);
1151 private static native
void closeFsNat(
long fsHandle);
1153 private static native
void closeFileNat(
long fileHandle);
1155 private static native
long findDeviceSizeNat(String devicePath) throws TskCoreException;
1157 private static native String
getCurDirNat(
long process);
static native boolean hashDbIsUpdateableNat(int dbHandle)
static int readImg(long imgHandle, byte[] readBuffer, long offset, long len)
static native long commitAddImgNat(long process)
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 long openImage(String[] imageFiles, boolean useCache)
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 void cancelFinishImage(long imgHandle)
static native int hashDbRollbackTransactionNat(int dbHandle)
static long openFile(long fsHandle, long fileId, TSK_FS_ATTR_TYPE_ENUM attrType, int attrId)
void run(String deviceId, String[] imageFilePaths)
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 int finishImageWriter(long imgHandle)
static native void runAddImgNat(long process, String deviceId, long a_img_info, String timeZone, String imageWriterPath)
static native void cancelFinishImageNat(long a_img_info)
static native int readFileNat(long fileHandle, byte[] readBuffer, long offset, int offset_type, long len)
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)
synchronized long commit()
static native int saveFileMetaDataTextNat(long fileHandle, String fileName)
static native boolean hashDbIsIdxOnlyNat(int dbHandle)
static native long openVolNat(long vsHandle, long volId)
volatile long tskAutoDbPointer
static native void closeFsNat(long fsHandle)
static native void hashDbClose(int dbHandle)
static int convertSignedToUnsigned(int val)
static native int readFsNat(long fsHandle, byte[] readBuffer, long offset, long len)
static boolean hashDatabaseIsIndexOnly(int dbHandle)
synchronized void revert()
static boolean isImageSupported(String imagePath)
static int readVsPart(long volHandle, byte[] readBuffer, long offset, long len)
static boolean loadSleuthkitJNI()
static native HashHitInfo hashDbLookupVerbose(String hash, int dbHandle)
static native int hashDbCommitTransactionNat(int dbHandle)
static final Map< Long, Map< Long, Long > > fsHandleCache
static native int hashDbOpenNat(String hashDbPath)
static void closeVs(long vsHandle)
static native long newCaseDbNat(String dbPath)
static long openImage(String[] imageFiles)
static native long openImgNat(String[] imgPath, int splits)
static final Object cacheLock
final boolean skipFatFsOrphans
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)
TSK_FS_FILE_READ_OFFSET_TYPE_ENUM(int val)
static long openFs(long imgHandle, long fsOffset)
final boolean addUnallocSpace
final String imageWriterPath
static native long openVsNat(long imgHandle, long vsOffset)
static native boolean hashDbLookup(String hash, int dbHandle)
void run(String[] imageFilePaths)
static final Map< String, Long > imageHandleCache
static native int readVolNat(long volHandle, byte[] readBuffer, long offset, long len)
static String timezoneLongToShort(String timezoneLongForm)
synchronized String currentDirectory()
static native long newCaseDbMultiNat(String hostNameOrIP, String portNumber, String userName, String password, int dbTypeOrdinal, String databaseName)
static native void hashDbCloseAll()
static int getFinishImageProgress(long imgHandle)
static int openHashDatabase(String path)
static native int hashDbBeginTransactionNat(int dbHandle)
AddImageProcess(String timeZone, boolean addUnallocSpace, boolean skipFatFsOrphans, String imageWriterPath)
static void closeFile(long fileHandle)
static boolean lookupInHashDatabase(String hash, int dbHandle)
static boolean hashDatabaseHasLookupIndex(int dbHandle)
static native long openFsNat(long imgHandle, long fsId)
CaseDbHandle(long caseDbPointer)
static native void closeCaseDbNat(long db)
static native long initAddImgNat(long db, String timezone, boolean addUnallocSpace, boolean skipFatFsOrphans)
static native void runOpenAndAddImgNat(long process, String deviceId, String[] imgPath, int splits, String timezone)
static long openVsPart(long vsHandle, long volId)
static native int finishImageWriterNat(long a_img_info)
static native boolean isImageSupportedNat(String imagePath)
static native int getFinishImageProgressNat(long a_img_info)
static int readFileSlack(long fileHandle, byte[] readBuffer, long offset, long len)
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 void closeAllHashDatabases()
static native long initializeAddImgNat(long db, String timezone, boolean addFileSystems, boolean addUnallocSpace, boolean skipFatFsOrphans)
static native void closeImgNat(long imgHandle)
static boolean hashDatabaseCanBeReindexed(int dbHandle)
static native String hashDbGetDisplayName(int dbHandle)
static native String hashDbIndexPathNat(int dbHandle)
static String getVersion()
static native long openCaseDbMultiNat(String hostNameOrIP, String portNumber, String userName, String password, int dbTypeOrdinal, String databaseName)
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)