From 7c2dc38819a144cc88e379ce379b8c85091ae1e8 Mon Sep 17 00:00:00 2001 From: evilsocket Date: Thu, 22 Feb 2018 21:52:37 +0100 Subject: [PATCH] misc: small fix or general refactoring i did not bother commenting --- modules/events_view.go | 2 +- modules/syn_scan.go | 32 +++++++------------------------- modules/syn_scan_event.go | 12 +++++++----- network/meta.go | 28 ++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 31 deletions(-) diff --git a/modules/events_view.go b/modules/events_view.go index 7cc6a828..68f1ce9c 100644 --- a/modules/events_view.go +++ b/modules/events_view.go @@ -100,7 +100,7 @@ func (s EventsStream) viewSynScanEvent(e session.Event) { e.Time.Format(eventTimeFormat), core.Green(e.Tag), se.Port, - core.Bold(se.Host.IpAddress)) + core.Bold(se.Address)) } func (s *EventsStream) View(e session.Event, refresh bool) { diff --git a/modules/syn_scan.go b/modules/syn_scan.go index c99952de..fdafa8f7 100644 --- a/modules/syn_scan.go +++ b/modules/syn_scan.go @@ -4,7 +4,6 @@ import ( "fmt" "net" "strconv" - "strings" "time" "github.com/evilsocket/bettercap-ng/core" @@ -39,7 +38,9 @@ func NewSynScanner(s *session.Session) *SynScanner { ss.AddHandler(session.NewModuleHandler("syn.scan IP-RANGE START-PORT END-PORT", "syn.scan ([^\\s]+) (\\d+)([\\s\\d]*)", "Perform a syn port scanning against an IP address within the provided ports range.", func(args []string) error { - var err error + if ss.Running() == true { + return fmt.Errorf("A scan is already running, wait for it to end before starting a new one.") + } list, err := iprange.Parse(args[0]) if err != nil { @@ -176,34 +177,15 @@ func (s *SynScanner) onPacket(pkt gopacket.Packet) { } if host != nil { - sports := strings.Split(host.Meta.Get("tcp-ports").(string), ",") - ports := []int{port} - - for _, s := range sports { - n, err := strconv.Atoi(s) - if err == nil { - ports = append(ports, n) - } - } - - ports = core.UniqueInts(ports, true) - list := make([]string, len(ports)) - for i, p := range ports { - list[i] = fmt.Sprintf("%d", p) - } - - host.Meta.Set("tcp-ports", strings.Join(list, ",")) - - NewSynScanEvent(host, port).Push() + ports := host.Meta.GetIntsWith("tcp-ports", port, true) + host.Meta.SetInts("tcp-ports", ports) } + + NewSynScanEvent(from, host, port).Push() } } func (s *SynScanner) synScan() error { - if s.Running() == true { - return fmt.Errorf("A scan is already running, wait for it to end before starting a new one.") - } - s.SetRunning(true, func() { defer s.SetRunning(false, nil) diff --git a/modules/syn_scan_event.go b/modules/syn_scan_event.go index 918c23de..95df92e8 100644 --- a/modules/syn_scan_event.go +++ b/modules/syn_scan_event.go @@ -6,14 +6,16 @@ import ( ) type SynScanEvent struct { - Host *network.Endpoint - Port int + Address string + Host *network.Endpoint + Port int } -func NewSynScanEvent(h *network.Endpoint, port int) SynScanEvent { +func NewSynScanEvent(address string, h *network.Endpoint, port int) SynScanEvent { return SynScanEvent{ - Host: h, - Port: port, + Address: address, + Host: h, + Port: port, } } diff --git a/network/meta.go b/network/meta.go index 5ac6fb01..8be876c8 100644 --- a/network/meta.go +++ b/network/meta.go @@ -2,7 +2,12 @@ package network import ( "encoding/json" + "fmt" + "strconv" + "strings" "sync" + + "github.com/evilsocket/bettercap-ng/core" ) type Meta struct { @@ -43,6 +48,29 @@ func (m *Meta) Get(name string) interface{} { return "" } +func (m *Meta) GetIntsWith(name string, with int, sorted bool) []int { + sints := strings.Split(m.Get(name).(string), ",") + ints := []int{with} + + for _, s := range sints { + n, err := strconv.Atoi(s) + if err == nil { + ints = append(ints, n) + } + } + + return core.UniqueInts(ints, sorted) +} + +func (m *Meta) SetInts(name string, ints []int) { + list := make([]string, len(ints)) + for i, n := range ints { + list[i] = fmt.Sprintf("%d", n) + } + + m.Set(name, strings.Join(list, ",")) +} + func (m *Meta) GetOr(name string, dflt interface{}) interface{} { m.Lock() defer m.Unlock()