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

scan.isRunningKernel   B

Complexity

Conditions 6

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
dl 0
loc 23
rs 8.6166
c 0
b 0
f 0
nop 3
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
	"fmt"
22
	"strings"
23
24
	"github.com/future-architect/vuls/config"
25
	"github.com/future-architect/vuls/models"
26
	"github.com/future-architect/vuls/util"
27
	"github.com/k0kubun/pp"
28
	"time"
29
)
30
31
func Wpscan() (err error) {
0 ignored issues
show
introduced by
exported function Wpscan should have comment or be unexported
Loading history...
32
	if err = wpscanVuln(); err != nil {
33
		return err
34
	}
35
	return
36
}
37
38
func wpscanVuln() (err error) {
39
	errChan := make(chan error, len(config.Conf.Servers))
40
	defer close(errChan)
41
	for _, s := range config.Conf.Servers {
42
		go func(s config.ServerInfo) {
43
			defer func() {
44
				if p := recover(); p != nil {
45
					util.Log.Debugf("Panic: %s on %s", p, s.ServerName)
46
				}
47
			}()
48
			errChan <- detectWp(s)
49
		}(s)
50
	}
51
52
	timeout := time.After(time.Duration(10000) * time.Second)
53
	for i := 0; i < len(config.Conf.Servers); i++ {
54
		select {
55
		case _ = <-errChan:
56
		/*
57
			if 0 < len(res.getErrs()) {
58
				errServers = append(errServers, res)
59
				util.Log.Errorf("(%d/%d) Failed: %s, err: %s",
60
					i+1, len(config.Conf.Servers),
61
					res.getServerInfo().ServerName,
62
					res.getErrs())
63
			} else {
64
				servers = append(servers, res)
65
				util.Log.Infof("(%d/%d) Detected: %s: %s",
66
					i+1, len(config.Conf.Servers),
67
					res.getServerInfo().ServerName,
68
					res.getDistro())
69
			}
70
			*/
71
		case <-timeout:
72
			msg := "Timed out while detecting servers"
73
			util.Log.Error(msg)
74
			for servername, sInfo := range config.Conf.Servers {
75
				found := false
76
				for _, o := range append(servers, errServers...) {
77
					if servername == o.getServerInfo().ServerName {
78
						found = true
79
						break
80
					}
81
				}
82
				if !found {
83
					u := &unknown{}
84
					u.setServerInfo(sInfo)
85
					u.setErrs([]error{
86
						fmt.Errorf("Timed out"),
87
					})
88
					errServers = append(errServers, u)
89
					util.Log.Errorf("(%d/%d) Timed out: %s",
90
						i+1, len(config.Conf.Servers),
91
						servername)
92
					i++
93
				}
94
			}
95
		}
96
	}
97
98
99
	return
100
}
101
102
func detectWp(c config.ServerInfo) (err error) {
103
	if len(c.WordpressPath) != 0 {
104
		pp.Print("1")
105
	}
106
	cmd := fmt.Sprintf("wp core version --path=%s", c.WordpressToken)
107
	pp.Print(cmd)
108
	if r := exec(c, cmd, noSudo); r.isSuccess() {
109
		pp.Print(r.Stdout)
110
	}
111
	return err
112
}
113
114
func isRunningKernel(pack models.Package, family string, kernel models.Kernel) (isKernel, running bool) {
115
	switch family {
116
	case config.SUSEEnterpriseServer:
117
		if pack.Name == "kernel-default" {
118
			// Remove the last period and later because uname don't show that.
119
			ss := strings.Split(pack.Release, ".")
120
			rel := strings.Join(ss[0:len(ss)-1], ".")
121
			ver := fmt.Sprintf("%s-%s-default", pack.Version, rel)
122
			return true, kernel.Release == ver
123
		}
124
		return false, false
125
126
	case config.RedHat, config.Oracle, config.CentOS, config.Amazon:
127
		if pack.Name == "kernel" {
128
			ver := fmt.Sprintf("%s-%s.%s", pack.Version, pack.Release, pack.Arch)
129
			return true, kernel.Release == ver
130
		}
131
		return false, false
132
133
	default:
134
		util.Log.Warnf("Reboot required is not implemented yet: %s, %v", family, kernel)
135
	}
136
	return false, false
137
}
138
139
func rpmQa(distro config.Distro) string {
140
	const old = "rpm -qa --queryformat \"%{NAME} %{EPOCH} %{VERSION} %{RELEASE} %{ARCH}\n\""
141
	const new = "rpm -qa --queryformat \"%{NAME} %{EPOCHNUM} %{VERSION} %{RELEASE} %{ARCH}\n\""
142
	switch distro.Family {
143
	case config.SUSEEnterpriseServer:
144
		if v, _ := distro.MajorVersion(); v < 12 {
145
			return old
146
		}
147
		return new
148
	default:
149
		if v, _ := distro.MajorVersion(); v < 6 {
150
			return old
151
		}
152
		return new
153
	}
154
}
155