Test Failed
Pull Request — master (#769)
by
unknown
11:07
created

scan.contentConvertVinfos   F

Complexity

Conditions 19

Size

Total Lines 72
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 46
dl 0
loc 72
rs 0.5999
c 0
b 0
f 0
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like scan.contentConvertVinfos often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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