Passed
Pull Request — master (#769)
by
unknown
06:18
created

scan.*base.isAwsInstanceID   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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