-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_analyzer.go
More file actions
238 lines (204 loc) · 4.84 KB
/
config_analyzer.go
File metadata and controls
238 lines (204 loc) · 4.84 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
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
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
)
type Severity string
const (
Low Severity = "Low"
Medium Severity = "Medium"
High Severity = "High"
)
type ConfigPattern struct {
pattern string
severity Severity
}
type ConfigCheck struct {
filename string
patterns []ConfigPattern
}
type Finding struct {
File string `json:"file"`
Line int `json:"line"`
Pattern string `json:"pattern"`
Content string `json:"content"`
Severity Severity `json:"severity"`
}
var configChecks = []ConfigCheck{
{
filename: "apache2.conf",
patterns: []ConfigPattern{
{"ServerTokens OS", Medium},
{"ServerSignature On", Medium},
{"TraceEnable On", High},
{"AllowOverride All", Medium},
{"Options All", Medium},
},
},
{
filename: "nginx.conf",
patterns: []ConfigPattern{
{"server_tokens on", Medium},
{"autoindex on", Low},
{"ssl_protocols TLSv1 TLSv1.1", High},
},
},
{
filename: "sshd_config",
patterns: []ConfigPattern{
{"PermitRootLogin yes", High},
{"PasswordAuthentication yes", Medium},
{"X11Forwarding yes", Low},
{"PermitEmptyPasswords yes", High},
},
},
{
filename: "my.cnf",
patterns: []ConfigPattern{
{"skip-networking", Low},
{"bind-address = 0.0.0.0", Medium},
{"local-infile=1", Medium},
},
},
}
type flagArray []string
func (i *flagArray) String() string {
return strings.Join(*i, ", ")
}
func (i *flagArray) Set(value string) error {
*i = append(*i, value)
return nil
}
var (
directories flagArray
jsonOutput bool
ignoreFile string
)
func main() {
flag.Var(&directories, "dir", "Directories to scan (can be specified multiple times)")
flag.BoolVar(&jsonOutput, "json", false, "Output results in JSON format")
flag.StringVar(&ignoreFile, "ignore", "", "File containing patterns to ignore")
flag.Parse()
if len(directories) == 0 {
directories = []string{"/etc/apache2", "/etc/nginx", "/etc/ssh", "/etc/mysql"}
}
var ignorePatterns []string
if ignoreFile != "" {
ignorePatterns = loadIgnorePatterns(ignoreFile)
}
var findings []Finding
var wg sync.WaitGroup
var mu sync.Mutex
for _, dir := range directories {
wg.Add(1)
go func(dir string) {
defer wg.Done()
dirFindings := scanDirectory(dir, ignorePatterns)
mu.Lock()
findings = append(findings, dirFindings...)
mu.Unlock()
}(dir)
}
wg.Wait()
if jsonOutput {
outputJSON(findings)
} else {
outputText(findings)
}
}
func loadIgnorePatterns(filename string) []string {
file, err := os.Open(filename)
if err != nil {
fmt.Printf("Error opening ignore file %s: %v\n", filename, err)
return nil
}
defer file.Close()
var patterns []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
patterns = append(patterns, scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Printf("Error reading ignore file %s: %v\n", filename, err)
}
return patterns
}
func scanDirectory(dir string, ignorePatterns []string) []Finding {
var findings []Finding
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
for _, check := range configChecks {
if strings.HasSuffix(path, check.filename) {
fmt.Printf("Scanning file: %s\n", path)
findings = append(findings, scanFile(path, check.patterns, ignorePatterns)...)
}
}
}
return nil
})
if err != nil {
fmt.Printf("Error walking directory %s: %v\n", dir, err)
}
return findings
}
func scanFile(filepath string, patterns []ConfigPattern, ignorePatterns []string) []Finding {
var findings []Finding
file, err := os.Open(filepath)
if err != nil {
fmt.Printf("Error opening file %s: %v\n", filepath, err)
return findings
}
defer file.Close()
scanner := bufio.NewScanner(file)
lineNum := 0
for scanner.Scan() {
lineNum++
line := scanner.Text()
for _, pattern := range patterns {
if strings.Contains(line, pattern.pattern) && !containsPattern(ignorePatterns, line) {
findings = append(findings, Finding{
File: filepath,
Line: lineNum,
Pattern: pattern.pattern,
Content: line,
Severity: pattern.severity,
})
}
}
}
if err := scanner.Err(); err != nil {
fmt.Printf("Error reading file %s: %v\n", filepath, err)
}
return findings
}
func containsPattern(patterns []string, line string) bool {
for _, pattern := range patterns {
if strings.Contains(line, pattern) {
return true
}
}
return false
}
func outputText(findings []Finding) {
for _, finding := range findings {
fmt.Printf("[%s] %s:%d - %s\n", finding.Severity, finding.File, finding.Line, finding.Pattern)
fmt.Printf(" %s\n", finding.Content)
}
}
func outputJSON(findings []Finding) {
json, err := json.MarshalIndent(findings, "", " ")
if err != nil {
fmt.Printf("Error generating JSON: %v\n", err)
return
}
fmt.Println(string(json))
}