misc: refactored modules.findMAC to session.FindMAC

This commit is contained in:
evilsocket 2019-01-18 20:13:41 +01:00
commit 31895569f7
No known key found for this signature in database
GPG key ID: 1564D7F30393A456
4 changed files with 36 additions and 50 deletions

View file

@ -162,7 +162,7 @@ func (p *ArpSpoofer) unSpoof() error {
neighbours := list.Expand()
for _, address := range neighbours {
if !p.Session.Skip(address) {
if realMAC, err := findMAC(p.Session, address, false); err == nil {
if realMAC, err := p.Session.FindMAC(address, false); err == nil {
p.sendArp(address, realMAC, false, false)
}
}
@ -209,7 +209,7 @@ func (p *ArpSpoofer) sendArp(saddr net.IP, smac net.HardwareAddr, check_running
}
// do we have this ip mac address?
hw, err := findMAC(p.Session, ip, probe)
hw, err := p.Session.FindMAC(ip, probe)
if err != nil {
log.Debug("Could not find hardware address for %s, retrying in one second.", ip.String())
continue

View file

@ -223,7 +223,7 @@ func (s *SynScanner) synScan() error {
if !s.Running() {
break
}
mac, err := findMAC(s.Session, address, true)
mac, err := s.Session.FindMAC(address, true)
if err != nil {
atomic.AddUint64(&s.stats.doneProbes, s.stats.numPorts)
log.Debug("Could not get MAC for %s: %s", address.String(), err)

View file

@ -1,47 +0,0 @@
package modules
import (
"fmt"
"net"
"time"
"github.com/bettercap/bettercap/log"
"github.com/bettercap/bettercap/network"
"github.com/bettercap/bettercap/packets"
"github.com/bettercap/bettercap/session"
)
func findMAC(s *session.Session, ip net.IP, probe bool) (net.HardwareAddr, error) {
var mac string
var hw net.HardwareAddr
var err error
// do we have this ip mac address?
mac, err = network.ArpLookup(s.Interface.Name(), ip.String(), false)
if err != nil && probe {
from := s.Interface.IP
from_hw := s.Interface.HW
if err, probe := packets.NewUDPProbe(from, from_hw, ip, 139); err != nil {
log.Error("Error while creating UDP probe packet for %s: %s", ip.String(), err)
} else {
s.Queue.Send(probe)
}
time.Sleep(500 * time.Millisecond)
mac, _ = network.ArpLookup(s.Interface.Name(), ip.String(), false)
}
if mac == "" {
return nil, fmt.Errorf("Could not find hardware address for %s.", ip.String())
}
mac = network.NormalizeMac(mac)
hw, err = net.ParseMAC(mac)
if err != nil {
return nil, fmt.Errorf("Error while parsing hardware address '%s' for %s: %s", mac, ip.String(), err)
}
return hw, nil
}

View file

@ -285,6 +285,39 @@ func (s *Session) Skip(ip net.IP) bool {
return false
}
func (s *Session) FindMAC(ip net.IP, probe bool) (net.HardwareAddr, error) {
var mac string
var hw net.HardwareAddr
var err error
// do we have this ip mac address?
mac, err = network.ArpLookup(s.Interface.Name(), ip.String(), false)
if err != nil && probe {
from := s.Interface.IP
from_hw := s.Interface.HW
if err, probe := packets.NewUDPProbe(from, from_hw, ip, 139); err != nil {
log.Error("Error while creating UDP probe packet for %s: %s", ip.String(), err)
} else {
s.Queue.Send(probe)
}
time.Sleep(500 * time.Millisecond)
mac, _ = network.ArpLookup(s.Interface.Name(), ip.String(), false)
}
if mac == "" {
return nil, fmt.Errorf("Could not find hardware address for %s.", ip.String())
}
mac = network.NormalizeMac(mac)
hw, err = net.ParseMAC(mac)
if err != nil {
return nil, fmt.Errorf("Error while parsing hardware address '%s' for %s: %s", mac, ip.String(), err)
}
return hw, nil
}
func (s *Session) IsOn(moduleName string) bool {
for _, m := range s.Modules {
if m.Name() == moduleName {