fix: better ipv6 detection logic

This commit is contained in:
Simone Margaritelli 2024-09-22 15:03:06 +02:00
parent 8446d66d12
commit a234c20650
5 changed files with 30 additions and 12 deletions

View file

@ -6,6 +6,7 @@ import (
"sync"
"github.com/bettercap/bettercap/v2/core"
"github.com/bettercap/bettercap/v2/log"
)
type ArpTable map[string]string
@ -33,6 +34,7 @@ func ArpUpdate(iface string) (ArpTable, error) {
for _, line := range strings.Split(output, "\n") {
m := ArpTableParser.FindStringSubmatch(line)
if len(m) == ArpTableTokens {
log.Debug("ARP TABLE MATCH: %v", m)
ipIndex := ArpTableTokenIndex[0]
hwIndex := ArpTableTokenIndex[1]
ifIndex := ArpTableTokenIndex[2]
@ -46,6 +48,7 @@ func ArpUpdate(iface string) (ArpTable, error) {
}
if ifname == iface {
log.Debug(" %s = %s", address, mac)
newTable[address] = mac
}
}

View file

@ -6,6 +6,7 @@ import (
"strings"
"sync"
"github.com/bettercap/bettercap/v2/log"
"github.com/evilsocket/islazy/data"
)
@ -136,11 +137,11 @@ func (lan *LAN) Remove(ip, mac string) {
func (lan *LAN) shouldIgnore(ip, mac string) bool {
// skip our own address
if ip == lan.iface.IpAddress || mac == lan.iface.HwAddress {
if ip == lan.iface.IpAddress || ip == lan.iface.Ip6Address || mac == lan.iface.HwAddress {
return true
}
// skip the gateway
if ip == lan.gateway.IpAddress || mac == lan.gateway.HwAddress {
if ip == lan.gateway.IpAddress || ip == lan.gateway.Ip6Address || mac == lan.gateway.HwAddress {
return true
}
// skip broadcast addresses
@ -190,6 +191,15 @@ func (lan *LAN) AddIfNew(ip, mac string) *Endpoint {
if lan.ttl[mac] < LANDefaultttl {
lan.ttl[mac]++
}
if strings.ContainsRune(ip, ':') && t.Ip6Address == "" {
log.Info("ipv6 %s detected for %s (%s)", ip, t.IpAddress, mac)
t.SetIPv6(ip)
} else if strings.ContainsRune(ip, '.') && t.IpAddress == "" {
log.Info("ipv4 %s detected for %s (%s)", ip, t.Ip6Address, mac)
t.SetIP(ip)
}
return t
}

View file

@ -17,11 +17,14 @@ type Endpoint struct {
Index int `json:"-"`
IP net.IP `json:"-"`
Net *net.IPNet `json:"-"`
Net6 *net.IPNet `json:"-"`
IPv6 net.IP `json:"-"`
CIDR6 string `json:"-"`
HW net.HardwareAddr `json:"-"`
IpAddress string `json:"ipv4"`
Ip6Address string `json:"ipv6"`
SubnetBits uint32 `json:"-"`
SubnetBits6 uint32 `json:"-"`
IpAddressUint32 uint32 `json:"-"`
HwAddress string `json:"mac"`
Hostname string `json:"hostname"`
@ -100,7 +103,13 @@ func (t *Endpoint) SetNetwork(netw string) {
func (t *Endpoint) SetIPv6(netw string) {
parts := strings.SplitN(netw, "/", 2)
address := parts[0]
// bits, _ := strconv.Atoi(parts[1])
if len(parts) > 1 {
bits6, _ := strconv.Atoi(parts[1])
t.SubnetBits6 = uint32(bits6)
t.CIDR6 = netw
_, netw, _ := net.ParseCIDR(netw)
t.Net6 = netw
}
t.IPv6 = net.ParseIP(address)
if t.IPv6 != nil {

View file

@ -196,16 +196,12 @@ func buildEndpointFromInterface(iface net.Interface) (*Endpoint, error) {
for _, a := range addrs {
address := a.String()
switch true {
case IPv4Validator.MatchString(address):
if IPv4Validator.MatchString(address) {
e.SetIP(address)
break
case IPv4BlockValidator.MatchString(address):
} else if IPv4BlockValidator.MatchString(address) {
e.SetNetwork(address)
break
default:
} else {
e.SetIPv6(address)
break
}
}

View file

@ -211,7 +211,7 @@ func (q *Queue) worker() {
// something coming from someone on the LAN
isFromMe := q.iface.IP.Equal(srcIP) || q.iface.IPv6.Equal(srcIP)
isFromLAN := q.iface.Net.Contains(srcIP)
isFromLAN := q.iface.Net.Contains(srcIP) || q.iface.Net6.Contains(srcIP)
if !isFromMe && isFromLAN {
meta := q.getPacketMeta(pkt)
q.trackActivity(eth, srcIP, meta, pktSize, true)
@ -219,7 +219,7 @@ func (q *Queue) worker() {
// something going to someone on the LAN
isToMe := q.iface.IP.Equal(dstIP) || q.iface.IPv6.Equal(dstIP)
isToLAN := q.iface.Net.Contains(dstIP)
isToLAN := q.iface.Net.Contains(dstIP) || q.iface.Net6.Contains(dstIP)
if !isToMe && isToLAN {
q.trackActivity(eth, dstIP, nil, pktSize, false)
}