Debian Codegraph @

modules/wifi/wifi_assoc.go

raw symbols SCIP
1 package wifi
2
3 import (
4 "bytes"
5 "fmt"
6 "net"
7 "sort"
8
9 "github.com/bettercap/bettercap/network"
10 "github.com/bettercap/bettercap/packets"
11 )
12
13 func (mod *WiFiModule) sendAssocPacket(ap *network.AccessPoint) {
14 if err, pkt := packets.NewDot11Auth(mod.iface.HW, ap.HW, 1); err != nil {
15 mod.Error("cloud not create auth packet: %s", err)
16 } else {
17 mod.injectPacket(pkt)
18 }
19
20 if err, pkt := packets.NewDot11AssociationRequest(mod.iface.HW, ap.HW, ap.ESSID(), 1); err != nil {
21 mod.Error("cloud not create association request packet: %s", err)
22 } else {
23 mod.injectPacket(pkt)
24 }
25 }
26
27 func (mod *WiFiModule) skipAssoc(to net.HardwareAddr) bool {
28 for _, mac := range mod.assocSkip {
29 if bytes.Equal(to, mac) {
30 return true
31 }
32 }
33 return false
34 }
35
36 func (mod *WiFiModule) isAssocSilent() bool {
37 if err, is := mod.BoolParam("wifi.assoc.silent"); err != nil {
38 mod.Warning("%v", err)
39 } else {
40 mod.assocSilent = is
41 }
42 return mod.assocSilent
43 }
44
45 func (mod *WiFiModule) doAssocOpen() bool {
46 if err, is := mod.BoolParam("wifi.assoc.open"); err != nil {
47 mod.Warning("%v", err)
48 } else {
49 mod.assocOpen = is
50 }
51 return mod.assocOpen
52 }
53
54 func (mod *WiFiModule) doAssocAcquired() bool {
55 if err, is := mod.BoolParam("wifi.assoc.acquired"); err != nil {
56 mod.Warning("%v", err)
57 } else {
58 mod.assocAcquired = is
59 }
60 return mod.assocAcquired
61 }
62
63 func (mod *WiFiModule) startAssoc(to net.HardwareAddr) error {
64 // parse skip list
65 if err, assocSkip := mod.StringParam("wifi.assoc.skip"); err != nil {
66 return err
67 } else if macs, err := network.ParseMACs(assocSkip); err != nil {
68 return err
69 } else {
70 mod.assocSkip = macs
71 }
72
73 // if not already running, temporarily enable the pcap handle
74 // for packet injection
75 if !mod.Running() {
76 if err := mod.Configure(); err != nil {
77 return err
78 }
79 defer mod.handle.Close()
80 }
81
82 toAssoc := make([]*network.AccessPoint, 0)
83 isBcast := network.IsBroadcastMac(to)
84 for _, ap := range mod.Session.WiFi.List() {
85 if isBcast || bytes.Equal(ap.HW, to) {
86 if !mod.skipAssoc(ap.HW) {
87 toAssoc = append(toAssoc, ap)
88 } else {
89 mod.Debug("skipping ap:%v because skip list %v", ap, mod.assocSkip)
90 }
91 }
92 }
93
94 if len(toAssoc) == 0 {
95 if isBcast {
96 return nil
97 }
98 return fmt.Errorf("%s is an unknown BSSID or it is in the association skip list.", to.String())
99 }
100 mod.writes.Add(1)
101 go func() {
102 defer mod.writes.Done()
103
104 // since we need to change the wifi adapter channel for each
105 // association request, let's sort by channel so we do the minimum
106 // amount of hops possible
107 sort.Slice(toAssoc, func(i, j int) bool {
108 return toAssoc[i].Channel < toAssoc[j].Channel
109 })
110
111 // send the association request frames
112 for _, ap := range toAssoc {
113 if mod.Running() {
114 logger := mod.Info
115 if mod.isAssocSilent() {
116 logger = mod.Debug
117 }
118
119 if ap.IsOpen() && !mod.doAssocOpen() {
120 mod.Debug("skipping association for open network %s (wifi.assoc.open is false)", ap.ESSID())
121 } else if ap.HasKeyMaterial() && !mod.doAssocAcquired() {
122 mod.Debug("skipping association for AP %s (key material already acquired)", ap.ESSID())
123 } else {
124 logger("sending association request to AP %s (channel:%d encryption:%s)", ap.ESSID(), ap.Channel, ap.Encryption)
125
126 mod.onChannel(ap.Channel, func() {
127 mod.sendAssocPacket(ap)
128 })
129 }
130 }
131 }
132 }()
133
134 return nil
135 }