mirror of
https://github.com/bettercap/bettercap
synced 2025-08-21 05:53:20 -07:00
Merge pull request #22 from melon3r/iproute2
Replace net-tools with iproute2
This commit is contained in:
commit
df120f5e21
6 changed files with 57 additions and 39 deletions
|
@ -4,3 +4,6 @@ import "regexp"
|
||||||
|
|
||||||
var ArpTableParser = regexp.MustCompile("^[^\\d\\.]+([\\d\\.]+).+\\s+([a-f0-9:]{11,17})\\s+on\\s+([^\\s]+)\\s+.+$")
|
var ArpTableParser = regexp.MustCompile("^[^\\d\\.]+([\\d\\.]+).+\\s+([a-f0-9:]{11,17})\\s+on\\s+([^\\s]+)\\s+.+$")
|
||||||
var ArpTableTokens = 4
|
var ArpTableTokens = 4
|
||||||
|
var ArpTableTokenIndex = []uint{1, 2, 3}
|
||||||
|
var ArpCmd = "arp"
|
||||||
|
var ArpCmdOpts = []string{"-a", "-n"}
|
||||||
|
|
|
@ -2,5 +2,8 @@ package net
|
||||||
|
|
||||||
import "regexp"
|
import "regexp"
|
||||||
|
|
||||||
var ArpTableParser = regexp.MustCompile("^[^\\d\\.]+([\\d\\.]+).+\\s+([a-f0-9:]{17}).+\\s+(.+)$")
|
var ArpTableParser = regexp.MustCompile("^([\\d\\.]+)\\s+dev\\s+(\\w+)\\s+\\w+\\s+([a-f0-9:]{17})\\s+\\w+$")
|
||||||
var ArpTableTokens = 4
|
var ArpTableTokens = 4
|
||||||
|
var ArpTableTokenIndex = []uint{1, 3, 2}
|
||||||
|
var ArpCmd = "ip"
|
||||||
|
var ArpCmdOpts = []string{"neigh"}
|
||||||
|
|
|
@ -12,8 +12,8 @@ func ArpUpdate(iface string) (ArpTable, error) {
|
||||||
// Signal we parsed the ARP table at least once.
|
// Signal we parsed the ARP table at least once.
|
||||||
arp_parsed = true
|
arp_parsed = true
|
||||||
|
|
||||||
// Run "arp -an" and parse the output.
|
// Run "arp -an" (darwin) or "ip neigh" (linux) and parse the output
|
||||||
output, err := core.Exec("arp", []string{"-a", "-n"})
|
output, err := core.Exec(ArpCmd, ArpCmdOpts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return arp_table, err
|
return arp_table, err
|
||||||
}
|
}
|
||||||
|
@ -22,9 +22,9 @@ func ArpUpdate(iface string) (ArpTable, error) {
|
||||||
for _, line := range strings.Split(output, "\n") {
|
for _, line := range strings.Split(output, "\n") {
|
||||||
m := ArpTableParser.FindStringSubmatch(line)
|
m := ArpTableParser.FindStringSubmatch(line)
|
||||||
if len(m) == ArpTableTokens {
|
if len(m) == ArpTableTokens {
|
||||||
address := m[1]
|
address := m[ArpTableTokenIndex[0]]
|
||||||
mac := m[2]
|
mac := m[ArpTableTokenIndex[1]]
|
||||||
ifname := m[3]
|
ifname := m[ArpTableTokenIndex[2]]
|
||||||
|
|
||||||
if ifname == iface {
|
if ifname == iface {
|
||||||
new_table[address] = mac
|
new_table[address] = mac
|
||||||
|
|
38
net/net.go
38
net/net.go
|
@ -4,20 +4,12 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"regexp"
|
"regexp"
|
||||||
"runtime"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/evilsocket/bettercap-ng/core"
|
"github.com/evilsocket/bettercap-ng/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
var IPv4RouteParser = regexp.MustCompile("^([\\d\\.]+)\\s+([\\d\\.]+)\\s+([\\d\\.]+)\\s+([A-Z]+)\\s+\\d+\\s+\\d+\\s+\\d+\\s+(.+)$")
|
|
||||||
var IPv4RouteTokens = 6
|
|
||||||
var IPv4RouteParserMac = regexp.MustCompile("^([a-z]+)+\\s+(\\d+\\.+\\d+.\\d.+\\d)+\\s+([a-zA-z]+)+\\s+(\\d+)+\\s+(\\d+)+\\s+([a-zA-Z]+\\d+)$")
|
|
||||||
var IPv4RouteTokensMac = 7
|
|
||||||
var IPv4RouteGWFlags = "UG"
|
|
||||||
var IPv4RouteGWFlagsMac = "UGSc"
|
|
||||||
|
|
||||||
func FindInterface(name string) (*Endpoint, error) {
|
func FindInterface(name string) (*Endpoint, error) {
|
||||||
ifaces, err := net.Interfaces()
|
ifaces, err := net.Interfaces()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -75,35 +67,15 @@ func FindInterface(name string) (*Endpoint, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindGateway(iface *Endpoint) (*Endpoint, error) {
|
func FindGateway(iface *Endpoint) (*Endpoint, error) {
|
||||||
var routeParser = IPv4RouteParser
|
output, err := core.Exec(IPv4RouteCmd, IPv4RouteCmdOpts)
|
||||||
var routeTokens = IPv4RouteTokens
|
|
||||||
var flagsIndex = 4
|
|
||||||
var ifnameIndex = 5
|
|
||||||
var gwFlags = IPv4RouteGWFlags
|
|
||||||
|
|
||||||
if runtime.GOOS == "darwin" {
|
|
||||||
// "MacOS detected"
|
|
||||||
routeParser = IPv4RouteParserMac
|
|
||||||
routeTokens = IPv4RouteTokensMac
|
|
||||||
flagsIndex = 3
|
|
||||||
ifnameIndex = 6
|
|
||||||
gwFlags = IPv4RouteGWFlagsMac
|
|
||||||
}
|
|
||||||
|
|
||||||
output, err := core.Exec("netstat", []string{"-n", "-r"})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, line := range strings.Split(output, "\n") {
|
for _, line := range strings.Split(output, "\n") {
|
||||||
m := routeParser.FindStringSubmatch(line)
|
m := IPv4RouteParser.FindStringSubmatch(line)
|
||||||
if len(m) == routeTokens {
|
if len(m) == IPv4RouteTokens {
|
||||||
// destination := m[1]
|
return IPv4RouteIsGateway(iface.Name(), m, func(gateway string) (*Endpoint, error) {
|
||||||
// mask := m[3]
|
|
||||||
flags := m[flagsIndex]
|
|
||||||
ifname := m[ifnameIndex]
|
|
||||||
if ifname == iface.Name() && flags == gwFlags {
|
|
||||||
gateway := m[2]
|
|
||||||
if gateway == iface.IpAddress {
|
if gateway == iface.IpAddress {
|
||||||
return iface, nil
|
return iface, nil
|
||||||
} else {
|
} else {
|
||||||
|
@ -114,7 +86,7 @@ func FindGateway(iface *Endpoint) (*Endpoint, error) {
|
||||||
}
|
}
|
||||||
return NewEndpoint(gateway, mac), nil
|
return NewEndpoint(gateway, mac), nil
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
20
net/net_darwin.go
Normal file
20
net/net_darwin.go
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
package net
|
||||||
|
|
||||||
|
import "regexp"
|
||||||
|
|
||||||
|
var IPv4RouteParser = regexp.MustCompile("^([a-z]+)+\\s+(\\d+\\.+\\d+.\\d.+\\d)+\\s+([a-zA-z]+)+\\s+(\\d+)+\\s+(\\d+)+\\s+([a-zA-Z]+\\d+)$")
|
||||||
|
var IPv4RouteTokens = 7
|
||||||
|
var IPv4RouteCmd = "netstat"
|
||||||
|
var IPv4RouteCmdOpts = []string{"-n", "-r"}
|
||||||
|
|
||||||
|
func IPv4RouteIsGateway(ifname string, tokens []string, f func(gateway string) (*Endpoint, error)) (*Endpoint, error) {
|
||||||
|
ifname2 := tokens[6]
|
||||||
|
flags := tokens[3]
|
||||||
|
|
||||||
|
if ifname == ifname2 && flags == "UGSc" {
|
||||||
|
gateway := tokens[2]
|
||||||
|
return f(gateway)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, nil
|
||||||
|
}
|
20
net/net_linux.go
Normal file
20
net/net_linux.go
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
package net
|
||||||
|
|
||||||
|
import "regexp"
|
||||||
|
|
||||||
|
// only matches gateway lines
|
||||||
|
var IPv4RouteParser = regexp.MustCompile("^(default|[0-9\\.]+)\\svia\\s([0-9\\.]+)\\sdev\\s(\\w+)\\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
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue