new: aliases can now be used along with MACs and IP ranges for the arp.spoof.targets variable (ref #204)

This commit is contained in:
evilsocket 2018-03-26 11:38:22 +02:00
commit 54607993ba
No known key found for this signature in database
GPG key ID: 1564D7F30393A456
4 changed files with 75 additions and 42 deletions

View file

@ -8,6 +8,8 @@ import (
"strings"
"github.com/bettercap/bettercap/core"
"github.com/malfunkt/iprange"
)
var ErrNoIfaces = errors.New("No active interfaces found.")
@ -22,8 +24,13 @@ const (
)
var (
BroadcastHw = []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
IPv4Validator = regexp.MustCompile("^[0-9\\.]+/?\\d*$")
BroadcastHw = []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
IPv4Validator = regexp.MustCompile(`^[0-9\.]+/?\d*$`)
IPv4RangeValidator = regexp.MustCompile(`^[0-9\.\-]+/?\d*$`)
MACValidator = regexp.MustCompile(`(?i)^[a-f0-9]{1,2}:[a-f0-9]{1,2}:[a-f0-9]{1,2}:[a-f0-9]{1,2}:[a-f0-9]{1,2}:[a-f0-9]{1,2}$`)
// lulz this sounds like a hamburger
macParser = regexp.MustCompile(`(?i)([a-f0-9]{1,2}:[a-f0-9]{1,2}:[a-f0-9]{1,2}:[a-f0-9]{1,2}:[a-f0-9]{1,2}:[a-f0-9]{1,2})`)
aliasParser = regexp.MustCompile(`(?i)([a-z_][a-z_0-9]+)`)
)
func IsZeroMac(mac net.HardwareAddr) bool {
@ -60,6 +67,53 @@ func NormalizeMac(mac string) string {
return strings.ToLower(strings.Join(parts, ":"))
}
func ParseTargets(targets string, aliasMap *Aliases) (ips []net.IP, macs []net.HardwareAddr, err error) {
ips = make([]net.IP, 0)
macs = make([]net.HardwareAddr, 0)
// first isolate MACs and parse them
for _, mac := range macParser.FindAllString(targets, -1) {
mac = NormalizeMac(mac)
hw, err := net.ParseMAC(mac)
if err != nil {
return nil, nil, fmt.Errorf("Error while parsing MAC '%s': %s", mac, err)
}
macs = append(macs, hw)
targets = strings.Replace(targets, mac, "", -1)
}
targets = strings.Trim(targets, ", ")
// check and resolve aliases
for _, alias := range aliasParser.FindAllString(targets, -1) {
if mac, found := aliasMap.Find(alias); found == true {
mac = NormalizeMac(mac)
hw, err := net.ParseMAC(mac)
if err != nil {
return nil, nil, fmt.Errorf("Error while parsing MAC '%s': %s", mac, err)
}
macs = append(macs, hw)
targets = strings.Replace(targets, alias, "", -1)
} else {
return nil, nil, fmt.Errorf("Could not resolve alias %s", alias)
}
}
targets = strings.Trim(targets, ", ")
// parse what's left
if targets != "" {
list, err := iprange.ParseList(targets)
if err != nil {
return nil, nil, fmt.Errorf("Error while parsing address list '%s': %s.", targets, err)
}
ips = list.Expand()
}
return
}
func buildEndpointFromInterface(iface net.Interface) (*Endpoint, error) {
addrs, err := iface.Addrs()
if err != nil {