diff --git a/modules/arp_spoof.go b/modules/arp_spoof.go index 25292c3d..4e0c49a1 100644 --- a/modules/arp_spoof.go +++ b/modules/arp_spoof.go @@ -6,7 +6,6 @@ import ( "time" "github.com/evilsocket/bettercap-ng/log" - "github.com/evilsocket/bettercap-ng/network" "github.com/evilsocket/bettercap-ng/packets" "github.com/evilsocket/bettercap-ng/session" @@ -64,41 +63,6 @@ func (p ArpSpoofer) Author() string { return "Simone Margaritelli " } -func (p *ArpSpoofer) getMAC(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(p.Session.Interface.Name(), ip.String(), false) - if err != nil && probe == true { - from := p.Session.Interface.IP - from_hw := p.Session.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 { - p.Session.Queue.Send(probe) - } - - time.Sleep(500 * time.Millisecond) - - mac, err = network.ArpLookup(p.Session.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 (p *ArpSpoofer) sendArp(saddr net.IP, smac net.HardwareAddr, check_running bool, probe bool) { for _, ip := range p.addresses { if check_running && p.Running() == false { @@ -109,7 +73,7 @@ func (p *ArpSpoofer) sendArp(saddr net.IP, smac net.HardwareAddr, check_running } // do we have this ip mac address? - hw, err := p.getMAC(ip, probe) + hw, err := findMAC(p.Session, ip, probe) if err != nil { log.Debug("Error while looking up hardware address for %s: %s", ip.String(), err) continue diff --git a/modules/syn_scan.go b/modules/syn_scan.go index fdafa8f7..4b29c867 100644 --- a/modules/syn_scan.go +++ b/modules/syn_scan.go @@ -101,41 +101,6 @@ func (s *SynScanner) Stop() error { return nil } -func (s *SynScanner) getMAC(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.Session.Interface.Name(), ip.String(), false) - if err != nil && probe == true { - from := s.Session.Interface.IP - from_hw := s.Session.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.Session.Queue.Send(probe) - } - - time.Sleep(500 * time.Millisecond) - - mac, err = network.ArpLookup(s.Session.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 *SynScanner) inRange(ip net.IP) bool { for _, a := range s.addresses { if a.Equal(ip) { @@ -207,7 +172,7 @@ func (s *SynScanner) synScan() error { // start sending SYN packets and wait for _, address := range s.addresses { - mac, err := s.getMAC(address, true) + mac, err := findMAC(s.Session, address, true) if err != nil { log.Debug("Could not get MAC for %s: %s", address.String(), err) continue diff --git a/modules/utils.go b/modules/utils.go new file mode 100644 index 00000000..bc7fb28c --- /dev/null +++ b/modules/utils.go @@ -0,0 +1,47 @@ +package modules + +import ( + "fmt" + "net" + "time" + + "github.com/evilsocket/bettercap-ng/log" + "github.com/evilsocket/bettercap-ng/network" + "github.com/evilsocket/bettercap-ng/packets" + "github.com/evilsocket/bettercap-ng/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 == true { + 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, err = 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 +}