Completed
Pull Request — master (#805)
by kota
05:47
created

scan.*base.detectServiceName   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
dl 0
loc 7
rs 10
c 0
b 0
f 0
nop 1
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
	warns []error
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 "", "", xerrors.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 "", "", xerrors.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, xerrors.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, xerrors.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, xerrors.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 "", xerrors.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 "", xerrors.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 "", xerrors.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, xerrors.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, xerrors.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, xerrors.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, "", xerrors.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, warns := []string{}, []string{}
390
	for _, e := range l.errs {
391
		errs = append(errs, fmt.Sprintf("%+v", e))
392
	}
393
	for _, w := range l.warns {
394
		warns = append(warns, fmt.Sprintf("%+v", w))
395
	}
396
397
	return models.ScanResult{
398
		JSONVersion:       models.JSONVersion,
399
		ServerName:        l.ServerInfo.ServerName,
400
		ScannedAt:         time.Now(),
401
		ScanMode:          l.ServerInfo.Mode.String(),
402
		Family:            l.Distro.Family,
403
		Release:           l.Distro.Release,
404
		Container:         container,
405
		Platform:          l.Platform,
406
		IPv4Addrs:         l.ServerInfo.IPv4Addrs,
407
		IPv6Addrs:         l.ServerInfo.IPv6Addrs,
408
		ScannedCves:       l.VulnInfos,
409
		RunningKernel:     l.Kernel,
410
		Packages:          l.Packages,
411
		SrcPackages:       l.SrcPackages,
412
		WordPressPackages: l.WordPress,
413
		Optional:          l.ServerInfo.Optional,
414
		Errors:            errs,
415
		Warnings:          warns,
416
	}
417
}
418
419
func (l *base) setErrs(errs []error) {
420
	l.errs = errs
421
}
422
423
func (l *base) getErrs() []error {
424
	return l.errs
425
}
426
427
const (
428
	systemd  = "systemd"
429
	upstart  = "upstart"
430
	sysVinit = "init"
431
)
432
433
// https://unix.stackexchange.com/questions/196166/how-to-find-out-if-a-system-uses-sysv-upstart-or-systemd-initsystem
434
func (l *base) detectInitSystem() (string, error) {
435
	var f func(string) (string, error)
436
	f = func(cmd string) (string, error) {
437
		r := l.exec(cmd, sudo)
438
		if !r.isSuccess() {
439
			return "", xerrors.Errorf("Failed to stat %s: %s", cmd, r)
440
		}
441
		scanner := bufio.NewScanner(strings.NewReader(r.Stdout))
442
		scanner.Scan()
443
		line := strings.TrimSpace(scanner.Text())
444
		if strings.Contains(line, "systemd") {
445
			return systemd, nil
446
		} else if strings.Contains(line, "upstart") {
447
			return upstart, nil
448
		} else if strings.Contains(line, "File: ‘/proc/1/exe’ -> ‘/sbin/init’") ||
449
			strings.Contains(line, "File: `/proc/1/exe' -> `/sbin/init'") {
450
			return f("stat /sbin/init")
451
		} else if line == "File: ‘/sbin/init’" ||
452
			line == "File: `/sbin/init'" {
453
			r := l.exec("/sbin/init --version", noSudo)
454
			if r.isSuccess() {
455
				if strings.Contains(r.Stdout, "upstart") {
456
					return upstart, nil
457
				}
458
			}
459
			return sysVinit, nil
460
		}
461
		return "", xerrors.Errorf("Failed to detect a init system: %s", line)
462
	}
463
	return f("stat /proc/1/exe")
464
}
465
466
func (l *base) detectServiceName(pid string) (string, error) {
467
	cmd := fmt.Sprintf("systemctl status --quiet --no-pager %s", pid)
468
	r := l.exec(cmd, noSudo)
469
	if !r.isSuccess() {
470
		return "", xerrors.Errorf("Failed to stat %s: %s", cmd, r)
471
	}
472
	return l.parseSystemctlStatus(r.Stdout), nil
473
}
474
475
func (l *base) parseSystemctlStatus(stdout string) string {
476
	scanner := bufio.NewScanner(strings.NewReader(stdout))
477
	scanner.Scan()
478
	line := scanner.Text()
479
	ss := strings.Fields(line)
480
	if len(ss) < 2 || strings.HasPrefix(line, "Failed to get unit for PID") {
481
		return ""
482
	}
483
	return ss[1]
484
}
485
486
func (l *base) scanWordPress() (err error) {
487
	wpOpts := []string{l.ServerInfo.WordPress.OSUser,
488
		l.ServerInfo.WordPress.DocRoot,
489
		l.ServerInfo.WordPress.CmdPath,
490
		l.ServerInfo.WordPress.WPVulnDBToken,
491
	}
492
	var isScanWp, hasEmptyOpt bool
493
	for _, opt := range wpOpts {
494
		if opt != "" {
495
			isScanWp = true
496
			break
497
		} else {
498
			hasEmptyOpt = true
499
		}
500
	}
501
	if !isScanWp {
502
		return nil
503
	}
504
505
	if hasEmptyOpt {
506
		return xerrors.Errorf("%s has empty WordPress opts: %s",
507
			l.getServerInfo().GetServerName(), wpOpts)
508
	}
509
510
	cmd := fmt.Sprintf("sudo -u %s -i -- %s cli version",
511
		l.ServerInfo.WordPress.OSUser,
512
		l.ServerInfo.WordPress.CmdPath)
513
	if r := exec(l.ServerInfo, cmd, noSudo); !r.isSuccess() {
514
		l.ServerInfo.WordPress.WPVulnDBToken = "secret"
515
		return xerrors.Errorf("Failed to exec `%s`. Check the OS user, command path of wp-cli, DocRoot and permission: %#v", cmd, l.ServerInfo.WordPress)
516
	}
517
518
	wp, err := l.detectWordPress()
519
	if err != nil {
520
		return xerrors.Errorf("Failed to scan wordpress: %w", err)
0 ignored issues
show
introduced by
unrecognized printf verb 'w'
Loading history...
521
	}
522
	l.WordPress = wp
523
	return nil
524
}
525
526
func (l *base) detectWordPress() (*models.WordPressPackages, error) {
527
	ver, err := l.detectWpCore()
528
	if err != nil {
529
		return nil, err
530
	}
531
532
	themes, err := l.detectWpThemes()
533
	if err != nil {
534
		return nil, err
535
	}
536
537
	plugins, err := l.detectWpPlugins()
538
	if err != nil {
539
		return nil, err
540
	}
541
542
	pkgs := models.WordPressPackages{
543
		models.WpPackage{
544
			Name:    models.WPCore,
545
			Version: ver,
546
			Type:    models.WPCore,
547
		},
548
	}
549
	pkgs = append(pkgs, themes...)
550
	pkgs = append(pkgs, plugins...)
551
	return &pkgs, nil
552
}
553
554
func (l *base) detectWpCore() (string, error) {
555
	cmd := fmt.Sprintf("sudo -u %s -i -- %s core version --path=%s",
556
		l.ServerInfo.WordPress.OSUser,
557
		l.ServerInfo.WordPress.CmdPath,
558
		l.ServerInfo.WordPress.DocRoot)
559
560
	r := exec(l.ServerInfo, cmd, noSudo)
561
	if !r.isSuccess() {
562
		return "", xerrors.Errorf("Failed to get wp core version: %s", r)
563
	}
564
	return strings.TrimSpace(r.Stdout), nil
565
}
566
567
func (l *base) detectWpThemes() ([]models.WpPackage, error) {
568
	cmd := fmt.Sprintf("sudo -u %s -i -- %s theme list --path=%s --format=json",
569
		l.ServerInfo.WordPress.OSUser,
570
		l.ServerInfo.WordPress.CmdPath,
571
		l.ServerInfo.WordPress.DocRoot)
572
573
	var themes []models.WpPackage
574
	r := exec(l.ServerInfo, cmd, noSudo)
575
	if !r.isSuccess() {
576
		return nil, xerrors.Errorf("Failed to get a list of WordPress plugins: %s", r)
577
	}
578
	err := json.Unmarshal([]byte(r.Stdout), &themes)
579
	if err != nil {
580
		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...
581
	}
582
	for i := range themes {
583
		themes[i].Type = models.WPTheme
584
	}
585
	return themes, nil
586
}
587
588
func (l *base) detectWpPlugins() ([]models.WpPackage, error) {
589
	cmd := fmt.Sprintf("sudo -u %s -i -- %s plugin list --path=%s --format=json",
590
		l.ServerInfo.WordPress.OSUser,
591
		l.ServerInfo.WordPress.CmdPath,
592
		l.ServerInfo.WordPress.DocRoot)
593
594
	var plugins []models.WpPackage
595
	r := exec(l.ServerInfo, cmd, noSudo)
596
	if !r.isSuccess() {
597
		return nil, xerrors.Errorf("Failed to wp plugin list: %s", r)
598
	}
599
	if err := json.Unmarshal([]byte(r.Stdout), &plugins); err != nil {
600
		return nil, err
601
	}
602
	for i := range plugins {
603
		plugins[i].Type = models.WPPlugin
604
	}
605
	return plugins, nil
606
}
607