-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.go
More file actions
109 lines (102 loc) · 2.14 KB
/
scanner.go
File metadata and controls
109 lines (102 loc) · 2.14 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
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
package injector
import (
"io"
"math/rand"
"os"
"path/filepath"
"time"
)
const defaultMinNumCaves = 350
// ScanOptions contains the scan options.
type ScanOptions struct {
MinNumCaves int `json:"min_num_caves"`
NoSignature bool `json:"no_signature"`
NoLoadConfig bool `json:"no_load_config"`
IgnoreRank bool `json:"ignore_rank"`
}
// ScanResult contain the scan image result.
type ScanResult struct {
Path string `json:"path"`
Info *AnalyzeInfo `json:"info"`
}
// Scan is used to scan the target image in directory.
func Scan(path string, opts *ScanOptions) ([]*ScanResult, error) {
if opts == nil {
opts = new(ScanOptions)
}
rd := rand.New(rand.NewSource(time.Now().UnixNano())) // #nosec
var results []*ScanResult
err := filepath.Walk(path, func(path string, file os.FileInfo, _ error) error {
if file.IsDir() {
return nil
}
ext := filepath.Ext(file.Name())
if ext == ".sys" {
return nil
}
path, _ = filepath.Abs(path)
if path == "" {
return nil
}
if !maybePEImage(rd, path) {
return nil
}
image, err := os.ReadFile(path) // #nosec
if err != nil {
return nil
}
info, err := Analyze(image)
if err != nil {
return nil
}
if !matchPEImage(info, opts) {
return nil
}
results = append(results, &ScanResult{
Path: path,
Info: info,
})
return nil
})
if err != nil {
return nil, err
}
return results, nil
}
func matchPEImage(info *AnalyzeInfo, opts *ScanOptions) bool {
minNumCaves := opts.MinNumCaves
if minNumCaves < 1 {
minNumCaves = defaultMinNumCaves
}
if info.NumCodeCaves < minNumCaves {
return false
}
if opts.NoSignature && info.HasSignature {
return false
}
if opts.NoLoadConfig && info.HasLoadConfig {
return false
}
if !opts.IgnoreRank {
if !info.CanCreateSection && !info.CanInjectLoader {
return false
}
}
return true
}
func maybePEImage(rd *rand.Rand, path string) bool {
f, err := os.Open(path) // #nosec
if err != nil {
return false
}
defer func() { _ = f.Close() }()
buf := make([]byte, 4+rd.Intn(64))
_, err = io.ReadFull(f, buf)
if err != nil {
return false
}
if buf[0] == 'M' && buf[1] == 'Z' {
return true
}
return false
}