21 package org.sleuthkit.autopsy.recentactivity;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.RandomAccessFile;
27 import java.nio.ByteBuffer;
28 import java.nio.ByteOrder;
29 import java.nio.channels.FileChannel;
30 import java.nio.charset.Charset;
31 import java.nio.file.Path;
32 import java.nio.file.Paths;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.HashMap;
36 import java.util.List;
38 import java.util.Map.Entry;
39 import java.util.Optional;
40 import java.util.logging.Level;
41 import org.openide.util.NbBundle;
42 import org.openide.util.NbBundle.Messages;
57 import org.
sleuthkit.datamodel.BlackboardArtifact.ARTIFACT_TYPE;
90 final class ChromeCacheExtractor {
92 private final static String DEFAULT_CACHE_PATH_STR =
"default/cache";
93 private final static String BROTLI_MIMETYPE =
"application/x-brotli";
95 private final static long UINT32_MASK = 0xFFFFFFFFl;
97 private final static int INDEXFILE_HDR_SIZE = 92*4;
98 private final static int DATAFILE_HDR_SIZE = 8192;
100 private final static Logger logger = Logger.getLogger(ChromeCacheExtractor.class.getName());
102 private static final String VERSION_NUMBER =
"1.0.0";
103 private final String moduleName;
105 private String absOutputFolderName;
106 private String relOutputFolderName;
108 private final Content dataSource;
109 private final IngestJobContext context;
110 private final DataSourceIngestModuleProgress progressBar;
111 private final IngestServices services = IngestServices.getInstance();
112 private Case currentCase;
113 private FileManager fileManager;
116 private final Map<String, FileWrapper> fileCopyCache =
new HashMap<>();
119 private final Map<String, AbstractFile> externalFilesTable =
new HashMap<>();
126 final class FileWrapper {
127 private final AbstractFile abstractFile;
128 private final RandomAccessFile fileCopy;
129 private final ByteBuffer byteBuffer;
131 FileWrapper (AbstractFile abstractFile, RandomAccessFile fileCopy, ByteBuffer buffer ) {
132 this.abstractFile = abstractFile;
133 this.fileCopy = fileCopy;
134 this.byteBuffer = buffer;
137 public RandomAccessFile getFileCopy() {
140 public ByteBuffer getByteBuffer() {
143 AbstractFile getAbstractFile() {
149 "ChromeCacheExtractor.moduleName=ChromeCacheExtractor",
150 "# {0} - module name",
151 "# {1} - row number",
152 "# {2} - table length",
153 "# {3} - cache path",
154 "ChromeCacheExtractor.progressMsg={0}: Extracting cache entry {1} of {2} entries from {3}"
156 ChromeCacheExtractor(Content dataSource, IngestJobContext context, DataSourceIngestModuleProgress progressBar ) {
157 moduleName = Bundle.ChromeCacheExtractor_moduleName();
158 this.dataSource = dataSource;
159 this.context = context;
160 this.progressBar = progressBar;
169 private void moduleInit() throws IngestModuleException {
172 currentCase = Case.getCurrentCaseThrows();
173 fileManager = currentCase.getServices().getFileManager();
176 absOutputFolderName = RAImageIngestModule.getRAOutputPath(currentCase, moduleName);
177 relOutputFolderName = Paths.get( RAImageIngestModule.getRelModuleOutputPath(), moduleName).normalize().toString();
179 File dir =
new File(absOutputFolderName);
180 if (dir.exists() ==
false) {
183 }
catch (NoCurrentCaseException ex) {
184 String msg =
"Failed to get current case.";
185 throw new IngestModuleException(msg, ex);
196 private void resetForNewCacheFolder(String cachePath)
throws IngestModuleException {
198 fileCopyCache.clear();
199 externalFilesTable.clear();
201 String cacheAbsOutputFolderName = this.getAbsOutputFolderName() + cachePath;
202 File outDir =
new File(cacheAbsOutputFolderName);
203 if (outDir.exists() ==
false) {
207 String cacheTempPath = RAImageIngestModule.getRATempPath(currentCase, moduleName) + cachePath;
208 File tempDir =
new File(cacheTempPath);
209 if (tempDir.exists() ==
false) {
220 private void cleanup () {
222 for (Entry<String, FileWrapper> entry : this.fileCopyCache.entrySet()) {
223 Path tempFilePath = Paths.get(RAImageIngestModule.getRATempPath(currentCase, moduleName), entry.getKey() );
225 entry.getValue().getFileCopy().getChannel().close();
226 entry.getValue().getFileCopy().close();
228 File tmpFile = tempFilePath.toFile();
229 if (!tmpFile.delete()) {
230 tmpFile.deleteOnExit();
232 }
catch (IOException ex) {
233 logger.log(Level.WARNING, String.format(
"Failed to delete cache file copy %s", tempFilePath.toString()), ex);
243 private String getAbsOutputFolderName() {
244 return absOutputFolderName;
252 private String getRelOutputFolderName() {
253 return relOutputFolderName;
262 void processCaches() {
266 }
catch (IngestModuleException ex) {
267 String msg =
"Failed to initialize ChromeCacheExtractor.";
268 logger.log(Level.SEVERE, msg, ex);
275 List<AbstractFile> indexFiles = findIndexFiles();
278 for (AbstractFile indexFile: indexFiles) {
280 if (context.dataSourceIngestIsCancelled()) {
284 processCacheFolder(indexFile);
287 }
catch (TskCoreException ex) {
288 String msg =
"Failed to find cache index files";
289 logger.log(Level.WARNING, msg, ex);
294 "ChromeCacheExtract_adding_extracted_files_msg=Chrome Cache: Adding %d extracted files for analysis.",
295 "ChromeCacheExtract_adding_artifacts_msg=Chrome Cache: Adding %d artifacts for analysis.",
296 "ChromeCacheExtract_loading_files_msg=Chrome Cache: Loading files from %s."
305 private void processCacheFolder(AbstractFile indexFile) {
307 String cacheFolderName = indexFile.getParentPath();
308 Optional<FileWrapper> indexFileWrapper;
316 progressBar.progress(String.format(Bundle.ChromeCacheExtract_loading_files_msg(), cacheFolderName));
317 resetForNewCacheFolder(cacheFolderName);
321 indexFileWrapper = findDataOrIndexFile(indexFile.getName(), cacheFolderName);
322 if (!indexFileWrapper.isPresent()) {
323 String msg = String.format(
"Failed to find copy cache index file %s", indexFile.getUniquePath());
324 logger.log(Level.WARNING, msg);
331 for (
int i = 0; i < 4; i ++) {
332 Optional<FileWrapper> dataFile = findDataOrIndexFile(String.format(
"data_%1d",i), cacheFolderName );
333 if (!dataFile.isPresent()) {
340 findExternalFiles(cacheFolderName);
342 }
catch (TskCoreException | IngestModuleException ex) {
343 String msg =
"Failed to find cache files in path " + cacheFolderName;
344 logger.log(Level.WARNING, msg, ex);
352 logger.log(Level.INFO,
"{0}- Now reading Cache index file from path {1}",
new Object[]{moduleName, cacheFolderName });
354 List<AbstractFile> derivedFiles =
new ArrayList<>();
355 Collection<BlackboardArtifact> artifactsAdded =
new ArrayList<>();
357 ByteBuffer indexFileROBuffer = indexFileWrapper.get().getByteBuffer();
358 IndexFileHeader indexHdr =
new IndexFileHeader(indexFileROBuffer);
361 indexFileROBuffer.position(INDEXFILE_HDR_SIZE);
365 for (
int i = 0; i < indexHdr.getTableLen(); i++) {
367 if (context.dataSourceIngestIsCancelled()) {
372 CacheAddress addr =
new CacheAddress(indexFileROBuffer.getInt() & UINT32_MASK, cacheFolderName);
373 if (addr.isInitialized()) {
374 progressBar.progress(NbBundle.getMessage(
this.getClass(),
375 "ChromeCacheExtractor.progressMsg",
376 moduleName, i, indexHdr.getTableLen(), cacheFolderName) );
378 List<DerivedFile> addedFiles = processCacheEntry(addr, artifactsAdded);
379 derivedFiles.addAll(addedFiles);
381 catch (TskCoreException | IngestModuleException ex) {
382 logger.log(Level.WARNING, String.format(
"Failed to get cache entry at address %s", addr), ex);
387 if (context.dataSourceIngestIsCancelled()) {
394 progressBar.progress(String.format(Bundle.ChromeCacheExtract_adding_extracted_files_msg(), derivedFiles.size()));
395 derivedFiles.forEach((derived) -> {
396 services.fireModuleContentEvent(
new ModuleContentEvent(derived));
398 context.addFilesToJob(derivedFiles);
401 progressBar.progress(String.format(Bundle.ChromeCacheExtract_adding_artifacts_msg(), artifactsAdded.size()));
402 Blackboard blackboard = currentCase.getSleuthkitCase().getBlackboard();
404 blackboard.postArtifacts(artifactsAdded, moduleName);
405 }
catch (Blackboard.BlackboardException ex) {
406 logger.log(Level.WARNING, String.format(
"Failed to post cacheIndex artifacts "), ex);
424 private List<DerivedFile> processCacheEntry(CacheAddress cacheAddress, Collection<BlackboardArtifact> artifactsAdded )
throws TskCoreException, IngestModuleException {
426 List<DerivedFile> derivedFiles =
new ArrayList<>();
429 String cacheEntryFileName = cacheAddress.getFilename();
430 String cachePath = cacheAddress.getCachePath();
432 Optional<FileWrapper> cacheEntryFileOptional = findDataOrIndexFile(cacheEntryFileName, cachePath);
433 if (!cacheEntryFileOptional.isPresent()) {
434 String msg = String.format(
"Failed to find data file %s", cacheEntryFileName);
435 throw new IngestModuleException(msg);
439 CacheEntry cacheEntry =
new CacheEntry(cacheAddress, cacheEntryFileOptional.get() );
440 List<CacheDataSegment> dataSegments = cacheEntry.getDataSegments();
444 if (dataSegments.size() < 2) {
447 CacheDataSegment dataSegment = dataSegments.get(1);
450 String segmentFileName = dataSegment.getCacheAddress().getFilename();
451 Optional<AbstractFile> segmentFileAbstractFile = findAbstractFile(segmentFileName, cachePath);
452 if (!segmentFileAbstractFile.isPresent()) {
453 logger.log(Level.WARNING,
"Error finding segment file: " + cachePath +
"/" + segmentFileName);
457 boolean isBrotliCompressed =
false;
458 if (dataSegment.getType() != CacheDataTypeEnum.HTTP_HEADER && cacheEntry.isBrotliCompressed() ) {
459 isBrotliCompressed =
true;
465 AbstractFile cachedItemFile;
467 if (dataSegment.isInExternalFile() ) {
468 cachedItemFile = segmentFileAbstractFile.get();
474 String filename = dataSegment.save();
475 String relPathname = getRelOutputFolderName() + dataSegment.getCacheAddress().getCachePath() + filename;
478 DerivedFile derivedFile = fileManager.addDerivedFile(filename, relPathname,
479 dataSegment.getDataLength(),
480 cacheEntry.getCreationTime(), cacheEntry.getCreationTime(), cacheEntry.getCreationTime(), cacheEntry.getCreationTime(),
482 segmentFileAbstractFile.get(),
487 TskData.EncodingType.NONE);
489 derivedFiles.add(derivedFile);
490 cachedItemFile = derivedFile;
493 addArtifacts(cacheEntry, cacheEntryFileOptional.get().getAbstractFile(), cachedItemFile, artifactsAdded);
496 if (isBrotliCompressed) {
497 cachedItemFile.setMIMEType(BROTLI_MIMETYPE);
498 cachedItemFile.save();
501 }
catch (TskException ex) {
502 logger.log(Level.SEVERE,
"Error while trying to add an artifact", ex);
517 private void addArtifacts(CacheEntry cacheEntry, AbstractFile cacheEntryFile, AbstractFile cachedItemFile, Collection<BlackboardArtifact> artifactsAdded)
throws TskCoreException {
520 BlackboardArtifact webCacheArtifact = cacheEntryFile.newArtifact(ARTIFACT_TYPE.TSK_WEB_CACHE);
521 if (webCacheArtifact != null) {
522 Collection<BlackboardAttribute> webAttr =
new ArrayList<>();
523 String url = cacheEntry.getKey() != null ? cacheEntry.getKey() :
"";
524 webAttr.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_URL,
526 webAttr.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DOMAIN,
527 moduleName, NetworkUtils.extractDomain(url)));
528 webAttr.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_CREATED,
529 moduleName, cacheEntry.getCreationTime()));
530 webAttr.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_HEADERS,
531 moduleName, cacheEntry.getHTTPHeaders()));
532 webAttr.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PATH,
533 moduleName, cachedItemFile.getUniquePath()));
534 webAttr.add(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PATH_ID,
535 moduleName, cachedItemFile.getId()));
536 webCacheArtifact.addAttributes(webAttr);
537 artifactsAdded.add(webCacheArtifact);
540 BlackboardArtifact associatedObjectArtifact = cachedItemFile.newArtifact(ARTIFACT_TYPE.TSK_ASSOCIATED_OBJECT);
541 if (associatedObjectArtifact != null) {
542 associatedObjectArtifact.addAttribute(
543 new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_ASSOCIATED_ARTIFACT,
544 moduleName, webCacheArtifact.getArtifactID()));
545 artifactsAdded.add(associatedObjectArtifact);
558 private void findExternalFiles(String cachePath)
throws TskCoreException {
560 List<AbstractFile> effFiles = fileManager.findFiles(dataSource,
"f_%", cachePath);
561 for (AbstractFile abstractFile : effFiles ) {
562 if (cachePath.equals(abstractFile.getParentPath()) && abstractFile.isFile()) {
563 this.externalFilesTable.put(cachePath + abstractFile.getName(), abstractFile);
575 private Optional<AbstractFile> findAbstractFile(String cacheFileName, String cacheFolderName)
throws TskCoreException {
578 String fileTableKey = cacheFolderName + cacheFileName;
579 if (cacheFileName.startsWith(
"f_") && externalFilesTable.containsKey(fileTableKey)) {
580 return Optional.of(externalFilesTable.get(fileTableKey));
583 if (fileCopyCache.containsKey(fileTableKey)) {
584 return Optional.of(fileCopyCache.get(fileTableKey).getAbstractFile());
588 List<AbstractFile> cacheFiles = fileManager.findFiles(dataSource, cacheFileName, cacheFolderName);
589 if (!cacheFiles.isEmpty()) {
590 for (AbstractFile abstractFile: cacheFiles ) {
591 if (abstractFile.getUniquePath().trim().endsWith(DEFAULT_CACHE_PATH_STR)) {
592 return Optional.of(abstractFile);
595 return Optional.of(cacheFiles.get(0));
598 return Optional.empty();
608 private List<AbstractFile> findIndexFiles() throws TskCoreException {
609 return fileManager.findFiles(dataSource,
"index", DEFAULT_CACHE_PATH_STR);
625 private Optional<FileWrapper> findDataOrIndexFile(String cacheFileName, String cacheFolderName)
throws TskCoreException, IngestModuleException {
628 String fileTableKey = cacheFolderName + cacheFileName;
629 if (fileCopyCache.containsKey(fileTableKey)) {
630 return Optional.of(fileCopyCache.get(fileTableKey));
634 Optional<AbstractFile> abstractFileOptional = findAbstractFile(cacheFileName, cacheFolderName);
635 if (!abstractFileOptional.isPresent()) {
636 return Optional.empty();
644 AbstractFile cacheFile = abstractFileOptional.get();
645 RandomAccessFile randomAccessFile = null;
646 String tempFilePathname = RAImageIngestModule.getRATempPath(currentCase, moduleName) + cacheFolderName + cacheFile.getName();
648 File newFile =
new File(tempFilePathname);
649 ContentUtils.writeToFile(cacheFile, newFile, context::dataSourceIngestIsCancelled);
651 randomAccessFile =
new RandomAccessFile(tempFilePathname,
"r");
652 FileChannel roChannel = randomAccessFile.getChannel();
653 ByteBuffer cacheFileROBuf = roChannel.map(FileChannel.MapMode.READ_ONLY, 0,
654 (
int) roChannel.size());
656 cacheFileROBuf.order(ByteOrder.nativeOrder());
657 FileWrapper cacheFileWrapper =
new FileWrapper(cacheFile, randomAccessFile, cacheFileROBuf );
659 if (!cacheFileName.startsWith(
"f_")) {
660 fileCopyCache.put(cacheFolderName + cacheFileName, cacheFileWrapper);
663 return Optional.of(cacheFileWrapper);
665 catch (IOException ex) {
668 if (randomAccessFile != null) {
669 randomAccessFile.close();
672 catch (IOException ex2) {
673 logger.log(Level.SEVERE,
"Error while trying to close temp file after exception.", ex2);
675 String msg = String.format(
"Error reading/copying Chrome cache file '%s' (id=%d).",
676 cacheFile.getName(), cacheFile.getId());
677 throw new IngestModuleException(msg, ex);
684 final class IndexFileHeader {
686 private final long magic;
687 private final int version;
688 private final int numEntries;
689 private final int numBytes;
690 private final int lastFile;
691 private final int tableLen;
693 IndexFileHeader(ByteBuffer indexFileROBuf) {
695 magic = indexFileROBuf.getInt() & UINT32_MASK;
697 indexFileROBuf.position(indexFileROBuf.position()+2);
699 version = indexFileROBuf.getShort();
700 numEntries = indexFileROBuf.getInt();
701 numBytes = indexFileROBuf.getInt();
702 lastFile = indexFileROBuf.getInt();
704 indexFileROBuf.position(indexFileROBuf.position()+4);
705 indexFileROBuf.position(indexFileROBuf.position()+4);
707 tableLen = indexFileROBuf.getInt();
710 public long getMagic() {
714 public int getVersion() {
718 public int getNumEntries() {
722 public int getNumBytes() {
726 public int getLastFile() {
730 public int getTableLen() {
735 public String toString() {
736 StringBuilder sb =
new StringBuilder();
738 sb.append(String.format(
"Index Header:"))
739 .append(String.format(
"\tMagic = %x" , getMagic()) )
740 .append(String.format(
"\tVersion = %x" , getVersion()) )
741 .append(String.format(
"\tNumEntries = %x" , getNumEntries()) )
742 .append(String.format(
"\tNumBytes = %x" , getNumBytes()) )
743 .append(String.format(
"\tLastFile = %x" , getLastFile()) )
744 .append(String.format(
"\tTableLen = %x" , getTableLen()) );
746 return sb.toString();
753 enum CacheFileTypeEnum {
788 final class CacheAddress {
790 private static final long ADDR_INITIALIZED_MASK = 0x80000000l;
791 private static final long FILE_TYPE_MASK = 0x70000000;
792 private static final long FILE_TYPE_OFFSET = 28;
793 private static final long NUM_BLOCKS_MASK = 0x03000000;
794 private static final long NUM_BLOCKS_OFFSET = 24;
795 private static final long FILE_SELECTOR_MASK = 0x00ff0000;
796 private static final long FILE_SELECTOR_OFFSET = 16;
797 private static final long START_BLOCK_MASK = 0x0000FFFF;
798 private static final long EXTERNAL_FILE_NAME_MASK = 0x0FFFFFFF;
800 private final long uint32CacheAddr;
801 private final CacheFileTypeEnum fileType;
802 private final int numBlocks;
803 private final int startBlock;
804 private final String fileName;
805 private final int fileNumber;
807 private final String cachePath;
815 CacheAddress(
long uint32, String cachePath) {
817 uint32CacheAddr = uint32;
818 this.cachePath = cachePath;
822 int fileTypeEnc = (int)(uint32CacheAddr & FILE_TYPE_MASK) >> FILE_TYPE_OFFSET;
823 fileType = CacheFileTypeEnum.values()[fileTypeEnc];
825 if (isInitialized()) {
826 if (isInExternalFile()) {
827 fileNumber = (int)(uint32CacheAddr & EXTERNAL_FILE_NAME_MASK);
828 fileName = String.format(
"f_%06x", getFileNumber() );
832 fileNumber = (int)((uint32CacheAddr & FILE_SELECTOR_MASK) >> FILE_SELECTOR_OFFSET);
833 fileName = String.format(
"data_%d", getFileNumber() );
834 numBlocks = (int)(uint32CacheAddr & NUM_BLOCKS_MASK >> NUM_BLOCKS_OFFSET);
835 startBlock = (int)(uint32CacheAddr & START_BLOCK_MASK);
846 boolean isInitialized() {
847 return ((uint32CacheAddr & ADDR_INITIALIZED_MASK) != 0);
850 CacheFileTypeEnum getFileType() {
858 String getFilename() {
862 String getCachePath() {
866 boolean isInExternalFile() {
867 return (fileType == CacheFileTypeEnum.EXTERNAL);
870 int getFileNumber() {
874 int getStartBlock() {
903 public long getUint32CacheAddr() {
904 return uint32CacheAddr;
908 public String toString() {
909 StringBuilder sb =
new StringBuilder();
910 sb.append(String.format(
"CacheAddr %08x : %s : filename %s",
912 isInitialized() ?
"Initialized" :
"UnInitialized",
915 if ((fileType == CacheFileTypeEnum.BLOCK_256) ||
916 (fileType == CacheFileTypeEnum.BLOCK_1K) ||
917 (fileType == CacheFileTypeEnum.BLOCK_4K) ) {
918 sb.append(String.format(
" (%d blocks starting at %08X)",
924 return sb.toString();
932 enum CacheDataTypeEnum {
946 final class CacheDataSegment {
949 private final CacheAddress cacheAddress;
950 private CacheDataTypeEnum type;
952 private boolean isHTTPHeaderHint;
954 private FileWrapper cacheFileCopy = null;
955 private byte[] data = null;
957 private String httpResponse;
958 private final Map<String, String> httpHeaders =
new HashMap<>();
960 CacheDataSegment(CacheAddress cacheAddress,
int len) {
961 this(cacheAddress, len,
false);
964 CacheDataSegment(CacheAddress cacheAddress,
int len,
boolean isHTTPHeader ) {
965 this.type = CacheDataTypeEnum.UNKNOWN;
967 this.cacheAddress = cacheAddress;
968 this.isHTTPHeaderHint = isHTTPHeader;
971 boolean isInExternalFile() {
972 return cacheAddress.isInExternalFile();
975 boolean hasHTTPHeaders() {
976 return this.type == CacheDataTypeEnum.HTTP_HEADER;
979 String getHTTPHeader(String key) {
980 return this.httpHeaders.get(key);
988 String getHTTPHeaders() {
989 if (!hasHTTPHeaders()) {
993 StringBuilder sb =
new StringBuilder();
994 httpHeaders.entrySet().forEach((entry) -> {
995 if (sb.length() > 0) {
998 sb.append(String.format(
"%s : %s",
999 entry.getKey(), entry.getValue()));
1002 return sb.toString();
1005 String getHTTPRespone() {
1006 return httpResponse;
1014 void extract() throws TskCoreException, IngestModuleException {
1022 if (!cacheAddress.isInExternalFile() ) {
1024 cacheFileCopy = findDataOrIndexFile(cacheAddress.getFilename(), cacheAddress.getCachePath()).
get();
1026 this.data =
new byte [length];
1027 ByteBuffer buf = cacheFileCopy.getByteBuffer();
1028 int dataOffset = DATAFILE_HDR_SIZE + cacheAddress.getStartBlock() * cacheAddress.getBlockSize();
1029 buf.position(dataOffset);
1030 buf.get(data, 0, length);
1033 if ((isHTTPHeaderHint)) {
1034 String strData =
new String(data);
1035 if (strData.contains(
"HTTP")) {
1046 type = CacheDataTypeEnum.HTTP_HEADER;
1048 int startOff = strData.indexOf(
"HTTP");
1049 Charset charset = Charset.forName(
"UTF-8");
1050 boolean done =
false;
1057 while (i < data.length && data[i] != 0) {
1062 if (i == data.length || data[i+1] == 0) {
1066 int len = (i - start);
1067 String headerLine =
new String(data, start, len, charset);
1071 httpResponse = headerLine;
1073 int nPos = headerLine.indexOf(
':');
1075 String key = headerLine.substring(0, nPos);
1076 String val= headerLine.substring(nPos+1);
1077 httpHeaders.put(key.toLowerCase(), val);
1089 String getDataString() throws TskCoreException, IngestModuleException {
1093 return new String(data);
1096 byte[] getDataBytes() throws TskCoreException, IngestModuleException {
1100 return data.clone();
1103 int getDataLength() {
1107 CacheDataTypeEnum getType() {
1111 CacheAddress getCacheAddress() {
1112 return cacheAddress;
1124 String save() throws TskCoreException, IngestModuleException {
1127 if (cacheAddress.isInExternalFile()) {
1128 fileName = cacheAddress.getFilename();
1130 fileName = String.format(
"%s__%08x", cacheAddress.getFilename(), cacheAddress.getUint32CacheAddr());
1133 String filePathName = getAbsOutputFolderName() + cacheAddress.getCachePath() + fileName;
1148 void save(String filePathName)
throws TskCoreException, IngestModuleException {
1156 if (!this.isInExternalFile()) {
1158 try (FileOutputStream stream =
new FileOutputStream(filePathName)) {
1160 }
catch (IOException ex) {
1161 throw new TskCoreException(String.format(
"Failed to write output file %s", filePathName), ex);
1167 public String toString() {
1168 StringBuilder strBuilder =
new StringBuilder();
1169 strBuilder.append(String.format(
"\t\tData type = : %s, Data Len = %d ",
1170 this.type.toString(), this.length ));
1172 if (hasHTTPHeaders()) {
1173 String str = getHTTPHeader(
"content-encoding");
1175 strBuilder.append(String.format(
"\t%s=%s",
"content-encoding", str ));
1179 return strBuilder.toString();
1188 enum EntryStateEnum {
1227 final class CacheEntry {
1230 private static final int MAX_KEY_LEN = 256-24*4;
1232 private final CacheAddress selfAddress;
1233 private final FileWrapper cacheFileCopy;
1235 private final long hash;
1236 private final CacheAddress nextAddress;
1237 private final CacheAddress rankingsNodeAddress;
1239 private final int reuseCount;
1240 private final int refetchCount;
1241 private final EntryStateEnum state;
1243 private final long creationTime;
1244 private final int keyLen;
1246 private final CacheAddress longKeyAddresses;
1248 private final int[] dataSegmentSizes;
1249 private final CacheAddress[] dataSegmentIndexFileEntries;
1250 private List<CacheDataSegment> dataSegments;
1252 private final long flags;
1256 CacheEntry(CacheAddress cacheAdress, FileWrapper cacheFileCopy ) {
1257 this.selfAddress = cacheAdress;
1258 this.cacheFileCopy = cacheFileCopy;
1260 ByteBuffer fileROBuf = cacheFileCopy.getByteBuffer();
1262 int entryOffset = DATAFILE_HDR_SIZE + cacheAdress.getStartBlock() * cacheAdress.getBlockSize();
1265 fileROBuf.position(entryOffset);
1267 hash = fileROBuf.getInt() & UINT32_MASK;
1269 long uint32 = fileROBuf.getInt() & UINT32_MASK;
1270 nextAddress = (uint32 != 0) ?
new CacheAddress(uint32, selfAddress.getCachePath()) : null;
1272 uint32 = fileROBuf.getInt() & UINT32_MASK;
1273 rankingsNodeAddress = (uint32 != 0) ?
new CacheAddress(uint32, selfAddress.getCachePath()) : null;
1275 reuseCount = fileROBuf.getInt();
1276 refetchCount = fileROBuf.getInt();
1278 state = EntryStateEnum.values()[fileROBuf.getInt()];
1279 creationTime = (fileROBuf.getLong() / 1000000) - Long.valueOf(
"11644473600");
1281 keyLen = fileROBuf.getInt();
1283 uint32 = fileROBuf.getInt() & UINT32_MASK;
1284 longKeyAddresses = (uint32 != 0) ?
new CacheAddress(uint32, selfAddress.getCachePath()) : null;
1286 dataSegments = null;
1287 dataSegmentSizes=
new int[4];
1288 for (
int i = 0; i < 4; i++) {
1289 dataSegmentSizes[i] = fileROBuf.getInt();
1291 dataSegmentIndexFileEntries =
new CacheAddress[4];
1292 for (
int i = 0; i < 4; i++) {
1293 dataSegmentIndexFileEntries[i] =
new CacheAddress(fileROBuf.getInt() & UINT32_MASK, selfAddress.getCachePath());
1296 flags = fileROBuf.getInt() & UINT32_MASK;
1298 for (
int i = 0; i < 4; i++) {
1306 if (longKeyAddresses != null) {
1309 CacheDataSegment data =
new CacheDataSegment(longKeyAddresses, this.keyLen,
true);
1310 key = data.getDataString();
1311 }
catch (TskCoreException | IngestModuleException ex) {
1312 logger.log(Level.WARNING, String.format(
"Failed to get external key from address %s", longKeyAddresses));
1316 StringBuilder strBuilder =
new StringBuilder(MAX_KEY_LEN);
1318 while (fileROBuf.remaining() > 0 && keyLen < MAX_KEY_LEN) {
1319 char keyChar = (char)fileROBuf.get();
1320 if (keyChar ==
'\0') {
1323 strBuilder.append(keyChar);
1327 key = strBuilder.toString();
1331 public CacheAddress getCacheAddress() {
1335 public long getHash() {
1339 public CacheAddress getNextCacheAddress() {
1343 public int getReuseCount() {
1347 public int getRefetchCount() {
1348 return refetchCount;
1351 public EntryStateEnum getState() {
1355 public long getCreationTime() {
1356 return creationTime;
1359 public long getFlags() {
1363 public String getKey() {
1375 public List<CacheDataSegment> getDataSegments() throws TskCoreException, IngestModuleException {
1377 if (dataSegments == null) {
1378 dataSegments =
new ArrayList<>();
1379 for (
int i = 0; i < 4; i++) {
1380 if (dataSegmentSizes[i] > 0) {
1381 CacheDataSegment cacheData =
new CacheDataSegment(dataSegmentIndexFileEntries[i], dataSegmentSizes[i],
true );
1383 cacheData.extract();
1384 dataSegments.add(cacheData);
1388 return dataSegments;
1398 boolean hasHTTPHeaders() {
1399 if ((dataSegments == null) || dataSegments.isEmpty()) {
1402 return dataSegments.get(0).hasHTTPHeaders();
1411 String getHTTPHeader(String key) {
1412 if ((dataSegments == null) || dataSegments.isEmpty()) {
1416 return dataSegments.get(0).getHTTPHeader(key);
1424 String getHTTPHeaders() {
1425 if ((dataSegments == null) || dataSegments.isEmpty()) {
1429 return dataSegments.get(0).getHTTPHeaders();
1440 boolean isBrotliCompressed() {
1442 if (hasHTTPHeaders() ) {
1443 String encodingHeader = getHTTPHeader(
"content-encoding");
1444 if (encodingHeader!= null) {
1445 return encodingHeader.trim().equalsIgnoreCase(
"br");
1453 public String toString() {
1454 StringBuilder sb =
new StringBuilder();
1455 sb.append(String.format(
"Entry = Hash: %08x, State: %s, ReuseCount: %d, RefetchCount: %d",
1456 this.hash,
this.state.toString(), this.reuseCount, this.refetchCount ))
1457 .append(String.format(
"\n\tKey: %s, Keylen: %d",
1458 this.key,
this.keyLen,
this.reuseCount,
this.refetchCount ))
1459 .append(String.format(
"\n\tCreationTime: %s",
1460 TimeUtilities.epochToTime(
this.creationTime) ))
1461 .append(String.format(
"\n\tNext Address: %s",
1462 (nextAddress != null) ? nextAddress.toString() :
"None"));
1464 for (
int i = 0; i < 4; i++) {
1465 if (dataSegmentSizes[i] > 0) {
1466 sb.append(String.format(
"\n\tData %d: cache address = %s, Data = %s",
1467 i, dataSegmentIndexFileEntries[i].toString(),
1468 (dataSegments != null)
1469 ? dataSegments.get(i).toString()
1470 :
"Data not retrived yet."));
1474 return sb.toString();