From e1023d4b666ab492b767f2cadf5240129b516115 Mon Sep 17 00:00:00 2001 From: rhaidiz Date: Sun, 10 Feb 2019 14:46:05 +0100 Subject: [PATCH 1/4] Add option to manually specify the gateway. --- core/options.go | 2 ++ network/net_gateway.go | 17 +++++++++++++++++ session/session.go | 17 ++++++++++------- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/core/options.go b/core/options.go index 10641dab..f84142c7 100644 --- a/core/options.go +++ b/core/options.go @@ -4,6 +4,7 @@ import "flag" type Options struct { InterfaceName *string + Gateway *string Caplet *string AutoStart *string Debug *bool @@ -19,6 +20,7 @@ type Options struct { func ParseOptions() (Options, error) { o := Options{ InterfaceName: flag.String("iface", "", "Network interface to bind to, if empty the default interface will be auto selected."), + Gateway: flag.String("gw","","Manually specify the gateway address, if not specified or invalid, the default gateway will be used."), AutoStart: flag.String("autostart", "events.stream, net.recon", "Comma separated list of modules to auto start."), Caplet: flag.String("caplet", "", "Read commands from this file and execute them in the interactive session."), Debug: flag.Bool("debug", false, "Print debug messages."), diff --git a/network/net_gateway.go b/network/net_gateway.go index 4b2cb268..3b17901e 100644 --- a/network/net_gateway.go +++ b/network/net_gateway.go @@ -3,6 +3,7 @@ package network import ( + "fmt" "strings" "github.com/bettercap/bettercap/core" @@ -48,3 +49,19 @@ func FindGateway(iface *Endpoint) (*Endpoint, error) { Debug("FindGateway(%s): nothing found :/", iface.Name()) return nil, ErrNoGateway } + +func GatewayProvidedByUser(iface *Endpoint, gateway string) (*Endpoint, error) { + Debug("GatewayProvidedByUser(%s) [cmd=%v opts=%v parser=%v]", gateway, IPv4RouteCmd, IPv4RouteCmdOpts, IPv4RouteParser) + if IPv4Validator.MatchString(gateway) { + Debug("valid gateway ip %s",gateway) + ifName := iface.Name() + // we have the address, now we need its mac + mac, err := ArpLookup(ifName, gateway, false) + if err != nil { + return nil, err + } + Debug("gateway is %s[%s]", gateway, mac) + return NewEndpoint(gateway, mac), nil + } + return nil, fmt.Errorf("Provided gateway %s not a valid IPv4 address! Revert to find default gateway.",gateway) +} diff --git a/session/session.go b/session/session.go index c331d927..60eff5c2 100644 --- a/session/session.go +++ b/session/session.go @@ -226,13 +226,16 @@ func (s *Session) Start() error { return err } - if s.Gateway, err = network.FindGateway(s.Interface); err != nil { - level := log.WARNING - if s.Interface.IsMonitor() { - level = log.DEBUG - } - s.Events.Log(level, "%s", err.Error()) - } + if s.Gateway, err = network.GatewayProvidedByUser(s.Interface, *s.Options.Gateway); err != nil { + level := log.WARNING + if s.Interface.IsMonitor() { + level = log.DEBUG + } + s.Events.Log(level, "%s", err.Error()) + if s.Gateway, err = network.FindGateway(s.Interface); err != nil { + s.Events.Log(level, "%s", err.Error()) + } + } if s.Gateway == nil || s.Gateway.IpAddress == s.Interface.IpAddress { s.Gateway = s.Interface From f9e2347e4f074d078313cfad5ec7e8f2373c3bd5 Mon Sep 17 00:00:00 2001 From: Federico <0x46444d@gmail.com> Date: Sun, 10 Feb 2019 14:47:14 +0100 Subject: [PATCH 2/4] Fix indentation --- core/options.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/options.go b/core/options.go index f84142c7..2fc9a6cb 100644 --- a/core/options.go +++ b/core/options.go @@ -20,7 +20,7 @@ type Options struct { func ParseOptions() (Options, error) { o := Options{ InterfaceName: flag.String("iface", "", "Network interface to bind to, if empty the default interface will be auto selected."), - Gateway: flag.String("gw","","Manually specify the gateway address, if not specified or invalid, the default gateway will be used."), + Gateway: flag.String("gw","","Manually specify the gateway address, if not specified or invalid, the default gateway will be used."), AutoStart: flag.String("autostart", "events.stream, net.recon", "Comma separated list of modules to auto start."), Caplet: flag.String("caplet", "", "Read commands from this file and execute them in the interactive session."), Debug: flag.Bool("debug", false, "Print debug messages."), From ccc66201896f037b106140230d8b3aa99010dafb Mon Sep 17 00:00:00 2001 From: Federico Date: Sun, 10 Feb 2019 14:51:45 +0100 Subject: [PATCH 3/4] Fix indentation --- session/session.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/session/session.go b/session/session.go index 60eff5c2..6a15e3a2 100644 --- a/session/session.go +++ b/session/session.go @@ -227,15 +227,15 @@ func (s *Session) Start() error { } if s.Gateway, err = network.GatewayProvidedByUser(s.Interface, *s.Options.Gateway); err != nil { - level := log.WARNING - if s.Interface.IsMonitor() { - level = log.DEBUG - } + level := log.WARNING + if s.Interface.IsMonitor() { + level = log.DEBUG + } + s.Events.Log(level, "%s", err.Error()) + if s.Gateway, err = network.FindGateway(s.Interface); err != nil { s.Events.Log(level, "%s", err.Error()) - if s.Gateway, err = network.FindGateway(s.Interface); err != nil { - s.Events.Log(level, "%s", err.Error()) } - } + } if s.Gateway == nil || s.Gateway.IpAddress == s.Interface.IpAddress { s.Gateway = s.Interface From 900468711c3b21cb4b87688d193c4f4b3abcd695 Mon Sep 17 00:00:00 2001 From: rhaidiz Date: Mon, 11 Feb 2019 19:40:40 +0100 Subject: [PATCH 4/4] Rename the parameter for overriding the value of the default gateway --- core/options.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/options.go b/core/options.go index f84142c7..6cf4403e 100644 --- a/core/options.go +++ b/core/options.go @@ -20,7 +20,7 @@ type Options struct { func ParseOptions() (Options, error) { o := Options{ InterfaceName: flag.String("iface", "", "Network interface to bind to, if empty the default interface will be auto selected."), - Gateway: flag.String("gw","","Manually specify the gateway address, if not specified or invalid, the default gateway will be used."), + Gateway: flag.String("gateway-override","","Use the provided IP address instead of the default gateway. If not specified or invalid, the default gateway will be used."), AutoStart: flag.String("autostart", "events.stream, net.recon", "Comma separated list of modules to auto start."), Caplet: flag.String("caplet", "", "Read commands from this file and execute them in the interactive session."), Debug: flag.Bool("debug", false, "Print debug messages."),