Completed
Pull Request — master (#769)
by
unknown
06:32
created

scan.*base.detectRunningOnAws   C

Complexity

Conditions 9

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

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