19package org.sleuthkit.autopsy.modules.yara;
22import java.io.IOException;
23import java.nio.file.Path;
24import java.nio.file.Paths;
25import java.util.ArrayList;
28import java.util.concurrent.ConcurrentHashMap;
29import java.util.logging.Level;
30import org.apache.commons.lang3.RandomStringUtils;
31import org.openide.util.NbBundle.Messages;
32import org.sleuthkit.autopsy.casemodule.Case;
33import org.sleuthkit.autopsy.casemodule.NoCurrentCaseException;
34import org.sleuthkit.autopsy.coreutils.Logger;
35import org.sleuthkit.autopsy.coreutils.PlatformUtil;
36import org.sleuthkit.autopsy.datamodel.ContentUtils;
37import org.sleuthkit.autopsy.ingest.FileIngestModuleAdapter;
38import org.sleuthkit.autopsy.ingest.IngestJobContext;
39import org.sleuthkit.autopsy.ingest.IngestModule;
40import org.sleuthkit.autopsy.ingest.IngestModuleReferenceCounter;
41import org.sleuthkit.autopsy.yara.YaraWrapperException;
42import org.sleuthkit.datamodel.AbstractFile;
43import org.sleuthkit.datamodel.Blackboard;
44import org.sleuthkit.datamodel.Blackboard.BlackboardException;
45import org.sleuthkit.datamodel.BlackboardArtifact;
46import org.sleuthkit.datamodel.TskCoreException;
47import org.sleuthkit.datamodel.TskData;
63 private static final Map<Long, Path>
pathsByJobId =
new ConcurrentHashMap<>();
81 "YaraIngestModule_windows_error_msg=The YARA ingest module is only available on 64bit Windows.",})
86 this.jobId =
context.getJobId();
95 Path tempRuleSetDir = Paths.get(tempDir.toString(),
RULESET_DIR);
96 if(!tempRuleSetDir.toFile().exists()) {
97 tempRuleSetDir.toFile().mkdir();
100 if(
settings.hasSelectedRuleSets()) {
101 YaraIngestHelper.compileRules(
settings.getSelectedRuleSetNames(), tempRuleSetDir);
103 logger.log(Level.INFO,
"YARA ingest module: No rule set was selected for this ingest job.");
113 if (jobPath !=
null) {
114 jobPath.toFile().delete();
123 if(!
settings.hasSelectedRuleSets()) {
127 if (
settings.onlyExecutableFiles()) {
128 String extension = file.getNameExtension();
129 if (!extension.equals(
"exe")) {
135 if (file.getSize() == 0 ||
137 file.getType() == TskData.TSK_DB_FILES_TYPE_ENUM.UNALLOC_BLOCKS) {
142 List<BlackboardArtifact> artifacts =
new ArrayList<>();
148 byte[] fileBuffer =
new byte[(int)file.getSize()];
150 int dataRead = file.read(fileBuffer, 0, file.getSize());
152 artifacts.addAll( YaraIngestHelper.scanFileForMatches(file, ruleSetsDir, fileBuffer, dataRead,
YARA_SCAN_TIMEOUT_SEC));
156 artifacts.addAll( YaraIngestHelper.scanFileForMatches(file, ruleSetsDir, tempCopy,
YARA_SCAN_TIMEOUT_SEC));
160 if(!artifacts.isEmpty()) {
166 logger.log(Level.SEVERE, String.format(
"YARA ingest module failed to process file id %d", file.getId()), ex);
168 }
catch(IOException ex) {
169 logger.log(Level.SEVERE, String.format(
"YARA ingest module failed to make a local copy of given file id %d", file.getId()), ex);
188 if (jobPath !=
null) {
196 throw new IngestModuleException(
"Failed to create YARA ingest model temp directory, no open case.", ex);
200 if (!baseDir.toFile().exists()) {
201 baseDir.toFile().mkdirs();
204 String randomDirName = String.format(
"%s_%d", RandomStringUtils.randomAlphabetic(8),
jobId);
205 jobPath = Paths.get(baseDir.toString(), randomDirName);
206 jobPath.toFile().mkdir();
224 String tempFileName = RandomStringUtils.randomAlphabetic(15) + file.getId() +
".temp";
SleuthkitCase getSleuthkitCase()
static Case getCurrentCaseThrows()
String getTempDirectory()
synchronized static Logger getLogger(String name)
static< T > long writeToFile(Content content, java.io.File outputFile, ProgressHandle progress, Future< T > worker, boolean source)
static final Map< Long, Path > pathsByJobId
static final int YARA_SCAN_TIMEOUT_SEC
static final IngestModuleReferenceCounter refCounter
void startUp(IngestJobContext context)
static final Logger logger
final YaraIngestJobSettings settings
File createLocalCopy(AbstractFile file)
synchronized Path getTempDirectory(long jobId)
static final int FILE_SIZE_THRESHOLD_MB
static final String YARA_DIR
static final String RULESET_DIR
static final int FILE_SIZE_THRESHOLD_BYTE
ProcessResult process(AbstractFile file)