From 39f4fbaa2d5c1a17234405920e90aaebfe870b7c Mon Sep 17 00:00:00 2001 From: MaineK00n Date: Wed, 14 Oct 2020 18:59:34 +0900 Subject: [PATCH] change impl scanPorts template --- models/packages.go | 5 ++-- scan/base.go | 59 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/models/packages.go b/models/packages.go index a0c1b340..edd9b963 100644 --- a/models/packages.go +++ b/models/packages.go @@ -181,8 +181,9 @@ type AffectedProcess struct { // ListenPorts has the result of parsing the port information to the address and port. type ListenPorts struct { - Address string `json:"address"` - Port string `json:"port"` + Address string `json:"address"` + Port string `json:"port"` + OpenStatus bool `json:"openStatus"` } // NeedRestartProcess keep a processes information affected by software update diff --git a/scan/base.go b/scan/base.go index ad43f106..674f780c 100644 --- a/scan/base.go +++ b/scan/base.go @@ -731,7 +731,12 @@ func (l *base) detectWpPlugins() ([]models.WpPackage, error) { func (l *base) scanPorts() (err error) { dest := l.detectScanDest() - fmt.Printf("%s\n", dest) + open, err := l.execPortsScan(dest) + if err != nil { + return err + } + l.updatePortStatus(open) + return nil } @@ -780,6 +785,58 @@ func (l *base) detectScanDest() []string { return dest } +func (l *base) execPortsScan(dest []string) ([]string, error) { + open := []string{} + + for _, d := range dest { + conn, err := net.DialTimeout("tcp", d, time.Duration(1)*time.Second) + if err != nil { + // if !strings.Contains(err.Error(), "refused") { + // return nil, xerrors.Errorf("Failed to execPortScan: %+v", err) + // } + continue + } + conn.Close() + open = append(open, d) + } + + return open, nil +} + +func (l *base) updatePortStatus(open []string) { + for _, p := range l.osPackages.Packages { + if p.AffectedProcs == nil { + continue + } + for _, proc := range p.AffectedProcs { + if proc.ListenPorts == nil { + continue + } + for _, port := range proc.ListenPorts { + port.OpenStatus = matchListenPorts(open, port) + } + } + } +} + +func matchListenPorts(open []string, port models.ListenPorts) bool { + l := base{} + for _, ip := range open { + i := l.parseListenPorts(ip) + if port.Address == "*" { + if port.Port == i.Port { + return true + } + } else { + if port.Address == i.Address && port.Port == i.Port { + return true + } + } + } + + return false +} + func (l *base) ps() (stdout string, err error) { cmd := `LANGUAGE=en_US.UTF-8 ps --no-headers --ppid 2 -p 2 --deselect -o pid,comm` r := l.exec(util.PrependProxyEnv(cmd), noSudo)