mirror of
https://github.com/bettercap/bettercap
synced 2025-08-19 21:13:18 -07:00
fix: subnet bits were not correctly set while creating the interface endpoint object (ref #82)
This commit is contained in:
parent
f9489a8d4e
commit
053cff0dd5
2 changed files with 50 additions and 34 deletions
|
@ -4,6 +4,8 @@ import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/bettercap/bettercap/core"
|
"github.com/bettercap/bettercap/core"
|
||||||
|
@ -31,23 +33,15 @@ type Endpoint struct {
|
||||||
Meta *Meta `json:"meta"`
|
Meta *Meta `json:"meta"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func ip2int(ip net.IP) uint32 {
|
|
||||||
if len(ip) == 16 {
|
|
||||||
return binary.BigEndian.Uint32(ip[12:16])
|
|
||||||
}
|
|
||||||
return binary.BigEndian.Uint32(ip)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewEndpointNoResolve(ip, mac, name string, bits uint32) *Endpoint {
|
func NewEndpointNoResolve(ip, mac, name string, bits uint32) *Endpoint {
|
||||||
addr := net.ParseIP(ip)
|
|
||||||
mac = NormalizeMac(mac)
|
mac = NormalizeMac(mac)
|
||||||
hw, _ := net.ParseMAC(mac)
|
hw, _ := net.ParseMAC(mac)
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
|
||||||
e := &Endpoint{
|
e := &Endpoint{
|
||||||
IP: addr,
|
IP: nil,
|
||||||
IpAddress: ip,
|
IpAddress: ip,
|
||||||
IpAddressUint32: ip2int(addr),
|
IpAddressUint32: 0,
|
||||||
Net: nil,
|
Net: nil,
|
||||||
HW: hw,
|
HW: hw,
|
||||||
SubnetBits: bits,
|
SubnetBits: bits,
|
||||||
|
@ -60,8 +54,8 @@ func NewEndpointNoResolve(ip, mac, name string, bits uint32) *Endpoint {
|
||||||
Meta: NewMeta(),
|
Meta: NewMeta(),
|
||||||
}
|
}
|
||||||
|
|
||||||
_, netw, _ := net.ParseCIDR(e.CIDR())
|
e.SetIP(ip)
|
||||||
e.Net = netw
|
e.SetBits(bits)
|
||||||
|
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
@ -87,15 +81,48 @@ func NewEndpointWithAlias(ip, mac, alias string) *Endpoint {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ip2int(ip net.IP) uint32 {
|
||||||
|
if len(ip) == 16 {
|
||||||
|
return binary.BigEndian.Uint32(ip[12:16])
|
||||||
|
}
|
||||||
|
return binary.BigEndian.Uint32(ip)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Endpoint) SetNetwork(netw string) {
|
||||||
|
parts := strings.Split(netw, "/")
|
||||||
|
address := parts[0]
|
||||||
|
bits, _ := strconv.Atoi(parts[1])
|
||||||
|
|
||||||
|
t.SetIP(address)
|
||||||
|
t.SetBits(uint32(bits))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Endpoint) SetIPv6Network(netw string) {
|
||||||
|
parts := strings.SplitN(netw, "/", 2)
|
||||||
|
address := parts[0]
|
||||||
|
bits, _ := strconv.Atoi(parts[1])
|
||||||
|
|
||||||
|
t.IPv6 = net.ParseIP(address)
|
||||||
|
if t.IPv6 != nil {
|
||||||
|
t.Ip6Address = t.IPv6.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
t.SetBits(uint32(bits))
|
||||||
|
}
|
||||||
|
|
||||||
func (t *Endpoint) SetIP(ip string) {
|
func (t *Endpoint) SetIP(ip string) {
|
||||||
|
|
||||||
addr := net.ParseIP(ip)
|
addr := net.ParseIP(ip)
|
||||||
|
|
||||||
t.IP = addr
|
t.IP = addr
|
||||||
t.IpAddress = ip
|
t.IpAddress = ip
|
||||||
t.IpAddressUint32 = ip2int(addr)
|
t.IpAddressUint32 = ip2int(addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Endpoint) SetBits(bits uint32) {
|
||||||
|
t.SubnetBits = bits
|
||||||
|
_, netw, _ := net.ParseCIDR(t.CIDR())
|
||||||
|
t.Net = netw
|
||||||
|
}
|
||||||
|
|
||||||
func (t *Endpoint) Name() string {
|
func (t *Endpoint) Name() string {
|
||||||
return t.Hostname
|
return t.Hostname
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/bettercap/bettercap/core"
|
"github.com/bettercap/bettercap/core"
|
||||||
|
@ -49,33 +48,23 @@ func buildEndpointFromInterface(iface net.Interface) (*Endpoint, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
e := NewEndpointNoResolve(MonitorModeAddress, iface.HardwareAddr.String(), "", 0)
|
e := NewEndpointNoResolve(MonitorModeAddress, iface.HardwareAddr.String(), iface.Name, 0)
|
||||||
|
|
||||||
e.Hostname = iface.Name
|
|
||||||
e.Index = iface.Index
|
e.Index = iface.Index
|
||||||
|
|
||||||
for _, addr := range addrs {
|
for _, a := range addrs {
|
||||||
ip := addr.String()
|
address := a.String()
|
||||||
|
if IPv4Validator.MatchString(address) {
|
||||||
if IPv4Validator.MatchString(ip) {
|
if strings.IndexRune(address, '/') == -1 {
|
||||||
if strings.IndexRune(ip, '/') == -1 {
|
|
||||||
// plain ip
|
// plain ip
|
||||||
e.SetIP(ip)
|
e.SetIP(address)
|
||||||
} else {
|
} else {
|
||||||
// ip/bits
|
// ip/bits
|
||||||
parts := strings.Split(ip, "/")
|
e.SetNetwork(address)
|
||||||
ip_part := parts[0]
|
|
||||||
bits, _ := strconv.Atoi(parts[1])
|
|
||||||
|
|
||||||
e.SetIP(ip_part)
|
|
||||||
e.SubnetBits = uint32(bits)
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
parts := strings.SplitN(ip, "/", 2)
|
// ipv6/bits
|
||||||
e.IPv6 = net.ParseIP(parts[0])
|
e.SetIPv6Network(address)
|
||||||
if e.IPv6 != nil {
|
|
||||||
e.Ip6Address = e.IPv6.String()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue