-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
309 lines (268 loc) · 7.58 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package main
import (
"archive/zip"
"encoding/csv"
"fmt"
"log"
"os"
"os/user"
"regexp"
"strconv"
"time"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
)
/* hold details on each match */
type match struct {
fullPath string
version string
md5hash string
whitelist bool
isContainer bool
containerImage string
ignore bool
timestamp string
}
var files []string /* all jars, wars, and ears */
var matches []match /* jars, wars, and ears with the vulnerable library */
func mainHandler(banner bool, path string, suppress bool, headers bool, imd5 string, ipath string, icimage string) {
bannerString := `
░█░░░█▀█░█▀▀░█░█░█▀▀░█░█░█▀▀░█░░░█░░░░░█▀▀░█▀▀░█▀█░▀█▀░▀█▀░█▀█░█▀▀░█░░
░█░░░█░█░█░█░░▀█░▀▀█░█▀█░█▀▀░█░░░█░░░░░▀▀█░█▀▀░█░█░░█░░░█░░█░█░█▀▀░█░░
░▀▀▀░▀▀▀░▀▀▀░░░▀░▀▀▀░▀░▀░▀▀▀░▀▀▀░▀▀▀░░░▀▀▀░▀▀▀░▀░▀░░▀░░▀▀▀░▀░▀░▀▀▀░▀▀▀
- A CVE-2021-44228/CVE-2021-45046/CVE-2021-45105 Scanner
- v1.0.0
- by Osama Elnaggar
`
if !banner && !suppress {
fmt.Printf(bannerString)
}
cwd, err := os.Getwd()
if err != nil {
cwd = "."
}
if path == "." {
path = cwd
}
// 0a. Check user permissions
currUser, err := user.Current()
if err == nil {
if currUser.Username != "root" && !suppress {
color.Set(color.FgHiRed)
fmt.Println("[*] WARNING: Running as non-root user.")
fmt.Println(" Non-readable files / dirs will be skipped")
fmt.Println(" Container mapping will fail\n")
color.Unset()
}
}
// 1. Find all jars, uber-jars, ears and wars
printLine("[*] Starting shallow scan ............ ", suppress)
findArchives(path)
color.Set(color.FgCyan)
printLine("[DONE]\n", suppress)
color.Unset()
// 2. Search for log4j-core-2.X in them
printLine("[*] Starting deep scan ............... ", suppress)
for _, f := range files {
b, v := checkLog4j(f)
// 2a. Search for direct log4j-core-2.X matches
if b {
m := match{fullPath: f, version: v, isContainer: false, timestamp: time.Now().Format(time.RFC3339)}
matches = append(matches, m)
// 2b. Go through uber-jars, wars and ears
} else {
// check for file size first
fi, err := os.Stat(f)
if err != nil {
continue
}
if fi.Size() > 0 {
r, err := zip.OpenReader(f)
if err != nil {
continue
}
for _, i := range r.File {
// fmt.Println(listZipFiles(i))
b, v := listZipFiles(i)
if b {
m := match{fullPath: f, version: v, isContainer: false, timestamp: time.Now().Format(time.RFC3339)}
matches = append(matches, m)
}
}
r.Close()
}
}
}
color.Set(color.FgCyan)
printLine("[DONE]\n", suppress)
color.Unset()
// 2b. Add MD5 sum details
printLine("[*] Calculating MD5 hashes ........... ", suppress)
for i := range matches {
match := &matches[i]
b, _ := checkLog4j(match.fullPath)
// we won't have log4j-core-X files
if b == false {
match.md5hash = md5er(match.fullPath)
}
}
color.Set(color.FgCyan)
printLine("[DONE]\n", suppress)
color.Unset()
// 3. Enrich them with Docker and Kubernetes information, etc.
// 3a. Docker with overlayfs storage driver
printLine("[*] Performing container image lookups ", suppress)
findDockerOverlayContainers()
findDockerAufsContainers()
// 3b. Containerd Runtime
findContainerdContainers()
color.Set(color.FgCyan)
printLine("[DONE]\n", suppress)
color.Unset()
// 3c. CRIO runtime
findCrioContainers()
// 4. Print results
// 4a. Get primary IP
ip := getPrimaryIPAddr()
// 4b. Get hostname
hostname, _ := os.Hostname()
// 4c. Process ignore list(s)
printLine("[*] Processing ignore list(s) ........ ", suppress)
if imd5 != "" {
ignoreMatches(imd5, "md5")
}
if ipath != "" {
ignoreMatches(ipath, "fullPath")
}
if icimage != "" {
ignoreMatches(icimage, "containerImage")
}
color.Set(color.FgCyan)
printLine("[DONE]\n", suppress)
color.Unset()
validMatches := 0
for _, i := range matches {
if !(i.ignore) {
validMatches++
}
}
// 4d. Ignore / filter our non-useful results such as snapshot directories -
re := regexp.MustCompile(`\/var\/lib\/containerd\/io.containerd.snapshotter.v1.overlayfs\/snapshots\/`)
for i := range matches {
res := re.FindStringSubmatch(matches[i].fullPath)
if len(res) > 0 {
match := &matches[i]
match.ignore = true
}
}
re = regexp.MustCompile(`\/var\/lib\/containers\/storage\/overlay\/\S{64}\/diff\/`)
for i := range matches {
res := re.FindStringSubmatch(matches[i].fullPath)
if len(res) > 0 {
match := &matches[i]
match.ignore = true
}
}
re = regexp.MustCompile(`\/var\/lib\/docker\/aufs\/(mnt|diff)\/\S{64}\/`)
for i := range matches {
res := re.FindStringSubmatch(matches[i].fullPath)
if len(res) > 0 {
match := &matches[i]
// no match means the container is not running / stale entries
if match.containerImage == "" {
match.ignore = true
}
}
}
// 4e. Write CSV output
printLine("[*] Generating output ................ ", suppress)
color.Set(color.FgCyan)
printLine("[DONE]\n\n", suppress)
color.Unset()
color.Unset()
if validMatches > 0 {
if !headers {
printCSVHeader()
}
w := csv.NewWriter(os.Stdout)
for _, i := range matches {
if !(i.ignore) {
row := []string{ip, hostname, "", "", "", "", i.md5hash, i.timestamp, strconv.FormatBool(i.isContainer), i.containerImage, i.fullPath, i.version}
if err := w.Write(row); err != nil {
log.Fatalln("Error writing record to csv")
}
}
}
w.Flush()
if err := w.Error(); err != nil {
log.Fatal(err)
}
}
}
func printCSVHeader() {
fmt.Println("IP,Hostname,AppName,Team,Ignore (Y/N),Comments,MD5Hash,Timestamp,Container,ContainerImage,FullPath,Version")
}
func main() {
// Steps
// 0. Read user input
app := &cli.App{
Version: "v1.0.0",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "path",
Value: ".",
Aliases: []string{"p"},
Usage: "Path to search",
},
&cli.BoolFlag{
Name: "no-banner",
Aliases: []string{"nb"},
Usage: "Suppress banner",
},
&cli.BoolFlag{
Name: "no-messages",
Aliases: []string{"nm"},
Usage: "Suppress messages except for CSV output",
},
&cli.BoolFlag{
Name: "no-header",
Aliases: []string{"nh"},
Usage: "Suppress header in CSV output",
},
&cli.StringFlag{
Name: "imd5",
Aliases: []string{"im"},
Usage: "Ignore MD5 hashes. Refer to the GitHub page for expected format",
},
&cli.StringFlag{
Name: "ipath",
Aliases: []string{"ip"},
Usage: "Ignore file path matches. Refer to the GitHub page for expected format",
},
&cli.StringFlag{
Name: "icimage",
Aliases: []string{"ic"},
Usage: "Ignore container image matches. Refer to the GitHub page for expected format",
},
&cli.BoolFlag{
Name: "print-headers",
Aliases: []string{"ph"},
Usage: "Print CSV Headers only",
},
},
Name: "Log4Shell Sentinel",
Usage: "by Osama Elnaggar",
Action: func(c *cli.Context) error {
if c.Bool("print-headers") {
printCSVHeader()
} else {
mainHandler(c.Bool("no-banner"), c.String("path"), c.Bool("no-messages"), c.Bool("no-header"), c.String("imd5"), c.String("ipath"), c.String("icimage"))
}
return nil
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}