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;
55 import org.
sleuthkit.datamodel.BlackboardArtifact.ARTIFACT_TYPE;
76 final class ChromeCacheExtractor {
78 private final static String DEFAULT_CACHE_PATH_STR =
"default/cache";
79 private final static String BROTLI_MIMETYPE =
"application/x-brotli";
81 private final static long UINT32_MASK = 0xFFFFFFFFl;
83 private final static int INDEXFILE_HDR_SIZE = 92*4;
84 private final static int DATAFILE_HDR_SIZE = 8192;
86 private final static Logger logger = Logger.getLogger(ChromeCacheExtractor.class.getName());
88 private static final String VERSION_NUMBER =
"1.0.0";
89 private final String moduleName;
91 private String absOutputFolderName;
92 private String relOutputFolderName;
94 private final Content dataSource;
95 private final IngestJobContext context;
96 private final DataSourceIngestModuleProgress progressBar;
97 private final IngestServices services = IngestServices.getInstance();
98 private Case currentCase;
99 private FileManager fileManager;
102 private final Map<String, CacheFileCopy> fileCopyCache =
new HashMap<>();
105 private final Map<String, AbstractFile> externalFilesTable =
new HashMap<>();
111 final class CacheFileCopy {
113 private final AbstractFile abstractFile;
114 private final RandomAccessFile fileCopy;
115 private final ByteBuffer byteBuffer;
117 CacheFileCopy (AbstractFile abstractFile, RandomAccessFile fileCopy, ByteBuffer buffer ) {
118 this.abstractFile = abstractFile;
119 this.fileCopy = fileCopy;
120 this.byteBuffer = buffer;
123 public RandomAccessFile getFileCopy() {
126 public ByteBuffer getByteBuffer() {
129 AbstractFile getAbstractFile() {
135 "ChromeCacheExtractor.moduleName=ChromeCacheExtractor",
136 "# {0} - module name",
137 "# {1} - row number",
138 "# {2} - table length",
139 "# {3} - cache path",
140 "ChromeCacheExtractor.progressMsg={0}: Extracting cache entry {1} of {2} entries from {3}"
142 ChromeCacheExtractor(Content dataSource, IngestJobContext context, DataSourceIngestModuleProgress progressBar ) {
143 moduleName = Bundle.ChromeCacheExtractor_moduleName();
144 this.dataSource = dataSource;
145 this.context = context;
146 this.progressBar = progressBar;
155 private void moduleInit() throws IngestModuleException {
158 currentCase = Case.getCurrentCaseThrows();
159 fileManager = currentCase.getServices().getFileManager();
162 absOutputFolderName = RAImageIngestModule.getRAOutputPath(currentCase, moduleName);
163 relOutputFolderName = Paths.get( RAImageIngestModule.getRelModuleOutputPath(), moduleName).normalize().toString();
165 File dir =
new File(absOutputFolderName);
166 if (dir.exists() ==
false) {
169 }
catch (NoCurrentCaseException ex) {
170 String msg =
"Failed to get current case.";
171 throw new IngestModuleException(msg, ex);
182 private void resetForNewFolder(String cachePath)
throws IngestModuleException {
184 fileCopyCache.clear();
185 externalFilesTable.clear();
187 String cacheAbsOutputFolderName = this.getAbsOutputFolderName() + cachePath;
188 File outDir =
new File(cacheAbsOutputFolderName);
189 if (outDir.exists() ==
false) {
193 String cacheTempPath = RAImageIngestModule.getRATempPath(currentCase, moduleName) + cachePath;
194 File tempDir =
new File(cacheTempPath);
195 if (tempDir.exists() ==
false) {
206 private void cleanup () {
208 for (Entry<String, CacheFileCopy> entry : this.fileCopyCache.entrySet()) {
209 Path tempFilePath = Paths.get(RAImageIngestModule.getRATempPath(currentCase, moduleName), entry.getKey() );
211 entry.getValue().getFileCopy().getChannel().close();
212 entry.getValue().getFileCopy().close();
214 File tmpFile = tempFilePath.toFile();
215 if (!tmpFile.delete()) {
216 tmpFile.deleteOnExit();
218 }
catch (IOException ex) {
219 logger.log(Level.WARNING, String.format(
"Failed to delete cache file copy %s", tempFilePath.toString()), ex);
229 private String getAbsOutputFolderName() {
230 return absOutputFolderName;
238 private String getRelOutputFolderName() {
239 return relOutputFolderName;
252 }
catch (IngestModuleException ex) {
253 String msg =
"Failed to initialize ChromeCacheExtractor.";
254 logger.log(Level.SEVERE, msg, ex);
259 List<AbstractFile> indexFiles;
261 indexFiles = findCacheIndexFiles();
264 for (AbstractFile indexFile: indexFiles) {
266 if (context.dataSourceIngestIsCancelled()) {
270 processCacheIndexFile(indexFile);
273 }
catch (TskCoreException ex) {
274 String msg =
"Failed to find cache index files";
275 logger.log(Level.WARNING, msg, ex);
284 private void processCacheIndexFile(AbstractFile indexAbstractFile) {
286 String cachePath = indexAbstractFile.getParentPath();
287 Optional<CacheFileCopy> indexFileCopy;
289 resetForNewFolder(cachePath);
292 indexFileCopy = this.getCacheFileCopy(indexAbstractFile.getName(), cachePath);
293 if (!indexFileCopy.isPresent()) {
294 String msg = String.format(
"Failed to find copy cache index file %s", indexAbstractFile.getUniquePath());
295 logger.log(Level.WARNING, msg);
301 for (
int i = 0; i < 4; i ++) {
302 Optional<CacheFileCopy> dataFile = findAndCopyCacheFile(String.format(
"data_%1d",i), cachePath );
303 if (!dataFile.isPresent()) {
309 findExternalFiles(cachePath);
311 }
catch (TskCoreException | IngestModuleException ex) {
312 String msg =
"Failed to find cache files in path " + cachePath;
313 logger.log(Level.WARNING, msg, ex);
319 logger.log(Level.INFO,
"{0}- Now reading Cache index file from path {1}",
new Object[]{moduleName, cachePath });
321 List<AbstractFile> derivedFiles =
new ArrayList<>();
322 Collection<BlackboardArtifact> sourceArtifacts =
new ArrayList<>();
323 Collection<BlackboardArtifact> webCacheArtifacts =
new ArrayList<>();
325 ByteBuffer indexFileROBuffer = indexFileCopy.get().getByteBuffer();
326 IndexFileHeader indexHdr =
new IndexFileHeader(indexFileROBuffer);
329 indexFileROBuffer.position(INDEXFILE_HDR_SIZE);
332 for (
int i = 0; i < indexHdr.getTableLen(); i++) {
334 if (context.dataSourceIngestIsCancelled()) {
339 CacheAddress addr =
new CacheAddress(indexFileROBuffer.getInt() & UINT32_MASK, cachePath);
340 if (addr.isInitialized()) {
341 progressBar.progress( NbBundle.getMessage(
this.getClass(),
342 "ChromeCacheExtractor.progressMsg",
343 moduleName, i, indexHdr.getTableLen(), cachePath) );
345 List<DerivedFile> addedFiles = this.processCacheEntry(addr, sourceArtifacts, webCacheArtifacts);
346 derivedFiles.addAll(addedFiles);
348 catch (TskCoreException | IngestModuleException ex) {
349 logger.log(Level.WARNING, String.format(
"Failed to get cache entry at address %s", addr), ex);
354 if (context.dataSourceIngestIsCancelled()) {
359 derivedFiles.forEach((derived) -> {
360 services.fireModuleContentEvent(
new ModuleContentEvent(derived));
363 context.addFilesToJob(derivedFiles);
365 Blackboard blackboard = currentCase.getSleuthkitCase().getBlackboard();
368 blackboard.postArtifacts(sourceArtifacts, moduleName);
369 blackboard.postArtifacts(webCacheArtifacts, moduleName);
370 }
catch (Blackboard.BlackboardException ex) {
371 logger.log(Level.WARNING, String.format(
"Failed to post cacheIndex artifacts "), ex);
388 private List<DerivedFile> processCacheEntry(CacheAddress cacheEntryAddress, Collection<BlackboardArtifact> sourceArtifacts, Collection<BlackboardArtifact> webCacheArtifacts )
throws TskCoreException, IngestModuleException {
390 List<DerivedFile> derivedFiles =
new ArrayList<>();
393 String dataFileName = cacheEntryAddress.getFilename();
394 String cachePath = cacheEntryAddress.getCachePath();
397 Optional<CacheFileCopy> cacheEntryFile = this.getCacheFileCopy(dataFileName, cachePath);
398 if (!cacheEntryFile.isPresent()) {
399 String msg = String.format(
"Failed to get cache entry at address %s", cacheEntryAddress);
400 throw new IngestModuleException(msg);
405 CacheEntry cacheEntry =
new CacheEntry(cacheEntryAddress, cacheEntryFile.get() );
407 List<CacheData> dataEntries = cacheEntry.getData();
410 if (dataEntries.size() < 2) {
413 CacheData dataSegment = dataEntries.get(1);
417 String cachedFileName = dataSegment.getAddress().getFilename();
418 Optional<AbstractFile> cachedFileAbstractFile = this.findCacheFile(cachedFileName, cachePath);
419 if (!cachedFileAbstractFile.isPresent()) {
420 logger.log(Level.WARNING,
"Error finding file: " + cachePath +
"/" + cachedFileName);
424 boolean isBrotliCompressed =
false;
425 if (dataSegment.getType() != CacheDataTypeEnum.HTTP_HEADER && cacheEntry.isBrotliCompressed() ) {
426 isBrotliCompressed =
true;
430 BlackboardAttribute urlAttr =
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_URL,
432 ((cacheEntry.getKey() != null) ? cacheEntry.getKey() :
""));
433 BlackboardAttribute createTimeAttr =
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_CREATED,
435 cacheEntry.getCreationTime());
436 BlackboardAttribute httpHeaderAttr =
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_HEADERS,
438 cacheEntry.getHTTPHeaders());
440 Collection<BlackboardAttribute> sourceArtifactAttributes =
new ArrayList<>();
441 sourceArtifactAttributes.add(urlAttr);
442 sourceArtifactAttributes.add(createTimeAttr);
444 Collection<BlackboardAttribute> webCacheAttributes =
new ArrayList<>();
445 webCacheAttributes.add(urlAttr);
446 webCacheAttributes.add(createTimeAttr);
447 webCacheAttributes.add(httpHeaderAttr);
451 if (dataSegment.isInExternalFile() ) {
453 BlackboardArtifact sourceArtifact = cachedFileAbstractFile.get().newArtifact(ARTIFACT_TYPE.TSK_DOWNLOAD_SOURCE);
454 if (sourceArtifact != null) {
455 sourceArtifact.addAttributes(sourceArtifactAttributes);
456 sourceArtifacts.add(sourceArtifact);
459 BlackboardArtifact webCacheArtifact = cacheEntryFile.get().getAbstractFile().newArtifact(ARTIFACT_TYPE.TSK_WEB_CACHE);
460 if (webCacheArtifact != null) {
461 webCacheArtifact.addAttributes(webCacheAttributes);
464 webCacheArtifact.addAttribute(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PATH,
466 cachedFileAbstractFile.get().getUniquePath()));
468 webCacheArtifact.addAttribute(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PATH_ID,
469 moduleName, cachedFileAbstractFile.get().getId()));
471 webCacheArtifacts.add(webCacheArtifact);
474 if (isBrotliCompressed) {
475 cachedFileAbstractFile.get().setMIMEType(BROTLI_MIMETYPE);
476 cachedFileAbstractFile.get().save();
478 }
catch (TskException ex) {
479 logger.log(Level.SEVERE,
"Error while trying to add an artifact", ex);
486 String filename = dataSegment.save();
487 String relPathname = getRelOutputFolderName() + dataSegment.getAddress().getCachePath() + filename;
489 DerivedFile derivedFile = fileManager.addDerivedFile(filename, relPathname,
490 dataSegment.getDataLength(),
491 cacheEntry.getCreationTime(), cacheEntry.getCreationTime(), cacheEntry.getCreationTime(), cacheEntry.getCreationTime(),
493 cachedFileAbstractFile.get(),
498 TskData.EncodingType.NONE);
500 BlackboardArtifact sourceArtifact = derivedFile.newArtifact(ARTIFACT_TYPE.TSK_DOWNLOAD_SOURCE);
501 if (sourceArtifact != null) {
502 sourceArtifact.addAttributes(sourceArtifactAttributes);
503 sourceArtifacts.add(sourceArtifact);
506 BlackboardArtifact webCacheArtifact = cacheEntryFile.get().getAbstractFile().newArtifact(ARTIFACT_TYPE.TSK_WEB_CACHE);
507 if (webCacheArtifact != null) {
508 webCacheArtifact.addAttributes(webCacheAttributes);
511 webCacheArtifact.addAttribute(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PATH,
513 derivedFile.getUniquePath()));
515 webCacheArtifact.addAttribute(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_PATH_ID,
516 moduleName, derivedFile.getId()));
518 webCacheArtifacts.add(webCacheArtifact);
521 if (isBrotliCompressed) {
522 derivedFile.setMIMEType(BROTLI_MIMETYPE);
526 derivedFiles.add(derivedFile);
527 }
catch (TskException ex) {
528 logger.log(Level.SEVERE,
"Error while trying to add an artifact", ex);
543 private void findExternalFiles(String cachePath)
throws TskCoreException {
545 List<AbstractFile> effFiles = fileManager.findFiles(dataSource,
"f_%", cachePath);
546 for (AbstractFile abstractFile : effFiles ) {
547 this.externalFilesTable.put(cachePath + abstractFile.getName(), abstractFile);
558 private Optional<AbstractFile> findCacheFile(String cacheFileName, String cachePath)
throws TskCoreException {
561 String fileTableKey = cachePath + cacheFileName;
562 if (cacheFileName.startsWith(
"f_") && externalFilesTable.containsKey(fileTableKey)) {
563 return Optional.of(externalFilesTable.get(fileTableKey));
565 if (fileCopyCache.containsKey(fileTableKey)) {
566 return Optional.of(fileCopyCache.get(fileTableKey).getAbstractFile());
570 List<AbstractFile> cacheFiles = fileManager.findFiles(dataSource, cacheFileName, cachePath);
571 if (!cacheFiles.isEmpty()) {
572 for (AbstractFile abstractFile: cacheFiles ) {
573 if (abstractFile.getUniquePath().trim().endsWith(DEFAULT_CACHE_PATH_STR)) {
574 return Optional.of(abstractFile);
577 return Optional.of(cacheFiles.get(0));
580 return Optional.empty();
589 private List<AbstractFile> findCacheIndexFiles() throws TskCoreException {
590 return fileManager.findFiles(dataSource,
"index", DEFAULT_CACHE_PATH_STR);
603 private Optional<CacheFileCopy> getCacheFileCopy(String cacheFileName, String cachePath)
throws TskCoreException, IngestModuleException {
606 String fileTableKey = cachePath + cacheFileName;
607 if (fileCopyCache.containsKey(fileTableKey)) {
608 return Optional.of(fileCopyCache.get(fileTableKey));
611 return findAndCopyCacheFile(cacheFileName, cachePath);
621 private Optional<CacheFileCopy> findAndCopyCacheFile(String cacheFileName, String cachePath)
throws TskCoreException, IngestModuleException {
623 Optional<AbstractFile> cacheFileOptional = findCacheFile(cacheFileName, cachePath);
624 if (!cacheFileOptional.isPresent()) {
625 return Optional.empty();
632 AbstractFile cacheFile = cacheFileOptional.get();
633 RandomAccessFile randomAccessFile = null;
634 String tempFilePathname = RAImageIngestModule.getRATempPath(currentCase, moduleName) + cachePath + cacheFile.getName();
636 File newFile =
new File(tempFilePathname);
637 ContentUtils.writeToFile(cacheFile, newFile, context::dataSourceIngestIsCancelled);
639 randomAccessFile =
new RandomAccessFile(tempFilePathname,
"r");
640 FileChannel roChannel = randomAccessFile.getChannel();
641 ByteBuffer cacheFileROBuf = roChannel.map(FileChannel.MapMode.READ_ONLY, 0,
642 (
int) roChannel.size());
644 cacheFileROBuf.order(ByteOrder.nativeOrder());
645 CacheFileCopy cacheFileCopy =
new CacheFileCopy(cacheFile, randomAccessFile, cacheFileROBuf );
647 if (!cacheFileName.startsWith(
"f_")) {
648 fileCopyCache.put(cachePath + cacheFileName, cacheFileCopy);
651 return Optional.of(cacheFileCopy);
653 catch (IOException ex) {
656 if (randomAccessFile != null) {
657 randomAccessFile.close();
660 catch (IOException ex2) {
661 logger.log(Level.SEVERE,
"Error while trying to close temp file after exception.", ex2);
663 String msg = String.format(
"Error reading/copying Chrome cache file '%s' (id=%d).",
664 cacheFile.getName(), cacheFile.getId());
665 throw new IngestModuleException(msg, ex);
672 final class IndexFileHeader {
674 private final long magic;
675 private final int version;
676 private final int numEntries;
677 private final int numBytes;
678 private final int lastFile;
679 private final int tableLen;
681 IndexFileHeader(ByteBuffer indexFileROBuf) {
683 magic = indexFileROBuf.getInt() & UINT32_MASK;
685 indexFileROBuf.position(indexFileROBuf.position()+2);
687 version = indexFileROBuf.getShort();
688 numEntries = indexFileROBuf.getInt();
689 numBytes = indexFileROBuf.getInt();
690 lastFile = indexFileROBuf.getInt();
692 indexFileROBuf.position(indexFileROBuf.position()+4);
693 indexFileROBuf.position(indexFileROBuf.position()+4);
695 tableLen = indexFileROBuf.getInt();
698 public long getMagic() {
702 public int getVersion() {
706 public int getNumEntries() {
710 public int getNumBytes() {
714 public int getLastFile() {
718 public int getTableLen() {
723 public String toString() {
724 StringBuilder sb =
new StringBuilder();
726 sb.append(String.format(
"Index Header:"))
727 .append(String.format(
"\tMagic = %x" , getMagic()) )
728 .append(String.format(
"\tVersion = %x" , getVersion()) )
729 .append(String.format(
"\tNumEntries = %x" , getNumEntries()) )
730 .append(String.format(
"\tNumBytes = %x" , getNumBytes()) )
731 .append(String.format(
"\tLastFile = %x" , getLastFile()) )
732 .append(String.format(
"\tTableLen = %x" , getTableLen()) );
734 return sb.toString();
741 enum CacheFileTypeEnum {
773 final class CacheAddress {
775 private static final long ADDR_INITIALIZED_MASK = 0x80000000l;
776 private static final long FILE_TYPE_MASK = 0x70000000;
777 private static final long FILE_TYPE_OFFSET = 28;
778 private static final long NUM_BLOCKS_MASK = 0x03000000;
779 private static final long NUM_BLOCKS_OFFSET = 24;
780 private static final long FILE_SELECTOR_MASK = 0x00ff0000;
781 private static final long FILE_SELECTOR_OFFSET = 16;
782 private static final long START_BLOCK_MASK = 0x0000FFFF;
783 private static final long EXTERNAL_FILE_NAME_MASK = 0x0FFFFFFF;
785 private final long uint32CacheAddr;
786 private final CacheFileTypeEnum fileType;
787 private final int numBlocks;
788 private final int startBlock;
789 private final String fileName;
790 private final int fileNumber;
792 private final String cachePath;
795 CacheAddress(
long uint32, String cachePath) {
797 uint32CacheAddr = uint32;
798 this.cachePath = cachePath;
800 int fileTypeEnc = (int)(uint32CacheAddr & FILE_TYPE_MASK) >> FILE_TYPE_OFFSET;
801 fileType = CacheFileTypeEnum.values()[fileTypeEnc];
803 if (isInitialized()) {
804 if (isInExternalFile()) {
805 fileNumber = (int)(uint32CacheAddr & EXTERNAL_FILE_NAME_MASK);
806 fileName = String.format(
"f_%06x", getFileNumber() );
810 fileNumber = (int)((uint32CacheAddr & FILE_SELECTOR_MASK) >> FILE_SELECTOR_OFFSET);
811 fileName = String.format(
"data_%d", getFileNumber() );
812 numBlocks = (int)(uint32CacheAddr & NUM_BLOCKS_MASK >> NUM_BLOCKS_OFFSET);
813 startBlock = (int)(uint32CacheAddr & START_BLOCK_MASK);
824 boolean isInitialized() {
825 return ((uint32CacheAddr & ADDR_INITIALIZED_MASK) != 0);
828 CacheFileTypeEnum getFileType() {
836 String getFilename() {
840 String getCachePath() {
844 boolean isInExternalFile() {
845 return (fileType == CacheFileTypeEnum.EXTERNAL);
848 int getFileNumber() {
852 int getStartBlock() {
881 public long getUint32CacheAddr() {
882 return uint32CacheAddr;
886 public String toString() {
887 StringBuilder sb =
new StringBuilder();
888 sb.append(String.format(
"CacheAddr %08x : %s : filename %s",
890 isInitialized() ?
"Initialized" :
"UnInitialized",
893 if ((fileType == CacheFileTypeEnum.BLOCK_256) ||
894 (fileType == CacheFileTypeEnum.BLOCK_1K) ||
895 (fileType == CacheFileTypeEnum.BLOCK_4K) ) {
896 sb.append(String.format(
" (%d blocks starting at %08X)",
902 return sb.toString();
910 enum CacheDataTypeEnum {
924 final class CacheData {
927 private final CacheAddress address;
928 private CacheDataTypeEnum type;
930 private boolean isHTTPHeaderHint;
932 private CacheFileCopy cacheFileCopy = null;
933 private byte[] data = null;
935 private String httpResponse;
936 private final Map<String, String> httpHeaders =
new HashMap<>();
938 CacheData(CacheAddress cacheAdress,
int len) {
939 this(cacheAdress, len,
false);
942 CacheData(CacheAddress cacheAdress,
int len,
boolean isHTTPHeader ) {
943 this.type = CacheDataTypeEnum.UNKNOWN;
945 this.address = cacheAdress;
946 this.isHTTPHeaderHint = isHTTPHeader;
949 boolean isInExternalFile() {
950 return address.isInExternalFile();
953 boolean hasHTTPHeaders() {
954 return this.type == CacheDataTypeEnum.HTTP_HEADER;
957 String getHTTPHeader(String key) {
958 return this.httpHeaders.get(key);
966 String getHTTPHeaders() {
967 if (!hasHTTPHeaders()) {
971 StringBuilder sb =
new StringBuilder();
972 httpHeaders.entrySet().forEach((entry) -> {
973 if (sb.length() > 0) {
976 sb.append(String.format(
"%s : %s",
977 entry.getKey(), entry.getValue()));
980 return sb.toString();
983 String getHTTPRespone() {
992 void extract() throws TskCoreException, IngestModuleException {
1000 if (!address.isInExternalFile() ) {
1002 cacheFileCopy = getCacheFileCopy(address.getFilename(), address.getCachePath()).
get();
1004 this.data =
new byte [length];
1005 ByteBuffer buf = cacheFileCopy.getByteBuffer();
1006 int dataOffset = DATAFILE_HDR_SIZE + address.getStartBlock() * address.getBlockSize();
1007 buf.position(dataOffset);
1008 buf.get(data, 0, length);
1011 if ((isHTTPHeaderHint)) {
1012 String strData =
new String(data);
1013 if (strData.contains(
"HTTP")) {
1024 type = CacheDataTypeEnum.HTTP_HEADER;
1026 int startOff = strData.indexOf(
"HTTP");
1027 Charset charset = Charset.forName(
"UTF-8");
1028 boolean done =
false;
1035 while (i < data.length && data[i] != 0) {
1040 if (i == data.length || data[i+1] == 0) {
1044 int len = (i - start);
1045 String headerLine =
new String(data, start, len, charset);
1049 httpResponse = headerLine;
1051 int nPos = headerLine.indexOf(
':');
1053 String key = headerLine.substring(0, nPos);
1054 String val= headerLine.substring(nPos+1);
1055 httpHeaders.put(key.toLowerCase(), val);
1067 String getDataString() throws TskCoreException, IngestModuleException {
1071 return new String(data);
1074 byte[] getDataBytes() throws TskCoreException, IngestModuleException {
1078 return data.clone();
1081 int getDataLength() {
1085 CacheDataTypeEnum getType() {
1089 CacheAddress getAddress() {
1102 String save() throws TskCoreException, IngestModuleException {
1105 if (address.isInExternalFile()) {
1106 fileName = address.getFilename();
1108 fileName = String.format(
"%s__%08x", address.getFilename(), address.getUint32CacheAddr());
1111 String filePathName = getAbsOutputFolderName() + address.getCachePath() + fileName;
1126 void save(String filePathName)
throws TskCoreException, IngestModuleException {
1134 if (!this.isInExternalFile()) {
1136 try (FileOutputStream stream =
new FileOutputStream(filePathName)) {
1138 }
catch (IOException ex) {
1139 throw new TskCoreException(String.format(
"Failed to write output file %s", filePathName), ex);
1145 public String toString() {
1146 StringBuilder strBuilder =
new StringBuilder();
1147 strBuilder.append(String.format(
"\t\tData type = : %s, Data Len = %d ",
1148 this.type.toString(), this.length ));
1150 if (hasHTTPHeaders()) {
1151 String str = getHTTPHeader(
"content-encoding");
1153 strBuilder.append(String.format(
"\t%s=%s",
"content-encoding", str ));
1157 return strBuilder.toString();
1166 enum EntryStateEnum {
1205 final class CacheEntry {
1208 private static final int MAX_KEY_LEN = 256-24*4;
1210 private final CacheAddress selfAddress;
1211 private final CacheFileCopy cacheFileCopy;
1213 private final long hash;
1214 private final CacheAddress nextAddress;
1215 private final CacheAddress rankingsNodeAddress;
1217 private final int reuseCount;
1218 private final int refetchCount;
1219 private final EntryStateEnum state;
1221 private final long creationTime;
1222 private final int keyLen;
1224 private final CacheAddress longKeyAddresses;
1226 private final int dataSizes[];
1227 private final CacheAddress dataAddresses[];
1228 private List<CacheData> dataList;
1230 private final long flags;
1234 CacheEntry(CacheAddress cacheAdress, CacheFileCopy cacheFileCopy ) {
1235 this.selfAddress = cacheAdress;
1236 this.cacheFileCopy = cacheFileCopy;
1238 ByteBuffer fileROBuf = cacheFileCopy.getByteBuffer();
1240 int entryOffset = DATAFILE_HDR_SIZE + cacheAdress.getStartBlock() * cacheAdress.getBlockSize();
1243 fileROBuf.position(entryOffset);
1245 hash = fileROBuf.getInt() & UINT32_MASK;
1247 long uint32 = fileROBuf.getInt() & UINT32_MASK;
1248 nextAddress = (uint32 != 0) ?
new CacheAddress(uint32, selfAddress.getCachePath()) : null;
1250 uint32 = fileROBuf.getInt() & UINT32_MASK;
1251 rankingsNodeAddress = (uint32 != 0) ?
new CacheAddress(uint32, selfAddress.getCachePath()) : null;
1253 reuseCount = fileROBuf.getInt();
1254 refetchCount = fileROBuf.getInt();
1256 state = EntryStateEnum.values()[fileROBuf.getInt()];
1257 creationTime = (fileROBuf.getLong() / 1000000) - Long.valueOf(
"11644473600");
1259 keyLen = fileROBuf.getInt();
1261 uint32 = fileROBuf.getInt() & UINT32_MASK;
1262 longKeyAddresses = (uint32 != 0) ?
new CacheAddress(uint32, selfAddress.getCachePath()) : null;
1265 dataSizes=
new int[4];
1266 for (
int i = 0; i < 4; i++) {
1267 dataSizes[i] = fileROBuf.getInt();
1269 dataAddresses =
new CacheAddress[4];
1270 for (
int i = 0; i < 4; i++) {
1271 dataAddresses[i] =
new CacheAddress(fileROBuf.getInt() & UINT32_MASK, selfAddress.getCachePath());
1274 flags = fileROBuf.getInt() & UINT32_MASK;
1276 for (
int i = 0; i < 4; i++) {
1284 if (longKeyAddresses != null) {
1287 CacheData data =
new CacheData(longKeyAddresses, this.keyLen,
true);
1288 key = data.getDataString();
1289 }
catch (TskCoreException | IngestModuleException ex) {
1290 logger.log(Level.WARNING, String.format(
"Failed to get external key from address %s", longKeyAddresses));
1294 StringBuilder strBuilder =
new StringBuilder(MAX_KEY_LEN);
1296 while (fileROBuf.remaining() > 0 && keyLen < MAX_KEY_LEN) {
1297 char keyChar = (char)fileROBuf.get();
1298 if (keyChar ==
'\0') {
1301 strBuilder.append(keyChar);
1305 key = strBuilder.toString();
1309 public CacheAddress getAddress() {
1313 public long getHash() {
1317 public CacheAddress getNextAddress() {
1321 public int getReuseCount() {
1325 public int getRefetchCount() {
1326 return refetchCount;
1329 public EntryStateEnum getState() {
1333 public long getCreationTime() {
1334 return creationTime;
1337 public long getFlags() {
1341 public String getKey() {
1353 public List<CacheData> getData() throws TskCoreException, IngestModuleException {
1355 if (dataList == null) {
1356 dataList =
new ArrayList<>();
1357 for (
int i = 0; i < 4; i++) {
1358 if (dataSizes[i] > 0) {
1359 CacheData cacheData =
new CacheData(dataAddresses[i], dataSizes[i],
true );
1361 cacheData.extract();
1362 dataList.add(cacheData);
1376 boolean hasHTTPHeaders() {
1377 if ((dataList == null) || dataList.isEmpty()) {
1380 return dataList.get(0).hasHTTPHeaders();
1389 String getHTTPHeader(String key) {
1390 if ((dataList == null) || dataList.isEmpty()) {
1394 return dataList.get(0).getHTTPHeader(key);
1402 String getHTTPHeaders() {
1403 if ((dataList == null) || dataList.isEmpty()) {
1407 return dataList.get(0).getHTTPHeaders();
1418 boolean isBrotliCompressed() {
1420 if (hasHTTPHeaders() ) {
1421 String encodingHeader = getHTTPHeader(
"content-encoding");
1422 if (encodingHeader!= null) {
1423 return encodingHeader.trim().equalsIgnoreCase(
"br");
1431 public String toString() {
1432 StringBuilder sb =
new StringBuilder();
1433 sb.append(String.format(
"Entry = Hash: %08x, State: %s, ReuseCount: %d, RefetchCount: %d",
1434 this.hash,
this.state.toString(), this.reuseCount, this.refetchCount ))
1435 .append(String.format(
"\n\tKey: %s, Keylen: %d",
1436 this.key,
this.keyLen,
this.reuseCount,
this.refetchCount ))
1437 .append(String.format(
"\n\tCreationTime: %s",
1438 TimeUtilities.epochToTime(
this.creationTime) ))
1439 .append(String.format(
"\n\tNext Address: %s",
1440 (nextAddress != null) ? nextAddress.toString() :
"None"));
1442 for (
int i = 0; i < 4; i++) {
1443 if (dataSizes[i] > 0) {
1444 sb.append(String.format(
"\n\tData %d: cache address = %s, Data = %s",
1445 i, dataAddresses[i].toString(),
1447 ? dataList.get(i).toString()
1448 :
"Data not retrived yet."));
1452 return sb.toString();