refact: using global wifi objects in order to expose them from the api.rest module

This commit is contained in:
evilsocket 2018-02-17 05:44:25 +01:00
parent 9390a580fd
commit e895dc6ab2
5 changed files with 29 additions and 46 deletions

View file

@ -27,7 +27,6 @@ import (
type WiFiRecon struct { type WiFiRecon struct {
session.SessionModule session.SessionModule
wifi *WiFi
handle *pcap.Handle handle *pcap.Handle
channel int channel int
client net.HardwareAddr client net.HardwareAddr
@ -79,9 +78,7 @@ func NewWiFiRecon(s *session.Session) *WiFiRecon {
"Set 802.11 base station address to filter for.", "Set 802.11 base station address to filter for.",
func(args []string) error { func(args []string) error {
var err error var err error
if w.wifi != nil { w.Session.WiFi.Clear()
w.wifi.Clear()
}
w.accessPoint, err = net.ParseMAC(args[0]) w.accessPoint, err = net.ParseMAC(args[0])
return err return err
})) }))
@ -89,9 +86,7 @@ func NewWiFiRecon(s *session.Session) *WiFiRecon {
w.AddHandler(session.NewModuleHandler("wifi.recon clear bs", "", w.AddHandler(session.NewModuleHandler("wifi.recon clear bs", "",
"Remove the 802.11 base station filter.", "Remove the 802.11 base station filter.",
func(args []string) error { func(args []string) error {
if w.wifi != nil { w.Session.WiFi.Clear()
w.wifi.Clear()
}
w.accessPoint = make([]byte, 0) w.accessPoint = make([]byte, 0)
return nil return nil
})) }))
@ -121,7 +116,7 @@ func (w WiFiRecon) Author() string {
return "Gianluca Braga <matrix86@protonmail.com>" return "Gianluca Braga <matrix86@protonmail.com>"
} }
func (w *WiFiRecon) getRow(station *WiFiStation) []string { func (w *WiFiRecon) getRow(station *network.WiFiStation) []string {
sinceStarted := time.Since(w.Session.StartedAt) sinceStarted := time.Since(w.Session.StartedAt)
sinceFirstSeen := time.Since(station.FirstSeen) sinceFirstSeen := time.Since(station.FirstSeen)
@ -201,11 +196,7 @@ func (w *WiFiRecon) isClientSelected() bool {
} }
func (w *WiFiRecon) Show(by string) error { func (w *WiFiRecon) Show(by string) error {
if w.wifi == nil { stations := w.Session.WiFi.List()
return errors.New("WiFi is not yet initialized.")
}
stations := w.wifi.List()
if by == "seen" { if by == "seen" {
sort.Sort(ByWiFiSeenSorter(stations)) sort.Sort(ByWiFiSeenSorter(stations))
} else if by == "essid" { } else if by == "essid" {
@ -273,8 +264,6 @@ func (w *WiFiRecon) Configure() error {
log.Info("WiFi recon active with channel hopping.") log.Info("WiFi recon active with channel hopping.")
} }
w.wifi = NewWiFi(w.Session, w.Session.Interface)
return nil return nil
} }
@ -322,11 +311,11 @@ func (w *WiFiRecon) startDeauth() error {
log.Info("Deauth packets sent for client station %s.", w.client.String()) log.Info("Deauth packets sent for client station %s.", w.client.String())
} else { } else {
// deauth every authenticated client // deauth every authenticated client
for _, station := range w.wifi.Stations { for _, station := range w.Session.WiFi.Stations {
w.sendDeauthPacket(w.accessPoint, station.HW) w.sendDeauthPacket(w.accessPoint, station.HW)
} }
n := len(w.wifi.Stations) n := len(w.Session.WiFi.Stations)
if n == 0 { if n == 0 {
log.Warning("No associated clients detected yet, deauth packets not sent.") log.Warning("No associated clients detected yet, deauth packets not sent.")
} else if n == 1 { } 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 { if ok, ssid := packets.Dot11ParseIDSSID(packet); ok == true {
bssid := dot11.Address3.String() bssid := dot11.Address3.String()
channel := mhz2chan(int(radiotap.ChannelFrequency)) 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 { if packets.Dot11IsDataFor(dot11, ap) == true {
src := dot11.Address2 src := dot11.Address2
channel := mhz2chan(int(radiotap.ChannelFrequency)) 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())) bytes := uint64(len(packet.Data()))
dst := dot11.Address1.String() 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 station.Received += bytes
} }
src := dot11.Address2.String() 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 station.Sent += bytes
} }
} }
if ok, enc := packets.Dot11ParseEncryption(packet, dot11); ok == true { if ok, enc := packets.Dot11ParseEncryption(packet, dot11); ok == true {
bssid := dot11.Address3.String() 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, ", ") station.Encryption = strings.Join(enc, ", ")
} }
} }

