-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathprocess_scanner.go
More file actions
74 lines (61 loc) · 2.31 KB
/
process_scanner.go
File metadata and controls
74 lines (61 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"fmt"
"runtime/debug"
"github.com/hillu/go-yara/v4"
)
// ProcessInformation wrap basic process information and memory dump in a structure
type ProcessInformation struct {
PID uint32
ProcessName string
ProcessPath string
MemoryDump []byte
}
// ScanMemory enumerates processes and scans their memory
func ScanMemory(config Configuration, rules *yara.Rules) {
LogMessage(LOG_INFO, "(INIT)", "Starting memory scan...")
procs := ListProcesses()
LogMessage(LOG_INFO, "(INFO)", fmt.Sprintf("Found %d running processes", len(procs)))
ScanProcesses(procs, config, rules)
LogMessage(LOG_INFO, "(INFO)", "Memory scan finished")
}
// ScanProcesses scans a list of processes for patterns and YARA rules
func ScanProcesses(procs []ProcessInformation, config Configuration, rules *yara.Rules) int {
totalMatches := 0
for _, proc := range procs {
LogMessage(LOG_VERBOSE, "(MEMORY)", "Scanning process:", proc.ProcessName, fmt.Sprintf("(PID: %d)", proc.PID))
// Check memory content with Grep
if len(config.Input.Content.Grep) > 0 {
// checkForStringPattern already handles logging and alerting
matches := checkForStringPattern(fmt.Sprintf("MEMORY:%s:%d", proc.ProcessName, proc.PID), proc.MemoryDump, config.Input.Content.Grep)
totalMatches += len(matches)
}
// Check memory content with YARA
if rules != nil && len(rules.GetRules()) > 0 {
matchs, err := PerformYaraScan(&proc.MemoryDump, rules)
if err != nil {
LogMessage(LOG_ERROR, "(ERROR)", "Memory YARA scan failed for", proc.ProcessName, err)
} else {
totalMatches += len(matchs)
}
for i := 0; i < len(matchs); i++ {
LogMessage(LOG_ALERT, "(ALERT)", "Memory YARA match:")
LogMessage(LOG_ALERT, " | process:", proc.ProcessName)
LogMessage(LOG_ALERT, " | PID:", fmt.Sprintf("%d", proc.PID))
LogMessage(LOG_ALERT, " | rule:", matchs[i].Rule)
LogMessage(LOG_ALERT, " | namespace:", matchs[i].Namespace)
// Forward alert
metadata := map[string]string{
"pid": fmt.Sprintf("%d", proc.PID),
"process_name": proc.ProcessName,
"rule_namespace": matchs[i].Namespace,
}
ForwardAlertEvent(matchs[i].Rule, "memory:"+proc.ProcessName, int64(len(proc.MemoryDump)), "", metadata)
}
}
// Clean memory
proc.MemoryDump = nil
debug.FreeOSMemory()
}
return totalMatches
}