diff --git a/modules/wifi_recon.go b/modules/wifi_recon.go index 033fa7aa..866a0ebc 100644 --- a/modules/wifi_recon.go +++ b/modules/wifi_recon.go @@ -27,7 +27,6 @@ import ( type WiFiRecon struct { session.SessionModule - wifi *WiFi handle *pcap.Handle channel int client net.HardwareAddr @@ -79,9 +78,7 @@ func NewWiFiRecon(s *session.Session) *WiFiRecon { "Set 802.11 base station address to filter for.", func(args []string) error { var err error - if w.wifi != nil { - w.wifi.Clear() - } + w.Session.WiFi.Clear() w.accessPoint, err = net.ParseMAC(args[0]) return err })) @@ -89,9 +86,7 @@ func NewWiFiRecon(s *session.Session) *WiFiRecon { w.AddHandler(session.NewModuleHandler("wifi.recon clear bs", "", "Remove the 802.11 base station filter.", func(args []string) error { - if w.wifi != nil { - w.wifi.Clear() - } + w.Session.WiFi.Clear() w.accessPoint = make([]byte, 0) return nil })) @@ -121,7 +116,7 @@ func (w WiFiRecon) Author() string { return "Gianluca Braga " } -func (w *WiFiRecon) getRow(station *WiFiStation) []string { +func (w *WiFiRecon) getRow(station *network.WiFiStation) []string { sinceStarted := time.Since(w.Session.StartedAt) sinceFirstSeen := time.Since(station.FirstSeen) @@ -201,11 +196,7 @@ func (w *WiFiRecon) isClientSelected() bool { } func (w *WiFiRecon) Show(by string) error { - if w.wifi == nil { - return errors.New("WiFi is not yet initialized.") - } - - stations := w.wifi.List() + stations := w.Session.WiFi.List() if by == "seen" { sort.Sort(ByWiFiSeenSorter(stations)) } else if by == "essid" { @@ -273,8 +264,6 @@ func (w *WiFiRecon) Configure() error { log.Info("WiFi recon active with channel hopping.") } - w.wifi = NewWiFi(w.Session, w.Session.Interface) - return nil } @@ -322,11 +311,11 @@ func (w *WiFiRecon) startDeauth() error { log.Info("Deauth packets sent for client station %s.", w.client.String()) } else { // deauth every authenticated client - for _, station := range w.wifi.Stations { + for _, station := range w.Session.WiFi.Stations { w.sendDeauthPacket(w.accessPoint, station.HW) } - n := len(w.wifi.Stations) + n := len(w.Session.WiFi.Stations) if n == 0 { log.Warning("No associated clients detected yet, deauth packets not sent.") } else if n == 1 { @@ -344,7 +333,7 @@ func (w *WiFiRecon) discoverAccessPoints(radiotap *layers.RadioTap, dot11 *layer if ok, ssid := packets.Dot11ParseIDSSID(packet); ok == true { bssid := dot11.Address3.String() channel := mhz2chan(int(radiotap.ChannelFrequency)) - w.wifi.AddIfNew(ssid, bssid, true, channel, radiotap.DBMAntennaSignal) + w.Session.WiFi.AddIfNew(ssid, bssid, true, channel, radiotap.DBMAntennaSignal) } } @@ -353,7 +342,7 @@ func (w *WiFiRecon) discoverClients(radiotap *layers.RadioTap, dot11 *layers.Dot if packets.Dot11IsDataFor(dot11, ap) == true { src := dot11.Address2 channel := mhz2chan(int(radiotap.ChannelFrequency)) - w.wifi.AddIfNew("", src.String(), false, channel, radiotap.DBMAntennaSignal) + w.Session.WiFi.AddIfNew("", src.String(), false, channel, radiotap.DBMAntennaSignal) } } @@ -363,19 +352,19 @@ func (w *WiFiRecon) updateStats(dot11 *layers.Dot11, packet gopacket.Packet) { bytes := uint64(len(packet.Data())) dst := dot11.Address1.String() - if station, found := w.wifi.Stations[dst]; found == true { + if station, found := w.Session.WiFi.Stations[dst]; found == true { station.Received += bytes } src := dot11.Address2.String() - if station, found := w.wifi.Stations[src]; found == true { + if station, found := w.Session.WiFi.Stations[src]; found == true { station.Sent += bytes } } if ok, enc := packets.Dot11ParseEncryption(packet, dot11); ok == true { bssid := dot11.Address3.String() - if station, found := w.wifi.Stations[bssid]; found == true { + if station, found := w.Session.WiFi.Stations[bssid]; found == true { station.Encryption = strings.Join(enc, ", ") } } diff --git a/modules/wifi_recon_sort.go b/modules/wifi_recon_sort.go index 1af352f8..ae532627 100644 --- a/modules/wifi_recon_sort.go +++ b/modules/wifi_recon_sort.go @@ -1,6 +1,10 @@ package modules -type ByRSSISorter []*WiFiStation +import ( + "github.com/evilsocket/bettercap-ng/network" +) + +type ByRSSISorter []*network.WiFiStation func (a ByRSSISorter) Len() int { return len(a) } func (a ByRSSISorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] } @@ -11,7 +15,7 @@ func (a ByRSSISorter) Less(i, j int) bool { return a[i].RSSI > a[j].RSSI } -type ByChannelSorter []*WiFiStation +type ByChannelSorter []*network.WiFiStation func (a ByChannelSorter) Len() int { return len(a) } func (a ByChannelSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] } @@ -22,7 +26,7 @@ func (a ByChannelSorter) Less(i, j int) bool { return a[i].Channel < a[j].Channel } -type ByEssidSorter []*WiFiStation +type ByEssidSorter []*network.WiFiStation func (a ByEssidSorter) Len() int { return len(a) } func (a ByEssidSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] } @@ -33,7 +37,7 @@ func (a ByEssidSorter) Less(i, j int) bool { return a[i].ESSID() < a[j].ESSID() } -type ByWiFiSeenSorter []*WiFiStation +type ByWiFiSeenSorter []*network.WiFiStation func (a ByWiFiSeenSorter) Len() int { return len(a) } func (a ByWiFiSeenSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] } diff --git a/modules/wifi.go b/network/wifi.go similarity index 67% rename from modules/wifi.go rename to network/wifi.go index 1ccc595d..02deeba4 100644 --- a/modules/wifi.go +++ b/network/wifi.go @@ -1,23 +1,18 @@ -package modules +package network import ( "sync" "time" - - "github.com/evilsocket/bettercap-ng/network" - "github.com/evilsocket/bettercap-ng/session" ) type WiFi struct { sync.Mutex - Session *session.Session - Interface *network.Endpoint + Interface *Endpoint Stations map[string]*WiFiStation } -func NewWiFi(s *session.Session, iface *network.Endpoint) *WiFi { +func NewWiFi(iface *Endpoint) *WiFi { return &WiFi{ - Session: s, Interface: iface, Stations: make(map[string]*WiFiStation), } @@ -38,8 +33,7 @@ func (w *WiFi) Remove(mac string) { w.Lock() defer w.Unlock() - if station, found := w.Stations[mac]; found { - w.Session.Events.Add("wifi.station.lost", station) + if _, found := w.Stations[mac]; found { delete(w.Stations, mac) } } @@ -48,7 +42,7 @@ func (w *WiFi) AddIfNew(ssid, mac string, isAp bool, channel int, rssi int8) *Wi w.Lock() defer w.Unlock() - mac = network.NormalizeMac(mac) + mac = NormalizeMac(mac) if station, found := w.Stations[mac]; found { station.LastSeen = time.Now() station.RSSI = rssi @@ -58,8 +52,6 @@ func (w *WiFi) AddIfNew(ssid, mac string, isAp bool, channel int, rssi int8) *Wi newStation := NewWiFiStation(ssid, mac, isAp, channel, rssi) w.Stations[mac] = newStation - w.Session.Events.Add("wifi.station.new", newStation) - return nil } diff --git a/modules/wifi_station.go b/network/wifi_station.go similarity index 70% rename from modules/wifi_station.go rename to network/wifi_station.go index ef791000..fa6d9796 100644 --- a/modules/wifi_station.go +++ b/network/wifi_station.go @@ -1,11 +1,7 @@ -package modules - -import ( - "github.com/evilsocket/bettercap-ng/network" -) +package network type WiFiStation struct { - *network.Endpoint + *Endpoint IsAP bool Channel int RSSI int8 @@ -16,7 +12,7 @@ type WiFiStation struct { func NewWiFiStation(essid, bssid string, isAp bool, channel int, rssi int8) *WiFiStation { return &WiFiStation{ - Endpoint: network.NewEndpointNoResolve(network.MonitorModeAddress, bssid, essid, 0), + Endpoint: NewEndpointNoResolve(MonitorModeAddress, bssid, essid, 0), IsAP: isAp, Channel: channel, RSSI: rssi, diff --git a/session/session.go b/session/session.go index 47572903..6f34a621 100644 --- a/session/session.go +++ b/session/session.go @@ -39,6 +39,7 @@ type Session struct { Firewall firewall.FirewallManager `json:"-"` Env *Environment `json:"env"` Targets *Targets `json:"targets"` + WiFi *network.WiFi `json:"wifi"` Queue *packets.Queue `json:"packets"` Input *readline.Instance `json:"-"` StartedAt time.Time `json:"started_at"` @@ -375,6 +376,7 @@ func (s *Session) Start() error { s.Gateway = s.Interface } + s.WiFi = network.NewWiFi(s.Interface) s.Targets = NewTargets(s, s.Interface, s.Gateway) s.Firewall = firewall.Make(s.Interface)