19 package org.sleuthkit.autopsy.modules.encryptiondetection;
21 import com.healthmarketscience.jackcess.CryptCodecProvider;
22 import com.healthmarketscience.jackcess.Database;
23 import com.healthmarketscience.jackcess.DatabaseBuilder;
24 import com.healthmarketscience.jackcess.InvalidCredentialsException;
25 import com.healthmarketscience.jackcess.impl.CodecProvider;
26 import com.healthmarketscience.jackcess.impl.UnsupportedCodecException;
27 import com.healthmarketscience.jackcess.util.MemFileChannel;
28 import java.io.IOException;
29 import java.util.Collections;
30 import java.util.logging.Level;
32 import java.io.BufferedInputStream;
33 import java.io.InputStream;
34 import java.nio.BufferUnderflowException;
35 import org.apache.tika.exception.EncryptedDocumentException;
36 import org.apache.tika.exception.TikaException;
37 import org.apache.tika.metadata.Metadata;
38 import org.apache.tika.parser.AutoDetectParser;
39 import org.apache.tika.parser.ParseContext;
40 import org.apache.tika.sax.BodyContentHandler;
41 import org.openide.util.NbBundle.Messages;
56 import org.
sleuthkit.datamodel.ReadContentInputStream.ReadContentInputStreamException;
59 import org.xml.sax.ContentHandler;
60 import org.xml.sax.SAXException;
65 final class EncryptionDetectionFileIngestModule
extends FileIngestModuleAdapter {
67 private static final int FILE_SIZE_MODULUS = 512;
69 private static final String DATABASE_FILE_EXTENSION =
"db";
70 private static final int MINIMUM_DATABASE_FILE_SIZE = 65536;
72 private static final String MIME_TYPE_OOXML_PROTECTED =
"application/x-ooxml-protected";
73 private static final String MIME_TYPE_MSWORD =
"application/msword";
74 private static final String MIME_TYPE_MSEXCEL =
"application/vnd.ms-excel";
75 private static final String MIME_TYPE_MSPOWERPOINT =
"application/vnd.ms-powerpoint";
76 private static final String MIME_TYPE_MSACCESS =
"application/x-msaccess";
77 private static final String MIME_TYPE_PDF =
"application/pdf";
79 private static final String[] FILE_IGNORE_LIST = {
"hiberfile.sys",
"pagefile.sys"};
81 private final IngestServices services = IngestServices.getInstance();
82 private final Logger logger = services.getLogger(EncryptionDetectionModuleFactory.getModuleName());
83 private FileTypeDetector fileTypeDetector;
84 private Blackboard blackboard;
85 private IngestJobContext context;
86 private double calculatedEntropy;
88 private final double minimumEntropy;
89 private final int minimumFileSize;
90 private final boolean fileSizeMultipleEnforced;
91 private final boolean slackFilesAllowed;
100 EncryptionDetectionFileIngestModule(EncryptionDetectionIngestJobSettings settings) {
101 minimumEntropy = settings.getMinimumEntropy();
102 minimumFileSize = settings.getMinimumFileSize();
103 fileSizeMultipleEnforced = settings.isFileSizeMultipleEnforced();
104 slackFilesAllowed = settings.isSlackFilesAllowed();
108 public void startUp(IngestJobContext context)
throws IngestModule.IngestModuleException {
111 this.context = context;
112 blackboard = Case.getCurrentCaseThrows().getServices().getBlackboard();
113 fileTypeDetector =
new FileTypeDetector();
114 }
catch (FileTypeDetector.FileTypeDetectorInitException ex) {
115 throw new IngestModule.IngestModuleException(
"Failed to create file type detector", ex);
116 }
catch (NoCurrentCaseException ex) {
117 throw new IngestModule.IngestModuleException(
"Exception while getting open case.", ex);
122 "EncryptionDetectionFileIngestModule.artifactComment.password=Password protection detected.",
123 "EncryptionDetectionFileIngestModule.artifactComment.suspected=Suspected encryption due to high entropy (%f)."
126 public IngestModule.ProcessResult process(AbstractFile file) {
133 if (!file.getType().equals(TskData.TSK_DB_FILES_TYPE_ENUM.UNALLOC_BLOCKS)
134 && !file.getType().equals(TskData.TSK_DB_FILES_TYPE_ENUM.UNUSED_BLOCKS)
135 && !file.getType().equals(TskData.TSK_DB_FILES_TYPE_ENUM.VIRTUAL_DIR)
136 && !file.getType().equals(TskData.TSK_DB_FILES_TYPE_ENUM.LOCAL_DIR)
137 && (!file.getType().equals(TskData.TSK_DB_FILES_TYPE_ENUM.SLACK) || slackFilesAllowed)
138 && !file.getKnown().equals(TskData.FileKnown.KNOWN)
139 && !file.isMetaFlagSet(TskData.TSK_FS_META_FLAG_ENUM.UNALLOC)) {
143 String filePath = file.getParentPath();
144 if (filePath.equals(
"/")) {
145 String fileName = file.getName();
146 for (String listEntry : FILE_IGNORE_LIST) {
147 if (fileName.equalsIgnoreCase(listEntry)) {
149 return IngestModule.ProcessResult.OK;
157 String mimeType = fileTypeDetector.getMIMEType(file);
158 if (mimeType.equals(
"application/octet-stream") && isFileEncryptionSuspected(file)) {
159 return flagFile(file, BlackboardArtifact.ARTIFACT_TYPE.TSK_ENCRYPTION_SUSPECTED,
160 String.format(Bundle.EncryptionDetectionFileIngestModule_artifactComment_suspected(), calculatedEntropy));
161 }
else if (isFilePasswordProtected(file)) {
162 return flagFile(file, BlackboardArtifact.ARTIFACT_TYPE.TSK_ENCRYPTION_DETECTED, Bundle.EncryptionDetectionFileIngestModule_artifactComment_password());
165 }
catch (ReadContentInputStreamException | SAXException | TikaException | UnsupportedCodecException ex) {
166 logger.log(Level.WARNING, String.format(
"Unable to read file '%s'", file.getParentPath() + file.getName()), ex);
167 return IngestModule.ProcessResult.ERROR;
168 }
catch (IOException ex) {
169 logger.log(Level.SEVERE, String.format(
"Unable to process file '%s'", file.getParentPath() + file.getName()), ex);
170 return IngestModule.ProcessResult.ERROR;
173 return IngestModule.ProcessResult.OK;
182 private void validateSettings() throws IngestModule.IngestModuleException {
183 EncryptionDetectionTools.validateMinEntropyValue(minimumEntropy);
184 EncryptionDetectionTools.validateMinFileSizeValue(minimumFileSize);
197 private IngestModule.ProcessResult flagFile(AbstractFile file, BlackboardArtifact.ARTIFACT_TYPE artifactType, String comment) {
199 if (context.fileIngestIsCancelled()) {
200 return IngestModule.ProcessResult.OK;
203 BlackboardArtifact artifact = file.newArtifact(artifactType);
204 artifact.addAttribute(
new BlackboardAttribute(BlackboardAttribute.ATTRIBUTE_TYPE.TSK_COMMENT,
205 EncryptionDetectionModuleFactory.getModuleName(), comment));
211 blackboard.indexArtifact(artifact);
212 }
catch (Blackboard.BlackboardException ex) {
213 logger.log(Level.SEVERE,
"Unable to index blackboard artifact " + artifact.getArtifactID(), ex);
219 services.fireModuleDataEvent(
new ModuleDataEvent(EncryptionDetectionModuleFactory.getModuleName(), artifactType, Collections.singletonList(artifact)));
224 StringBuilder detailsSb =
new StringBuilder();
225 detailsSb.append(
"File: ").append(file.getParentPath()).append(file.getName());
226 if (artifactType.equals(BlackboardArtifact.ARTIFACT_TYPE.TSK_ENCRYPTION_SUSPECTED)) {
227 detailsSb.append(
"<br/>\nEntropy: ").append(calculatedEntropy);
230 services.postMessage(IngestMessage.createDataMessage(EncryptionDetectionModuleFactory.getModuleName(),
231 artifactType.getDisplayName() +
" Match: " + file.getName(),
232 detailsSb.toString(),
236 return IngestModule.ProcessResult.OK;
237 }
catch (TskCoreException ex) {
238 logger.log(Level.SEVERE, String.format(
"Failed to create blackboard artifact for '%s'.", file.getParentPath() + file.getName()), ex);
239 return IngestModule.ProcessResult.ERROR;
262 private boolean isFilePasswordProtected(AbstractFile file)
throws ReadContentInputStreamException, IOException, SAXException, TikaException, UnsupportedCodecException {
264 boolean passwordProtected =
false;
266 switch (file.getMIMEType()) {
267 case MIME_TYPE_OOXML_PROTECTED:
272 passwordProtected =
true;
275 case MIME_TYPE_MSWORD:
276 case MIME_TYPE_MSEXCEL:
277 case MIME_TYPE_MSPOWERPOINT:
278 case MIME_TYPE_PDF: {
283 InputStream in = null;
284 BufferedInputStream bin = null;
287 in =
new ReadContentInputStream(file);
288 bin =
new BufferedInputStream(in);
289 ContentHandler handler =
new BodyContentHandler(-1);
290 Metadata metadata =
new Metadata();
291 metadata.add(Metadata.RESOURCE_NAME_KEY, file.getName());
292 AutoDetectParser parser =
new AutoDetectParser();
293 parser.parse(bin, handler, metadata,
new ParseContext());
294 }
catch (EncryptedDocumentException ex) {
298 passwordProtected =
true;
310 case MIME_TYPE_MSACCESS: {
318 InputStream in = null;
319 BufferedInputStream bin = null;
322 in =
new ReadContentInputStream(file);
323 bin =
new BufferedInputStream(in);
324 MemFileChannel memFileChannel = MemFileChannel.newChannel(bin);
325 CodecProvider codecProvider =
new CryptCodecProvider();
326 DatabaseBuilder databaseBuilder =
new DatabaseBuilder();
327 databaseBuilder.setChannel(memFileChannel);
328 databaseBuilder.setCodecProvider(codecProvider);
329 Database accessDatabase;
331 accessDatabase = databaseBuilder.open();
332 }
catch (IOException | BufferUnderflowException | IndexOutOfBoundsException ignored) {
333 return passwordProtected;
340 if (accessDatabase.getDatabasePassword() != null) {
341 passwordProtected =
true;
343 }
catch (InvalidCredentialsException ex) {
347 passwordProtected =
true;
359 return passwordProtected;
377 private boolean isFileEncryptionSuspected(AbstractFile file)
throws ReadContentInputStreamException, IOException {
383 boolean possiblyEncrypted =
false;
388 boolean fileSizeQualified =
false;
389 String fileExtension = file.getNameExtension();
390 long contentSize = file.getSize();
392 if (fileExtension.equalsIgnoreCase(DATABASE_FILE_EXTENSION)) {
393 if (contentSize >= MINIMUM_DATABASE_FILE_SIZE) {
394 fileSizeQualified =
true;
396 }
else if (contentSize >= minimumFileSize) {
397 if (!fileSizeMultipleEnforced || (contentSize % FILE_SIZE_MODULUS) == 0) {
398 fileSizeQualified =
true;
402 if (fileSizeQualified) {
406 calculatedEntropy = EncryptionDetectionTools.calculateEntropy(file, context);
407 if (calculatedEntropy >= minimumEntropy) {
408 possiblyEncrypted =
true;
412 return possiblyEncrypted;