Debian Codegraph @

network/wifi.go

raw symbols SCIP
1 package network
2
3 import (
4 "encoding/json"
5 "os"
6 "path/filepath"
7 "strconv"
8 "sync"
9 "time"
10
11 "github.com/google/gopacket"
12 "github.com/google/gopacket/layers"
13 "github.com/google/gopacket/pcapgo"
14
15 "github.com/evilsocket/islazy/data"
16 "github.com/evilsocket/islazy/fs"
17 )
18
19 func Dot11Freq2Chan(freq int) int {
20 if freq <= 2472 {
21 return ((freq - 2412) / 5) + 1
22 } else if freq == 2484 {
23 return 14
24 } else if freq >= 5035 && freq <= 5865 {
25 return ((freq - 5035) / 5) + 7
26 } else if freq >= 5875 && freq <= 5895 {
27 return 177
28 }
29 return 0
30 }
31
32 func Dot11Chan2Freq(channel int) int {
33 if channel <= 13 {
34 return ((channel - 1) * 5) + 2412
35 } else if channel == 14 {
36 return 2484
37 } else if channel <= 173 {
38 return ((channel - 7) * 5) + 5035
39 } else if channel == 177 {
40 return 5885
41 }
42
43 return 0
44 }
45
46 type APNewCallback func(ap *AccessPoint)
47 type APLostCallback func(ap *AccessPoint)
48
49 type WiFi struct {
50 sync.RWMutex
51
52 aliases *data.UnsortedKV
53 aps map[string]*AccessPoint
54 iface *Endpoint
55 newCb APNewCallback
56 lostCb APLostCallback
57 }
58
59 type wifiJSON struct {
60 AccessPoints []*AccessPoint `json:"aps"`
61 }
62
63 func NewWiFi(iface *Endpoint, aliases *data.UnsortedKV, newcb APNewCallback, lostcb APLostCallback) *WiFi {
64 return &WiFi{
65 aps: make(map[string]*AccessPoint),
66 aliases: aliases,
67 iface: iface,
68 newCb: newcb,
69 lostCb: lostcb,
70 }
71 }
72
73 func (w *WiFi) MarshalJSON() ([]byte, error) {
74
75 doc := wifiJSON{
76 // we know the length so preallocate to reduce memory allocations
77 AccessPoints: make([]*AccessPoint, 0, len(w.aps)),
78 }
79
80 for _, ap := range w.aps {
81 doc.AccessPoints = append(doc.AccessPoints, ap)
82 }
83
84 return json.Marshal(doc)
85 }
86
87 func (w *WiFi) EachAccessPoint(cb func(mac string, ap *AccessPoint)) {
88 w.Lock()
89 defer w.Unlock()
90
91 for m, ap := range w.aps {
92 cb(m, ap)
93 }
94 }
95
96 func (w *WiFi) Stations() (list []*Station) {
97 w.RLock()
98 defer w.RUnlock()
99
100 list = make([]*Station, 0, len(w.aps))
101
102 for _, ap := range w.aps {
103 list = append(list, ap.Station)
104 }
105 return
106 }
107
108 func (w *WiFi) List() (list []*AccessPoint) {
109 w.RLock()
110 defer w.RUnlock()
111
112 list = make([]*AccessPoint, 0, len(w.aps))
113
114 for _, ap := range w.aps {
115 list = append(list, ap)
116 }
117 return
118 }
119
120 func (w *WiFi) Remove(mac string) {
121 w.Lock()
122 defer w.Unlock()
123
124 if ap, found := w.aps[mac]; found {
125 delete(w.aps, mac)
126 if w.lostCb != nil {
127 w.lostCb(ap)
128 }
129 }
130 }
131
132 // when iface is in monitor mode, error
133 // correction on macOS is crap and we
134 // get non printable characters .... (ref #61)
135 func isBogusMacESSID(essid string) bool {
136 for _, c := range essid {
137 if !strconv.IsPrint(c) {
138 return true
139 }
140 }
141 return false
142 }
143
144 func (w *WiFi) AddIfNew(ssid, mac string, frequency int, rssi int8) (*AccessPoint, bool) {
145 w.Lock()
146 defer w.Unlock()
147
148 mac = NormalizeMac(mac)
149 alias := w.aliases.GetOr(mac, "")
150 if ap, found := w.aps[mac]; found {
151 ap.LastSeen = time.Now()
152 if rssi != 0 {
153 ap.RSSI = rssi
154 }
155 // always get the cleanest one
156 if !isBogusMacESSID(ssid) {
157 ap.Hostname = ssid
158 }
159
160 if alias != "" {
161 ap.Alias = alias
162 }
163 return ap, false
164 }
165
166 newAp := NewAccessPoint(ssid, mac, frequency, rssi, w.aliases)
167 newAp.Alias = alias
168 w.aps[mac] = newAp
169
170 if w.newCb != nil {
171 w.newCb(newAp)
172 }
173
174 return newAp, true
175 }
176
177 func (w *WiFi) Get(mac string) (*AccessPoint, bool) {
178 w.RLock()
179 defer w.RUnlock()
180
181 mac = NormalizeMac(mac)
182 ap, found := w.aps[mac]
183 return ap, found
184 }
185
186 func (w *WiFi) GetClient(mac string) (*Station, bool) {
187 w.RLock()
188 defer w.RUnlock()
189
190 mac = NormalizeMac(mac)
191 for _, ap := range w.aps {
192 if client, found := ap.Get(mac); found {
193 return client, true
194 }
195 }
196
197 return nil, false
198 }
199
200 func (w *WiFi) Clear() {
201 w.Lock()
202 defer w.Unlock()
203 w.aps = make(map[string]*AccessPoint)
204 }
205
206 func (w *WiFi) NumHandshakes() int {
207 w.RLock()
208 defer w.RUnlock()
209
210 sum := 0
211 for _, ap := range w.aps {
212 for _, station := range ap.Clients() {
213 if station.Handshake.Complete() {
214 sum++
215 }
216 }
217 }
218
219 return sum
220 }
221
222 func (w *WiFi) SaveHandshakesTo(fileName string, linkType layers.LinkType) error {
223 // check if folder exists first
224 dirName := filepath.Dir(fileName)
225 if _, err := os.Stat(dirName); err != nil {
226 if err = os.MkdirAll(dirName, os.ModePerm); err != nil {
227 return err
228 }
229 }
230
231 doHead := !fs.Exists(fileName)
232 fp, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
233 if err != nil {
234 return err
235 }
236 defer fp.Close()
237
238 writer := pcapgo.NewWriter(fp)
239
240 if doHead {
241 if err = writer.WriteFileHeader(65536, linkType); err != nil {
242 return err
243 }
244 }
245
246 w.RLock()
247 defer w.RUnlock()
248
249 for _, ap := range w.aps {
250 for _, station := range ap.Clients() {
251 // if half (which includes also complete) or has pmkid
252 if station.Handshake.Any() {
253 err = nil
254 station.Handshake.EachUnsavedPacket(func(pkt gopacket.Packet) {
255 if err == nil {
256 err = writer.WritePacket(pkt.Metadata().CaptureInfo, pkt.Data())
257 }
258 })
259 if err != nil {
260 return err
261 }
262 }
263 }
264 }
265
266 return nil
267 }