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