forked from google/log4jscanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog4jscanner.go
139 lines (125 loc) · 3.06 KB
/
log4jscanner.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
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// The log4jscanner tool scans a set of directories for log4j vulnerable JARs.
package main
import (
"flag"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"github.com/google/log4jscanner/jar"
)
func usage() {
fmt.Fprint(os.Stderr, `Usage: log4jscanner [flag] [directories]
A log4j vulnerability scanner. The scanner walks the provided directories
attempting to find vulnerable JARs. Paths of vulnerable JARs are printed
to stdout.
Flags:
-s, --skip Glob pattern to skip when scanning (e.g. '/var/run/*'). May
be provided multiple times.
-w, --rewrite Rewrite vulnerable JARs as they are detected.
-v, --verbose Print verbose logs to stderr.
`)
}
var skipDirs = map[string]bool{
".hg": true,
".git": true,
"node_modules": true,
// TODO(ericchiang): expand
}
func main() {
var (
rewrite bool
w bool
verbose bool
v bool
toSkip []string
)
appendSkip := func(dir string) error {
toSkip = append(toSkip, dir)
return nil
}
flag.BoolVar(&rewrite, "rewrite", false, "")
flag.BoolVar(&w, "w", false, "")
flag.BoolVar(&verbose, "verbose", false, "")
flag.BoolVar(&v, "v", false, "")
flag.Func("s", "", appendSkip)
flag.Func("skip", "", appendSkip)
flag.Usage = usage
flag.Parse()
dirs := flag.Args()
if len(dirs) == 0 {
usage()
os.Exit(1)
}
if v {
verbose = v
}
if w {
rewrite = w
}
log.SetFlags(log.LstdFlags | log.Lshortfile)
logf := func(format string, v ...interface{}) {
if verbose {
log.Printf(format, v...)
}
}
seen := 0
walker := jar.Walker{
Rewrite: rewrite,
SkipDir: func(path string, d fs.DirEntry) bool {
seen++
if seen%5000 == 0 {
logf("Scanned %d files", seen)
}
if !d.IsDir() {
return false
}
for _, pattern := range toSkip {
if ok, err := filepath.Match(pattern, path); err == nil && ok {
return true
}
}
if skipDirs[filepath.Base(path)] {
return true
}
ignore, err := ignoreDir(path)
if err != nil {
log.Printf("Error scanning %s: %v", path, err)
}
return ignore
},
HandleError: func(path string, err error) {
log.Printf("Error: scanning %s: %v", path, err)
},
HandleReport: func(path string, r *jar.Report) {
if !rewrite {
fmt.Println(path)
}
},
HandleRewrite: func(path string, r *jar.Report) {
if rewrite {
fmt.Println(path)
}
},
}
for _, dir := range dirs {
logf("Scanning %s", dir)
if err := walker.Walk(dir); err != nil {
log.Printf("Error: walking %s: %v", dir, err)
}
}
}