MCPcopy Index your code
hub / github.com/codeyourweb/irma / FileAnalysis

Function FileAnalysis

analysis.go:25–91  ·  view source on GitHub ↗

FileAnalysis sub-routine for file analysis (used in registry / task scheduler / startmenu scan)

(path string, pQuarantine string, pKill bool, pNotifications bool, pVerbose bool, rules *yara.Rules, sourceIndex string)

Source from the content-addressed store, hash-verified

23
24// FileAnalysis sub-routine for file analysis (used in registry / task scheduler / startmenu scan)
25func FileAnalysis(path string, pQuarantine string, pKill bool, pNotifications bool, pVerbose bool, rules *yara.Rules, sourceIndex string) {
26 var f os.FileInfo
27 var err error
28 var content []byte
29 var result yara.MatchRules
30
31 if f, err = os.Stat(path); err != nil {
32 if pVerbose {
33 logMessage(LOG_ERROR, "[ERROR]", path, err)
34 }
35 } else {
36 if RegisterFileInHistory(f, path, &filescanHistory, pVerbose) {
37
38 content, err = os.ReadFile(path)
39 if err != nil && pVerbose {
40 logMessage(LOG_ERROR, "[ERROR]", path, err)
41 }
42
43 filetype, err := filetype.Match(content)
44 if err != nil && pVerbose {
45 logMessage(LOG_ERROR, "[ERROR]", path, err)
46 }
47
48 if pVerbose {
49 logMessage(LOG_INFO, "[INFO] ["+sourceIndex+"] Analyzing", path, fmt.Sprintf("%x", md5.Sum(content)))
50 }
51
52 // cleaning memory if file size is greater than 512Mb
53 if len(content) > 1024*1024*cleanIfFileSizeGreaterThan {
54 defer debug.FreeOSMemory()
55 }
56
57 // archive or other file format scan
58 if StringInSlice(filetype.MIME.Value, archivesFormats) {
59 result = PerformArchiveYaraScan(path, rules, pVerbose)
60 } else {
61 result = PerformYaraScan(&content, rules, pVerbose)
62 }
63
64 if len(result) > 0 {
65 // windows notifications
66 if pNotifications {
67 NotifyUser("YARA match", path+" match "+fmt.Sprint(len(result))+" rules")
68 }
69
70 // logging
71 for _, match := range result {
72 logMessage(LOG_INFO, "[ALERT]", "["+sourceIndex+"] YARA match", path, match.Namespace, match.Rule)
73 }
74
75 // kill
76 if pKill {
77 killQueue = append(killQueue, path)
78 }
79
80 // dump matching file to quarantine
81 if len(pQuarantine) > 0 {
82 logMessage(LOG_INFO, "[INFO]", "Dumping file", path)

Callers 7

MemoryAnalysisRoutineFunction · 0.85
StartMenuAnalysisRoutineFunction · 0.85
RegistryAnalysisRoutineFunction · 0.85
mainFunction · 0.85

Calls 7

logMessageFunction · 0.85
RegisterFileInHistoryFunction · 0.85
StringInSliceFunction · 0.85
PerformArchiveYaraScanFunction · 0.85
PerformYaraScanFunction · 0.85
NotifyUserFunction · 0.85
QuarantineFileFunction · 0.85

Tested by

no test coverage detected