mirror of
https://github.com/bettercap/bettercap
synced 2025-08-19 13:09:49 -07:00
refact: i will just comment with 'Thank you Microsoft <3'
This commit is contained in:
parent
52d7ccf3cd
commit
8a5916c685
4 changed files with 53 additions and 39 deletions
37
net/net.go
37
net/net.go
|
@ -4,51 +4,14 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"regexp"
|
"regexp"
|
||||||
"runtime"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/evilsocket/bettercap-ng/core"
|
"github.com/evilsocket/bettercap-ng/core"
|
||||||
|
|
||||||
"github.com/google/gopacket/pcap"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const MonitorModeAddress = "0.0.0.0"
|
const MonitorModeAddress = "0.0.0.0"
|
||||||
|
|
||||||
func areTheSame(iface net.Interface, piface pcap.Interface) bool {
|
|
||||||
if addrs, err := iface.Addrs(); err == nil {
|
|
||||||
for _, ia := range addrs {
|
|
||||||
for _, ib := range piface.Addresses {
|
|
||||||
if ia.String() == ib.IP.String() || strings.HasPrefix(ia.String(), ib.IP.String()) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func getInterfaceName(iface net.Interface) string {
|
|
||||||
// all normal operating systems
|
|
||||||
if runtime.GOOS != "windows" {
|
|
||||||
return iface.Name
|
|
||||||
}
|
|
||||||
|
|
||||||
// Microsoft Windows
|
|
||||||
devs, err := pcap.FindAllDevs()
|
|
||||||
if err != nil {
|
|
||||||
return iface.Name
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, dev := range devs {
|
|
||||||
if areTheSame(iface, dev) {
|
|
||||||
return dev.Name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return iface.Name
|
|
||||||
}
|
|
||||||
|
|
||||||
func NormalizeMac(mac string) string {
|
func NormalizeMac(mac string) string {
|
||||||
var parts []string
|
var parts []string
|
||||||
if strings.ContainsRune(mac, '-') {
|
if strings.ContainsRune(mac, '-') {
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package net
|
package net
|
||||||
|
|
||||||
import "regexp"
|
import (
|
||||||
|
"net"
|
||||||
|
"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 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 IPv4RouteTokens = 7
|
||||||
|
@ -18,3 +21,8 @@ func IPv4RouteIsGateway(ifname string, tokens []string, f func(gateway string) (
|
||||||
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// see Windows version to understand why ....
|
||||||
|
func getInterfaceName(iface net.Interface) string {
|
||||||
|
return iface.Name
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package net
|
package net
|
||||||
|
|
||||||
import "regexp"
|
import (
|
||||||
|
"net"
|
||||||
|
"regexp"
|
||||||
|
)
|
||||||
|
|
||||||
// only matches gateway lines
|
// only matches gateway lines
|
||||||
var IPv4RouteParser = regexp.MustCompile("^(default|[0-9\\.]+)\\svia\\s([0-9\\.]+)\\sdev\\s(\\w+)\\s.*$")
|
var IPv4RouteParser = regexp.MustCompile("^(default|[0-9\\.]+)\\svia\\s([0-9\\.]+)\\sdev\\s(\\w+)\\s.*$")
|
||||||
|
@ -18,3 +21,8 @@ func IPv4RouteIsGateway(ifname string, tokens []string, f func(gateway string) (
|
||||||
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// see Windows version to understand why ....
|
||||||
|
func getInterfaceName(iface net.Interface) string {
|
||||||
|
return iface.Name
|
||||||
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package net
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
// only matches gateway lines
|
// only matches gateway lines
|
||||||
|
@ -16,3 +17,37 @@ func IPv4RouteIsGateway(ifname string, tokens []string, f func(gateway string) (
|
||||||
gateway := tokens[2]
|
gateway := tokens[2]
|
||||||
return f(gateway)
|
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
|
||||||
|
* had to do in Windows ... tnx Microsoft <3
|
||||||
|
*/
|
||||||
|
|
||||||
|
func areTheSame(iface net.Interface, piface pcap.Interface) bool {
|
||||||
|
if addrs, err := iface.Addrs(); err == nil {
|
||||||
|
for _, ia := range addrs {
|
||||||
|
for _, ib := range piface.Addresses {
|
||||||
|
if ia.String() == ib.IP.String() || strings.HasPrefix(ia.String(), ib.IP.String()) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func getInterfaceName(iface net.Interface) string {
|
||||||
|
devs, err := pcap.FindAllDevs()
|
||||||
|
if err != nil {
|
||||||
|
return iface.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, dev := range devs {
|
||||||
|
if areTheSame(iface, dev) {
|
||||||
|
return dev.Name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return iface.Name
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue