mirror of
https://github.com/bettercap/bettercap
synced 2025-08-20 21:43:18 -07:00
fix: refactored routing logic (fixes #701)
This commit is contained in:
parent
88a83192ef
commit
43a93fd866
11 changed files with 202 additions and 80 deletions
|
@ -305,7 +305,6 @@ func SetInterfaceTxPower(name string, txpower int) error {
|
|||
}
|
||||
|
||||
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)
|
||||
// we have the address, now we need its mac
|
||||
|
|
|
@ -13,23 +13,8 @@ import (
|
|||
|
||||
const airPortPath = "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport"
|
||||
|
||||
var IPv4RouteParser = regexp.MustCompile(`([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+).*`)
|
||||
var IPv4RouteTokens = 5
|
||||
var IPv4RouteCmd = "netstat"
|
||||
var IPv4RouteCmdOpts = []string{"-n", "-r"}
|
||||
var WiFiChannelParser = regexp.MustCompile(`(?m)^.*Supported Channels: (.*)$`)
|
||||
|
||||
func IPv4RouteIsGateway(ifname string, tokens []string, f func(gateway string) (*Endpoint, error)) (*Endpoint, error) {
|
||||
flags := tokens[3]
|
||||
ifname2 := tokens[4]
|
||||
if ifname == ifname2 && flags == "UGSc" {
|
||||
gateway := tokens[2]
|
||||
return f(gateway)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// see Windows version to understand why ....
|
||||
func getInterfaceName(iface net.Interface) string {
|
||||
return iface.Name
|
||||
|
|
|
@ -3,46 +3,26 @@
|
|||
package network
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/bettercap/bettercap/core"
|
||||
|
||||
"github.com/evilsocket/islazy/str"
|
||||
"github.com/bettercap/bettercap/routing"
|
||||
)
|
||||
|
||||
func FindGateway(iface *Endpoint) (*Endpoint, error) {
|
||||
Debug("FindGateway(%s) [cmd=%v opts=%v parser=%v]", iface.Name(), IPv4RouteCmd, IPv4RouteCmdOpts, IPv4RouteParser)
|
||||
|
||||
output, err := core.Exec(IPv4RouteCmd, IPv4RouteCmdOpts)
|
||||
gateway, err := routing.Gateway(routing.IPv4, iface.Name())
|
||||
if err != nil {
|
||||
Debug("FindGateway(%s): core.Exec failed with %s", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Debug("FindGateway(%s) output:\n%s", iface.Name(), output)
|
||||
|
||||
ifName := iface.Name()
|
||||
for _, line := range strings.Split(output, "\n") {
|
||||
if line = str.Trim(line); strings.Contains(line, ifName) {
|
||||
m := IPv4RouteParser.FindStringSubmatch(line)
|
||||
if len(m) >= IPv4RouteTokens {
|
||||
Debug("FindGateway(%s) line '%s' matched with %v", iface.Name(), line, m)
|
||||
return IPv4RouteIsGateway(ifName, m, func(gateway string) (*Endpoint, error) {
|
||||
if gateway == iface.IpAddress {
|
||||
Debug("gateway is the interface")
|
||||
return iface, nil
|
||||
} else {
|
||||
// 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
|
||||
}
|
||||
})
|
||||
}
|
||||
if gateway == iface.IpAddress {
|
||||
Debug("gateway is the interface")
|
||||
return iface, nil
|
||||
} else {
|
||||
// we have the address, now we need its mac
|
||||
mac, err := ArpLookup(iface.Name(), gateway, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
Debug("gateway is %s[%s]", gateway, mac)
|
||||
return NewEndpoint(gateway, mac), nil
|
||||
}
|
||||
|
||||
Debug("FindGateway(%s): nothing found :/", iface.Name())
|
||||
|
|
|
@ -24,4 +24,4 @@ func FindGateway(iface *Endpoint) (*Endpoint, error) {
|
|||
}
|
||||
|
||||
return nil, ErrNoGateway
|
||||
}
|
||||
}
|
|
@ -11,23 +11,6 @@ import (
|
|||
"github.com/bettercap/bettercap/core"
|
||||
)
|
||||
|
||||
// only matches gateway lines
|
||||
var IPv4RouteParser = regexp.MustCompile(`^(default|[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\svia\s([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\sdev\s(\S+).*$`)
|
||||
var IPv4RouteTokens = 4
|
||||
var IPv4RouteCmd = "ip"
|
||||
var IPv4RouteCmdOpts = []string{"route"}
|
||||
|
||||
func IPv4RouteIsGateway(ifname string, tokens []string, f func(gateway string) (*Endpoint, error)) (*Endpoint, error) {
|
||||
ifname2 := tokens[3]
|
||||
|
||||
if ifname == ifname2 {
|
||||
gateway := tokens[2]
|
||||
return f(gateway)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// see Windows version to understand why ....
|
||||
func getInterfaceName(iface net.Interface) string {
|
||||
return iface.Name
|
||||
|
|
|
@ -3,25 +3,11 @@ package network
|
|||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/google/gopacket/pcap"
|
||||
)
|
||||
|
||||
// only matches gateway lines
|
||||
var IPv4RouteParser = regexp.MustCompile("^.+\\s+.+\\s+\\d+\\s+([0-9\\.]+/\\d+)\\s+\\d+\\s+([0-9\\.]+).*$")
|
||||
var IPv4RouteTokens = 3
|
||||
var IPv4RouteCmd = "netsh"
|
||||
var IPv4RouteCmdOpts = []string{"interface", "ipv4", "show", "route"}
|
||||
|
||||
func IPv4RouteIsGateway(ifname string, tokens []string, f func(gateway string) (*Endpoint, error)) (*Endpoint, error) {
|
||||
// TODO check if the subnet is the same as iface ?
|
||||
// subnet := tokens[1]
|
||||
gateway := tokens[2]
|
||||
return f(gateway)
|
||||
}
|
||||
|
||||
/*
|
||||
* net.Interface does not have the correct name on Windows and pcap.Interface
|
||||
* does not have the hardware address for some reason ... so this is what I
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue