-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
253 lines (226 loc) · 6.49 KB
/
utils.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
package main
import (
"archive/zip"
"crypto/md5"
"encoding/csv"
"encoding/hex"
"fmt"
"io"
"log"
"net"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
mapset "github.com/deckarep/golang-set"
)
func printLine(line string, suppress bool) {
if !suppress {
fmt.Print(line)
}
}
// *getPrimaryIPAddr* returns the primary interface on the machine
func getPrimaryIPAddr() string {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP.String()
}
// *md5er* takes a file path and returns the corresponding MD5 checksum
func md5er(fullPath string) string {
file, err := os.Open(fullPath)
if err != nil {
return ""
}
defer file.Close()
hash := md5.New()
_, err = io.Copy(hash, file)
if err != nil {
return ""
}
return hex.EncodeToString(hash.Sum(nil))
}
// *listZipFiles* takes a zip file and lists it contents
func listZipFiles(file *zip.File) (bool, string) {
f, err := file.Open()
if err != nil {
msg := "Failed to open zip %s for reading: %s"
fmt.Println(msg)
return false, ""
}
defer f.Close()
return checkLog4j(file.Name)
}
// *findArchives* searches the path passed to it for .jar, .war and .ear files
// It does not look into them to see if they have log4-core-2.x
// ignore entries such as /var/lib/docker/overlay2/9e570f0cec8dcff5662a940f205600b541f82bd7d5d9c9bea8975ecb072506f4/diff/app/spring-boot-application.jar because
// they do not reflect running containers
func findArchives(path string) {
filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
// suppress errors / ignore files we can't read
if err == nil {
if !info.IsDir() && (filepath.Ext(path) == ".jar" || filepath.Ext(path) == ".war" || filepath.Ext(path) == ".ear") {
reOverlay := regexp.MustCompile(`\/var\/lib\/docker\/overlay2?\/\S{64}\/diff\/`)
resOverlay := reOverlay.FindStringSubmatch(path)
if len(resOverlay) == 0 {
files = append(files, path)
}
}
}
return nil
})
}
// *checkLog4j* takes a file and checks to see if it is one of the affected versions or not
// if it finds a match, it returns true + the version
// *NOTE* anything below 7 is considered vulnerable to one of the high / critical CVEs as
// mentioned here - https://logging.apache.org/log4j/2.x/security.html
// CVE-2021-45105, CVE-2021-45046, CVE-2021-44228
func checkLog4j(path string) (bool, string) {
// taken from: https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core
log4jSlice := []interface{}{
"log4j-core-2.16.0.jar",
"log4j-core-2.15.0.jar",
"log4j-core-2.14.1.jar",
"log4j-core-2.14.0.jar",
"log4j-core-2.13.3.jar",
"log4j-core-2.13.2.jar",
"log4j-core-2.13.1.jar",
"log4j-core-2.13.0.jar",
"log4j-core-2.12.2.jar",
"log4j-core-2.12.1.jar",
"log4j-core-2.12.0.jar",
"log4j-core-2.11.2.jar",
"log4j-core-2.11.1.jar",
"log4j-core-2.11.0.jar",
"log4j-core-2.10.0.jar",
"log4j-core-2.9.1.jar",
"log4j-core-2.9.0.jar",
"log4j-core-2.8.2.jar",
"log4j-core-2.8.1.jar",
"log4j-core-2.8.jar",
"log4j-core-2.7.jar",
"log4j-core-2.6.2.jar",
"log4j-core-2.6.1.jar",
"log4j-core-2.6.jar",
"log4j-core-2.5.jar",
"log4j-core-2.4.1.jar",
"log4j-core-2.4.jar",
"log4j-core-2.3.jar",
"log4j-core-2.2.jar",
"log4j-core-2.1.jar",
"log4j-core-2.0.2.jar",
"log4j-core-2.0.1.jar",
"log4j-core-2.0.jar",
"log4j-core-2.0-rc2.jar",
"log4j-core-2.0-rc1.jar",
"log4j-core-2.0-beta9.jar",
"log4j-core-2.0-beta8.jar",
"log4j-core-2.0-beta7.jar",
"log4j-core-2.0-beta6.jar",
"log4j-core-2.0-beta5.jar",
"log4j-core-2.0-beta4.jar",
"log4j-core-2.0-beta3.jar",
"log4j-core-2.0-beta2.jar",
"log4j-core-2.0-beta1.jar",
"log4j-core-2.0-alpha2.jar",
"log4j-core-2.0-alpha1.jar"}
log4jSet := mapset.NewSetFromSlice(log4jSlice)
_, file := filepath.Split(path)
if log4jSet.Contains(file) {
return true, file
}
return false, file
}
// *ingoreRow* is a generic structure to read our ignore lists, regardless of if they are
// for MD5 hashes, paths or container images
type ignoreRow struct {
key string
appName string
reason string
}
// *ignoreMatches* allows us to ignore / suppress results with the corresponding MD5 hashes,
// file paths or container image names
func ignoreMatches(file string, ignoreKey string) {
// intercept malformed CSV files
defer func() {
if err := recover(); err != nil {
fmt.Println("\nERORR: Malformed *ignore file*. Exiting")
os.Exit(1)
}
}()
csvFile, err := os.Open(file)
if err != nil {
log.Fatal("Error opening ignore list", file)
}
defer csvFile.Close()
csvLines, err := csv.NewReader(csvFile).ReadAll()
if err != nil {
log.Fatal(err)
}
// the first row may just be the headers. Won't skip it as even if
// it is a header, it won't match any MD5 sum so it is harmless
for _, line := range csvLines {
row := ignoreRow{
key: line[0],
appName: line[1],
reason: line[2],
}
for i := range matches {
match := &matches[i]
if ignoreKey == "md5" {
if match.md5hash == row.key {
match.ignore = true
}
} else if ignoreKey == "fullPath" {
if match.fullPath == row.key {
match.ignore = true
}
} else if ignoreKey == "containerImage" {
if match.containerImage == row.key {
match.ignore = true
}
}
}
}
}
// *checkBinary* checks to see if a binary is in the
// PATH or not
func checkBinary(cmd string) bool {
_, err := exec.LookPath(cmd)
if err != nil {
return false
}
return true
}
// *crictlCheckContainer* takes an image name and checks to see
// if the corresponding container is running or not
// crictl ps does not show image tags - https://github.com/kubernetes-sigs/cri-tools/issues/454
// but critcl images does. So we will do a best effort and check both
// this isn't definitive
func crictlCheckContainer(name string) bool {
re := regexp.MustCompile(`(?P<image>[^:]+):(?P<tag>\S+)`)
res := re.FindStringSubmatch(name)
if len(res) > 0 {
image := res[1]
tag := res[2]
// check *crictl ps* output first using image name
outp, _ := exec.Command("crictl", "ps").Output()
if strings.Contains(fmt.Sprint(string(outp)), image) {
// check *crictl images* output using image name + tag
outi, _ := exec.Command("crictl", "images").Output()
// build dynamic regex
whitespace := `\s+`
regDynamicString := fmt.Sprintf("%s%s%s", image, whitespace, tag)
rei := regexp.MustCompile(regDynamicString)
resi := rei.FindStringSubmatch(string(outi))
if len(resi) > 0 {
return true
}
}
}
return false
}