-
Notifications
You must be signed in to change notification settings - Fork 5
/
filesystem.go
147 lines (138 loc) · 4.16 KB
/
filesystem.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
package main
import (
"fmt"
"path/filepath"
"os"
"strings"
"io/ioutil"
"bytes"
"github.com/fatih/color"
"strconv"
"github.com/pkg/errors"
)
type FileStruct struct {
Filename string
Path string
}
const MAX_FILE_SIZE_TO_SCAN = 200000
var ignoreExts = []string{".dll", ".nupkg", ".mp4", ".mkv", ".avi", ".flv", ".wmv", ".mov", ".pdf" }
func appendFilesystemMatch(pattern Pattern, filestruct FileStruct, lineMatched string,
lineNum int, table map[string]*Match) {
name := filestruct.Filename
_, exists := table[name]
if !exists {
table[name] = &Match{filestruct.Filename, filestruct.Path,
nil, pattern.Description,
pattern.Value, lineMatched, lineNum}
fmt.Println(color.BlueString("Found match %s %s: ", pattern.Description, pattern.Value))
if lineNum == NO_LINE_NUMBER_APPLICABLE{
color.Green("%s", filestruct.Path)
} else{
color.Green("%s:%d ", filestruct.Path, lineNum)
}
}
}
func getAllFilesInDirectory(dir string) ([]FileStruct, error) {
fileList := []FileStruct{}
err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
fileList = append(fileList, FileStruct{f.Name(), path})
return nil
})
if err != nil {
return nil, err
}
return fileList, nil
}
func searchThroughFile(path string) (bool, Pattern, string, int, error) {
fi, err := os.Stat(path)
if err != nil {
return false, Pattern{}, "", -1, err
}
ext := filepath.Ext(path)
for _, ignore := range ignoreExts {
if ext == ignore {
return false, Pattern{}, IS_FILENAME_MATCH,
NO_LINE_NUMBER_APPLICABLE, errors.New("Ignoring file extension " + path)
}
}
if fi.IsDir() || checkIfInsideIgnoredDirectory(path) {
return false, Pattern{}, IS_FILENAME_MATCH,
NO_LINE_NUMBER_APPLICABLE, err
}
if fi.Size() / 1024 >= MAX_FILE_SIZE_TO_SCAN{
return false, Pattern{}, IS_FILENAME_MATCH,
NO_LINE_NUMBER_APPLICABLE, errors.New(path + " is over max file size to scan")
}
f, err := ioutil.ReadFile(path)
if err != nil {
return false, Pattern{}, IS_FILENAME_MATCH,
NO_LINE_NUMBER_APPLICABLE, err
}
lines := bytes.Split(f, []byte("\n"))
for _, pattern := range fileContentRegexes {
lineNum := 0
for _, line := range lines {
lineNum++
if pattern.Regex.Match(line) {
return true, pattern, string(line), lineNum, nil
}
}
}
return false, Pattern{}, IS_FILENAME_MATCH, NO_LINE_NUMBER_APPLICABLE, nil
}
func searchFileContents(files []FileStruct, table map[string]*Match) {
for _, f := range files {
match, p, line, lineNum, err := searchThroughFile(f.Path)
if err == nil && match {
appendFilesystemMatch(p, f, line, lineNum, table)
}
}
}
func outputCSVFilesystem(matches []*Match) {
records := [][]string{{"Filename", "Description", "Filepath", "LineMatched", "LineNumber"}}
for _, match := range matches {
records = append(records, []string{match.Filename, match.Description,
match.Filepath, truncateString(match.LineMatched, 160),
strconv.Itoa(match.LineNumber)})
}
outputCSV(*outputCSVFlag, records)
}
func filesystemSecretFilenameRegexSearch(files []FileStruct, table map[string]*Match) {
for _, filestruct := range files {
for _, pattern := range secretFileNameRegexes {
if pattern.Regex.MatchString(filestruct.Filename) {
appendFilesystemMatch(pattern, filestruct, IS_FILENAME_MATCH, NO_LINE_NUMBER_APPLICABLE, table)
break
}
}
}
}
func filesystemSecretFilenameLiteralSearch(files []FileStruct, table map[string]*Match) {
for _, pattern := range secretFileNameLiterals {
for _, filename := range files {
if strings.Contains(filename.Filename, pattern.Value) {
appendFilesystemMatch(pattern, filename, IS_FILENAME_MATCH, NO_LINE_NUMBER_APPLICABLE, table)
}
}
}
}
func startFileSystemScan() {
files, err := getAllFilesInDirectory(*dirToScanFlag)
if err != nil {
color.Red(err.Error())
os.Exit(-1)
}
var uniqFileSet = make(map[string]*Match)
filesystemSecretFilenameLiteralSearch(files, uniqFileSet)
filesystemSecretFilenameRegexSearch(files, uniqFileSet)
if !*fileNamesOnlyFlag {
searchFileContents(files, uniqFileSet)
}
if len(*outputCSVFlag) != 0 {
var results []*Match
for _, values := range uniqFileSet {
results = append(results, values)
}
outputCSVFilesystem(results)
}
}