misc: small fix or general refactoring i did not bother commenting

This commit is contained in:
evilsocket 2018-02-22 21:52:37 +01:00
commit 7c2dc38819
4 changed files with 43 additions and 31 deletions

View file

@ -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) {

View file

@ -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)

View file

@ -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,
}
}

View file

@ -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()