-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathanalysis.go
182 lines (151 loc) · 4.81 KB
/
analysis.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"crypto/md5"
"crypto/rc4"
b64 "encoding/base64"
"fmt"
"os"
"path/filepath"
"runtime/debug"
"time"
"github.com/h2non/filetype"
"github.com/hillu/go-yara/v4"
)
// FileDescriptor wrap path, filehash and last update into a structure. It is used for performance improvements and avoid reading file if it has not changed
type FileDescriptor struct {
FilePath string
FileSize int64
LastModified time.Time
}
// FileAnalysis sub-routine for file analysis (used in registry / task scheduler / startmenu scan)
func FileAnalysis(path string, pQuarantine string, pKill bool, pNotifications bool, pVerbose bool, rules *yara.Rules, sourceIndex string) {
var f os.FileInfo
var err error
var content []byte
var result yara.MatchRules
if f, err = os.Stat(path); err != nil {
if pVerbose {
logMessage(LOG_ERROR, "[ERROR]", path, err)
}
} else {
if RegisterFileInHistory(f, path, &filescanHistory, pVerbose) {
content, err = os.ReadFile(path)
if err != nil && pVerbose {
logMessage(LOG_ERROR, "[ERROR]", path, err)
}
filetype, err := filetype.Match(content)
if err != nil && pVerbose {
logMessage(LOG_ERROR, "[ERROR]", path, err)
}
if pVerbose {
logMessage(LOG_INFO, "[INFO] ["+sourceIndex+"] Analyzing", path, fmt.Sprintf("%x", md5.Sum(content)))
}
// cleaning memory if file size is greater than 512Mb
if len(content) > 1024*1024*cleanIfFileSizeGreaterThan {
defer debug.FreeOSMemory()
}
// archive or other file format scan
if StringInSlice(filetype.MIME.Value, archivesFormats) {
result = PerformArchiveYaraScan(path, rules, pVerbose)
} else {
result = PerformYaraScan(&content, rules, pVerbose)
}
if len(result) > 0 {
// windows notifications
if pNotifications {
NotifyUser("YARA match", path+" match "+fmt.Sprint(len(result))+" rules")
}
// logging
for _, match := range result {
logMessage(LOG_INFO, "[ALERT]", "["+sourceIndex+"] YARA match", path, match.Namespace, match.Rule)
}
// kill
if pKill {
killQueue = append(killQueue, path)
}
// dump matching file to quarantine
if len(pQuarantine) > 0 {
logMessage(LOG_INFO, "[INFO]", "Dumping file", path)
err := QuarantineFile(path, pQuarantine)
if err != nil {
logMessage(LOG_ERROR, "[ERROR]", "Cannot quarantine file", path, err)
}
}
}
}
}
}
// MemoryAnalysis sub-routine for running processes analysis
func MemoryAnalysis(proc *ProcessInformation, pQuarantine string, pKill bool, pNotifications bool, pVerbose bool, rules *yara.Rules) {
if pVerbose {
logMessage(LOG_INFO, "[INFO] [MEMORY] Analyzing", proc.ProcessName, "PID:", proc.PID)
}
result := PerformYaraScan(&proc.MemoryDump, rules, pVerbose)
if len(result) > 0 {
// windows notifications
if pNotifications {
NotifyUser("YARA match", proc.ProcessName+" - PID:"+fmt.Sprint(proc.PID)+" match "+fmt.Sprint(len(result))+" rules")
}
// logging
for _, match := range result {
logMessage(LOG_INFO, "[ALERT]", "[MEMORY] YARA match", proc.ProcessName, "PID:", fmt.Sprint(proc.PID), match.Namespace, match.Rule)
}
// dump matching process to quarantine
if len(pQuarantine) > 0 {
logMessage(LOG_INFO, "[INFO]", "DUMPING PID", proc.PID)
err := QuarantineProcess(proc, pQuarantine)
if err != nil {
logMessage(LOG_ERROR, "[ERROR]", "Cannot quarantine PID", proc.PID, err)
}
}
// killing process
if pKill {
logMessage(LOG_INFO, "[INFO]", "KILLING PID", proc.PID)
KillProcessByID(proc.PID, pVerbose)
}
}
}
// QuarantineProcess dump process memory and cipher them in quarantine folder
func QuarantineProcess(proc *ProcessInformation, quarantinePath string) (err error) {
err = quarantineContent(proc.MemoryDump, proc.ProcessName+fmt.Sprint(proc.PID)+".mem", quarantinePath)
if err != nil {
return err
}
err = QuarantineFile(proc.ProcessPath, quarantinePath)
if err != nil {
return err
}
return nil
}
// QuarantineFile dump specified file and cipher them in quarantine folder
func QuarantineFile(path, quarantinePath string) (err error) {
fileContent, err := os.ReadFile(path)
if err != nil {
return err
}
err = quarantineContent(fileContent, filepath.Base(path), quarantinePath)
if err != nil {
return err
}
return nil
}
// quarantineContent copy and encrypt suspicious content
func quarantineContent(content []byte, filename string, quarantinePath string) (err error) {
_, err = os.Stat(quarantinePath)
if os.IsNotExist(err) {
if err := os.MkdirAll(quarantinePath, 0600); err != nil {
return err
}
}
c, err := rc4.NewCipher([]byte(quarantineKey))
if err != nil {
return err
}
xPE := make([]byte, len(content))
c.XORKeyStream(xPE, content)
err = os.WriteFile(quarantinePath+"/"+filename+".irma", []byte(b64.StdEncoding.EncodeToString(xPE)), 0644)
if err != nil {
return err
}
return nil
}