Passed
Push — master ( 824fbb...53aaea )
by kota
05:24
created

scan.*base.convertToModel   B

Complexity

Conditions 6

Size

Total Lines 43
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 36
dl 0
loc 43
rs 8.0826
c 0
b 0
f 0
nop 0
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
	"encoding/json"
23
	"fmt"
24
	"net"
25
	"regexp"
26
	"strings"
27
	"time"
28
29
	"github.com/future-architect/vuls/config"
30
	"github.com/future-architect/vuls/models"
31
	"github.com/sirupsen/logrus"
32
	"golang.org/x/xerrors"
33
)
34
35
type base struct {
36
	ServerInfo config.ServerInfo
37
	Distro     config.Distro
38
	Platform   models.Platform
39
	osPackages
40
	WordPress *models.WordPressPackages
41
42
	log  *logrus.Entry
43
	errs []error
44
}
45
46
func (l *base) exec(cmd string, sudo bool) execResult {
47
	return exec(l.ServerInfo, cmd, sudo, l.log)
48
}
49
50
func (l *base) setServerInfo(c config.ServerInfo) {
51
	l.ServerInfo = c
52
}
53
54
func (l *base) getServerInfo() config.ServerInfo {
55
	return l.ServerInfo
56
}
57
58
func (l *base) setDistro(fam, rel string) {
59
	d := config.Distro{
60
		Family:  fam,
61
		Release: rel,
62
	}
63
	l.Distro = d
64
65
	s := l.getServerInfo()
66
	s.Distro = d
67
	l.setServerInfo(s)
68
}
69
70
func (l *base) getDistro() config.Distro {
71
	return l.Distro
72
}
73
74
func (l *base) setPlatform(p models.Platform) {
75
	l.Platform = p
76
}
77
78
func (l *base) getPlatform() models.Platform {
79
	return l.Platform
80
}
81
82
func (l *base) runningKernel() (release, version string, err error) {
83
	r := l.exec("uname -r", noSudo)
84
	if !r.isSuccess() {
85
		return "", "", xerrors.Errorf("Failed to SSH: %s", r)
86
	}
87
	release = strings.TrimSpace(r.Stdout)
88
89
	switch l.Distro.Family {
90
	case config.Debian:
91
		r := l.exec("uname -a", noSudo)
92
		if !r.isSuccess() {
93
			return "", "", xerrors.Errorf("Failed to SSH: %s", r)
94
		}
95
		ss := strings.Fields(r.Stdout)
96
		if 6 < len(ss) {
97
			version = ss[6]
98
		}
99
	}
100
	return
101
}
102
103
func (l *base) allContainers() (containers []config.Container, err error) {
104
	switch l.ServerInfo.ContainerType {
105
	case "", "docker":
106
		stdout, err := l.dockerPs("-a --format '{{.ID}} {{.Names}} {{.Image}}'")
107
		if err != nil {
108
			return containers, err
109
		}
110
		return l.parseDockerPs(stdout)
111
	case "lxd":
112
		stdout, err := l.lxdPs("-c n")
113
		if err != nil {
114
			return containers, err
115
		}
116
		return l.parseLxdPs(stdout)
117
	case "lxc":
118
		stdout, err := l.lxcPs("-1")
119
		if err != nil {
120
			return containers, err
121
		}
122
		return l.parseLxcPs(stdout)
123
	default:
124
		return containers, xerrors.Errorf(
125
			"Not supported yet: %s", l.ServerInfo.ContainerType)
126
	}
127
}
128
129
func (l *base) runningContainers() (containers []config.Container, err error) {
130
	switch l.ServerInfo.ContainerType {
131
	case "", "docker":
132
		stdout, err := l.dockerPs("--format '{{.ID}} {{.Names}} {{.Image}}'")
133
		if err != nil {
134
			return containers, err
135
		}
136
		return l.parseDockerPs(stdout)
137
	case "lxd":
138
		stdout, err := l.lxdPs("volatile.last_state.power=RUNNING -c n")
139
		if err != nil {
140
			return containers, err
141
		}
142
		return l.parseLxdPs(stdout)
143
	case "lxc":
144
		stdout, err := l.lxcPs("-1 --running")
145
		if err != nil {
146
			return containers, err
147
		}
148
		return l.parseLxcPs(stdout)
149
	default:
150
		return containers, xerrors.Errorf(
151
			"Not supported yet: %s", l.ServerInfo.ContainerType)
152
	}
153
}
154
155
func (l *base) exitedContainers() (containers []config.Container, err error) {
156
	switch l.ServerInfo.ContainerType {
157
	case "", "docker":
158
		stdout, err := l.dockerPs("--filter 'status=exited' --format '{{.ID}} {{.Names}} {{.Image}}'")
159
		if err != nil {
160
			return containers, err
161
		}
162
		return l.parseDockerPs(stdout)
163
	case "lxd":
164
		stdout, err := l.lxdPs("volatile.last_state.power=STOPPED -c n")
165
		if err != nil {
166
			return containers, err
167
		}
168
		return l.parseLxdPs(stdout)
169
	case "lxc":
170
		stdout, err := l.lxcPs("-1 --stopped")
171
		if err != nil {
172
			return containers, err
173
		}
174
		return l.parseLxcPs(stdout)
175
	default:
176
		return containers, xerrors.Errorf(
177
			"Not supported yet: %s", l.ServerInfo.ContainerType)
178
	}
179
}
180
181
func (l *base) dockerPs(option string) (string, error) {
182
	cmd := fmt.Sprintf("docker ps %s", option)
183
	r := l.exec(cmd, noSudo)
184
	if !r.isSuccess() {
185
		return "", xerrors.Errorf("Failed to SSH: %s", r)
186
	}
187
	return r.Stdout, nil
188
}
189
190
func (l *base) lxdPs(option string) (string, error) {
191
	cmd := fmt.Sprintf("lxc list %s", option)
192
	r := l.exec(cmd, noSudo)
193
	if !r.isSuccess() {
194
		return "", xerrors.Errorf("failed to SSH: %s", r)
195
	}
196
	return r.Stdout, nil
197
}
198
199
func (l *base) lxcPs(option string) (string, error) {
200
	cmd := fmt.Sprintf("lxc-ls %s 2>/dev/null", option)
201
	r := l.exec(cmd, sudo)
202
	if !r.isSuccess() {
203
		return "", xerrors.Errorf("failed to SSH: %s", r)
204
	}
205
	return r.Stdout, nil
206
}
207
208
func (l *base) parseDockerPs(stdout string) (containers []config.Container, err error) {
209
	lines := strings.Split(stdout, "\n")
210
	for _, line := range lines {
211
		fields := strings.Fields(line)
212
		if len(fields) == 0 {
213
			break
214
		}
215
		if len(fields) != 3 {
216
			return containers, xerrors.Errorf("Unknown format: %s", line)
217
		}
218
		containers = append(containers, config.Container{
219
			ContainerID: fields[0],
220
			Name:        fields[1],
221
			Image:       fields[2],
222
		})
223
	}
224
	return
225
}
226
227
func (l *base) parseLxdPs(stdout string) (containers []config.Container, err error) {
228
	lines := strings.Split(stdout, "\n")
229
	for i, line := range lines[3:] {
230
		if i%2 == 1 {
231
			continue
232
		}
233
		fields := strings.Fields(strings.Replace(line, "|", " ", -1))
234
		if len(fields) == 0 {
235
			break
236
		}
237
		if len(fields) != 1 {
238
			return containers, xerrors.Errorf("Unknown format: %s", line)
239
		}
240
		containers = append(containers, config.Container{
241
			ContainerID: fields[0],
242
			Name:        fields[0],
243
		})
244
	}
245
	return
246
}
247
248
func (l *base) parseLxcPs(stdout string) (containers []config.Container, err error) {
249
	lines := strings.Split(stdout, "\n")
250
	for _, line := range lines {
251
		fields := strings.Fields(line)
252
		if len(fields) == 0 {
253
			break
254
		}
255
		containers = append(containers, config.Container{
256
			ContainerID: fields[0],
257
			Name:        fields[0],
258
		})
259
	}
260
	return
261
}
262
263
// ip executes ip command and returns IP addresses
264
func (l *base) ip() ([]string, []string, error) {
265
	// e.g.
266
	// 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
267
	// 2: eth0    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
268
	// 2: eth0    inet6 fe80::5054:ff:fe2a:864c/64 scope link \       valid_lft forever preferred_lft forever
269
	r := l.exec("/sbin/ip -o addr", noSudo)
270
	if !r.isSuccess() {
271
		return nil, nil, xerrors.Errorf("Failed to detect IP address: %v", r)
272
	}
273
	ipv4Addrs, ipv6Addrs := l.parseIP(r.Stdout)
274
	return ipv4Addrs, ipv6Addrs, nil
275
}
276
277
// parseIP parses the results of ip command
278
func (l *base) parseIP(stdout string) (ipv4Addrs []string, ipv6Addrs []string) {
279
	lines := strings.Split(stdout, "\n")
280
	for _, line := range lines {
281
		fields := strings.Fields(line)
282
		if len(fields) < 4 {
283
			continue
284
		}
285
		ip, _, err := net.ParseCIDR(fields[3])
286
		if err != nil {
287
			continue
288
		}
289
		if !ip.IsGlobalUnicast() {
290
			continue
291
		}
292
		if ipv4 := ip.To4(); ipv4 != nil {
293
			ipv4Addrs = append(ipv4Addrs, ipv4.String())
294
		} else {
295
			ipv6Addrs = append(ipv6Addrs, ip.String())
296
		}
297
	}
298
	return
299
}
300
301
func (l *base) detectPlatform() {
302
	if l.getServerInfo().Mode.IsOffline() {
303
		l.setPlatform(models.Platform{Name: "unknown"})
304
		return
305
	}
306
	ok, instanceID, err := l.detectRunningOnAws()
307
	if err != nil {
308
		l.setPlatform(models.Platform{Name: "other"})
309
		return
310
	}
311
	if ok {
312
		l.setPlatform(models.Platform{
313
			Name:       "aws",
314
			InstanceID: instanceID,
315
		})
316
		return
317
	}
318
319
	//TODO Azure, GCP...
320
	l.setPlatform(models.Platform{Name: "other"})
321
	return
322
}
323
324
func (l *base) detectRunningOnAws() (ok bool, instanceID string, err error) {
325
	if r := l.exec("type curl", noSudo); r.isSuccess() {
326
		cmd := "curl --max-time 1 --noproxy 169.254.169.254 http://169.254.169.254/latest/meta-data/instance-id"
327
		r := l.exec(cmd, noSudo)
328
		if r.isSuccess() {
329
			id := strings.TrimSpace(r.Stdout)
330
			if !l.isAwsInstanceID(id) {
331
				return false, "", nil
332
			}
333
			return true, id, nil
334
		}
335
336
		switch r.ExitStatus {
337
		case 28, 7:
338
			// Not running on AWS
339
			//  7   Failed to connect to host.
340
			// 28  operation timeout.
341
			return false, "", nil
342
		}
343
	}
344
345
	if r := l.exec("type wget", noSudo); r.isSuccess() {
346
		cmd := "wget --tries=3 --timeout=1 --no-proxy -q -O - http://169.254.169.254/latest/meta-data/instance-id"
347
		r := l.exec(cmd, noSudo)
348
		if r.isSuccess() {
349
			id := strings.TrimSpace(r.Stdout)
350
			if !l.isAwsInstanceID(id) {
351
				return false, "", nil
352
			}
353
			return true, id, nil
354
		}
355
356
		switch r.ExitStatus {
357
		case 4, 8:
358
			// Not running on AWS
359
			// 4   Network failure
360
			// 8   Server issued an error response.
361
			return false, "", nil
362
		}
363
	}
364
	return false, "", xerrors.Errorf(
365
		"Failed to curl or wget to AWS instance metadata on %s. container: %s",
366
		l.ServerInfo.ServerName, l.ServerInfo.Container.Name)
367
}
368
369
// http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/resource-ids.html
370
var awsInstanceIDPattern = regexp.MustCompile(`^i-[0-9a-f]+$`)
371
372
func (l *base) isAwsInstanceID(str string) bool {
373
	return awsInstanceIDPattern.MatchString(str)
374
}
375
376
func (l *base) convertToModel() models.ScanResult {
377
	ctype := l.ServerInfo.ContainerType
378
	if l.ServerInfo.Container.ContainerID != "" && ctype == "" {
379
		ctype = "docker"
380
	}
381
	container := models.Container{
382
		ContainerID: l.ServerInfo.Container.ContainerID,
383
		Name:        l.ServerInfo.Container.Name,
384
		Image:       l.ServerInfo.Container.Image,
385
		Type:        ctype,
386
	}
387
388
	errs := []string{}
389
	for _, e := range l.errs {
390
		errs = append(errs, fmt.Sprintf("%s", e))
391
	}
392
393
	scannedVia := scannedViaRemote
394
	if isLocalExec(l.ServerInfo.Port, l.ServerInfo.Host) {
395
		scannedVia = scannedViaLocal
396
	} else if l.ServerInfo.Type == config.ServerTypePseudo {
397
		scannedVia = scannedViaPseudo
398
	}
399
400
	return models.ScanResult{
401
		JSONVersion:       models.JSONVersion,
402
		ServerName:        l.ServerInfo.ServerName,
403
		ScannedAt:         time.Now(),
404
		ScanMode:          l.ServerInfo.Mode.String(),
405
		Family:            l.Distro.Family,
406
		Release:           l.Distro.Release,
407
		Container:         container,
408
		Platform:          l.Platform,
409
		IPv4Addrs:         l.ServerInfo.IPv4Addrs,
410
		IPv6Addrs:         l.ServerInfo.IPv6Addrs,
411
		ScannedCves:       l.VulnInfos,
412
		ScannedVia:        scannedVia,
413
		RunningKernel:     l.Kernel,
414
		Packages:          l.Packages,
415
		SrcPackages:       l.SrcPackages,
416
		WordPressPackages: l.WordPress,
417
		Optional:          l.ServerInfo.Optional,
418
		Errors:            errs,
419
	}
420
}
421
422
func (l *base) setErrs(errs []error) {
423
	l.errs = errs
424
}
425
426
func (l *base) getErrs() []error {
427
	return l.errs
428
}
429
430
const (
431
	systemd  = "systemd"
432
	upstart  = "upstart"
433
	sysVinit = "init"
434
)
435
436
// https://unix.stackexchange.com/questions/196166/how-to-find-out-if-a-system-uses-sysv-upstart-or-systemd-initsystem
437
func (l *base) detectInitSystem() (string, error) {
438
	var f func(string) (string, error)
439
	f = func(cmd string) (string, error) {
440
		r := l.exec(cmd, sudo)
441
		if !r.isSuccess() {
442
			return "", xerrors.Errorf("Failed to stat %s: %s", cmd, r)
443
		}
444
		scanner := bufio.NewScanner(strings.NewReader(r.Stdout))
445
		scanner.Scan()
446
		line := strings.TrimSpace(scanner.Text())
447
		if strings.Contains(line, "systemd") {
448
			return systemd, nil
449
		} else if strings.Contains(line, "upstart") {
450
			return upstart, nil
451
		} else if strings.Contains(line, "File: ‘/proc/1/exe’ -> ‘/sbin/init’") ||
452
			strings.Contains(line, "File: `/proc/1/exe' -> `/sbin/init'") {
453
			return f("stat /sbin/init")
454
		} else if line == "File: ‘/sbin/init’" ||
455
			line == "File: `/sbin/init'" {
456
			r := l.exec("/sbin/init --version", noSudo)
457
			if r.isSuccess() {
458
				if strings.Contains(r.Stdout, "upstart") {
459
					return upstart, nil
460
				}
461
			}
462
			return sysVinit, nil
463
		}
464
		return "", xerrors.Errorf("Failed to detect a init system: %s", line)
465
	}
466
	return f("stat /proc/1/exe")
467
}
468
469
func (l *base) detectServiceName(pid string) (string, error) {
470
	cmd := fmt.Sprintf("systemctl status --quiet --no-pager %s", pid)
471
	r := l.exec(cmd, noSudo)
472
	if !r.isSuccess() {
473
		return "", xerrors.Errorf("Failed to stat %s: %s", cmd, r)
474
	}
475
	return l.parseSystemctlStatus(r.Stdout), nil
476
}
477
478
func (l *base) parseSystemctlStatus(stdout string) string {
479
	scanner := bufio.NewScanner(strings.NewReader(stdout))
480
	scanner.Scan()
481
	line := scanner.Text()
482
	ss := strings.Fields(line)
483
	if len(ss) < 2 || strings.HasPrefix(line, "Failed to get unit for PID") {
484
		return ""
485
	}
486
	return ss[1]
487
}
488
489
func (l *base) scanWordPress() (err error) {
490
	wpOpts := []string{l.ServerInfo.WordPress.OSUser,
491
		l.ServerInfo.WordPress.DocRoot,
492
		l.ServerInfo.WordPress.CmdPath,
493
		l.ServerInfo.WordPress.WPVulnDBToken,
494
	}
495
	var isScanWp, hasEmptyOpt bool
496
	for _, opt := range wpOpts {
497
		if opt != "" {
498
			isScanWp = true
499
			break
500
		} else {
501
			hasEmptyOpt = true
502
		}
503
	}
504
	if !isScanWp {
505
		return nil
506
	}
507
508
	if hasEmptyOpt {
509
		return xerrors.Errorf("%s has empty WordPress opts: %s",
510
			l.getServerInfo().GetServerName(), wpOpts)
511
	}
512
513
	cmd := fmt.Sprintf("sudo -u %s -i -- %s cli version",
514
		l.ServerInfo.WordPress.OSUser,
515
		l.ServerInfo.WordPress.CmdPath)
516
	if r := exec(l.ServerInfo, cmd, noSudo); !r.isSuccess() {
517
		l.ServerInfo.WordPress.WPVulnDBToken = "secret"
518
		return xerrors.Errorf("Failed to exec `%s`. Check the OS user, command path of wp-cli, DocRoot and permission: %#v", cmd, l.ServerInfo.WordPress)
519
	}
520
521
	wp, err := l.detectWordPress()
522
	if err != nil {
523
		return xerrors.Errorf("Failed to scan wordpress: %w", err)
0 ignored issues
show
introduced by
unrecognized printf verb 'w'
Loading history...
524
	}
525
	l.WordPress = wp
526
	return nil
527
}
528
529
func (l *base) detectWordPress() (*models.WordPressPackages, error) {
530
	ver, err := l.detectWpCore()
531
	if err != nil {
532
		return nil, err
533
	}
534
535
	themes, err := l.detectWpThemes()
536
	if err != nil {
537
		return nil, err
538
	}
539
540
	plugins, err := l.detectWpPlugins()
541
	if err != nil {
542
		return nil, err
543
	}
544
545
	pkgs := models.WordPressPackages{
546
		models.WpPackage{
547
			Name:    models.WPCore,
548
			Version: ver,
549
			Type:    models.WPCore,
550
		},
551
	}
552
	pkgs = append(pkgs, themes...)
553
	pkgs = append(pkgs, plugins...)
554
	return &pkgs, nil
555
}
556
557
func (l *base) detectWpCore() (string, error) {
558
	cmd := fmt.Sprintf("sudo -u %s -i -- %s core version --path=%s",
559
		l.ServerInfo.WordPress.OSUser,
560
		l.ServerInfo.WordPress.CmdPath,
561
		l.ServerInfo.WordPress.DocRoot)
562
563
	r := exec(l.ServerInfo, cmd, noSudo)
564
	if !r.isSuccess() {
565
		return "", xerrors.Errorf("Failed to get wp core version: %s", r)
566
	}
567
	return strings.TrimSpace(r.Stdout), nil
568
}
569
570
func (l *base) detectWpThemes() ([]models.WpPackage, error) {
571
	cmd := fmt.Sprintf("sudo -u %s -i -- %s theme list --path=%s --format=json",
572
		l.ServerInfo.WordPress.OSUser,
573
		l.ServerInfo.WordPress.CmdPath,
574
		l.ServerInfo.WordPress.DocRoot)
575
576
	var themes []models.WpPackage
577
	r := exec(l.ServerInfo, cmd, noSudo)
578
	if !r.isSuccess() {
579
		return nil, xerrors.Errorf("Failed to get a list of WordPress plugins: %s", r)
580
	}
581
	err := json.Unmarshal([]byte(r.Stdout), &themes)
582
	if err != nil {
583
		return nil, xerrors.Errorf("Failed to unmarshal wp theme list: %w", cmd, err)
0 ignored issues
show
introduced by
unrecognized printf verb 'w'
Loading history...
584
	}
585
	for i := range themes {
586
		themes[i].Type = models.WPTheme
587
	}
588
	return themes, nil
589
}
590
591
func (l *base) detectWpPlugins() ([]models.WpPackage, error) {
592
	cmd := fmt.Sprintf("sudo -u %s -i -- %s plugin list --path=%s --format=json",
593
		l.ServerInfo.WordPress.OSUser,
594
		l.ServerInfo.WordPress.CmdPath,
595
		l.ServerInfo.WordPress.DocRoot)
596
597
	var plugins []models.WpPackage
598
	r := exec(l.ServerInfo, cmd, noSudo)
599
	if !r.isSuccess() {
600
		return nil, xerrors.Errorf("Failed to wp plugin list: %s", r)
601
	}
602
	if err := json.Unmarshal([]byte(r.Stdout), &plugins); err != nil {
603
		return nil, err
604
	}
605
	for i := range plugins {
606
		plugins[i].Type = models.WPPlugin
607
	}
608
	return plugins, nil
609
}
610