1
|
|
|
/* Vuls - Vulnerability Scanner |
2
|
|
|
Copyright (C) 2016 Future Corporation , Japan. |
3
|
|
|
|
4
|
|
|
This program is free software: you can redistribute it and/or modify |
5
|
|
|
it under the terms of the GNU General Public License as published by |
6
|
|
|
the Free Software Foundation, either version 3 of the License, or |
7
|
|
|
(at your option) any later version. |
8
|
|
|
|
9
|
|
|
This program is distributed in the hope that it will be useful, |
10
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12
|
|
|
GNU General Public License for more details. |
13
|
|
|
|
14
|
|
|
You should have received a copy of the GNU General Public License |
15
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
package scan |
19
|
|
|
|
20
|
|
|
import ( |
21
|
|
|
"bufio" |
22
|
|
|
"fmt" |
23
|
|
|
"net" |
24
|
|
|
"regexp" |
25
|
|
|
"strings" |
26
|
|
|
"time" |
27
|
|
|
|
28
|
|
|
"github.com/future-architect/vuls/config" |
29
|
|
|
"github.com/future-architect/vuls/models" |
30
|
|
|
"github.com/sirupsen/logrus" |
31
|
|
|
) |
32
|
|
|
|
33
|
|
|
type base struct { |
34
|
|
|
ServerInfo config.ServerInfo |
35
|
|
|
Distro config.Distro |
36
|
|
|
Platform models.Platform |
37
|
|
|
osPackages |
38
|
|
|
|
39
|
|
|
log *logrus.Entry |
40
|
|
|
errs []error |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
func (l *base) scanWp() (err error) { |
44
|
|
|
return err |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
func (l *base) exec(cmd string, sudo bool) execResult { |
48
|
|
|
return exec(l.ServerInfo, cmd, sudo, l.log) |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
func (l *base) setServerInfo(c config.ServerInfo) { |
52
|
|
|
l.ServerInfo = c |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
func (l *base) getServerInfo() config.ServerInfo { |
56
|
|
|
return l.ServerInfo |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
func (l *base) setDistro(fam, rel string) { |
60
|
|
|
d := config.Distro{ |
61
|
|
|
Family: fam, |
62
|
|
|
Release: rel, |
63
|
|
|
} |
64
|
|
|
l.Distro = d |
65
|
|
|
|
66
|
|
|
s := l.getServerInfo() |
67
|
|
|
s.Distro = d |
68
|
|
|
l.setServerInfo(s) |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
func (l *base) getDistro() config.Distro { |
72
|
|
|
return l.Distro |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
func (l *base) setPlatform(p models.Platform) { |
76
|
|
|
l.Platform = p |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
func (l *base) getPlatform() models.Platform { |
80
|
|
|
return l.Platform |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
func (l *base) runningKernel() (release, version string, err error) { |
84
|
|
|
r := l.exec("uname -r", noSudo) |
85
|
|
|
if !r.isSuccess() { |
86
|
|
|
return "", "", fmt.Errorf("Failed to SSH: %s", r) |
87
|
|
|
} |
88
|
|
|
release = strings.TrimSpace(r.Stdout) |
89
|
|
|
|
90
|
|
|
switch l.Distro.Family { |
91
|
|
|
case config.Debian: |
92
|
|
|
r := l.exec("uname -a", noSudo) |
93
|
|
|
if !r.isSuccess() { |
94
|
|
|
return "", "", fmt.Errorf("Failed to SSH: %s", r) |
95
|
|
|
} |
96
|
|
|
ss := strings.Fields(r.Stdout) |
97
|
|
|
if 6 < len(ss) { |
98
|
|
|
version = ss[6] |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
return |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
func (l *base) allContainers() (containers []config.Container, err error) { |
105
|
|
|
switch l.ServerInfo.ContainerType { |
106
|
|
|
case "", "docker": |
107
|
|
|
stdout, err := l.dockerPs("-a --format '{{.ID}} {{.Names}} {{.Image}}'") |
108
|
|
|
if err != nil { |
109
|
|
|
return containers, err |
110
|
|
|
} |
111
|
|
|
return l.parseDockerPs(stdout) |
112
|
|
|
case "lxd": |
113
|
|
|
stdout, err := l.lxdPs("-c n") |
114
|
|
|
if err != nil { |
115
|
|
|
return containers, err |
116
|
|
|
} |
117
|
|
|
return l.parseLxdPs(stdout) |
118
|
|
|
case "lxc": |
119
|
|
|
stdout, err := l.lxcPs("-1") |
120
|
|
|
if err != nil { |
121
|
|
|
return containers, err |
122
|
|
|
} |
123
|
|
|
return l.parseLxcPs(stdout) |
124
|
|
|
default: |
125
|
|
|
return containers, fmt.Errorf( |
126
|
|
|
"Not supported yet: %s", l.ServerInfo.ContainerType) |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
func (l *base) runningContainers() (containers []config.Container, err error) { |
131
|
|
|
switch l.ServerInfo.ContainerType { |
132
|
|
|
case "", "docker": |
133
|
|
|
stdout, err := l.dockerPs("--format '{{.ID}} {{.Names}} {{.Image}}'") |
134
|
|
|
if err != nil { |
135
|
|
|
return containers, err |
136
|
|
|
} |
137
|
|
|
return l.parseDockerPs(stdout) |
138
|
|
|
case "lxd": |
139
|
|
|
stdout, err := l.lxdPs("volatile.last_state.power=RUNNING -c n") |
140
|
|
|
if err != nil { |
141
|
|
|
return containers, err |
142
|
|
|
} |
143
|
|
|
return l.parseLxdPs(stdout) |
144
|
|
|
case "lxc": |
145
|
|
|
stdout, err := l.lxcPs("-1 --running") |
146
|
|
|
if err != nil { |
147
|
|
|
return containers, err |
148
|
|
|
} |
149
|
|
|
return l.parseLxcPs(stdout) |
150
|
|
|
default: |
151
|
|
|
return containers, fmt.Errorf( |
152
|
|
|
"Not supported yet: %s", l.ServerInfo.ContainerType) |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
func (l *base) exitedContainers() (containers []config.Container, err error) { |
157
|
|
|
switch l.ServerInfo.ContainerType { |
158
|
|
|
case "", "docker": |
159
|
|
|
stdout, err := l.dockerPs("--filter 'status=exited' --format '{{.ID}} {{.Names}} {{.Image}}'") |
160
|
|
|
if err != nil { |
161
|
|
|
return containers, err |
162
|
|
|
} |
163
|
|
|
return l.parseDockerPs(stdout) |
164
|
|
|
case "lxd": |
165
|
|
|
stdout, err := l.lxdPs("volatile.last_state.power=STOPPED -c n") |
166
|
|
|
if err != nil { |
167
|
|
|
return containers, err |
168
|
|
|
} |
169
|
|
|
return l.parseLxdPs(stdout) |
170
|
|
|
case "lxc": |
171
|
|
|
stdout, err := l.lxcPs("-1 --stopped") |
172
|
|
|
if err != nil { |
173
|
|
|
return containers, err |
174
|
|
|
} |
175
|
|
|
return l.parseLxcPs(stdout) |
176
|
|
|
default: |
177
|
|
|
return containers, fmt.Errorf( |
178
|
|
|
"Not supported yet: %s", l.ServerInfo.ContainerType) |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
func (l *base) dockerPs(option string) (string, error) { |
183
|
|
|
cmd := fmt.Sprintf("docker ps %s", option) |
184
|
|
|
r := l.exec(cmd, noSudo) |
185
|
|
|
if !r.isSuccess() { |
186
|
|
|
return "", fmt.Errorf("Failed to SSH: %s", r) |
187
|
|
|
} |
188
|
|
|
return r.Stdout, nil |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
func (l *base) lxdPs(option string) (string, error) { |
192
|
|
|
cmd := fmt.Sprintf("lxc list %s", option) |
193
|
|
|
r := l.exec(cmd, noSudo) |
194
|
|
|
if !r.isSuccess() { |
195
|
|
|
return "", fmt.Errorf("failed to SSH: %s", r) |
196
|
|
|
} |
197
|
|
|
return r.Stdout, nil |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
func (l *base) lxcPs(option string) (string, error) { |
201
|
|
|
cmd := fmt.Sprintf("lxc-ls %s 2>/dev/null", option) |
202
|
|
|
r := l.exec(cmd, sudo) |
203
|
|
|
if !r.isSuccess() { |
204
|
|
|
return "", fmt.Errorf("failed to SSH: %s", r) |
205
|
|
|
} |
206
|
|
|
return r.Stdout, nil |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
func (l *base) parseDockerPs(stdout string) (containers []config.Container, err error) { |
210
|
|
|
lines := strings.Split(stdout, "\n") |
211
|
|
|
for _, line := range lines { |
212
|
|
|
fields := strings.Fields(line) |
213
|
|
|
if len(fields) == 0 { |
214
|
|
|
break |
215
|
|
|
} |
216
|
|
|
if len(fields) != 3 { |
217
|
|
|
return containers, fmt.Errorf("Unknown format: %s", line) |
218
|
|
|
} |
219
|
|
|
containers = append(containers, config.Container{ |
220
|
|
|
ContainerID: fields[0], |
221
|
|
|
Name: fields[1], |
222
|
|
|
Image: fields[2], |
223
|
|
|
}) |
224
|
|
|
} |
225
|
|
|
return |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
func (l *base) parseLxdPs(stdout string) (containers []config.Container, err error) { |
229
|
|
|
lines := strings.Split(stdout, "\n") |
230
|
|
|
for i, line := range lines[3:] { |
231
|
|
|
if i%2 == 1 { |
232
|
|
|
continue |
233
|
|
|
} |
234
|
|
|
fields := strings.Fields(strings.Replace(line, "|", " ", -1)) |
235
|
|
|
if len(fields) == 0 { |
236
|
|
|
break |
237
|
|
|
} |
238
|
|
|
if len(fields) != 1 { |
239
|
|
|
return containers, fmt.Errorf("Unknown format: %s", line) |
240
|
|
|
} |
241
|
|
|
containers = append(containers, config.Container{ |
242
|
|
|
ContainerID: fields[0], |
243
|
|
|
Name: fields[0], |
244
|
|
|
}) |
245
|
|
|
} |
246
|
|
|
return |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
func (l *base) parseLxcPs(stdout string) (containers []config.Container, err error) { |
250
|
|
|
lines := strings.Split(stdout, "\n") |
251
|
|
|
for _, line := range lines { |
252
|
|
|
fields := strings.Fields(line) |
253
|
|
|
if len(fields) == 0 { |
254
|
|
|
break |
255
|
|
|
} |
256
|
|
|
containers = append(containers, config.Container{ |
257
|
|
|
ContainerID: fields[0], |
258
|
|
|
Name: fields[0], |
259
|
|
|
}) |
260
|
|
|
} |
261
|
|
|
return |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
// ip executes ip command and returns IP addresses |
265
|
|
|
func (l *base) ip() ([]string, []string, error) { |
266
|
|
|
// e.g. |
267
|
|
|
// 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000\ link/ether 52:54:00:2a:86:4c brd ff:ff:ff:ff:ff:ff |
268
|
|
|
// 2: eth0 inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0 |
269
|
|
|
// 2: eth0 inet6 fe80::5054:ff:fe2a:864c/64 scope link \ valid_lft forever preferred_lft forever |
270
|
|
|
r := l.exec("/sbin/ip -o addr", noSudo) |
271
|
|
|
if !r.isSuccess() { |
272
|
|
|
return nil, nil, fmt.Errorf("Failed to detect IP address: %v", r) |
273
|
|
|
} |
274
|
|
|
ipv4Addrs, ipv6Addrs := l.parseIP(r.Stdout) |
275
|
|
|
return ipv4Addrs, ipv6Addrs, nil |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
// parseIP parses the results of ip command |
279
|
|
|
func (l *base) parseIP(stdout string) (ipv4Addrs []string, ipv6Addrs []string) { |
280
|
|
|
lines := strings.Split(stdout, "\n") |
281
|
|
|
for _, line := range lines { |
282
|
|
|
fields := strings.Fields(line) |
283
|
|
|
if len(fields) < 4 { |
284
|
|
|
continue |
285
|
|
|
} |
286
|
|
|
ip, _, err := net.ParseCIDR(fields[3]) |
287
|
|
|
if err != nil { |
288
|
|
|
continue |
289
|
|
|
} |
290
|
|
|
if !ip.IsGlobalUnicast() { |
291
|
|
|
continue |
292
|
|
|
} |
293
|
|
|
if ipv4 := ip.To4(); ipv4 != nil { |
294
|
|
|
ipv4Addrs = append(ipv4Addrs, ipv4.String()) |
295
|
|
|
} else { |
296
|
|
|
ipv6Addrs = append(ipv6Addrs, ip.String()) |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
return |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
func (l *base) detectPlatform() { |
303
|
|
|
if l.getServerInfo().Mode.IsOffline() { |
304
|
|
|
l.setPlatform(models.Platform{Name: "unknown"}) |
305
|
|
|
return |
306
|
|
|
} |
307
|
|
|
ok, instanceID, err := l.detectRunningOnAws() |
308
|
|
|
if err != nil { |
309
|
|
|
l.setPlatform(models.Platform{Name: "other"}) |
310
|
|
|
return |
311
|
|
|
} |
312
|
|
|
if ok { |
313
|
|
|
l.setPlatform(models.Platform{ |
314
|
|
|
Name: "aws", |
315
|
|
|
InstanceID: instanceID, |
316
|
|
|
}) |
317
|
|
|
return |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
//TODO Azure, GCP... |
321
|
|
|
l.setPlatform(models.Platform{Name: "other"}) |
322
|
|
|
return |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
func (l *base) detectRunningOnAws() (ok bool, instanceID string, err error) { |
326
|
|
|
if r := l.exec("type curl", noSudo); r.isSuccess() { |
327
|
|
|
cmd := "curl --max-time 1 --noproxy 169.254.169.254 http://169.254.169.254/latest/meta-data/instance-id" |
328
|
|
|
r := l.exec(cmd, noSudo) |
329
|
|
|
if r.isSuccess() { |
330
|
|
|
id := strings.TrimSpace(r.Stdout) |
331
|
|
|
if !l.isAwsInstanceID(id) { |
332
|
|
|
return false, "", nil |
333
|
|
|
} |
334
|
|
|
return true, id, nil |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
switch r.ExitStatus { |
338
|
|
|
case 28, 7: |
339
|
|
|
// Not running on AWS |
340
|
|
|
// 7 Failed to connect to host. |
341
|
|
|
// 28 operation timeout. |
342
|
|
|
return false, "", nil |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
if r := l.exec("type wget", noSudo); r.isSuccess() { |
347
|
|
|
cmd := "wget --tries=3 --timeout=1 --no-proxy -q -O - http://169.254.169.254/latest/meta-data/instance-id" |
348
|
|
|
r := l.exec(cmd, noSudo) |
349
|
|
|
if r.isSuccess() { |
350
|
|
|
id := strings.TrimSpace(r.Stdout) |
351
|
|
|
if !l.isAwsInstanceID(id) { |
352
|
|
|
return false, "", nil |
353
|
|
|
} |
354
|
|
|
return true, id, nil |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
switch r.ExitStatus { |
358
|
|
|
case 4, 8: |
359
|
|
|
// Not running on AWS |
360
|
|
|
// 4 Network failure |
361
|
|
|
// 8 Server issued an error response. |
362
|
|
|
return false, "", nil |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
return false, "", fmt.Errorf( |
366
|
|
|
"Failed to curl or wget to AWS instance metadata on %s. container: %s", |
367
|
|
|
l.ServerInfo.ServerName, l.ServerInfo.Container.Name) |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
// http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/resource-ids.html |
371
|
|
|
var awsInstanceIDPattern = regexp.MustCompile(`^i-[0-9a-f]+$`) |
372
|
|
|
|
373
|
|
|
func (l *base) isAwsInstanceID(str string) bool { |
374
|
|
|
return awsInstanceIDPattern.MatchString(str) |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
func (l *base) convertToModel() models.ScanResult { |
378
|
|
|
ctype := l.ServerInfo.ContainerType |
379
|
|
|
if l.ServerInfo.Container.ContainerID != "" && ctype == "" { |
380
|
|
|
ctype = "docker" |
381
|
|
|
} |
382
|
|
|
container := models.Container{ |
383
|
|
|
ContainerID: l.ServerInfo.Container.ContainerID, |
384
|
|
|
Name: l.ServerInfo.Container.Name, |
385
|
|
|
Image: l.ServerInfo.Container.Image, |
386
|
|
|
Type: ctype, |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
errs := []string{} |
390
|
|
|
for _, e := range l.errs { |
391
|
|
|
errs = append(errs, fmt.Sprintf("%s", e)) |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
return models.ScanResult{ |
395
|
|
|
JSONVersion: models.JSONVersion, |
396
|
|
|
ServerName: l.ServerInfo.ServerName, |
397
|
|
|
ScannedAt: time.Now(), |
398
|
|
|
ScanMode: l.ServerInfo.Mode.String(), |
399
|
|
|
Family: l.Distro.Family, |
400
|
|
|
Release: l.Distro.Release, |
401
|
|
|
Container: container, |
402
|
|
|
Platform: l.Platform, |
403
|
|
|
IPv4Addrs: l.ServerInfo.IPv4Addrs, |
404
|
|
|
IPv6Addrs: l.ServerInfo.IPv6Addrs, |
405
|
|
|
ScannedCves: l.VulnInfos, |
406
|
|
|
RunningKernel: l.Kernel, |
407
|
|
|
Packages: l.Packages, |
408
|
|
|
SrcPackages: l.SrcPackages, |
409
|
|
|
Optional: l.ServerInfo.Optional, |
410
|
|
|
Errors: errs, |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
func (l *base) setErrs(errs []error) { |
415
|
|
|
l.errs = errs |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
func (l *base) getErrs() []error { |
419
|
|
|
return l.errs |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
const ( |
423
|
|
|
systemd = "systemd" |
424
|
|
|
upstart = "upstart" |
425
|
|
|
sysVinit = "init" |
426
|
|
|
) |
427
|
|
|
|
428
|
|
|
// https://unix.stackexchange.com/questions/196166/how-to-find-out-if-a-system-uses-sysv-upstart-or-systemd-initsystem |
429
|
|
|
func (l *base) detectInitSystem() (string, error) { |
430
|
|
|
var f func(string) (string, error) |
431
|
|
|
f = func(cmd string) (string, error) { |
432
|
|
|
r := l.exec(cmd, sudo) |
433
|
|
|
if !r.isSuccess() { |
434
|
|
|
return "", fmt.Errorf("Failed to stat %s: %s", cmd, r) |
435
|
|
|
} |
436
|
|
|
scanner := bufio.NewScanner(strings.NewReader(r.Stdout)) |
437
|
|
|
scanner.Scan() |
438
|
|
|
line := strings.TrimSpace(scanner.Text()) |
439
|
|
|
if strings.Contains(line, "systemd") { |
440
|
|
|
return systemd, nil |
441
|
|
|
} else if strings.Contains(line, "upstart") { |
442
|
|
|
return upstart, nil |
443
|
|
|
} else if strings.Contains(line, "File: ‘/proc/1/exe’ -> ‘/sbin/init’") || |
444
|
|
|
strings.Contains(line, "File: `/proc/1/exe' -> `/sbin/init'") { |
445
|
|
|
return f("stat /sbin/init") |
446
|
|
|
} else if line == "File: ‘/sbin/init’" || |
447
|
|
|
line == "File: `/sbin/init'" { |
448
|
|
|
r := l.exec("/sbin/init --version", noSudo) |
449
|
|
|
if r.isSuccess() { |
450
|
|
|
if strings.Contains(r.Stdout, "upstart") { |
451
|
|
|
return upstart, nil |
452
|
|
|
} |
453
|
|
|
} |
454
|
|
|
return sysVinit, nil |
455
|
|
|
} |
456
|
|
|
return "", fmt.Errorf("Failed to detect a init system: %s", line) |
457
|
|
|
} |
458
|
|
|
return f("stat /proc/1/exe") |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
func (l *base) detectServiceName(pid string) (string, error) { |
462
|
|
|
cmd := fmt.Sprintf("systemctl status --quiet --no-pager %s", pid) |
463
|
|
|
r := l.exec(cmd, noSudo) |
464
|
|
|
if !r.isSuccess() { |
465
|
|
|
return "", fmt.Errorf("Failed to stat %s: %s", cmd, r) |
466
|
|
|
} |
467
|
|
|
return l.parseSystemctlStatus(r.Stdout), nil |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
func (l *base) parseSystemctlStatus(stdout string) string { |
471
|
|
|
scanner := bufio.NewScanner(strings.NewReader(stdout)) |
472
|
|
|
scanner.Scan() |
473
|
|
|
line := scanner.Text() |
474
|
|
|
ss := strings.Fields(line) |
475
|
|
|
if len(ss) < 2 || strings.HasPrefix(line, "Failed to get unit for PID") { |
476
|
|
|
return "" |
477
|
|
|
} |
478
|
|
|
return ss[1] |
479
|
|
|
} |
480
|
|
|
|