mirror of
https://github.com/bettercap/bettercap
synced 2025-08-19 21:13:18 -07:00
misc: small fix or general refactoring i did not bother commenting
This commit is contained in:
parent
fa104c0877
commit
de61c2a1d2
3 changed files with 104 additions and 150 deletions
|
@ -28,7 +28,7 @@ const MAC48Validator = "((?:[0-9A-Fa-f]{2}[:-]){5}(?:[0-9A-Fa-f]{2}))"
|
|||
|
||||
type WDiscovery struct {
|
||||
session.SessionModule
|
||||
Targets *WlanTargets
|
||||
Stations *WiFi
|
||||
|
||||
ClientTarget net.HardwareAddr
|
||||
BSTarget net.HardwareAddr
|
||||
|
@ -81,8 +81,8 @@ func NewWDiscovery(s *session.Session) *WDiscovery {
|
|||
"Set 802.11 base station address to filter for.",
|
||||
func(args []string) error {
|
||||
var err error
|
||||
if w.Targets != nil {
|
||||
w.Targets.ClearAll()
|
||||
if w.Stations != nil {
|
||||
w.Stations.Clear()
|
||||
}
|
||||
w.BSTarget, err = net.ParseMAC(args[0])
|
||||
return err
|
||||
|
@ -91,8 +91,8 @@ func NewWDiscovery(s *session.Session) *WDiscovery {
|
|||
w.AddHandler(session.NewModuleHandler("wlan.recon clear bs", "",
|
||||
"Remove the 802.11 base station filter.",
|
||||
func(args []string) error {
|
||||
if w.Targets != nil {
|
||||
w.Targets.ClearAll()
|
||||
if w.Stations != nil {
|
||||
w.Stations.Clear()
|
||||
}
|
||||
w.BSTarget = make([]byte, 0)
|
||||
return nil
|
||||
|
@ -119,15 +119,12 @@ func (w WDiscovery) Author() string {
|
|||
return "Gianluca Braga <matrix86@protonmail.com>"
|
||||
}
|
||||
|
||||
func (w *WDiscovery) getRow(e *WlanEndpoint) []string {
|
||||
func (w *WDiscovery) getRow(e *WirelessStation) []string {
|
||||
sinceStarted := time.Since(w.Session.StartedAt)
|
||||
sinceFirstSeen := time.Since(e.Endpoint.FirstSeen)
|
||||
|
||||
mac := e.Endpoint.HwAddress
|
||||
if w.Targets.WasMissed(e.Endpoint.HwAddress) == true {
|
||||
// if endpoint was not found at least once
|
||||
mac = core.Dim(mac)
|
||||
} else if sinceStarted > (justJoinedTimeInterval*2) && sinceFirstSeen <= justJoinedTimeInterval {
|
||||
if sinceStarted > (justJoinedTimeInterval*2) && sinceFirstSeen <= justJoinedTimeInterval {
|
||||
// if endpoint was first seen in the last 10 seconds
|
||||
mac = core.Bold(mac)
|
||||
}
|
||||
|
@ -171,7 +168,7 @@ func WlanMhzToChannel(freq int) int {
|
|||
return 0
|
||||
}
|
||||
|
||||
type ByEssidSorter []*WlanEndpoint
|
||||
type ByEssidSorter []*WirelessStation
|
||||
|
||||
func (a ByEssidSorter) Len() int { return len(a) }
|
||||
func (a ByEssidSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
|
@ -182,7 +179,7 @@ func (a ByEssidSorter) Less(i, j int) bool {
|
|||
return a[i].Essid < a[j].Essid
|
||||
}
|
||||
|
||||
type ByWlanSeenSorter []*WlanEndpoint
|
||||
type ByWlanSeenSorter []*WirelessStation
|
||||
|
||||
func (a ByWlanSeenSorter) Len() int { return len(a) }
|
||||
func (a ByWlanSeenSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
|
@ -200,11 +197,11 @@ func (w *WDiscovery) showTable(header []string, rows [][]string) {
|
|||
}
|
||||
|
||||
func (w *WDiscovery) Show(by string) error {
|
||||
if w.Targets == nil {
|
||||
return errors.New("Targets are not yet initialized")
|
||||
if w.Stations == nil {
|
||||
return errors.New("Stations are not yet initialized")
|
||||
}
|
||||
|
||||
targets := w.Targets.List()
|
||||
targets := w.Stations.List()
|
||||
if by == "seen" {
|
||||
sort.Sort(ByWlanSeenSorter(targets))
|
||||
} else {
|
||||
|
@ -303,7 +300,7 @@ func (w *WDiscovery) SendDeauth() error {
|
|||
w.SendDeauthPacket(w.BSTarget, w.ClientTarget)
|
||||
|
||||
case len(w.BSTarget) > 0:
|
||||
for _, t := range w.Targets.Targets {
|
||||
for _, t := range w.Stations.Stations {
|
||||
w.SendDeauthPacket(w.BSTarget, t.Endpoint.HW)
|
||||
}
|
||||
|
||||
|
@ -351,7 +348,7 @@ func (w *WDiscovery) BSScan(packet gopacket.Packet) {
|
|||
|
||||
if bytes.Compare(dst, w.BroadcastMac) == 0 && len(ssid) > 0 {
|
||||
channel = WlanMhzToChannel(int(radiotap.ChannelFrequency))
|
||||
w.Targets.AddIfNew(ssid, bssid, true, channel)
|
||||
w.Stations.AddIfNew(ssid, bssid, true, channel)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -384,7 +381,7 @@ func (w *WDiscovery) ClientScan(bs net.HardwareAddr, packet gopacket.Packet) {
|
|||
|
||||
if bytes.Compare(bssid, bs) == 0 {
|
||||
channel := WlanMhzToChannel(int(radiotap.ChannelFrequency))
|
||||
w.Targets.AddIfNew("", src.String(), false, channel)
|
||||
w.Stations.AddIfNew("", src.String(), false, channel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -392,8 +389,7 @@ func (w *WDiscovery) ClientScan(bs net.HardwareAddr, packet gopacket.Packet) {
|
|||
func (w *WDiscovery) Configure() error {
|
||||
var err error
|
||||
|
||||
w.Targets = NewWlanTargets(w.Session, w.Session.Interface)
|
||||
|
||||
w.Stations = NewWiFi(w.Session, w.Session.Interface)
|
||||
w.BroadcastMac, _ = net.ParseMAC(BROADCAST_MAC)
|
||||
|
||||
inactive, err := pcap.NewInactiveHandle(w.Session.Interface.Name())
|
||||
|
|
88
modules/wlan_stations.go
Normal file
88
modules/wlan_stations.go
Normal file
|
@ -0,0 +1,88 @@
|
|||
package modules
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/evilsocket/bettercap-ng/network"
|
||||
"github.com/evilsocket/bettercap-ng/session"
|
||||
)
|
||||
|
||||
const StationsDefaultTTL = 30
|
||||
|
||||
type WirelessStation struct {
|
||||
Endpoint *network.Endpoint
|
||||
Essid string
|
||||
IsAP bool
|
||||
Channel int
|
||||
}
|
||||
|
||||
type WiFi struct {
|
||||
sync.Mutex
|
||||
|
||||
Session *session.Session `json:"-"`
|
||||
Interface *network.Endpoint
|
||||
Stations map[string]*WirelessStation
|
||||
}
|
||||
|
||||
func NewWirelessStation(essid, mac string, isAp bool, channel int) *WirelessStation {
|
||||
return &WirelessStation{
|
||||
Endpoint: network.NewEndpointNoResolve("0.0.0.0", mac, "", 0),
|
||||
Essid: essid,
|
||||
IsAP: isAp,
|
||||
Channel: channel,
|
||||
}
|
||||
}
|
||||
|
||||
func NewWiFi(s *session.Session, iface *network.Endpoint) *WiFi {
|
||||
return &WiFi{
|
||||
Session: s,
|
||||
Interface: iface,
|
||||
Stations: make(map[string]*WirelessStation),
|
||||
}
|
||||
}
|
||||
|
||||
func (w *WiFi) List() (list []*WirelessStation) {
|
||||
w.Lock()
|
||||
defer w.Unlock()
|
||||
|
||||
list = make([]*WirelessStation, 0)
|
||||
for _, t := range w.Stations {
|
||||
list = append(list, t)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (w *WiFi) Remove(mac string) {
|
||||
w.Lock()
|
||||
defer w.Unlock()
|
||||
|
||||
if e, found := w.Stations[mac]; found {
|
||||
w.Session.Events.Add("wifi.station.lost", e.Endpoint)
|
||||
delete(w.Stations, mac)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *WiFi) AddIfNew(ssid, mac string, isAp bool, channel int) *WirelessStation {
|
||||
w.Lock()
|
||||
defer w.Unlock()
|
||||
|
||||
mac = network.NormalizeMac(mac)
|
||||
if t, found := w.Stations[mac]; found {
|
||||
w.Stations[mac].Endpoint.LastSeen = time.Now()
|
||||
return t
|
||||
}
|
||||
|
||||
e := NewWirelessStation(ssid, mac, isAp, channel)
|
||||
|
||||
w.Stations[mac] = e
|
||||
|
||||
w.Session.Events.Add("wifi.station.new", e.Endpoint)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *WiFi) Clear() error {
|
||||
w.Stations = make(map[string]*WirelessStation)
|
||||
return nil
|
||||
}
|
|
@ -1,130 +0,0 @@
|
|||
package modules
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
bnet "github.com/evilsocket/bettercap-ng/network"
|
||||
session "github.com/evilsocket/bettercap-ng/session"
|
||||
)
|
||||
|
||||
const TargetsDefaultTTL = 30
|
||||
|
||||
type WlanEndpoint struct {
|
||||
Endpoint *bnet.Endpoint
|
||||
Essid string
|
||||
IsAP bool
|
||||
Channel int
|
||||
}
|
||||
|
||||
func NewWlanEndpoint(essid, mac string, isAp bool, channel int) *WlanEndpoint {
|
||||
e := bnet.NewEndpointNoResolve("0.0.0.0", mac, "", 0)
|
||||
|
||||
we := &WlanEndpoint{
|
||||
Endpoint: e,
|
||||
Essid: essid,
|
||||
IsAP: isAp,
|
||||
Channel: channel,
|
||||
}
|
||||
|
||||
return we
|
||||
}
|
||||
|
||||
type WlanTargets struct {
|
||||
sync.Mutex
|
||||
|
||||
Session *session.Session `json:"-"`
|
||||
Interface *bnet.Endpoint
|
||||
Targets map[string]*WlanEndpoint
|
||||
TTL map[string]uint
|
||||
Aliases map[string]string
|
||||
}
|
||||
|
||||
func NewWlanTargets(s *session.Session, iface *bnet.Endpoint) *WlanTargets {
|
||||
t := &WlanTargets{
|
||||
Session: s,
|
||||
Interface: iface,
|
||||
Targets: make(map[string]*WlanEndpoint),
|
||||
TTL: make(map[string]uint),
|
||||
Aliases: s.Targets.Aliases,
|
||||
}
|
||||
|
||||
return t
|
||||
}
|
||||
|
||||
func (tp *WlanTargets) List() (list []*WlanEndpoint) {
|
||||
tp.Lock()
|
||||
defer tp.Unlock()
|
||||
|
||||
list = make([]*WlanEndpoint, 0)
|
||||
for _, t := range tp.Targets {
|
||||
list = append(list, t)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (tp *WlanTargets) WasMissed(mac string) bool {
|
||||
if mac == tp.Session.Interface.HwAddress {
|
||||
return false
|
||||
}
|
||||
|
||||
tp.Lock()
|
||||
defer tp.Unlock()
|
||||
|
||||
if ttl, found := tp.TTL[mac]; found == true {
|
||||
return ttl < TargetsDefaultTTL
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (tp *WlanTargets) Remove(mac string) {
|
||||
tp.Lock()
|
||||
defer tp.Unlock()
|
||||
|
||||
if e, found := tp.Targets[mac]; found {
|
||||
tp.TTL[mac]--
|
||||
if tp.TTL[mac] == 0 {
|
||||
tp.Session.Events.Add("endpoint.lost", e.Endpoint)
|
||||
delete(tp.Targets, mac)
|
||||
delete(tp.TTL, mac)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (tp *WlanTargets) AddIfNew(ssid, mac string, isAp bool, channel int) *WlanEndpoint {
|
||||
tp.Lock()
|
||||
defer tp.Unlock()
|
||||
|
||||
mac = bnet.NormalizeMac(mac)
|
||||
if t, found := tp.Targets[mac]; found {
|
||||
if tp.TTL[mac] < TargetsDefaultTTL {
|
||||
tp.TTL[mac]++
|
||||
}
|
||||
|
||||
tp.Targets[mac].Endpoint.LastSeen = time.Now()
|
||||
|
||||
return t
|
||||
}
|
||||
|
||||
e := NewWlanEndpoint(ssid, mac, isAp, channel)
|
||||
|
||||
if alias, found := tp.Aliases[mac]; found {
|
||||
e.Endpoint.Alias = alias
|
||||
}
|
||||
|
||||
tp.Targets[mac] = e
|
||||
tp.TTL[mac] = TargetsDefaultTTL
|
||||
|
||||
tp.Session.Events.Add("endpoint.new", e.Endpoint)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tp *WlanTargets) ClearAll() error {
|
||||
tp.Targets = make(map[string]*WlanEndpoint)
|
||||
tp.TTL = make(map[string]uint)
|
||||
tp.Aliases = make(map[string]string)
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue