forked from eth0izzle/shhgit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
match.go
77 lines (63 loc) · 1.58 KB
/
match.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
package core
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type MatchFile struct {
Path string
Filename string
Extension string
Contents []byte
}
func NewMatchFile(path string) MatchFile {
path = filepath.ToSlash(path)
_, filename := filepath.Split(path)
extension := filepath.Ext(path)
contents, _ := ioutil.ReadFile(path)
return MatchFile{
Path: path,
Filename: filename,
Extension: extension,
Contents: contents,
}
}
func IsSkippableFile(path string) bool {
extension := strings.ToLower(filepath.Ext(path))
for _, skippableExt := range session.Config.BlacklistedExtensions {
if extension == skippableExt {
return true
}
}
for _, skippablePathIndicator := range session.Config.BlacklistedPaths {
skippablePathIndicator = strings.Replace(skippablePathIndicator, "{sep}", string(os.PathSeparator), -1)
if strings.Contains(path, skippablePathIndicator) {
return true
}
}
return false
}
func (match MatchFile) CanCheckEntropy() bool {
if match.Filename == "id_rsa" {
return false
}
for _, skippableExt := range session.Config.BlacklistedEntropyExtensions {
if match.Extension == skippableExt {
return false
}
}
return true
}
func GetMatchingFiles(dir string) []MatchFile {
fileList := make([]MatchFile, 0)
maxFileSize := *session.Options.MaximumFileSize * 1024
filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
if err != nil || f.IsDir() || uint(f.Size()) > maxFileSize || IsSkippableFile(path) {
return nil
}
fileList = append(fileList, NewMatchFile(path))
return nil
})
return fileList
}