* Change config.toml, Auto-generate UUIDs, change structure of optional field * Detect processes affected by update using yum-ps (#482) Detect processes affected by update using yum-ps * Detect processes needs restart using checkrestart on Debian and Ubuntu. * pass cpename by args when calling FillCveInfo (#513) * fix new db (#502) * Include Version,Revision in JSON * Include hostname in JSON * Update goval-dictionary's commit hash in Gopkg.lock * Remove README.ja.md * update packages (#596) * fix: change ControlPath to .vuls of SSH option (#618) * feat: checkrestart for Ubuntu and Debian (#622) * feat: checkrestart for Ubuntu and Debian * fix: dependencies check logic of configtest * feat: need-restarting on RedHat * refactor: Process.ProcName to Process.Name * feat: detect a systemd service name of need-restarting-process * feat: detect a systemd service name of need-restarting-process on Ubuntu * feat: fill a service name of need-restarting-process, init-system * Support NVD JSON and CVSS3 of JVN (#605) * fix: compile errors * fix: Show CVSS3 on TUI * fix: test cases * fix: Avoid null in JSON * Fix maxCvssScore (#621) * Fix maxCvssScore * Update vulninfos.go * fix(init): remove unnecessary log initialization * refactor(nvd): use only json feed if exists json data. if not, use xml feed * fix(scan): make Confidence slice * feat(CWE): Display CWE name to TUI * feat(cwe): import CWE defs in Japanese * feat(cwe): add OWASP Top 10 ranking to CWE if applicable * feat(scan): add -fast-root mode, implement scan/amazon.go * refactor(const): change const name JVN to Jvn * feat(scan): add -fast-root mode, implement scan/centos.go * refactor(dep): update deps * fix(amazon): deps check * feat(scan): add -fast-root mode, implement scan/rhel.go * feat(scan): add -fast-root mode, implement scan/oracle.go * fix complile err * feat(scan): add -fast-root mode, implement scan/debian.go * fix testcase * fix(amazon): scan using yum * fix(configtest): change error message, status when no scannnable servers * Fix(scan): detect init process logic * fix(tui): display cvss as table format * fix(scan): parse a output of reboot-notifier on CentOS6.9 * fix(tui): don't display score, vector when score is zero * fix(scan): add -offline mode to suse scanner * fix(scan): fix help message * feat(scan): enable to define scan mode for each servers in config.toml #510 * refactor(config): chagne cpeNames to cpeURIs * refactor(config): change dependencyCheckXMLPath to owaspDCXMLPath * fix(config): containers -> containersIncluded, Excluded, containerType * feature(report): enable to define cpeURIs for each contaner * feature(report): enable to specify owasp dc xml path for each container * fix(discover): fix a template displayed at the end of discover * feature(report): add ignorePkgsRegexp #665 * feature(report): enable to define ignoreCves for each container #666 * fix(report): Displayed nothing in TUI detail area when CweID is nil * Gopkg.toml diet * feat(server): support server mode (#678) * feat(server): support server mode * Lock go version * Use the latest kernel release among the installed release when the running kernel release is unknown * Add TestViaHTTP * Set logger to go-cve-dictionary client * Add -to-localfile * Add -to-http option to report * Load -to-http conf from config.toml * Support gost (#676) * feat(gost): Support RedHat API * feat(gost): Support Debian Security Tracker * feat(db): display error msg when SQLite3 is locked at the beginning of reporting. * feat(gost): TUI * Only use RedHat information of installed packages * feat(tui): show mitigation on TUI * feat(gost): support redis backend * fix test case * fix nil pointer when db is nil * fix(gost): detect vulns of src packages for Debian * feat(gost): implement redis backend for gost redhat api * feat(report): display fixState of unfixed pkgs * fix(report): display distincted cweIDs * feat(slack): display gost info * feat(slack): display mitigation * feat(report): display available patch state as fixed/total * fix(tui): display - if source of reference is empty * update deps * fix(report): key in ScanResult JSON be lowerCamelcase. * some keys to lower camel * fix(configtest): dep check logic of yum-plugin-ps * fix(tui): format * feat(report): add -format-list option * fix(report): -format-full-text * fix(report): report -format-full-text * fix(report): display v3 score detected by gost * fix(scan): scan in fast mode if not defined in config.toml * fix(gost): fetch RedHat data for fixed CVEs * feat(report): show number of cves detected in each database * fix(report): show new version as `Unknown` in offline and fast scan mode * fix(report): fix num of upadtable and fixed * fix(report): set `Not fixed yet` if packageStatus is empty * refact(gost): make convertToModel public * fix(test): fix test case * update deps * fix(report): include gost score in MaxCvssScore * [WIP] feat(config): enable to set options in config.toml instead of cmd opt (#690) * feat(config): enable to set options in config.toml instead of cmd opt * fix(config): change Conf.Report.Slack to Conf.Slack * fix(discover): change tempalte * fix(report): fix config.toml auto-generate with -uuid * Add endpoint for health check and change endpoint * refact(cmd): refactor flag set * fix(report): enable to specify opts with cmd arg and env value * fix(scan): enable to parse the release version of amazon linux 2 * add(report) add -to-saas option (#695) * add(report) add -to-saas option * ignore other writer if -to-saas * fix(saas) fix bug * fix(scan): need-restarting needs internet connection * fix(scan,configtest): check scan mode * refactor(scan): change func name * fix(suse): support offline mode, bug fix on AWS, zypper --no-color * fix(tui): fix nil pointer when no vulns in tui * feat(report): enable to define CPE FS format in config.toml * fix(vet): fix warnings of go vet * fix(travis): go version to 1.11 * update deps
626 lines
16 KiB
Go
626 lines
16 KiB
Go
/* Vuls - Vulnerability Scanner
|
|
Copyright (C) 2016 Future Corporation , Japan.
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package scan
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/future-architect/vuls/cache"
|
|
"github.com/future-architect/vuls/config"
|
|
"github.com/future-architect/vuls/models"
|
|
"github.com/future-architect/vuls/report"
|
|
"github.com/future-architect/vuls/util"
|
|
)
|
|
|
|
var (
|
|
errOSFamilyHeader = errors.New("X-Vuls-OS-Family header is required")
|
|
errOSReleaseHeader = errors.New("X-Vuls-OS-Release header is required")
|
|
errKernelVersionHeader = errors.New("X-Vuls-Kernel-Version header is required")
|
|
errServerNameHeader = errors.New("X-Vuls-Server-Name header is required")
|
|
)
|
|
|
|
var servers, errServers []osTypeInterface
|
|
|
|
// Base Interface of redhat, debian, freebsd
|
|
type osTypeInterface interface {
|
|
setServerInfo(config.ServerInfo)
|
|
getServerInfo() config.ServerInfo
|
|
setDistro(string, string)
|
|
getDistro() config.Distro
|
|
detectPlatform()
|
|
getPlatform() models.Platform
|
|
|
|
checkScanMode() error
|
|
checkDeps() error
|
|
checkIfSudoNoPasswd() error
|
|
|
|
preCure() error
|
|
postScan() error
|
|
scanPackages() error
|
|
convertToModel() models.ScanResult
|
|
|
|
parseInstalledPackages(string) (models.Packages, models.SrcPackages, error)
|
|
|
|
runningContainers() ([]config.Container, error)
|
|
exitedContainers() ([]config.Container, error)
|
|
allContainers() ([]config.Container, error)
|
|
|
|
getErrs() []error
|
|
setErrs([]error)
|
|
}
|
|
|
|
// osPackages is included by base struct
|
|
type osPackages struct {
|
|
// installed packages
|
|
Packages models.Packages
|
|
|
|
// installed source packages (Debian based only)
|
|
SrcPackages models.SrcPackages
|
|
|
|
// unsecure packages
|
|
VulnInfos models.VulnInfos
|
|
|
|
// kernel information
|
|
Kernel models.Kernel
|
|
}
|
|
|
|
func detectOS(c config.ServerInfo) (osType osTypeInterface) {
|
|
var itsMe bool
|
|
var fatalErr error
|
|
|
|
if itsMe, osType, _ = detectPseudo(c); itsMe {
|
|
util.Log.Debugf("Pseudo")
|
|
return
|
|
}
|
|
|
|
itsMe, osType, fatalErr = detectDebian(c)
|
|
if fatalErr != nil {
|
|
osType.setErrs([]error{
|
|
fmt.Errorf("Failed to detect OS: %s", fatalErr)})
|
|
return
|
|
}
|
|
|
|
if itsMe {
|
|
util.Log.Debugf("Debian like Linux. Host: %s:%s", c.Host, c.Port)
|
|
return
|
|
}
|
|
|
|
if itsMe, osType = detectRedhat(c); itsMe {
|
|
util.Log.Debugf("Redhat like Linux. Host: %s:%s", c.Host, c.Port)
|
|
return
|
|
}
|
|
|
|
if itsMe, osType = detectSUSE(c); itsMe {
|
|
util.Log.Debugf("SUSE Linux. Host: %s:%s", c.Host, c.Port)
|
|
return
|
|
}
|
|
|
|
if itsMe, osType = detectFreebsd(c); itsMe {
|
|
util.Log.Debugf("FreeBSD. Host: %s:%s", c.Host, c.Port)
|
|
return
|
|
}
|
|
|
|
if itsMe, osType = detectAlpine(c); itsMe {
|
|
util.Log.Debugf("Alpine. Host: %s:%s", c.Host, c.Port)
|
|
return
|
|
}
|
|
|
|
//TODO darwin https://github.com/mizzy/specinfra/blob/master/lib/specinfra/helper/detect_os/darwin.rb
|
|
osType.setErrs([]error{fmt.Errorf("Unknown OS Type")})
|
|
return
|
|
}
|
|
|
|
// PrintSSHableServerNames print SSH-able servernames
|
|
func PrintSSHableServerNames() bool {
|
|
if len(servers) == 0 {
|
|
util.Log.Error("No scannable servers")
|
|
return false
|
|
}
|
|
util.Log.Info("Scannable servers are below...")
|
|
for _, s := range servers {
|
|
if s.getServerInfo().IsContainer() {
|
|
fmt.Printf("%s@%s ",
|
|
s.getServerInfo().Container.Name,
|
|
s.getServerInfo().ServerName,
|
|
)
|
|
} else {
|
|
fmt.Printf("%s ", s.getServerInfo().ServerName)
|
|
}
|
|
}
|
|
fmt.Printf("\n")
|
|
return true
|
|
}
|
|
|
|
// InitServers detect the kind of OS distribution of target servers
|
|
func InitServers(timeoutSec int) error {
|
|
servers, errServers = detectServerOSes(timeoutSec)
|
|
if len(servers) == 0 {
|
|
return fmt.Errorf("No scannable servers")
|
|
}
|
|
|
|
actives, inactives := detectContainerOSes(timeoutSec)
|
|
if config.Conf.ContainersOnly {
|
|
servers = actives
|
|
errServers = inactives
|
|
} else {
|
|
servers = append(servers, actives...)
|
|
errServers = append(errServers, inactives...)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func detectServerOSes(timeoutSec int) (servers, errServers []osTypeInterface) {
|
|
util.Log.Info("Detecting OS of servers... ")
|
|
osTypeChan := make(chan osTypeInterface, len(config.Conf.Servers))
|
|
defer close(osTypeChan)
|
|
for _, s := range config.Conf.Servers {
|
|
go func(s config.ServerInfo) {
|
|
defer func() {
|
|
if p := recover(); p != nil {
|
|
util.Log.Debugf("Panic: %s on %s", p, s.ServerName)
|
|
}
|
|
}()
|
|
osTypeChan <- detectOS(s)
|
|
}(s)
|
|
}
|
|
|
|
timeout := time.After(time.Duration(timeoutSec) * time.Second)
|
|
for i := 0; i < len(config.Conf.Servers); i++ {
|
|
select {
|
|
case res := <-osTypeChan:
|
|
if 0 < len(res.getErrs()) {
|
|
errServers = append(errServers, res)
|
|
util.Log.Errorf("(%d/%d) Failed: %s, err: %s",
|
|
i+1, len(config.Conf.Servers),
|
|
res.getServerInfo().ServerName,
|
|
res.getErrs())
|
|
} else {
|
|
servers = append(servers, res)
|
|
util.Log.Infof("(%d/%d) Detected: %s: %s",
|
|
i+1, len(config.Conf.Servers),
|
|
res.getServerInfo().ServerName,
|
|
res.getDistro())
|
|
}
|
|
case <-timeout:
|
|
msg := "Timed out while detecting servers"
|
|
util.Log.Error(msg)
|
|
for servername, sInfo := range config.Conf.Servers {
|
|
found := false
|
|
for _, o := range append(servers, errServers...) {
|
|
if servername == o.getServerInfo().ServerName {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
u := &unknown{}
|
|
u.setServerInfo(sInfo)
|
|
u.setErrs([]error{
|
|
fmt.Errorf("Timed out"),
|
|
})
|
|
errServers = append(errServers, u)
|
|
util.Log.Errorf("(%d/%d) Timed out: %s",
|
|
i+1, len(config.Conf.Servers),
|
|
servername)
|
|
i++
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func detectContainerOSes(timeoutSec int) (actives, inactives []osTypeInterface) {
|
|
util.Log.Info("Detecting OS of containers... ")
|
|
osTypesChan := make(chan []osTypeInterface, len(servers))
|
|
defer close(osTypesChan)
|
|
for _, s := range servers {
|
|
go func(s osTypeInterface) {
|
|
defer func() {
|
|
if p := recover(); p != nil {
|
|
util.Log.Debugf("Panic: %s on %s",
|
|
p, s.getServerInfo().GetServerName())
|
|
}
|
|
}()
|
|
osTypesChan <- detectContainerOSesOnServer(s)
|
|
}(s)
|
|
}
|
|
|
|
timeout := time.After(time.Duration(timeoutSec) * time.Second)
|
|
for i := 0; i < len(servers); i++ {
|
|
select {
|
|
case res := <-osTypesChan:
|
|
for _, osi := range res {
|
|
sinfo := osi.getServerInfo()
|
|
if 0 < len(osi.getErrs()) {
|
|
inactives = append(inactives, osi)
|
|
util.Log.Errorf("Failed: %s err: %s", sinfo.ServerName, osi.getErrs())
|
|
continue
|
|
}
|
|
actives = append(actives, osi)
|
|
util.Log.Infof("Detected: %s@%s: %s",
|
|
sinfo.Container.Name, sinfo.ServerName, osi.getDistro())
|
|
}
|
|
case <-timeout:
|
|
msg := "Timed out while detecting containers"
|
|
util.Log.Error(msg)
|
|
for servername, sInfo := range config.Conf.Servers {
|
|
found := false
|
|
for _, o := range append(actives, inactives...) {
|
|
if servername == o.getServerInfo().ServerName {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
u := &unknown{}
|
|
u.setServerInfo(sInfo)
|
|
u.setErrs([]error{
|
|
fmt.Errorf("Timed out"),
|
|
})
|
|
inactives = append(inactives)
|
|
util.Log.Errorf("Timed out: %s", servername)
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func detectContainerOSesOnServer(containerHost osTypeInterface) (oses []osTypeInterface) {
|
|
containerHostInfo := containerHost.getServerInfo()
|
|
if len(containerHostInfo.ContainersIncluded) == 0 {
|
|
return
|
|
}
|
|
|
|
running, err := containerHost.runningContainers()
|
|
if err != nil {
|
|
containerHost.setErrs([]error{fmt.Errorf(
|
|
"Failed to get running containers on %s. err: %s",
|
|
containerHost.getServerInfo().ServerName, err)})
|
|
return append(oses, containerHost)
|
|
}
|
|
|
|
if containerHostInfo.ContainersIncluded[0] == "${running}" {
|
|
for _, containerInfo := range running {
|
|
found := false
|
|
for _, ex := range containerHost.getServerInfo().ContainersExcluded {
|
|
if containerInfo.Name == ex || containerInfo.ContainerID == ex {
|
|
found = true
|
|
}
|
|
}
|
|
if found {
|
|
continue
|
|
}
|
|
|
|
copied := containerHostInfo
|
|
copied.SetContainer(config.Container{
|
|
ContainerID: containerInfo.ContainerID,
|
|
Name: containerInfo.Name,
|
|
Image: containerInfo.Image,
|
|
})
|
|
os := detectOS(copied)
|
|
oses = append(oses, os)
|
|
}
|
|
return oses
|
|
}
|
|
|
|
exitedContainers, err := containerHost.exitedContainers()
|
|
if err != nil {
|
|
containerHost.setErrs([]error{fmt.Errorf(
|
|
"Failed to get exited containers on %s. err: %s",
|
|
containerHost.getServerInfo().ServerName, err)})
|
|
return append(oses, containerHost)
|
|
}
|
|
|
|
var exited, unknown []string
|
|
for _, container := range containerHostInfo.ContainersIncluded {
|
|
found := false
|
|
for _, c := range running {
|
|
if c.ContainerID == container || c.Name == container {
|
|
copied := containerHostInfo
|
|
copied.SetContainer(c)
|
|
os := detectOS(copied)
|
|
oses = append(oses, os)
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if !found {
|
|
foundInExitedContainers := false
|
|
for _, c := range exitedContainers {
|
|
if c.ContainerID == container || c.Name == container {
|
|
exited = append(exited, container)
|
|
foundInExitedContainers = true
|
|
break
|
|
}
|
|
}
|
|
if !foundInExitedContainers {
|
|
unknown = append(unknown, container)
|
|
}
|
|
}
|
|
}
|
|
if 0 < len(exited) || 0 < len(unknown) {
|
|
containerHost.setErrs([]error{fmt.Errorf(
|
|
"Some containers on %s are exited or unknown. exited: %s, unknown: %s",
|
|
containerHost.getServerInfo().ServerName, exited, unknown)})
|
|
return append(oses, containerHost)
|
|
}
|
|
return oses
|
|
}
|
|
|
|
// CheckScanModes checks scan mode
|
|
func CheckScanModes() error {
|
|
for _, s := range servers {
|
|
if err := s.checkScanMode(); err != nil {
|
|
return fmt.Errorf("servers.%s.scanMode err: %s",
|
|
s.getServerInfo().GetServerName(), err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CheckDependencies checks dependencies are installed on target servers.
|
|
func CheckDependencies(timeoutSec int) {
|
|
parallelExec(func(o osTypeInterface) error {
|
|
return o.checkDeps()
|
|
}, timeoutSec)
|
|
return
|
|
}
|
|
|
|
// CheckIfSudoNoPasswd checks whether vuls can sudo with nopassword via SSH
|
|
func CheckIfSudoNoPasswd(timeoutSec int) {
|
|
parallelExec(func(o osTypeInterface) error {
|
|
return o.checkIfSudoNoPasswd()
|
|
}, timeoutSec)
|
|
return
|
|
}
|
|
|
|
// DetectPlatforms detects the platform of each servers.
|
|
func DetectPlatforms(timeoutSec int) {
|
|
detectPlatforms(timeoutSec)
|
|
for i, s := range servers {
|
|
if s.getServerInfo().IsContainer() {
|
|
util.Log.Infof("(%d/%d) %s on %s is running on %s",
|
|
i+1, len(servers),
|
|
s.getServerInfo().Container.Name,
|
|
s.getServerInfo().ServerName,
|
|
s.getPlatform().Name,
|
|
)
|
|
|
|
} else {
|
|
util.Log.Infof("(%d/%d) %s is running on %s",
|
|
i+1, len(servers),
|
|
s.getServerInfo().ServerName,
|
|
s.getPlatform().Name,
|
|
)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func detectPlatforms(timeoutSec int) {
|
|
parallelExec(func(o osTypeInterface) error {
|
|
o.detectPlatform()
|
|
// Logging only if platform can not be specified
|
|
return nil
|
|
}, timeoutSec)
|
|
return
|
|
}
|
|
|
|
// Scan scan
|
|
func Scan(timeoutSec int) error {
|
|
if len(servers) == 0 {
|
|
return fmt.Errorf("No server defined. Check the configuration")
|
|
}
|
|
|
|
if err := setupChangelogCache(); err != nil {
|
|
return err
|
|
}
|
|
defer func() {
|
|
if cache.DB != nil {
|
|
cache.DB.Close()
|
|
}
|
|
}()
|
|
|
|
util.Log.Info("Scanning vulnerable OS packages...")
|
|
scannedAt := time.Now()
|
|
dir, err := EnsureResultDir(scannedAt)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return scanVulns(dir, scannedAt, timeoutSec)
|
|
}
|
|
|
|
// ViaHTTP scans servers by HTTP header and body
|
|
func ViaHTTP(header http.Header, body string) (models.ScanResult, error) {
|
|
family := header.Get("X-Vuls-OS-Family")
|
|
if family == "" {
|
|
return models.ScanResult{}, errOSFamilyHeader
|
|
}
|
|
|
|
release := header.Get("X-Vuls-OS-Release")
|
|
if release == "" {
|
|
return models.ScanResult{}, errOSReleaseHeader
|
|
}
|
|
|
|
kernelRelease := header.Get("X-Vuls-Kernel-Release")
|
|
if kernelRelease == "" {
|
|
util.Log.Warn("If X-Vuls-Kernel-Release is not specified, there is a possibility of false detection")
|
|
}
|
|
|
|
kernelVersion := header.Get("X-Vuls-Kernel-Version")
|
|
if family == config.Debian && kernelVersion == "" {
|
|
return models.ScanResult{}, errKernelVersionHeader
|
|
}
|
|
|
|
serverName := header.Get("X-Vuls-Server-Name")
|
|
if config.Conf.ToLocalFile && serverName == "" {
|
|
return models.ScanResult{}, errServerNameHeader
|
|
}
|
|
|
|
distro := config.Distro{
|
|
Family: family,
|
|
Release: release,
|
|
}
|
|
|
|
kernel := models.Kernel{
|
|
Release: kernelRelease,
|
|
Version: kernelVersion,
|
|
}
|
|
base := base{
|
|
Distro: distro,
|
|
osPackages: osPackages{
|
|
Kernel: kernel,
|
|
},
|
|
log: util.Log,
|
|
}
|
|
|
|
var osType osTypeInterface
|
|
switch family {
|
|
case config.Debian, config.Ubuntu:
|
|
osType = &debian{base: base}
|
|
case config.RedHat:
|
|
osType = &rhel{
|
|
redhatBase: redhatBase{base: base},
|
|
}
|
|
case config.CentOS:
|
|
osType = ¢os{
|
|
redhatBase: redhatBase{base: base},
|
|
}
|
|
default:
|
|
return models.ScanResult{}, fmt.Errorf("Server mode for %s is not implemented yet", family)
|
|
}
|
|
|
|
installedPackages, srcPackages, err := osType.parseInstalledPackages(body)
|
|
if err != nil {
|
|
return models.ScanResult{}, err
|
|
}
|
|
|
|
result := models.ScanResult{
|
|
ServerName: serverName,
|
|
Family: family,
|
|
Release: release,
|
|
RunningKernel: models.Kernel{
|
|
Release: kernelRelease,
|
|
Version: kernelVersion,
|
|
},
|
|
Packages: installedPackages,
|
|
SrcPackages: srcPackages,
|
|
ScannedCves: models.VulnInfos{},
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func setupChangelogCache() error {
|
|
needToSetupCache := false
|
|
for _, s := range servers {
|
|
switch s.getDistro().Family {
|
|
case config.Raspbian:
|
|
needToSetupCache = true
|
|
break
|
|
case config.Ubuntu, config.Debian:
|
|
//TODO changelopg cache for RedHat, Oracle, Amazon, CentOS is not implemented yet.
|
|
if s.getServerInfo().Mode.IsDeep() {
|
|
needToSetupCache = true
|
|
}
|
|
break
|
|
}
|
|
}
|
|
if needToSetupCache {
|
|
if err := cache.SetupBolt(config.Conf.CacheDBPath, util.Log); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func scanVulns(jsonDir string, scannedAt time.Time, timeoutSec int) error {
|
|
var results models.ScanResults
|
|
parallelExec(func(o osTypeInterface) (err error) {
|
|
if err = o.preCure(); err != nil {
|
|
return err
|
|
}
|
|
if err = o.scanPackages(); err != nil {
|
|
return err
|
|
}
|
|
return o.postScan()
|
|
}, timeoutSec)
|
|
|
|
hostname, _ := os.Hostname()
|
|
for _, s := range append(servers, errServers...) {
|
|
r := s.convertToModel()
|
|
r.ScannedAt = scannedAt
|
|
r.ScannedVersion = config.Version
|
|
r.ScannedRevision = config.Revision
|
|
r.ScannedBy = hostname
|
|
r.Config.Scan = config.Conf
|
|
results = append(results, r)
|
|
}
|
|
|
|
config.Conf.FormatJSON = true
|
|
ws := []report.ResultWriter{
|
|
report.LocalFileWriter{CurrentDir: jsonDir},
|
|
}
|
|
for _, w := range ws {
|
|
if err := w.Write(results...); err != nil {
|
|
return fmt.Errorf("Failed to write summary report: %s", err)
|
|
}
|
|
}
|
|
|
|
report.StdoutWriter{}.WriteScanSummary(results...)
|
|
return nil
|
|
}
|
|
|
|
// EnsureResultDir ensures the directory for scan results
|
|
func EnsureResultDir(scannedAt time.Time) (currentDir string, err error) {
|
|
jsonDirName := scannedAt.Format(time.RFC3339)
|
|
|
|
resultsDir := config.Conf.ResultsDir
|
|
if len(resultsDir) == 0 {
|
|
wd, _ := os.Getwd()
|
|
resultsDir = filepath.Join(wd, "results")
|
|
}
|
|
jsonDir := filepath.Join(resultsDir, jsonDirName)
|
|
if err := os.MkdirAll(jsonDir, 0700); err != nil {
|
|
return "", fmt.Errorf("Failed to create dir: %s", err)
|
|
}
|
|
|
|
symlinkPath := filepath.Join(resultsDir, "current")
|
|
if _, err := os.Lstat(symlinkPath); err == nil {
|
|
if err := os.Remove(symlinkPath); err != nil {
|
|
return "", fmt.Errorf(
|
|
"Failed to remove symlink. path: %s, err: %s", symlinkPath, err)
|
|
}
|
|
}
|
|
|
|
if err := os.Symlink(jsonDir, symlinkPath); err != nil {
|
|
return "", fmt.Errorf(
|
|
"Failed to create symlink: path: %s, err: %s", symlinkPath, err)
|
|
}
|
|
return jsonDir, nil
|
|
}
|