From 895491dd92cb828f8c8b1567aefdabb8105c3e82 Mon Sep 17 00:00:00 2001 From: Rob Gonnella Date: Tue, 8 Aug 2023 10:34:49 -0400 Subject: [PATCH] Remove nmap scanner in favor of internal arp scanner --- go.mod | 2 - go.sum | 4 -- internal/discovery/arpscan.go | 5 ++ internal/discovery/nmap.go | 129 ---------------------------------- 4 files changed, 5 insertions(+), 135 deletions(-) delete mode 100644 internal/discovery/nmap.go diff --git a/go.mod b/go.mod index 0eb6273..98ecdbb 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/robgonnella/ops go 1.19 require ( - github.com/Ullaakut/nmap/v3 v3.0.2 github.com/apenella/go-ansible v1.1.7 github.com/gdamore/tcell/v2 v2.6.0 github.com/golang/mock v1.6.0 @@ -56,7 +55,6 @@ require ( github.com/stretchr/objx v0.5.0 // indirect github.com/subosito/gotenv v1.4.2 // indirect golang.org/x/net v0.10.0 // indirect - golang.org/x/sync v0.1.0 // indirect golang.org/x/sys v0.9.0 // indirect golang.org/x/term v0.9.0 // indirect golang.org/x/text v0.10.0 // indirect diff --git a/go.sum b/go.sum index a14bab1..5b053fe 100644 --- a/go.sum +++ b/go.sum @@ -40,8 +40,6 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= -github.com/Ullaakut/nmap/v3 v3.0.2 h1:AqQ9UYxLWzYZTv/rzMzVn8+LIgFGxGi+4h+3pDkFOII= -github.com/Ullaakut/nmap/v3 v3.0.2/go.mod h1:dd5K68P7LHc5nKrFwQx6EdTt61O9UN5x3zn1R4SLcco= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/apenella/go-ansible v1.1.7 h1:seJcEZbRjALS6RjbO5UjPQTHpCnnaRADmCCo0MT26BU= @@ -488,8 +486,6 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= diff --git a/internal/discovery/arpscan.go b/internal/discovery/arpscan.go index 0a39e5d..383d684 100644 --- a/internal/discovery/arpscan.go +++ b/internal/discovery/arpscan.go @@ -128,6 +128,11 @@ func (s *ARPScanner) handleARPLayer(arp *layers.ARP) { ip := net.IP(arp.SourceProtAddress) mac := net.HardwareAddr(arp.SourceHwAddress) + if !util.SliceIncludes(s.targets, ip.String()) { + // not an arp request we care about + return + } + s.mux.Lock() s.arpMap[ip.String()] = mac s.mux.Unlock() diff --git a/internal/discovery/nmap.go b/internal/discovery/nmap.go deleted file mode 100644 index 3ee16e8..0000000 --- a/internal/discovery/nmap.go +++ /dev/null @@ -1,129 +0,0 @@ -package discovery - -import ( - "context" - "crypto/sha1" - "encoding/hex" - "strconv" - - "github.com/Ullaakut/nmap/v3" - "github.com/robgonnella/ops/internal/logger" - "github.com/robgonnella/ops/internal/server" -) - -// NmapScanner is an implementation of the Scanner interface -type NmapScanner struct { - ctx context.Context - cancel context.CancelFunc - scanner *nmap.Scanner - resultChan chan *DiscoveryResult - log logger.Logger -} - -// NewNmapScanner returns a new instance of NmapScanner -func NewNmapScanner(targets []string, resultChan chan *DiscoveryResult) (*NmapScanner, error) { - log := logger.New() - - // Use a cancelable context so we can properly cleanup when needed - ctxWithCancel, cancel := context.WithCancel(context.Background()) - - scanner, err := nmap.NewScanner( - ctxWithCancel, - nmap.WithTargets(targets...), - nmap.WithPorts("22"), - nmap.WithTimingTemplate(nmap.TimingFastest), - nmap.WithACKDiscovery(), - nmap.WithVerbosity(10), - ) - - if err != nil { - cancel() - return nil, err - } - - return &NmapScanner{ - ctx: ctxWithCancel, - cancel: cancel, - log: log, - scanner: scanner, - resultChan: resultChan, - }, nil -} - -// Stop stops network scanning. Once called this scanner will be useless, -// a new one will need to be instantiated to continue scanning. -func (s *NmapScanner) Stop() { - s.cancel() -} - -// scan targets and ports and return network results -func (s *NmapScanner) Scan() error { - s.log.Info().Msg("Scanning network...") - - result, warnings, err := s.scanner.Run() - - if len(*warnings) > 0 { - fields := map[string]interface{}{} - - for i, warning := range *warnings { - fields[strconv.Itoa(i)] = warning - } - - s.log.Warn(). - Fields(fields). - Msg("encountered network scan warnings") - } - - if err != nil { - s.log.Error().Err(err).Msg("encountered network scan error") - return err - } - - for _, host := range result.Hosts { - ports := []Port{} - - for _, port := range host.Ports { - status := PortClosed - - if port.Status() == nmap.Open { - status = PortOpen - } - - ports = append(ports, Port{ - ID: port.ID, - Status: status, - }) - } - - status := server.StatusOffline - nmapStatus := host.Status - - if nmapStatus.String() == "up" { - status = server.StatusOnline - } - - ip := "" - - if len(host.Addresses) > 0 { - ip = host.Addresses[0].String() - } - - if ip == "" { - continue - } - - hashedIP := sha1.Sum([]byte(ip)) - id := hex.EncodeToString(hashedIP[:]) - - res := &DiscoveryResult{ - ID: id, - IP: ip, - Status: status, - Ports: ports, - } - - s.resultChan <- res - } - - return nil -}