new: wifi.recon will activate the interface if it's down instead of failing

This commit is contained in:
evilsocket 2019-02-06 09:42:48 +01:00
parent 3c798794eb
commit 003139a2e3
No known key found for this signature in database
GPG key ID: 1564D7F30393A456
3 changed files with 55 additions and 24 deletions

View file

@ -225,6 +225,12 @@ func (w WiFiModule) Author() string {
return "Gianluca Braga <matrix86@protonmail.com> && Simone Margaritelli <evilsocket@protonmail.com>>" return "Gianluca Braga <matrix86@protonmail.com> && Simone Margaritelli <evilsocket@protonmail.com>>"
} }
const (
// Ugly, but gopacket folks are not exporting pcap errors, so ...
// ref. https://github.com/google/gopacket/blob/96986c90e3e5c7e01deed713ff8058e357c0c047/pcap/pcap.go#L281
ErrIfaceNotUp = "Interface Not Up"
)
func (w *WiFiModule) Configure() error { func (w *WiFiModule) Configure() error {
var hopPeriod int var hopPeriod int
var err error var err error
@ -241,31 +247,44 @@ func (w *WiFiModule) Configure() error {
} }
} }
ifName := w.Session.Interface.Name()
if w.source != "" { if w.source != "" {
if w.handle, err = pcap.OpenOffline(w.source); err != nil { if w.handle, err = pcap.OpenOffline(w.source); err != nil {
return err return fmt.Errorf("error while opening file %s: %s", w.source, err)
} }
} else { } else {
ihandle, err := pcap.NewInactiveHandle(w.Session.Interface.Name()) for retry := 0; ; retry++ {
if err != nil { ihandle, err := pcap.NewInactiveHandle(ifName)
return err if err != nil {
} return fmt.Errorf("error while opening interface %s: %s", ifName, err)
defer ihandle.CleanUp() }
defer ihandle.CleanUp()
if err = ihandle.SetRFMon(true); err != nil { if err = ihandle.SetRFMon(true); err != nil {
return fmt.Errorf("Error while setting interface %s in monitor mode: %s", tui.Bold(w.Session.Interface.Name()), err) return fmt.Errorf("error while setting interface %s in monitor mode: %s", tui.Bold(ifName), err)
} else if err = ihandle.SetSnapLen(65536); err != nil { } else if err = ihandle.SetSnapLen(65536); err != nil {
return err return fmt.Errorf("error while settng span len: %s", err)
} }
/* /*
* We don't want to pcap.BlockForever otherwise pcap_close(handle) * We don't want to pcap.BlockForever otherwise pcap_close(handle)
* could hang waiting for a timeout to expire ... * could hang waiting for a timeout to expire ...
*/ */
readTimeout := 500 * time.Millisecond readTimeout := 500 * time.Millisecond
if err = ihandle.SetTimeout(readTimeout); err != nil { if err = ihandle.SetTimeout(readTimeout); err != nil {
return err return fmt.Errorf("error while setting timeout: %s", err)
} else if w.handle, err = ihandle.Activate(); err != nil { } else if w.handle, err = ihandle.Activate(); err != nil {
return err if retry == 0 && err.Error() == ErrIfaceNotUp {
log.Warning("interface %s is down, bringing it up ...", ifName)
if err := network.ActivateInterface(ifName); err != nil {
return err
}
continue
}
return fmt.Errorf("error while activating handle: %s", err)
}
break
} }
} }
@ -280,16 +299,16 @@ func (w *WiFiModule) Configure() error {
if w.source == "" { if w.source == "" {
// No channels setted, retrieve frequencies supported by the card // No channels setted, retrieve frequencies supported by the card
if len(w.frequencies) == 0 { if len(w.frequencies) == 0 {
if w.frequencies, err = network.GetSupportedFrequencies(w.Session.Interface.Name()); err != nil { if w.frequencies, err = network.GetSupportedFrequencies(ifName); err != nil {
return err return fmt.Errorf("error while getting supported frequencies of %s: %s", ifName, err)
} }
log.Debug("wifi supported frequencies: %v", w.frequencies) log.Debug("wifi supported frequencies: %v", w.frequencies)
// we need to start somewhere, this is just to check if // we need to start somewhere, this is just to check if
// this OS supports switching channel programmatically. // this OS supports switching channel programmatically.
if err = network.SetInterfaceChannel(w.Session.Interface.Name(), 1); err != nil { if err = network.SetInterfaceChannel(ifName, 1); err != nil {
return err return fmt.Errorf("error while initializing %s to channel 1: %s", ifName, err)
} }
log.Info("WiFi recon active with channel hopping.") log.Info("WiFi recon active with channel hopping.")
} }

View file

@ -7,6 +7,8 @@ import (
"regexp" "regexp"
"strings" "strings"
"github.com/bettercap/bettercap/core"
"github.com/evilsocket/islazy/data" "github.com/evilsocket/islazy/data"
"github.com/evilsocket/islazy/str" "github.com/evilsocket/islazy/str"
@ -256,3 +258,12 @@ func FindInterface(name string) (*Endpoint, error) {
return nil, ErrNoIfaces return nil, ErrNoIfaces
} }
func ActivateInterface(name string) error {
if out, err := core.Exec("ifconfig", []string{name, "up"}); err != nil {
return err
} else if out != "" {
return fmt.Errorf("unexpected output while activating interface %s: %s", name, out)
}
return nil
}

View file

@ -26,6 +26,7 @@ func NewARPTo(from net.IP, from_hw net.HardwareAddr, to net.IP, to_hw net.Hardwa
return eth, arp return eth, arp
} }
func NewARP(from net.IP, from_hw net.HardwareAddr, to net.IP, req uint16) (layers.Ethernet, layers.ARP) { func NewARP(from net.IP, from_hw net.HardwareAddr, to net.IP, req uint16) (layers.Ethernet, layers.ARP) {
return NewARPTo(from, from_hw, to, []byte{0, 0, 0, 0, 0, 0}, req) return NewARPTo(from, from_hw, to, []byte{0, 0, 0, 0, 0, 0}, req)
} }