| 1 | package wifi |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "net" |
| 7 | "regexp" |
| 8 | "strconv" |
| 9 | "sync" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/bettercap/bettercap/modules/utils" |
| 13 | "github.com/bettercap/bettercap/network" |
| 14 | "github.com/bettercap/bettercap/packets" |
| 15 | "github.com/bettercap/bettercap/session" |
| 16 | |
| 17 | "github.com/google/gopacket" |
| 18 | "github.com/google/gopacket/layers" |
| 19 | "github.com/google/gopacket/pcap" |
| 20 | |
| 21 | "github.com/evilsocket/islazy/fs" |
| 22 | "github.com/evilsocket/islazy/ops" |
| 23 | "github.com/evilsocket/islazy/str" |
| 24 | ) |
| 25 | |
| 26 | type WiFiModule struct { |
| 27 | session.SessionModule |
| 28 | |
| 29 | iface *network.Endpoint |
| 30 | handle *pcap.Handle |
| 31 | source string |
| 32 | region string |
| 33 | txPower int |
| 34 | minRSSI int |
| 35 | apTTL int |
| 36 | staTTL int |
| 37 | channel int |
| 38 | hopPeriod time.Duration |
| 39 | hopChanges chan bool |
| 40 | frequencies []int |
| 41 | ap *network.AccessPoint |
| 42 | stickChan int |
| 43 | shakesFile string |
| 44 | shakesAggregate bool |
| 45 | skipBroken bool |
| 46 | pktSourceChan chan gopacket.Packet |
| 47 | pktSourceChanClosed bool |
| 48 | deauthSkip []net.HardwareAddr |
| 49 | deauthSilent bool |
| 50 | deauthOpen bool |
| 51 | deauthAcquired bool |
| 52 | assocSkip []net.HardwareAddr |
| 53 | assocSilent bool |
| 54 | assocOpen bool |
| 55 | assocAcquired bool |
| 56 | csaSilent bool |
| 57 | fakeAuthSilent bool |
| 58 | filterProbeSTA *regexp.Regexp |
| 59 | filterProbeAP *regexp.Regexp |
| 60 | apRunning bool |
| 61 | showManuf bool |
| 62 | apConfig packets.Dot11ApConfig |
| 63 | probeMac net.HardwareAddr |
| 64 | writes *sync.WaitGroup |
| 65 | reads *sync.WaitGroup |
| 66 | chanLock *sync.Mutex |
| 67 | selector *utils.ViewSelector |
| 68 | } |
| 69 | |
| 70 | func NewWiFiModule(s *session.Session) *WiFiModule { |
| 71 | mod := &WiFiModule{ |
| 72 | SessionModule: session.NewSessionModule("wifi", s), |
| 73 | iface: s.Interface, |
| 74 | minRSSI: -200, |
| 75 | apTTL: 300, |
| 76 | staTTL: 300, |
| 77 | channel: 0, |
| 78 | stickChan: 0, |
| 79 | hopPeriod: 250 * time.Millisecond, |
| 80 | hopChanges: make(chan bool), |
| 81 | ap: nil, |
| 82 | skipBroken: true, |
| 83 | apRunning: false, |
| 84 | deauthSkip: []net.HardwareAddr{}, |
| 85 | deauthSilent: false, |
| 86 | deauthOpen: false, |
| 87 | deauthAcquired: false, |
| 88 | assocSkip: []net.HardwareAddr{}, |
| 89 | assocSilent: false, |
| 90 | assocOpen: false, |
| 91 | assocAcquired: false, |
| 92 | csaSilent: false, |
| 93 | fakeAuthSilent: false, |
| 94 | showManuf: false, |
| 95 | shakesAggregate: true, |
| 96 | writes: &sync.WaitGroup{}, |
| 97 | reads: &sync.WaitGroup{}, |
| 98 | chanLock: &sync.Mutex{}, |
| 99 | } |
| 100 | |
| 101 | mod.InitState("channels") |
| 102 | |
| 103 | mod.AddParam(session.NewStringParameter("wifi.interface", |
| 104 | "", |
| 105 | "", |
| 106 | "If filled, will use this interface name instead of the one provided by the -iface argument or detected automatically.")) |
| 107 | |
| 108 | mod.AddHandler(session.NewModuleHandler("wifi.recon on", "", |
| 109 | "Start 802.11 wireless base stations discovery and channel hopping.", |
| 110 | func(args []string) error { |
| 111 | return mod.Start() |
| 112 | })) |
| 113 | |
| 114 | mod.AddHandler(session.NewModuleHandler("wifi.recon off", "", |
| 115 | "Stop 802.11 wireless base stations discovery and channel hopping.", |
| 116 | func(args []string) error { |
| 117 | return mod.Stop() |
| 118 | })) |
| 119 | |
| 120 | mod.AddHandler(session.NewModuleHandler("wifi.clear", "", |
| 121 | "Clear all access points collected by the WiFi discovery module.", |
| 122 | func(args []string) error { |
| 123 | mod.Session.WiFi.Clear() |
| 124 | return nil |
| 125 | })) |
| 126 | |
| 127 | mod.AddHandler(session.NewModuleHandler("wifi.recon MAC", "wifi.recon ((?:[0-9A-Fa-f]{2}[:-]){5}(?:[0-9A-Fa-f]{2}))", |
| 128 | "Set 802.11 base station address to filter for.", |
| 129 | func(args []string) error { |
| 130 | bssid, err := net.ParseMAC(args[0]) |
| 131 | if err != nil { |
| 132 | return err |
| 133 | } else if ap, found := mod.Session.WiFi.Get(bssid.String()); found { |
| 134 | mod.ap = ap |
| 135 | mod.stickChan = ap.Channel |
| 136 | return nil |
| 137 | } |
| 138 | return fmt.Errorf("Could not find station with BSSID %s", args[0]) |
| 139 | })) |
| 140 | |
| 141 | mod.AddHandler(session.NewModuleHandler("wifi.recon clear", "", |
| 142 | "Remove the 802.11 base station filter.", |
| 143 | func(args []string) (err error) { |
| 144 | mod.ap = nil |
| 145 | mod.stickChan = 0 |
| 146 | freqs, err := network.GetSupportedFrequencies(mod.iface.Name()) |
| 147 | mod.setFrequencies(freqs) |
| 148 | mod.hopChanges <- true |
| 149 | return err |
| 150 | })) |
| 151 | |
| 152 | mod.AddHandler(session.NewModuleHandler("wifi.client.probe.sta.filter FILTER", "wifi.client.probe.sta.filter (.+)", |
| 153 | "Use this regular expression on the station address to filter client probes, 'clear' to reset the filter.", |
| 154 | func(args []string) (err error) { |
| 155 | filter := args[0] |
| 156 | if filter == "clear" { |
| 157 | mod.filterProbeSTA = nil |
| 158 | return |
| 159 | } else if mod.filterProbeSTA, err = regexp.Compile(filter); err != nil { |
| 160 | return |
| 161 | } |
| 162 | return |
| 163 | })) |
| 164 | |
| 165 | mod.AddHandler(session.NewModuleHandler("wifi.client.probe.ap.filter FILTER", "wifi.client.probe.ap.filter (.+)", |
| 166 | "Use this regular expression on the access point name to filter client probes, 'clear' to reset the filter.", |
| 167 | func(args []string) (err error) { |
| 168 | filter := args[0] |
| 169 | if filter == "clear" { |
| 170 | mod.filterProbeAP = nil |
| 171 | return |
| 172 | } else if mod.filterProbeAP, err = regexp.Compile(filter); err != nil { |
| 173 | return |
| 174 | } |
| 175 | return |
| 176 | })) |
| 177 | |
| 178 | minRSSI := session.NewIntParameter("wifi.rssi.min", |
| 179 | "-200", |
| 180 | "Minimum WiFi signal strength in dBm.") |
| 181 | |
| 182 | mod.AddObservableParam(minRSSI, func(v string) { |
| 183 | if err, v := minRSSI.Get(s); err != nil { |
| 184 | mod.Error("%v", err) |
| 185 | } else if mod.minRSSI = v.(int); mod.Started { |
| 186 | mod.Info("wifi.rssi.min set to %d", mod.minRSSI) |
| 187 | } |
| 188 | }) |
| 189 | |
| 190 | deauth := session.NewModuleHandler("wifi.deauth BSSID", `wifi\.deauth ((?:[a-fA-F0-9:]{11,})|all|\*)`, |
| 191 | "Start a 802.11 deauth attack, if an access point BSSID is provided, every client will be deauthenticated, otherwise only the selected client. Use 'all', '*' or a broadcast BSSID (ff:ff:ff:ff:ff:ff) to iterate every access point with at least one client and start a deauth attack for each one.", |
| 192 | func(args []string) error { |
| 193 | if args[0] == "all" || args[0] == "*" { |
| 194 | args[0] = "ff:ff:ff:ff:ff:ff" |
| 195 | } |
| 196 | bssid, err := net.ParseMAC(args[0]) |
| 197 | if err != nil { |
| 198 | return err |
| 199 | } |
| 200 | return mod.startDeauth(bssid) |
| 201 | }) |
| 202 | |
| 203 | deauth.Complete("wifi.deauth", s.WiFiCompleterFull) |
| 204 | |
| 205 | mod.AddHandler(deauth) |
| 206 | |
| 207 | probe := session.NewModuleHandler("wifi.probe BSSID ESSID", |
| 208 | `wifi\.probe\s+([a-fA-F0-9:]{11,})\s+([^\s].+)`, |
| 209 | "Sends a fake client probe with the given station BSSID, searching for ESSID.", |
| 210 | func(args []string) (err error) { |
| 211 | if mod.probeMac, err = net.ParseMAC(args[0]); err != nil { |
| 212 | return err |
| 213 | } |
| 214 | return mod.startProbing(mod.probeMac, args[1]) |
| 215 | }) |
| 216 | |
| 217 | probe.Complete("wifi.probe", s.WiFiCompleterFull) |
| 218 | |
| 219 | mod.AddHandler(probe) |
| 220 | |
| 221 | channelSwitchAnnounce := session.NewModuleHandler("wifi.channel_switch_announce bssid channel ", `wifi\.channel_switch_announce ((?:[a-fA-F0-9:]{11,}))\s+((?:[0-9]+))`, |
| 222 | "Start a 802.11 channel hop attack, all client will be force to change the channel lead to connection down.", |
| 223 | func(args []string) error { |
| 224 | bssid, err := net.ParseMAC(args[0]) |
| 225 | if err != nil { |
| 226 | return err |
| 227 | } |
| 228 | channel, _ := strconv.Atoi(args[1]) |
| 229 | if channel > 180 || channel < 1 { |
| 230 | return fmt.Errorf("%d is not a valid channel number", channel) |
| 231 | } |
| 232 | return mod.startCSA(bssid, int8(channel)) |
| 233 | }) |
| 234 | |
| 235 | channelSwitchAnnounce.Complete("wifi.channel_switch_announce", s.WiFiCompleterFull) |
| 236 | |
| 237 | mod.AddHandler(channelSwitchAnnounce) |
| 238 | |
| 239 | fakeAuth := session.NewModuleHandler("wifi.fake_auth bssid client", `wifi\.fake_auth ((?:[a-fA-F0-9:]{11,}))\s+((?:[a-fA-F0-9:]{11,}))`, |
| 240 | "send an fake authentication with client mac to ap lead to client disconnect", |
| 241 | func(args []string) error { |
| 242 | bssid, err := net.ParseMAC(args[0]) |
| 243 | if err != nil { |
| 244 | return err |
| 245 | } |
| 246 | client, err := net.ParseMAC(args[1]) |
| 247 | if err != nil { |
| 248 | return err |
| 249 | } |
| 250 | return mod.startFakeAuth(bssid, client) |
| 251 | }) |
| 252 | |
| 253 | fakeAuth.Complete("wifi.fake_auth", s.WiFiCompleterFull) |
| 254 | |
| 255 | mod.AddHandler(fakeAuth) |
| 256 | |
| 257 | mod.AddParam(session.NewBoolParameter("wifi.channel_switch_announce.silent", |
| 258 | "false", |
| 259 | "If true, messages from wifi.channel_switch_announce will be suppressed.")) |
| 260 | |
| 261 | mod.AddParam(session.NewBoolParameter("wifi.fake_auth.silent", |
| 262 | "false", |
| 263 | "If true, messages from wifi.fake_auth will be suppressed.")) |
| 264 | |
| 265 | mod.AddParam(session.NewStringParameter("wifi.deauth.skip", |
| 266 | "", |
| 267 | "", |
| 268 | "Comma separated list of BSSID to skip while sending deauth packets.")) |
| 269 | |
| 270 | mod.AddParam(session.NewBoolParameter("wifi.deauth.silent", |
| 271 | "false", |
| 272 | "If true, messages from wifi.deauth will be suppressed.")) |
| 273 | |
| 274 | mod.AddParam(session.NewBoolParameter("wifi.deauth.open", |
| 275 | "true", |
| 276 | "Send wifi deauth packets to open networks.")) |
| 277 | |
| 278 | mod.AddParam(session.NewBoolParameter("wifi.deauth.acquired", |
| 279 | "false", |
| 280 | "Send wifi deauth packets from AP's for which key material was already acquired.")) |
| 281 | |
| 282 | assoc := session.NewModuleHandler("wifi.assoc BSSID", `wifi\.assoc ((?:[a-fA-F0-9:]{11,})|all|\*)`, |
| 283 | "Send an association request to the selected BSSID in order to receive a RSN PMKID key. Use 'all', '*' or a broadcast BSSID (ff:ff:ff:ff:ff:ff) to iterate for every access point.", |
| 284 | func(args []string) error { |
| 285 | if args[0] == "all" || args[0] == "*" { |
| 286 | args[0] = "ff:ff:ff:ff:ff:ff" |
| 287 | } |
| 288 | bssid, err := net.ParseMAC(args[0]) |
| 289 | if err != nil { |
| 290 | return err |
| 291 | } |
| 292 | return mod.startAssoc(bssid) |
| 293 | }) |
| 294 | |
| 295 | assoc.Complete("wifi.assoc", s.WiFiCompleter) |
| 296 | |
| 297 | mod.AddHandler(assoc) |
| 298 | |
| 299 | apTTL := session.NewIntParameter("wifi.ap.ttl", |
| 300 | "300", |
| 301 | "Seconds of inactivity for an access points to be considered not in range anymore.") |
| 302 | |
| 303 | mod.AddObservableParam(apTTL, func(v string) { |
| 304 | if err, v := apTTL.Get(s); err != nil { |
| 305 | mod.Error("%v", err) |
| 306 | } else if mod.apTTL = v.(int); mod.Started { |
| 307 | mod.Info("wifi.ap.ttl set to %d", mod.apTTL) |
| 308 | } |
| 309 | }) |
| 310 | |
| 311 | staTTL := session.NewIntParameter("wifi.sta.ttl", |
| 312 | "300", |
| 313 | "Seconds of inactivity for a client station to be considered not in range or not connected to its access point anymore.") |
| 314 | |
| 315 | mod.AddObservableParam(staTTL, func(v string) { |
| 316 | if err, v := staTTL.Get(s); err != nil { |
| 317 | mod.Error("%v", err) |
| 318 | } else if mod.staTTL = v.(int); mod.Started { |
| 319 | mod.Info("wifi.sta.ttl set to %d", mod.staTTL) |
| 320 | } |
| 321 | }) |
| 322 | |
| 323 | mod.AddParam(session.NewStringParameter("wifi.region", |
| 324 | "", |
| 325 | "", |
| 326 | "Set the WiFi region to this value before activating the interface.")) |
| 327 | |
| 328 | mod.AddParam(session.NewIntParameter("wifi.txpower", |
| 329 | "30", |
| 330 | "Set WiFi transmission power to this value before activating the interface.")) |
| 331 | |
| 332 | mod.AddParam(session.NewStringParameter("wifi.assoc.skip", |
| 333 | "", |
| 334 | "", |
| 335 | "Comma separated list of BSSID to skip while sending association requests.")) |
| 336 | |
| 337 | mod.AddParam(session.NewBoolParameter("wifi.assoc.silent", |
| 338 | "false", |
| 339 | "If true, messages from wifi.assoc will be suppressed.")) |
| 340 | |
| 341 | mod.AddParam(session.NewBoolParameter("wifi.assoc.open", |
| 342 | "false", |
| 343 | "Send association requests to open networks.")) |
| 344 | |
| 345 | mod.AddParam(session.NewBoolParameter("wifi.assoc.acquired", |
| 346 | "false", |
| 347 | "Send association to AP's for which key material was already acquired.")) |
| 348 | |
| 349 | mod.AddHandler(session.NewModuleHandler("wifi.ap", "", |
| 350 | "Inject fake management beacons in order to create a rogue access point.", |
| 351 | func(args []string) error { |
| 352 | if err := mod.parseApConfig(); err != nil { |
| 353 | return err |
| 354 | } else { |
| 355 | return mod.startAp() |
| 356 | } |
| 357 | })) |
| 358 | |
| 359 | mod.AddParam(session.NewStringParameter("wifi.handshakes.file", |
| 360 | "~/bettercap-wifi-handshakes.pcap", |
| 361 | "", |
| 362 | "File path of the pcap file to save handshakes to.")) |
| 363 | |
| 364 | mod.AddParam(session.NewBoolParameter("wifi.handshakes.aggregate", |
| 365 | "true", |
| 366 | "If true, all handshakes will be saved inside a single file, otherwise a folder with per-network pcap files will be created.")) |
| 367 | |
| 368 | mod.AddParam(session.NewStringParameter("wifi.ap.ssid", |
| 369 | "FreeWiFi", |
| 370 | "", |
| 371 | "SSID of the fake access point.")) |
| 372 | |
| 373 | mod.AddParam(session.NewStringParameter("wifi.ap.bssid", |
| 374 | session.ParamRandomMAC, |
| 375 | "[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}", |
| 376 | "BSSID of the fake access point.")) |
| 377 | |
| 378 | mod.AddParam(session.NewIntParameter("wifi.ap.channel", |
| 379 | "1", |
| 380 | "Channel of the fake access point.")) |
| 381 | |
| 382 | mod.AddParam(session.NewBoolParameter("wifi.ap.encryption", |
| 383 | "true", |
| 384 | "If true, the fake access point will use WPA2, otherwise it'll result as an open AP.")) |
| 385 | |
| 386 | mod.AddHandler(session.NewModuleHandler("wifi.show.wps BSSID", |
| 387 | `wifi\.show\.wps ((?:[a-fA-F0-9:]{11,})|all|\*)`, |
| 388 | "Show WPS information about a given station (use 'all', '*' or a broadcast BSSID for all).", |
| 389 | func(args []string) error { |
| 390 | if args[0] == "all" || args[0] == "*" { |
| 391 | args[0] = "ff:ff:ff:ff:ff:ff" |
| 392 | } |
| 393 | return mod.ShowWPS(args[0]) |
| 394 | })) |
| 395 | |
| 396 | mod.AddHandler(session.NewModuleHandler("wifi.show", "", |
| 397 | "Show current wireless stations list (default sorting by essid).", |
| 398 | func(args []string) error { |
| 399 | return mod.Show() |
| 400 | })) |
| 401 | |
| 402 | mod.selector = utils.ViewSelectorFor(&mod.SessionModule, "wifi.show", |
| 403 | []string{"rssi", "bssid", "essid", "channel", "encryption", "clients", "seen", "sent", "rcvd"}, "rssi asc") |
| 404 | |
| 405 | mod.AddParam(session.NewBoolParameter("wifi.show.manufacturer", |
| 406 | "false", |
| 407 | "If true, wifi.show will also show the devices manufacturers.")) |
| 408 | |
| 409 | mod.AddHandler(session.NewModuleHandler("wifi.recon.channel CHANNEL", `wifi\.recon\.channel[\s]+([0-9]+(?:[, ]+[0-9]+)*|clear)`, |
| 410 | "WiFi channels (comma separated) or 'clear' for channel hopping.", |
| 411 | func(args []string) (err error) { |
| 412 | freqs := []int{} |
| 413 | |
| 414 | if args[0] != "clear" { |
| 415 | mod.Debug("setting hopping channels to %s", args[0]) |
| 416 | for _, s := range str.Comma(args[0]) { |
| 417 | if ch, err := strconv.Atoi(s); err != nil { |
| 418 | return err |
| 419 | } else { |
| 420 | if f := network.Dot11Chan2Freq(ch); f == 0 { |
| 421 | return fmt.Errorf("%d is not a valid wifi channel.", ch) |
| 422 | } else { |
| 423 | freqs = append(freqs, f) |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | if len(freqs) == 0 { |
| 430 | mod.Debug("resetting hopping channels") |
| 431 | if mod.iface == nil { |
| 432 | return fmt.Errorf("wifi.interface not set or not found") |
| 433 | } else if freqs, err = network.GetSupportedFrequencies(mod.iface.Name()); err != nil { |
| 434 | return err |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | mod.setFrequencies(freqs) |
| 439 | |
| 440 | // if wifi.recon is not running, this would block forever |
| 441 | if mod.Running() { |
| 442 | mod.hopChanges <- true |
| 443 | } |
| 444 | |
| 445 | return nil |
| 446 | })) |
| 447 | |
| 448 | mod.AddParam(session.NewStringParameter("wifi.source.file", |
| 449 | "", |
| 450 | "", |
| 451 | "If set, the wifi module will read from this pcap file instead of the hardware interface.")) |
| 452 | |
| 453 | mod.AddParam(session.NewIntParameter("wifi.hop.period", |
| 454 | "250", |
| 455 | "If channel hopping is enabled (empty wifi.recon.channel), this is the time in milliseconds the algorithm will hop on every channel (it'll be doubled if both 2.4 and 5.0 bands are available).")) |
| 456 | |
| 457 | mod.AddParam(session.NewBoolParameter("wifi.skip-broken", |
| 458 | "true", |
| 459 | "If true, dot11 packets with an invalid checksum will be skipped.")) |
| 460 | |
| 461 | return mod |
| 462 | } |
| 463 | |
| 464 | func (mod WiFiModule) Name() string { |
| 465 | return "wifi" |
| 466 | } |
| 467 | |
| 468 | func (mod WiFiModule) Description() string { |
| 469 | return "A module to monitor and perform wireless attacks on 802.11." |
| 470 | } |
| 471 | |
| 472 | func (mod WiFiModule) Author() string { |
| 473 | return "Simone Margaritelli <[email protected]> && Gianluca Braga <[email protected]>" |
| 474 | } |
| 475 | |
| 476 | const ( |
| 477 | // Ugly, but gopacket folks are not exporting pcap errors, so ... |
| 478 | // ref. https://github.com/google/gopacket/blob/96986c90e3e5c7e01deed713ff8058e357c0c047/pcap/pcap.go#L281 |
| 479 | ErrIfaceNotUp = "Interface Not Up" |
| 480 | ) |
| 481 | |
| 482 | func (mod *WiFiModule) setFrequencies(freqs []int) { |
| 483 | mod.Debug("new frequencies: %v", freqs) |
| 484 | |
| 485 | mod.frequencies = freqs |
| 486 | channels := []int{} |
| 487 | for _, freq := range freqs { |
| 488 | channels = append(channels, network.Dot11Freq2Chan(freq)) |
| 489 | } |
| 490 | mod.State.Store("channels", channels) |
| 491 | |
| 492 | mod.Info("channels: %v", channels) |
| 493 | } |
| 494 | |
| 495 | func (mod *WiFiModule) Configure() error { |
| 496 | var ifName string |
| 497 | var hopPeriod int |
| 498 | var err error |
| 499 | |
| 500 | if err, mod.apTTL = mod.IntParam("wifi.ap.ttl"); err != nil { |
| 501 | return err |
| 502 | } else if err, mod.staTTL = mod.IntParam("wifi.sta.ttl"); err != nil { |
| 503 | return err |
| 504 | } |
| 505 | |
| 506 | if err, mod.region = mod.StringParam("wifi.region"); err != nil { |
| 507 | return err |
| 508 | } else if err, mod.txPower = mod.IntParam("wifi.txpower"); err != nil { |
| 509 | return err |
| 510 | } else if err, mod.source = mod.StringParam("wifi.source.file"); err != nil { |
| 511 | return err |
| 512 | } else if err, mod.minRSSI = mod.IntParam("wifi.rssi.min"); err != nil { |
| 513 | return err |
| 514 | } |
| 515 | |
| 516 | if err, mod.shakesAggregate = mod.BoolParam("wifi.handshakes.aggregate"); err != nil { |
| 517 | return err |
| 518 | } else if err, mod.shakesFile = mod.StringParam("wifi.handshakes.file"); err != nil { |
| 519 | return err |
| 520 | } else if mod.shakesFile != "" { |
| 521 | if mod.shakesFile, err = fs.Expand(mod.shakesFile); err != nil { |
| 522 | return err |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | if err, ifName = mod.StringParam("wifi.interface"); err != nil { |
| 527 | return err |
| 528 | } else if ifName == "" { |
| 529 | mod.iface = mod.Session.Interface |
| 530 | ifName = mod.iface.Name() |
| 531 | } else if mod.iface, err = network.FindInterface(ifName); err != nil { |
| 532 | return fmt.Errorf("could not find interface %s: %v", ifName, err) |
| 533 | } else if mod.iface == nil { |
| 534 | return fmt.Errorf("could not find interface %s", ifName) |
| 535 | } |
| 536 | |
| 537 | mod.Info("using interface %s (%s)", ifName, mod.iface.HwAddress) |
| 538 | |
| 539 | if mod.source != "" { |
| 540 | if mod.handle, err = pcap.OpenOffline(mod.source); err != nil { |
| 541 | return fmt.Errorf("error while opening file %s: %s", mod.source, err) |
| 542 | } |
| 543 | } else { |
| 544 | if mod.region != "" { |
| 545 | if err := network.SetWiFiRegion(mod.region); err != nil { |
| 546 | return err |
| 547 | } else { |
| 548 | mod.Debug("WiFi region set to '%s'", mod.region) |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | if mod.txPower > 0 { |
| 553 | if err := network.SetInterfaceTxPower(ifName, mod.txPower); err != nil { |
| 554 | mod.Warning("could not set interface %s txpower to %d, 'Set Tx Power' requests not supported: %v", ifName, mod.txPower, err) |
| 555 | } else { |
| 556 | mod.Debug("interface %s txpower set to %d", ifName, mod.txPower) |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * We don't want to pcap.BlockForever otherwise pcap_close(handle) |
| 562 | * could hang waiting for a timeout to expire ... |
| 563 | */ |
| 564 | opts := network.CAPTURE_DEFAULTS |
| 565 | opts.Timeout = 500 * time.Millisecond |
| 566 | opts.Monitor = true |
| 567 | |
| 568 | for retry := 0; ; retry++ { |
| 569 | if mod.handle, err = network.CaptureWithOptions(ifName, opts); err == nil { |
| 570 | // we're done |
| 571 | break |
| 572 | } else if retry == 0 && err.Error() == ErrIfaceNotUp { |
| 573 | // try to bring interface up and try again |
| 574 | mod.Info("interface %s is down, bringing it up ...", ifName) |
| 575 | if err := network.ActivateInterface(ifName); err != nil { |
| 576 | return err |
| 577 | } |
| 578 | continue |
| 579 | } else if !opts.Monitor { |
| 580 | // second fatal error, just bail |
| 581 | return fmt.Errorf("error while activating handle: %s", err) |
| 582 | } else { |
| 583 | // first fatal error, forcing monitor mode |
| 584 | // https://github.com/bettercap/bettercap/issues/819 |
| 585 | opts.Monitor = false; |
| 586 | if err := network.ForceMonitorMode(ifName); err != nil { |
| 587 | return err |
| 588 | } |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | if err, mod.skipBroken = mod.BoolParam("wifi.skip-broken"); err != nil { |
| 594 | return err |
| 595 | } else if err, hopPeriod = mod.IntParam("wifi.hop.period"); err != nil { |
| 596 | return err |
| 597 | } |
| 598 | |
| 599 | mod.hopPeriod = time.Duration(hopPeriod) * time.Millisecond |
| 600 | |
| 601 | if mod.source == "" { |
| 602 | if freqs, err := network.GetSupportedFrequencies(ifName); err != nil { |
| 603 | return fmt.Errorf("error while getting supported frequencies of %s: %s", ifName, err) |
| 604 | } else { |
| 605 | mod.setFrequencies(freqs) |
| 606 | } |
| 607 | |
| 608 | mod.Debug("wifi supported frequencies: %v", mod.frequencies) |
| 609 | |
| 610 | // we need to start somewhere, this is just to check if |
| 611 | // this OS supports switching channel programmatically. |
| 612 | if err = network.SetInterfaceChannel(ifName, 1); err != nil { |
| 613 | return fmt.Errorf("error while initializing %s to channel 1: %s", ifName, err) |
| 614 | } |
| 615 | |
| 616 | mod.Info("started (min rssi: %d dBm)", mod.minRSSI) |
| 617 | } |
| 618 | |
| 619 | return nil |
| 620 | } |
| 621 | |
| 622 | func (mod *WiFiModule) updateInfo(dot11 *layers.Dot11, packet gopacket.Packet) { |
| 623 | // avoid parsing info from frames we're sending |
| 624 | staMac := ops.Ternary(dot11.Flags.FromDS(), dot11.Address1, dot11.Address2).(net.HardwareAddr) |
| 625 | if !bytes.Equal(staMac, mod.iface.HW) { |
| 626 | if ok, enc, cipher, auth := packets.Dot11ParseEncryption(packet, dot11); ok { |
| 627 | // Sometimes we get incomplete info about encryption, which |
| 628 | // makes stations with encryption enabled switch to OPEN. |
| 629 | // Prevent this behaviour by not downgrading the encryption. |
| 630 | bssid := dot11.Address3.String() |
| 631 | if station, found := mod.Session.WiFi.Get(bssid); found && station.IsOpen() { |
| 632 | station.Encryption = enc |
| 633 | station.Cipher = cipher |
| 634 | station.Authentication = auth |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | if ok, bssid, info := packets.Dot11ParseWPS(packet, dot11); ok { |
| 639 | if station, found := mod.Session.WiFi.Get(bssid.String()); found { |
| 640 | for name, value := range info { |
| 641 | station.WPS[name] = value |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | func (mod *WiFiModule) updateStats(dot11 *layers.Dot11, packet gopacket.Packet) { |
| 649 | // collect stats from data frames |
| 650 | if dot11.Type.MainType() == layers.Dot11TypeData { |
| 651 | bytes := uint64(len(packet.Data())) |
| 652 | |
| 653 | dst := dot11.Address1.String() |
| 654 | if ap, found := mod.Session.WiFi.Get(dst); found { |
| 655 | ap.Received += bytes |
| 656 | } else if sta, found := mod.Session.WiFi.GetClient(dst); found { |
| 657 | sta.Received += bytes |
| 658 | } |
| 659 | |
| 660 | src := dot11.Address2.String() |
| 661 | if ap, found := mod.Session.WiFi.Get(src); found { |
| 662 | ap.Sent += bytes |
| 663 | } else if sta, found := mod.Session.WiFi.GetClient(src); found { |
| 664 | sta.Sent += bytes |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | func (mod *WiFiModule) Start() error { |
| 670 | if err := mod.Configure(); err != nil { |
| 671 | return err |
| 672 | } |
| 673 | |
| 674 | mod.SetRunning(true, func() { |
| 675 | // start channel hopper if needed |
| 676 | if mod.channel == 0 && mod.source == "" { |
| 677 | go mod.channelHopper() |
| 678 | } |
| 679 | |
| 680 | // start the pruner |
| 681 | go mod.stationPruner() |
| 682 | |
| 683 | mod.reads.Add(1) |
| 684 | defer mod.reads.Done() |
| 685 | |
| 686 | src := gopacket.NewPacketSource(mod.handle, mod.handle.LinkType()) |
| 687 | mod.pktSourceChan = src.Packets() |
| 688 | for packet := range mod.pktSourceChan { |
| 689 | if !mod.Running() { |
| 690 | break |
| 691 | } else if packet == nil { |
| 692 | continue |
| 693 | } |
| 694 | |
| 695 | if mod.iface == mod.Session.Interface { |
| 696 | mod.Session.Queue.TrackPacket(uint64(len(packet.Data()))) |
| 697 | } |
| 698 | |
| 699 | // perform initial dot11 parsing and layers validation |
| 700 | if ok, radiotap, dot11 := packets.Dot11Parse(packet); ok { |
| 701 | // check FCS checksum |
| 702 | if mod.skipBroken && !dot11.ChecksumValid() { |
| 703 | mod.Debug("skipping dot11 packet with invalid checksum.") |
| 704 | continue |
| 705 | } |
| 706 | |
| 707 | mod.discoverProbes(radiotap, dot11, packet) |
| 708 | mod.discoverAccessPoints(radiotap, dot11, packet) |
| 709 | mod.discoverClients(radiotap, dot11, packet) |
| 710 | mod.discoverHandshakes(radiotap, dot11, packet) |
| 711 | mod.discoverDeauths(radiotap, dot11, packet) |
| 712 | mod.updateInfo(dot11, packet) |
| 713 | mod.updateStats(dot11, packet) |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | mod.pktSourceChanClosed = true |
| 718 | }) |
| 719 | |
| 720 | return nil |
| 721 | } |
| 722 | |
| 723 | func (mod *WiFiModule) forcedStop() error { |
| 724 | return mod.SetRunning(false, func() { |
| 725 | // signal the main for loop we want to exit |
| 726 | if !mod.pktSourceChanClosed { |
| 727 | mod.pktSourceChan <- nil |
| 728 | } |
| 729 | // close the pcap handle to make the main for exit |
| 730 | mod.handle.Close() |
| 731 | }) |
| 732 | } |
| 733 | |
| 734 | func (mod *WiFiModule) Stop() error { |
| 735 | return mod.SetRunning(false, func() { |
| 736 | // wait any pending write operation |
| 737 | mod.writes.Wait() |
| 738 | // signal the main for loop we want to exit |
| 739 | if !mod.pktSourceChanClosed { |
| 740 | mod.pktSourceChan <- nil |
| 741 | } |
| 742 | mod.reads.Wait() |
| 743 | // close the pcap handle to make the main for exit |
| 744 | mod.handle.Close() |
| 745 | }) |
| 746 | } |