mirror of
https://github.com/bettercap/bettercap
synced 2025-08-19 04:59:25 -07:00
misc: small fix or general refactoring i did not bother commenting
This commit is contained in:
parent
43941b0a3f
commit
b8056e2026
3 changed files with 27 additions and 78 deletions
|
@ -23,31 +23,22 @@ import (
|
||||||
"github.com/evilsocket/islazy/tui"
|
"github.com/evilsocket/islazy/tui"
|
||||||
)
|
)
|
||||||
|
|
||||||
type parsedShake struct {
|
|
||||||
Radiotap *layers.RadioTap
|
|
||||||
Dot11 *layers.Dot11
|
|
||||||
Packet gopacket.Packet
|
|
||||||
}
|
|
||||||
|
|
||||||
type WiFiModule struct {
|
type WiFiModule struct {
|
||||||
session.SessionModule
|
session.SessionModule
|
||||||
|
|
||||||
iface *network.Endpoint
|
iface *network.Endpoint
|
||||||
handle *pcap.Handle
|
handle *pcap.Handle
|
||||||
source string
|
source string
|
||||||
region string
|
region string
|
||||||
txPower int
|
txPower int
|
||||||
minRSSI int
|
minRSSI int
|
||||||
channel int
|
channel int
|
||||||
hopPeriod time.Duration
|
hopPeriod time.Duration
|
||||||
hopChanges chan bool
|
hopChanges chan bool
|
||||||
frequencies []int
|
frequencies []int
|
||||||
ap *network.AccessPoint
|
ap *network.AccessPoint
|
||||||
stickChan int
|
stickChan int
|
||||||
|
shakesFile string
|
||||||
shakesFile string
|
|
||||||
shakesHistory []parsedShake
|
|
||||||
|
|
||||||
skipBroken bool
|
skipBroken bool
|
||||||
pktSourceChan chan gopacket.Packet
|
pktSourceChan chan gopacket.Packet
|
||||||
pktSourceChanClosed bool
|
pktSourceChanClosed bool
|
||||||
|
@ -75,7 +66,6 @@ func NewWiFiModule(s *session.Session) *WiFiModule {
|
||||||
stickChan: 0,
|
stickChan: 0,
|
||||||
hopPeriod: 250 * time.Millisecond,
|
hopPeriod: 250 * time.Millisecond,
|
||||||
hopChanges: make(chan bool),
|
hopChanges: make(chan bool),
|
||||||
shakesHistory: make([]parsedShake, 0),
|
|
||||||
ap: nil,
|
ap: nil,
|
||||||
skipBroken: true,
|
skipBroken: true,
|
||||||
apRunning: false,
|
apRunning: false,
|
||||||
|
@ -520,41 +510,11 @@ func (mod *WiFiModule) updateStats(dot11 *layers.Dot11, packet gopacket.Packet)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mod *WiFiModule) loadHandshakes() {
|
|
||||||
mod.shakesHistory = make([]parsedShake, 0)
|
|
||||||
|
|
||||||
if !fs.Exists(mod.shakesFile) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
handle, err := pcap.OpenOffline(mod.shakesFile)
|
|
||||||
if err != nil {
|
|
||||||
mod.Debug("can't open handshakes file: %v", mod.shakesFile)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer handle.Close()
|
|
||||||
|
|
||||||
mod.Info("loading handshakes from %s", mod.shakesFile)
|
|
||||||
|
|
||||||
src := gopacket.NewPacketSource(handle, handle.LinkType())
|
|
||||||
for packet := range src.Packets() {
|
|
||||||
if ok, radiotap, dot11 := packets.Dot11Parse(packet); ok {
|
|
||||||
mod.shakesHistory = append(mod.shakesHistory, parsedShake{
|
|
||||||
Radiotap: radiotap,
|
|
||||||
Dot11: dot11,
|
|
||||||
Packet: packet,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mod *WiFiModule) Start() error {
|
func (mod *WiFiModule) Start() error {
|
||||||
if err := mod.Configure(); err != nil {
|
if err := mod.Configure(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
mod.loadHandshakes()
|
|
||||||
|
|
||||||
mod.SetRunning(true, func() {
|
mod.SetRunning(true, func() {
|
||||||
// start channel hopper if needed
|
// start channel hopper if needed
|
||||||
if mod.channel == 0 && mod.source == "" {
|
if mod.channel == 0 && mod.source == "" {
|
||||||
|
@ -591,7 +551,7 @@ func (mod *WiFiModule) Start() error {
|
||||||
mod.discoverProbes(radiotap, dot11, packet)
|
mod.discoverProbes(radiotap, dot11, packet)
|
||||||
mod.discoverAccessPoints(radiotap, dot11, packet)
|
mod.discoverAccessPoints(radiotap, dot11, packet)
|
||||||
mod.discoverClients(radiotap, dot11, packet)
|
mod.discoverClients(radiotap, dot11, packet)
|
||||||
mod.discoverHandshakes(radiotap, dot11, packet, false)
|
mod.discoverHandshakes(radiotap, dot11, packet)
|
||||||
mod.updateInfo(dot11, packet)
|
mod.updateInfo(dot11, packet)
|
||||||
mod.updateStats(dot11, packet)
|
mod.updateStats(dot11, packet)
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,12 +70,6 @@ func (mod *WiFiModule) discoverAccessPoints(radiotap *layers.RadioTap, dot11 *la
|
||||||
ap.EachClient(func(mac string, station *network.Station) {
|
ap.EachClient(func(mac string, station *network.Station) {
|
||||||
station.Handshake.SetBeacon(packet)
|
station.Handshake.SetBeacon(packet)
|
||||||
})
|
})
|
||||||
} else {
|
|
||||||
// every time we detect a new ap, see if we have
|
|
||||||
// its handshakes in our pcap already
|
|
||||||
for _, h := range mod.shakesHistory {
|
|
||||||
mod.discoverHandshakes(h.Radiotap, h.Dot11, h.Packet, true)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
mod.Debug("skipping %s with %d dBm", from.String(), radiotap.DBMAntennaSignal)
|
mod.Debug("skipping %s with %d dBm", from.String(), radiotap.DBMAntennaSignal)
|
||||||
|
|
|
@ -18,14 +18,12 @@ func allZeros(s []byte) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mod *WiFiModule) discoverHandshakes(radiotap *layers.RadioTap, dot11 *layers.Dot11, packet gopacket.Packet, readOnly bool) {
|
func (mod *WiFiModule) discoverHandshakes(radiotap *layers.RadioTap, dot11 *layers.Dot11, packet gopacket.Packet) {
|
||||||
if ok, key, apMac, staMac := packets.Dot11ParseEAPOL(packet, dot11); ok {
|
if ok, key, apMac, staMac := packets.Dot11ParseEAPOL(packet, dot11); ok {
|
||||||
// first, locate the AP in our list by its BSSID
|
// first, locate the AP in our list by its BSSID
|
||||||
ap, found := mod.Session.WiFi.Get(apMac.String())
|
ap, found := mod.Session.WiFi.Get(apMac.String())
|
||||||
if !found {
|
if !found {
|
||||||
if !readOnly {
|
mod.Warning("could not find AP with BSSID %s", apMac.String())
|
||||||
mod.Warning("could not find AP with BSSID %s", apMac.String())
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,8 +76,8 @@ func (mod *WiFiModule) discoverHandshakes(radiotap *layers.RadioTap, dot11 *laye
|
||||||
// if we have unsaved packets as part of the handshake, save them.
|
// if we have unsaved packets as part of the handshake, save them.
|
||||||
numUnsaved := station.Handshake.NumUnsaved()
|
numUnsaved := station.Handshake.NumUnsaved()
|
||||||
doSave := numUnsaved > 0
|
doSave := numUnsaved > 0
|
||||||
if !readOnly && doSave && mod.shakesFile != "" {
|
if doSave && mod.shakesFile != "" {
|
||||||
mod.Debug("saving handshake frames to %s", mod.shakesFile)
|
mod.Info("saving handshake frames to %s", mod.shakesFile)
|
||||||
if err := mod.Session.WiFi.SaveHandshakesTo(mod.shakesFile, mod.handle.LinkType()); err != nil {
|
if err := mod.Session.WiFi.SaveHandshakesTo(mod.shakesFile, mod.handle.LinkType()); err != nil {
|
||||||
mod.Error("error while saving handshake frames to %s: %s", mod.shakesFile, err)
|
mod.Error("error while saving handshake frames to %s: %s", mod.shakesFile, err)
|
||||||
}
|
}
|
||||||
|
@ -88,23 +86,20 @@ func (mod *WiFiModule) discoverHandshakes(radiotap *layers.RadioTap, dot11 *laye
|
||||||
// if we had unsaved packets and either the handshake is complete
|
// if we had unsaved packets and either the handshake is complete
|
||||||
// or it contains the PMKID, generate a new event.
|
// or it contains the PMKID, generate a new event.
|
||||||
if doSave && (rawPMKID != nil || station.Handshake.Complete()) {
|
if doSave && (rawPMKID != nil || station.Handshake.Complete()) {
|
||||||
if !readOnly {
|
mod.Session.Events.Add("wifi.client.handshake", HandshakeEvent{
|
||||||
mod.Session.Events.Add("wifi.client.handshake", HandshakeEvent{
|
File: mod.shakesFile,
|
||||||
File: mod.shakesFile,
|
NewPackets: numUnsaved,
|
||||||
NewPackets: numUnsaved,
|
AP: apMac.String(),
|
||||||
AP: apMac.String(),
|
Station: staMac.String(),
|
||||||
Station: staMac.String(),
|
PMKID: rawPMKID,
|
||||||
PMKID: rawPMKID,
|
})
|
||||||
})
|
|
||||||
}
|
|
||||||
// make sure the info that we have key material for this AP
|
// make sure the info that we have key material for this AP
|
||||||
// is persisted even after stations are pruned due to inactivity
|
// is persisted even after stations are pruned due to inactivity
|
||||||
ap.WithKeyMaterial(true)
|
ap.WithKeyMaterial(true)
|
||||||
}
|
}
|
||||||
// if we're only collecting handshakes from history or if we
|
// if we added ourselves as a client station but we didn't get any
|
||||||
// added ourselves as a client station but we didn't get any
|
|
||||||
// PMKID, just remove it from the list of clients of this AP.
|
// PMKID, just remove it from the list of clients of this AP.
|
||||||
if (readOnly && staAdded) || (staIsUs && rawPMKID == nil) {
|
if staAdded || (staIsUs && rawPMKID == nil) {
|
||||||
ap.RemoveClient(staMac.String())
|
ap.RemoveClient(staMac.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue