From 63a07cf2626a1be92697cc561df1dd3a9791cbe0 Mon Sep 17 00:00:00 2001 From: evilsocket Date: Mon, 12 Mar 2018 15:51:23 +0100 Subject: [PATCH] misc: small fix or general refactoring i did not bother commenting --- modules/wifi_hopping.go | 69 +++++++++++++++++++++++++++++++++++++++++ modules/wifi_recon.go | 61 ------------------------------------ 2 files changed, 69 insertions(+), 61 deletions(-) create mode 100644 modules/wifi_hopping.go diff --git a/modules/wifi_hopping.go b/modules/wifi_hopping.go new file mode 100644 index 00000000..79d9b454 --- /dev/null +++ b/modules/wifi_hopping.go @@ -0,0 +1,69 @@ +package modules + +import ( + "time" + + "github.com/bettercap/bettercap/log" + "github.com/bettercap/bettercap/network" +) + +func mhz2chan(freq int) int { + // ambo! + if freq <= 2472 { + return ((freq - 2412) / 5) + 1 + } else if freq == 2484 { + return 14 + } else if freq >= 5035 && freq <= 5865 { + return ((freq - 5035) / 5) + 7 + } + return 0 +} + +func (w *WiFiModule) onChannel(channel int, cb func()) { + prev := w.stickChan + w.stickChan = channel + + if err := network.SetInterfaceChannel(w.Session.Interface.Name(), channel); err != nil { + log.Warning("Error while hopping to channel %d: %s", channel, err) + } else { + log.Debug("Hopped on channel %d", channel) + } + + cb() + + w.stickChan = prev +} + +func (w *WiFiModule) channelHopper() { + w.reads.Add(1) + defer w.reads.Done() + + log.Info("Channel hopper started.") + for w.Running() == true { + delay := w.hopPeriod + // if we have both 2.4 and 5ghz capabilities, we have + // more channels, therefore we need to increase the time + // we hop on each one otherwise me lose information + if len(w.frequencies) > 14 { + delay = 500 * time.Millisecond + } + + for _, frequency := range w.frequencies { + channel := mhz2chan(frequency) + // stick to the access point channel as long as it's selected + // or as long as we're deauthing on it + if w.stickChan != 0 { + channel = w.stickChan + } + + if err := network.SetInterfaceChannel(w.Session.Interface.Name(), channel); err != nil { + log.Warning("Error while hopping to channel %d: %s", channel, err) + } + + time.Sleep(delay) + if w.Running() == false { + return + } + } + } +} diff --git a/modules/wifi_recon.go b/modules/wifi_recon.go index 4cbc5978..6f2839b4 100644 --- a/modules/wifi_recon.go +++ b/modules/wifi_recon.go @@ -138,18 +138,6 @@ func (w WiFiModule) Author() string { return "Gianluca Braga && Simone Margaritelli >" } -func mhz2chan(freq int) int { - // ambo! - if freq <= 2472 { - return ((freq - 2412) / 5) + 1 - } else if freq == 2484 { - return 14 - } else if freq >= 5035 && freq <= 5865 { - return ((freq - 5035) / 5) + 7 - } - return 0 -} - func (w *WiFiModule) Configure() error { var hopPeriod int var err error @@ -214,21 +202,6 @@ func (w *WiFiModule) Configure() error { return nil } -func (w *WiFiModule) onChannel(channel int, cb func()) { - prev := w.stickChan - w.stickChan = channel - - if err := network.SetInterfaceChannel(w.Session.Interface.Name(), channel); err != nil { - log.Warning("Error while hopping to channel %d: %s", channel, err) - } else { - log.Debug("Hopped on channel %d", channel) - } - - cb() - - w.stickChan = prev -} - func isZeroBSSID(bssid net.HardwareAddr) bool { for _, b := range bssid { if b != 0x00 { @@ -331,40 +304,6 @@ func (w *WiFiModule) updateStats(dot11 *layers.Dot11, packet gopacket.Packet) { } } -func (w *WiFiModule) channelHopper() { - w.reads.Add(1) - defer w.reads.Done() - - log.Info("Channel hopper started.") - for w.Running() == true { - delay := w.hopPeriod - // if we have both 2.4 and 5ghz capabilities, we have - // more channels, therefore we need to increase the time - // we hop on each one otherwise me lose information - if len(w.frequencies) > 14 { - delay = 500 * time.Millisecond - } - - for _, frequency := range w.frequencies { - channel := mhz2chan(frequency) - // stick to the access point channel as long as it's selected - // or as long as we're deauthing on it - if w.stickChan != 0 { - channel = w.stickChan - } - - if err := network.SetInterfaceChannel(w.Session.Interface.Name(), channel); err != nil { - log.Warning("Error while hopping to channel %d: %s", channel, err) - } - - time.Sleep(delay) - if w.Running() == false { - return - } - } - } -} - func (w *WiFiModule) stationPruner() { w.reads.Add(1) defer w.reads.Done()