forked from fkie-cad/yapscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yara.go
480 lines (422 loc) · 13.9 KB
/
yara.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package yapscan
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/fkie-cad/yapscan/report"
"github.com/fkie-cad/yapscan/system"
"github.com/yeka/zip"
"github.com/hillu/go-yara/v4"
"github.com/sirupsen/logrus"
)
// RulesZIPPassword is the password yapscan uses to de-/encrypt the rules zip file.
const RulesZIPPassword = "infected"
// DefaultYaraRulesNamespace is the default namespace when compiling rules.
var DefaultYaraRulesNamespace = ""
// YaraRulesFileExtensions are the file extensions yapscan expects rules files to have.
// This is used when loading files from a directory.
var YaraRulesFileExtensions = []string{
".yar",
".yara",
}
type ProfilingInformation struct {
Time report.Time `json:"time"`
FreeRAM uintptr `json:"freeRAM"`
FreeSwap uintptr `json:"freeSwap"`
LoadAvgOneMinute float64 `json:"loadAvgOneMinute"`
LoadAvgFiveMinutes float64 `json:"loadAvgFiveMinutes"`
LoadAvgFifteenMinutes float64 `json:"loadAvgFifteenMinutes"`
}
// ScanningStatistics holds statistic information about a scan.
type ScanningStatistics struct {
Start report.Time `json:"start"`
End report.Time `json:"end"`
NumberOfProcessesScanned uint64 `json:"numberOfProcessesScanned"`
NumberOfSegmentsScanned uint64 `json:"numberOfSegmentsScanned"`
NumberOfMemoryBytesScanned uint64 `json:"numberOfMemoryBytesScanned"`
NumberOfFileBytesScanned uint64 `json:"numberOfFileBytesScanned"`
NumberOfFilesScanned uint64 `json:"numberOfFilesScanned"`
ProfilingInformation []*ProfilingInformation `json:"profilingInformation"`
mux *sync.Mutex
ctx context.Context
ctxCancel context.CancelFunc
profilerDone chan interface{}
}
func NewScanningStatistics() *ScanningStatistics {
return &ScanningStatistics{
Start: report.Now(),
mux: &sync.Mutex{},
}
}
// StartProfiler starts a goroutine, regularly saving information about free memory, free swap and
// CPU load.
func (s *ScanningStatistics) StartProfiler(ctx context.Context, scanInterval time.Duration) {
s.ProfilingInformation = make([]*ProfilingInformation, 0, 16)
s.ctx, s.ctxCancel = context.WithCancel(ctx)
s.profilerDone = make(chan interface{})
go func() {
defer func() {
s.profilerDone <- nil
close(s.profilerDone)
}()
run := true
for run {
select {
case <-s.ctx.Done():
run = false
case <-time.After(scanInterval):
freeRAM, err := system.FreeRAM()
if err != nil {
logrus.WithError(err).Error("Could not retrieve free RAM.")
continue
}
freeSwap, err := system.FreeSwap()
if err != nil {
logrus.WithError(err).Error("Could not retrieve free RAM.")
continue
}
loadAvg1, loadAvg5, loadAvg15 := system.CPULoad()
logrus.WithFields(logrus.Fields{
"freeRAM": freeRAM,
"freeSwap": freeSwap,
}).Trace("Memory profile.")
s.ProfilingInformation = append(s.ProfilingInformation, &ProfilingInformation{
Time: report.Now(),
FreeRAM: freeRAM,
FreeSwap: freeSwap,
LoadAvgOneMinute: loadAvg1,
LoadAvgFiveMinutes: loadAvg5,
LoadAvgFifteenMinutes: loadAvg15,
})
}
}
}()
}
// IncrementFilesScanned increments the number of files scanned as
// well as the number of bytes scanned.
// This function is thread safe.
func (s *ScanningStatistics) IncrementFilesScanned(numOfBytes uint64) {
s.mux.Lock()
defer s.mux.Unlock()
s.NumberOfFilesScanned++
s.NumberOfFileBytesScanned += numOfBytes
}
// IncrementMemorySegmentsScanned increments the number of segments scanned as
// well as the number of bytes scanned.
// This function is thread safe.
func (s *ScanningStatistics) IncrementMemorySegmentsScanned(numOfBytes uint64) {
s.mux.Lock()
defer s.mux.Unlock()
s.NumberOfSegmentsScanned++
s.NumberOfMemoryBytesScanned += numOfBytes
}
// IncrementNumberOfProcessesScanned increments the number of scanned processes.
// This function is thread safe.
func (s *ScanningStatistics) IncrementNumberOfProcessesScanned() {
s.mux.Lock()
defer s.mux.Unlock()
s.NumberOfProcessesScanned++
}
// Finalize finalizes the statistics, stopping the memory profile routine if its running.
// Use this function before processing the statistics further.
// This function is thread safe.
func (s *ScanningStatistics) Finalize() {
s.mux.Lock()
defer s.mux.Unlock()
if s.ctxCancel != nil {
s.ctxCancel()
<-s.profilerDone
}
s.End = report.Now()
}
// YaraScanner is a wrapper for yara.Rules, with a more go-like interface.
type YaraScanner struct {
rules Rules
stats *ScanningStatistics
}
// Rules are a yara.Rules compatible interface, defining the functions required by yapscan.
// The choice of an interface over the concrete struct yara.Rules is mostly to make testing
// easier.
type Rules interface {
ScanFile(filename string, flags yara.ScanFlags, timeout time.Duration, cb yara.ScanCallback) (err error)
ScanMem(buf []byte, flags yara.ScanFlags, timeout time.Duration, cb yara.ScanCallback) (err error)
}
// NewYaraScanner creates a new YaraScanner from the given yara.Rules.
func NewYaraScanner(rules Rules) (*YaraScanner, error) {
if rules == nil {
return nil, fmt.Errorf("cannot create a yara scanner with nil rules")
}
return &YaraScanner{
rules: rules,
stats: NewScanningStatistics(),
}, nil
}
// ScanFile scans the file with the given filename.
// This function simply calls ScanFile on the underlying yara.Rules object.
func (s *YaraScanner) ScanFile(filename string) ([]yara.MatchRule, error) {
info, err := os.Stat(filename)
if err != nil {
logrus.WithError(err).Error("Could not stat file for filesize computation.")
} else {
s.stats.IncrementFilesScanned(uint64(info.Size()))
}
var matches yara.MatchRules
err = s.rules.ScanFile(filename, 0, 0, &matches)
return matches, err
}
// ScanMem scans the given buffer.
// This function simply calls ScanMem on the underlying yara.Rules object.
func (s *YaraScanner) ScanMem(buf []byte) ([]yara.MatchRule, error) {
s.stats.IncrementMemorySegmentsScanned(uint64(len(buf)))
var matches yara.MatchRules
// TODO: See if we need yara.ScanFlagsProcessMemory here.
err := s.rules.ScanMem(buf, 0, 0, &matches)
return matches, err
}
// Statistics returns the mutable statistics of the scanner.
func (s *YaraScanner) Statistics() *ScanningStatistics {
return s.stats
}
// LoadYaraRules loads yara.Rules from a file (or files) and compiles if necessary.
// The given path can be a path to a directory, a compiled rules-file, a plain text
// file containing rules, or an encrypted zip file containing rules.
//
// If the path is a directory, all files with one of the file extensions in YaraRulesFileExtensions
// are loaded (recursively if recurseIfDir is true). All files are assumed to be
// uncompiled and will be compiled. Loading multiple already compiled files into one
// yara.Rules object is not supported.
// Each file will be compiled with the namespace equal to its filename, relative to
// the given path.
//
// If the path is a single file, it may be compiled, uncompiled or a zip file.
// An uncompiled file will be compiled with the namespace
// `DefaultYaraRulesNamespace+"/"+filename`. A zip file will be opened and decrypted
// with the RulesZIPPassword. The contents of the zip file will be treated similar to
// the way a directory is treated (see above), however *all* files are assumed to be
// rules-files, recursion is always enabled and there may be either a single compiled
// file or arbitrarily many uncompiled files in the zip.
func LoadYaraRules(path string, recurseIfDir bool) (*yara.Rules, error) {
stat, err := os.Stat(path)
if err != nil {
return nil, fmt.Errorf("could not stat file \"%s\", reason: %w", path, err)
}
if stat.IsDir() {
return loadYaraRulesDirectory(path, recurseIfDir)
}
return loadYaraRulesSingleFile(path)
}
// IsYaraRulesFile returns true, if the given filename has one of the extensions
// in YaraRulesFileExtensions.
func IsYaraRulesFile(name string) bool {
for _, ext := range YaraRulesFileExtensions {
nLen := len(name)
eLen := len(ext)
if nLen < eLen {
continue
}
if strings.ToLower(name[nLen-eLen:]) == ext {
return true
}
}
return false
}
func loadYaraRulesDirectory(rulesPath string, recurse bool) (*yara.Rules, error) {
compiler, err := yara.NewCompiler()
if err != nil {
return nil, fmt.Errorf("could not create yara compiler, reason: %w", err)
}
compileFn := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if !IsYaraRulesFile(info.Name()) {
return nil
}
namespace, err := filepath.Rel(rulesPath, path)
if err != nil {
namespace = path
}
namespace = filepath.ToSlash(namespace)
file, err := os.OpenFile(path, os.O_RDONLY, 0666)
if err != nil {
return fmt.Errorf("could not open rules file \"%s\", reason: %w", path, err)
}
defer file.Close()
err = compiler.AddFile(file, namespace)
if err != nil {
return fmt.Errorf("could not compile rules file \"%s\", reason: %w", path, err)
}
return nil
}
if recurse {
err = filepath.Walk(rulesPath, compileFn)
if err != nil {
return nil, err
}
} else {
f, err := os.Open(rulesPath)
if err != nil {
return nil, fmt.Errorf("could not read directory \"%s\", reason: %w", rulesPath, err)
}
names, err := f.Readdirnames(-1)
if err != nil {
return nil, fmt.Errorf("could not read directory \"%s\", reason: %w", rulesPath, err)
}
for _, name := range names {
filename := filepath.Join(rulesPath, name)
stat, err := os.Stat(filename)
err = compileFn(filename, stat, err)
if err != nil {
return nil, err
}
}
}
return compiler.GetRules()
}
func loadCompiledRules(in io.Reader) (*yara.Rules, error) {
logrus.Debug("Yara rules file contains compiled rules.")
rules, err := yara.ReadRules(in)
if err != nil {
err = fmt.Errorf("could not read rules file, reason: %w", err)
}
return rules, err
}
func loadUncompiledRules(compiler *yara.Compiler, in io.Reader, name string) error {
logrus.Debug("Yara rules file needs to be compiled.")
data, err := ioutil.ReadAll(in)
if err != nil {
return fmt.Errorf("could not read yara rules, reason: %w", err)
}
err = compiler.AddString(string(data), DefaultYaraRulesNamespace+"/"+name)
if err != nil {
return fmt.Errorf("could not compile yara rules, reason: %w", err)
}
return nil
}
func loadZippedRules(in io.ReaderAt, size int64) (*yara.Rules, error) {
zipRdr, err := zip.NewReader(in, size)
if err != nil {
return nil, fmt.Errorf("could not open zipped rules file, reason: %w", err)
}
compiler, err := yara.NewCompiler()
if err != nil {
return nil, fmt.Errorf("could not intialize compiler")
}
// includes will not work in zips
compiler.DisableIncludes()
for _, file := range zipRdr.File {
if file.IsEncrypted() {
file.SetPassword(RulesZIPPassword)
}
if file.FileInfo().IsDir() {
continue
}
f, err := file.Open()
if err != nil {
return nil, fmt.Errorf("could not read rules file in zip, reason: %w", err)
}
t, rdr, err := detectRuleType(f)
if err != nil {
return nil, fmt.Errorf("invalid rules zip, reason: %v", err)
}
switch t {
case ruleTypeCompiled:
if len(zipRdr.File) != 1 {
return nil, fmt.Errorf("invalid rules zip, it must either contain a single compiled rules file or multiple *un*compiled rules files")
}
rules, err := loadCompiledRules(rdr)
f.Close()
return rules, err
case ruleTypePlain:
if !IsYaraRulesFile(file.Name) {
continue
}
err = loadUncompiledRules(compiler, rdr, file.FileInfo().Name())
f.Close()
if err != nil {
return nil, err
}
default:
f.Close()
return nil, fmt.Errorf("invalid rules zip, it cannot contain other zip files")
}
}
rules, err := compiler.GetRules()
if err != nil {
return nil, fmt.Errorf("could not compile rules in zip, reason: %w", err)
}
return rules, nil
}
type ruleType int
const (
ruleTypeCompiled ruleType = iota
ruleTypeZipped
ruleTypePlain
)
func detectRuleType(in io.Reader) (ruleType, io.Reader, error) {
buff := make([]byte, 4)
_, err := io.ReadFull(in, buff)
if err != nil {
return 0, in, fmt.Errorf("could not read rules file, reason: %w", err)
}
inWithMagic := io.MultiReader(bytes.NewReader(buff), in)
if bytes.Equal(buff, []byte("YARA")) {
return ruleTypeCompiled, inWithMagic, nil
} else if bytes.Equal(buff, []byte("PK\x03\x04")) {
return ruleTypeZipped, inWithMagic, nil
} else {
// Uncompiled rules are just plain text without magic number
return ruleTypePlain, inWithMagic, nil
}
}
func loadYaraRulesSingleFile(path string) (*yara.Rules, error) {
rulesFile, err := os.OpenFile(path, os.O_RDONLY, 0644)
if err != nil {
return nil, fmt.Errorf("could not open rules file, reason: %w", err)
}
defer rulesFile.Close()
var t ruleType
t, _, err = detectRuleType(rulesFile)
if err != nil {
return nil, fmt.Errorf("could not determine rules type, reason: %w", err)
}
_, err = rulesFile.Seek(0, io.SeekStart)
if err != nil {
return nil, fmt.Errorf("could not determine rules type, reason: %w", err)
}
switch t {
case ruleTypePlain:
compiler, err := yara.NewCompiler()
if err != nil {
return nil, fmt.Errorf("could not create yara compiler, reason: %w", err)
}
err = loadUncompiledRules(compiler, rulesFile, rulesFile.Name())
if err != nil {
return nil, err
}
rules, err := compiler.GetRules()
if err != nil {
err = fmt.Errorf("could not compile yara rules, reason: %w", err)
}
return rules, err
case ruleTypeZipped:
s, err := rulesFile.Stat()
if err != nil {
return nil, fmt.Errorf("could not stat file \"%s\", reason: %w", rulesFile.Name(), err)
}
return loadZippedRules(rulesFile, s.Size())
case ruleTypeCompiled:
return loadCompiledRules(rulesFile)
}
panic("invalid rules type, this should never happen")
}