diff --git a/report/saas.go b/report/saas.go index 5173f47c..3f175ea5 100644 --- a/report/saas.go +++ b/report/saas.go @@ -24,7 +24,9 @@ import ( "io/ioutil" "net/http" "net/url" + "os" "path" + "strings" "time" "github.com/aws/aws-sdk-go/aws" @@ -48,8 +50,11 @@ type TempCredential struct { } type payload struct { - GroupID int `json:"GroupID"` - Token string `json:"Token"` + GroupID int `json:"GroupID"` + Token string `json:"Token"` + ScannedBy string `json:"ScannedBy"` + ScannedIPv4s string `json:"ScannedIPv4s"` + ScannedIPv6s string `json:"ScannedIPv6s"` } // UploadSaas : UploadSaas @@ -59,9 +64,18 @@ func (w SaasWriter) Write(rs ...models.ScanResult) (err error) { return nil } + ipv4s, ipv6s, err := util.IP() + if err != nil { + util.Log.Errorf("Failed to fetch scannedIPs: %s", err) + } + hostname, _ := os.Hostname() + payload := payload{ - GroupID: c.Conf.Saas.GroupID, - Token: c.Conf.Saas.Token, + GroupID: c.Conf.Saas.GroupID, + Token: c.Conf.Saas.Token, + ScannedBy: hostname, + ScannedIPv4s: strings.Join(ipv4s, ", "), + ScannedIPv6s: strings.Join(ipv6s, ", "), } var body []byte diff --git a/scan/serverapi.go b/scan/serverapi.go index b684d998..105eb7e4 100644 --- a/scan/serverapi.go +++ b/scan/serverapi.go @@ -20,7 +20,6 @@ package scan import ( "errors" "fmt" - "net" "net/http" "os" "path/filepath" @@ -596,7 +595,7 @@ func scanVulns(jsonDir string, scannedAt time.Time, timeoutSec int) error { }, timeoutSec) hostname, _ := os.Hostname() - ipv4s, ipv6s, err := ip() + ipv4s, ipv6s, err := util.IP() if err != nil { util.Log.Errorf("Failed to fetch scannedIPs: %s", err) } @@ -627,38 +626,6 @@ func scanVulns(jsonDir string, scannedAt time.Time, timeoutSec int) error { return nil } -// ip returns scanner network ip addresses -func ip() (ipv4Addrs []string, ipv6Addrs []string, err error) { - ifaces, err := net.Interfaces() - if err != nil { - return nil, nil, err - } - for _, i := range ifaces { - addrs, _ := i.Addrs() - for _, addr := range addrs { - var ip net.IP - switch v := addr.(type) { - case *net.IPNet: - ip = v.IP - case *net.IPAddr: - ip = v.IP - } - - // only global unicast address - if !ip.IsGlobalUnicast() { - continue - } - - if ok := ip.To4(); ok != nil { - ipv4Addrs = append(ipv4Addrs, ip.String()) - } else { - ipv6Addrs = append(ipv6Addrs, ip.String()) - } - } - } - return ipv4Addrs, ipv6Addrs, nil -} - // EnsureResultDir ensures the directory for scan results func EnsureResultDir(scannedAt time.Time) (currentDir string, err error) { jsonDirName := scannedAt.Format(time.RFC3339) diff --git a/util/util.go b/util/util.go index cdf80fd5..21d28d1f 100644 --- a/util/util.go +++ b/util/util.go @@ -19,6 +19,7 @@ package util import ( "fmt" + "net" "net/url" "strings" @@ -93,6 +94,38 @@ func URLPathParamJoin(baseURL string, paths []string, params map[string]string) return u.String(), nil } +// IP returns scanner network ip addresses +func IP() (ipv4Addrs []string, ipv6Addrs []string, err error) { + ifaces, err := net.Interfaces() + if err != nil { + return nil, nil, err + } + for _, i := range ifaces { + addrs, _ := i.Addrs() + for _, addr := range addrs { + var ip net.IP + switch v := addr.(type) { + case *net.IPNet: + ip = v.IP + case *net.IPAddr: + ip = v.IP + } + + // only global unicast address + if !ip.IsGlobalUnicast() { + continue + } + + if ok := ip.To4(); ok != nil { + ipv4Addrs = append(ipv4Addrs, ip.String()) + } else { + ipv6Addrs = append(ipv6Addrs, ip.String()) + } + } + } + return ipv4Addrs, ipv6Addrs, nil +} + // ProxyEnv returns shell environment variables to set proxy func ProxyEnv() string { httpProxyEnv := ""