forked from hillu/local-spring-vuln-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
194 lines (175 loc) · 4.79 KB
/
main.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
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
package main
import (
"archive/zip"
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
var logFile = os.Stdout
var errFile = os.Stderr
func handleJar(path string, ra io.ReaderAt, sz int64) {
if verbose {
fmt.Fprintf(logFile, "Inspecting %s...\n", path)
}
zr, err := zip.NewReader(ra, sz)
if err != nil {
fmt.Fprintf(logFile, "cant't open JAR file: %s (size %d): %v\n", path, sz, err)
return
}
for _, file := range zr.File {
if file.FileInfo().IsDir() {
continue
}
switch strings.ToLower(filepath.Ext(file.Name)) {
case ".jar", ".war", ".ear":
fr, err := file.Open()
if err != nil {
fmt.Fprintf(logFile, "can't open JAR file member for reading: %s (%s): %v\n", path, file.Name, err)
continue
}
buf, err := ioutil.ReadAll(fr)
fr.Close()
if err != nil {
fmt.Fprintf(logFile, "can't read JAR file member: %s (%s): %v\n", path, file.Name, err)
}
handleJar(path+"::"+file.Name, bytes.NewReader(buf), int64(len(buf)))
default:
fr, err := file.Open()
if err != nil {
fmt.Fprintf(logFile, "can't open JAR file member for reading: %s (%s): %v\n", path, file.Name, err)
continue
}
// Identify class filess by magic bytes
buf := bytes.NewBuffer(nil)
if _, err := io.CopyN(buf, fr, 4); err != nil {
if err != io.EOF && !quiet {
fmt.Fprintf(logFile, "can't read magic from JAR file member: %s (%s): %v\n", path, file.Name, err)
}
fr.Close()
continue
} else if !bytes.Equal(buf.Bytes(), []byte{0xca, 0xfe, 0xba, 0xbe}) {
fr.Close()
continue
}
_, err = io.Copy(buf, fr)
fr.Close()
if err != nil {
fmt.Fprintf(logFile, "can't read JAR file member: %s (%s): %v\n", path, file.Name, err)
continue
}
if info := IsVulnerableClass(buf.Bytes(), file.Name, vulns); info != nil {
fmt.Fprintf(logFile, "indicator for vulnerable component found in %s (%s): %s %s %s\n",
path, file.Name, info.Filename, info.Version, info.Vulnerabilities&vulns)
continue
}
}
}
}
type excludeFlags []string
func (flags *excludeFlags) String() string {
return fmt.Sprint(*flags)
}
func (flags *excludeFlags) Set(value string) error {
*flags = append(*flags, filepath.Clean(value))
return nil
}
func (flags excludeFlags) Has(path string) bool {
for _, exclude := range flags {
if path == exclude {
return true
}
}
return false
}
var excludes excludeFlags
var verbose bool
var logFileName string
var quiet bool
var vulns, ignoreVulns Vulnerabilities
var network bool
func main() {
flag.Var(&excludes, "exclude", "paths to exclude (can be used multiple times)")
flag.BoolVar(&verbose, "verbose", false, "log every archive file considered")
flag.StringVar(&logFileName, "log", "", "log file to write output to")
flag.BoolVar(&quiet, "quiet", false, "no ouput unless vulnerable")
// flag.Var(&ignoreVulns, "ignore-vulns", "ignore vulnerabilities")
flag.BoolVar(&network, "scan-network", false, "search network filesystems")
flag.Parse()
vulns = CheckAllVulnerabilities ^ ignoreVulns
if !quiet {
fmt.Printf("%s - a simple local Spring vulnerability scanner\n\n", filepath.Base(os.Args[0]))
}
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Usage: %s [--verbose] [--quiet] [--exclude <path>] [--log <file>] [ paths ... ]\n", os.Args[0])
os.Exit(1)
}
if logFileName != "" {
f, err := os.Create(logFileName)
if err != nil {
fmt.Fprintln(os.Stderr, "Could not create log file")
os.Exit(2)
}
logFile = f
errFile = f
defer f.Close()
}
fmt.Fprintf(logFile, "Checking for vulnerabilities: %s\n", vulns)
for _, root := range flag.Args() {
filepath.Walk(filepath.Clean(root), func(path string, info os.FileInfo, err error) error {
if isPseudoFS(path) {
if !quiet {
fmt.Fprintf(logFile, "Skipping %s: pseudo filesystem\n", path)
}
return filepath.SkipDir
}
if !network && isNetworkFS(path) {
if !quiet {
fmt.Fprintf(logFile, "Skipping %s: network filesystem\n", path)
}
return filepath.SkipDir
}
if !quiet {
fmt.Fprintf(logFile, "examining %s\n", path)
}
if err != nil {
fmt.Fprintf(errFile, "%s: %s\n", path, err)
return nil
}
if excludes.Has(path) {
if !quiet {
fmt.Fprintf(logFile, "Skipping %s: explicitly excluded\n", path)
}
return filepath.SkipDir
}
if info.IsDir() {
return nil
}
switch ext := strings.ToLower(filepath.Ext(path)); ext {
case ".jar", ".war", ".ear":
f, err := os.Open(path)
if err != nil {
fmt.Fprintf(errFile, "can't open %s: %v\n", path, err)
return nil
}
defer f.Close()
sz, err := f.Seek(0, os.SEEK_END)
if err != nil {
fmt.Fprintf(errFile, "can't seek in %s: %v\n", path, err)
return nil
}
handleJar(path, f, sz)
default:
return nil
}
return nil
})
}
if !quiet {
fmt.Println("\nScan finished")
}
}