fix: subnet bits were not correctly set while creating the interface endpoint object (ref #82)

This commit is contained in:
evilsocket 2018-02-25 13:40:52 +01:00
commit 053cff0dd5
2 changed files with 50 additions and 34 deletions

View file

@ -4,6 +4,8 @@ import (
"encoding/binary"
"fmt"
"net"
"strconv"
"strings"
"time"
"github.com/bettercap/bettercap/core"
@ -31,23 +33,15 @@ type Endpoint struct {
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 {
addr := net.ParseIP(ip)
mac = NormalizeMac(mac)
hw, _ := net.ParseMAC(mac)
now := time.Now()
e := &Endpoint{
IP: addr,
IP: nil,
IpAddress: ip,
IpAddressUint32: ip2int(addr),
IpAddressUint32: 0,
Net: nil,
HW: hw,
SubnetBits: bits,
@ -60,8 +54,8 @@ func NewEndpointNoResolve(ip, mac, name string, bits uint32) *Endpoint {
Meta: NewMeta(),
}
_, netw, _ := net.ParseCIDR(e.CIDR())
e.Net = netw
e.SetIP(ip)
e.SetBits(bits)
return e
}
@ -87,15 +81,48 @@ func NewEndpointWithAlias(ip, mac, alias string) *Endpoint {
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) {
addr := net.ParseIP(ip)
t.IP = addr
t.IpAddress = ip
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 {
return t.Hostname
}

View file

@ -5,7 +5,6 @@ import (
"fmt"
"net"
"regexp"
"strconv"
"strings"
"github.com/bettercap/bettercap/core"
@ -49,33 +48,23 @@ func buildEndpointFromInterface(iface net.Interface) (*Endpoint, error) {
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
for _, addr := range addrs {
ip := addr.String()
if IPv4Validator.MatchString(ip) {
if strings.IndexRune(ip, '/') == -1 {
for _, a := range addrs {
address := a.String()
if IPv4Validator.MatchString(address) {
if strings.IndexRune(address, '/') == -1 {
// plain ip
e.SetIP(ip)
e.SetIP(address)
} else {
// ip/bits
parts := strings.Split(ip, "/")
ip_part := parts[0]
bits, _ := strconv.Atoi(parts[1])
e.SetIP(ip_part)
e.SubnetBits = uint32(bits)
e.SetNetwork(address)
}
} else {
parts := strings.SplitN(ip, "/", 2)
e.IPv6 = net.ParseIP(parts[0])
if e.IPv6 != nil {
e.Ip6Address = e.IPv6.String()
}
// ipv6/bits
e.SetIPv6Network(address)
}
}