misc: small fix or general refactoring i did not bother commenting

This commit is contained in:
evilsocket 2018-02-22 21:59:13 +01:00
parent 7c2dc38819
commit ee4d1e9b8a
3 changed files with 49 additions and 73 deletions

View file

@ -6,7 +6,6 @@ import (
"time" "time"
"github.com/evilsocket/bettercap-ng/log" "github.com/evilsocket/bettercap-ng/log"
"github.com/evilsocket/bettercap-ng/network"
"github.com/evilsocket/bettercap-ng/packets" "github.com/evilsocket/bettercap-ng/packets"
"github.com/evilsocket/bettercap-ng/session" "github.com/evilsocket/bettercap-ng/session"
@ -64,41 +63,6 @@ func (p ArpSpoofer) Author() string {
return "Simone Margaritelli <evilsocket@protonmail.com>" return "Simone Margaritelli <evilsocket@protonmail.com>"
} }
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) { func (p *ArpSpoofer) sendArp(saddr net.IP, smac net.HardwareAddr, check_running bool, probe bool) {
for _, ip := range p.addresses { for _, ip := range p.addresses {
if check_running && p.Running() == false { 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? // do we have this ip mac address?
hw, err := p.getMAC(ip, probe) hw, err := findMAC(p.Session, ip, probe)
if err != nil { if err != nil {
log.Debug("Error while looking up hardware address for %s: %s", ip.String(), err) log.Debug("Error while looking up hardware address for %s: %s", ip.String(), err)
continue continue

View file

@ -101,41 +101,6 @@ func (s *SynScanner) Stop() error {
return nil 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 { func (s *SynScanner) inRange(ip net.IP) bool {
for _, a := range s.addresses { for _, a := range s.addresses {
if a.Equal(ip) { if a.Equal(ip) {
@ -207,7 +172,7 @@ func (s *SynScanner) synScan() error {
// start sending SYN packets and wait // start sending SYN packets and wait
for _, address := range s.addresses { for _, address := range s.addresses {
mac, err := s.getMAC(address, true) mac, err := findMAC(s.Session, address, true)
if err != nil { if err != nil {
log.Debug("Could not get MAC for %s: %s", address.String(), err) log.Debug("Could not get MAC for %s: %s", address.String(), err)
continue continue

47
modules/utils.go Normal file
View file

@ -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
}