-
Notifications
You must be signed in to change notification settings - Fork 347
Expand file tree
/
Copy pathmatch.go
More file actions
125 lines (107 loc) · 3.42 KB
/
Copy pathmatch.go
File metadata and controls
125 lines (107 loc) · 3.42 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package core
import (
"bytes"
"os"
"path/filepath"
"strings"
)
type MatchFile struct {
Path string
Filename string
Extension string
Contents []byte
}
// NewMatchFile Creates a new Matchfile data structure
func NewMatchFile(path string) MatchFile {
path = filepath.ToSlash(path)
_, filename := filepath.Split(path)
extension := filepath.Ext(path)
return MatchFile{
Path: path,
Filename: filename,
Extension: extension,
Contents: []byte(""), // contents,
}
}
// IsSkippableDir Checks if the path is blacklisted
func IsSkippableDir(path string, baseDir string) bool {
hostMountPath := *session.Options.HostMountPath
if hostMountPath != "" {
baseDir = hostMountPath
}
for _, skippablePathIndicator := range session.Config.BlacklistedPaths {
if strings.HasPrefix(path, skippablePathIndicator) || strings.HasPrefix(path, filepath.Join(baseDir, skippablePathIndicator)) {
return true
}
}
for _, excludePathIndicator := range session.Config.ExcludePaths {
if strings.Contains(path, excludePathIndicator) || strings.Contains(path, filepath.Join(baseDir, excludePathIndicator)) {
return true
}
}
return false
}
// IsSkippableFileExtension Checks if the file extension is blacklisted
func IsSkippableFileExtension(path string) bool {
extension := strings.ToLower(filepath.Ext(path))
for _, skippableExt := range session.Config.BlacklistedExtensions {
if extension == skippableExt {
return true
}
}
return false
}
// CanCheckEntropy Checks if entropy based scanning is appropriate for this file
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
}
// ContainsBlacklistedString Checks if the input contains a blacklisted string
func ContainsBlacklistedString(input []byte) bool {
for _, blacklistedString := range session.Config.BlacklistedStrings {
blacklistedByteStr := []byte(blacklistedString)
if bytes.Contains(input, blacklistedByteStr) {
GetSession().Log.Debug("Blacklisted string %s matched", blacklistedString)
return true
}
}
return false
}
// // GetMatchingFiles Return the list of all applicable files inside the given directory for scanning
// func GetMatchingFiles(dir string, baseDir string) (*bytes.Buffer, *bytes.Buffer, error) {
// findCmd := "find " + dir
// for _, skippableExt := range session.Config.BlacklistedExtensions {
// findCmd += " -not -name \"*" + skippableExt + "\""
// }
// hostMountPath := *session.Options.HostMountPath
// if hostMountPath != "" {
// baseDir = hostMountPath
// }
// for _, skippablePathIndicator := range session.Config.BlacklistedPaths {
// findCmd += " -path " + baseDir + skippablePathIndicator + " -prune -o"
// }
// maxFileSize := strconv.FormatUint(uint64(*session.Options.MaximumFileSize), 10)
// findCmd += " -type f -size " + maxFileSize + "M"
// GetSession().Log.Info("find command: %s", findCmd)
//
// return ExecuteCommand(findCmd)
// }
// UpdateDirsPermissionsRW Update permissions for dirs in container images, so that they can be properly deleted
func UpdateDirsPermissionsRW(dir string) {
filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
if f.IsDir() {
err := os.Chmod(path, 0700)
if err != nil {
GetSession().Log.Error("Failed to change dir %s permission: %s", path, err)
}
}
return nil
})
}