View file

@ -1,6 +1,10 @@
package modules 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) Len() int { return len(a) }
func (a ByRSSISorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] } 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 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) Len() int { return len(a) }
func (a ByChannelSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] } 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 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) Len() int { return len(a) }
func (a ByEssidSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] } 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() 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) Len() int { return len(a) }
func (a ByWiFiSeenSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a ByWiFiSeenSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

View file

@ -1,23 +1,18 @@
package modules package network
import ( import (
"sync" "sync"
"time" "time"
"github.com/evilsocket/bettercap-ng/network"
"github.com/evilsocket/bettercap-ng/session"
) )
type WiFi struct { type WiFi struct {
sync.Mutex sync.Mutex
Session *session.Session Interface *Endpoint
Interface *network.Endpoint
Stations map[string]*WiFiStation Stations map[string]*WiFiStation
} }
func NewWiFi(s *session.Session, iface *network.Endpoint) *WiFi { func NewWiFi(iface *Endpoint) *WiFi {
return &WiFi{ return &WiFi{
Session: s,
Interface: iface, Interface: iface,
Stations: make(map[string]*WiFiStation), Stations: make(map[string]*WiFiStation),
} }
@ -38,8 +33,7 @@ func (w *WiFi) Remove(mac string) {
w.Lock() w.Lock()
defer w.Unlock() defer w.Unlock()
if station, found := w.Stations[mac]; found { if _, found := w.Stations[mac]; found {
w.Session.Events.Add("wifi.station.lost", station)
delete(w.Stations, mac) delete(w.Stations, mac)
} }
} }
@ -48,7 +42,7 @@ func (w *WiFi) AddIfNew(ssid, mac string, isAp bool, channel int, rssi int8) *Wi
w.Lock() w.Lock()
defer w.Unlock() defer w.Unlock()
mac = network.NormalizeMac(mac) mac = NormalizeMac(mac)
if station, found := w.Stations[mac]; found { if station, found := w.Stations[mac]; found {
station.LastSeen = time.Now() station.LastSeen = time.Now()
station.RSSI = rssi 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) newStation := NewWiFiStation(ssid, mac, isAp, channel, rssi)
w.Stations[mac] = newStation w.Stations[mac] = newStation
w.Session.Events.Add("wifi.station.new", newStation)
return nil return nil
} }

View file

@ -1,11 +1,7 @@
package modules package network
import (
"github.com/evilsocket/bettercap-ng/network"
)
type WiFiStation struct { type WiFiStation struct {
*network.Endpoint *Endpoint
IsAP bool IsAP bool
Channel int Channel int
RSSI int8 RSSI int8
@ -16,7 +12,7 @@ type WiFiStation struct {
func NewWiFiStation(essid, bssid string, isAp bool, channel int, rssi int8) *WiFiStation { func NewWiFiStation(essid, bssid string, isAp bool, channel int, rssi int8) *WiFiStation {
return &WiFiStation{ return &WiFiStation{
Endpoint: network.NewEndpointNoResolve(network.MonitorModeAddress, bssid, essid, 0), Endpoint: NewEndpointNoResolve(MonitorModeAddress, bssid, essid, 0),
IsAP: isAp, IsAP: isAp,
Channel: channel, Channel: channel,
RSSI: rssi, RSSI: rssi,

View file

@ -39,6 +39,7 @@ type Session struct {
Firewall firewall.FirewallManager `json:"-"` Firewall firewall.FirewallManager `json:"-"`
Env *Environment `json:"env"` Env *Environment `json:"env"`
Targets *Targets `json:"targets"` Targets *Targets `json:"targets"`
WiFi *network.WiFi `json:"wifi"`
Queue *packets.Queue `json:"packets"` Queue *packets.Queue `json:"packets"`
Input *readline.Instance `json:"-"` Input *readline.Instance `json:"-"`
StartedAt time.Time `json:"started_at"` StartedAt time.Time `json:"started_at"`
@ -375,6 +376,7 @@ func (s *Session) Start() error {
s.Gateway = s.Interface s.Gateway = s.Interface
} }
s.WiFi = network.NewWiFi(s.Interface)
s.Targets = NewTargets(s, s.Interface, s.Gateway) s.Targets = NewTargets(s, s.Interface, s.Gateway)
s.Firewall = firewall.Make(s.Interface) s.Firewall = firewall.Make(s.Interface)