| 1 | package wifi |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "net" |
| 6 | "time" |
| 7 | |
| 8 | "github.com/bettercap/bettercap/network" |
| 9 | "github.com/bettercap/bettercap/packets" |
| 10 | |
| 11 | "github.com/google/gopacket" |
| 12 | "github.com/google/gopacket/layers" |
| 13 | ) |
| 14 | |
| 15 | func (mod *WiFiModule) stationPruner() { |
| 16 | mod.reads.Add(1) |
| 17 | defer mod.reads.Done() |
| 18 | |
| 19 | maxApTTL := time.Duration(mod.apTTL) * time.Second |
| 20 | maxStaTTL := time.Duration(mod.staTTL) * time.Second |
| 21 | |
| 22 | mod.Debug("wifi stations pruner started (ap.ttl:%v sta.ttl:%v).", maxApTTL, maxStaTTL) |
| 23 | for mod.Running() { |
| 24 | // loop every AP |
| 25 | for _, ap := range mod.Session.WiFi.List() { |
| 26 | sinceLastSeen := time.Since(ap.LastSeen) |
| 27 | if sinceLastSeen > maxApTTL { |
| 28 | mod.Debug("station %s not seen in %s, removing.", ap.BSSID(), sinceLastSeen) |
| 29 | mod.Session.WiFi.Remove(ap.BSSID()) |
| 30 | continue |
| 31 | } |
| 32 | // loop every AP client |
| 33 | for _, c := range ap.Clients() { |
| 34 | sinceLastSeen := time.Since(c.LastSeen) |
| 35 | if sinceLastSeen > maxStaTTL { |
| 36 | mod.Debug("client %s of station %s not seen in %s, removing.", c.String(), ap.BSSID(), sinceLastSeen) |
| 37 | ap.RemoveClient(c.BSSID()) |
| 38 | |
| 39 | mod.Session.Events.Add("wifi.client.lost", ClientEvent{ |
| 40 | AP: ap, |
| 41 | Client: c, |
| 42 | }) |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | time.Sleep(1 * time.Second) |
| 47 | // refresh |
| 48 | maxApTTL = time.Duration(mod.apTTL) * time.Second |
| 49 | maxStaTTL = time.Duration(mod.staTTL) * time.Second |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func (mod *WiFiModule) discoverAccessPoints(radiotap *layers.RadioTap, dot11 *layers.Dot11, packet gopacket.Packet) { |
| 54 | // search for Dot11InformationElementIDSSID |
| 55 | if ok, ssid := packets.Dot11ParseIDSSID(packet); ok { |
| 56 | from := dot11.Address3 |
| 57 | |
| 58 | // skip stuff we're sending |
| 59 | if mod.apRunning && bytes.Equal(from, mod.apConfig.BSSID) { |
| 60 | return |
| 61 | } |
| 62 | |
| 63 | if !network.IsZeroMac(from) && !network.IsBroadcastMac(from) { |
| 64 | if int(radiotap.DBMAntennaSignal) >= mod.minRSSI { |
| 65 | var frequency int |
| 66 | bssid := from.String() |
| 67 | |
| 68 | if found, channel := packets.Dot11ParseDSSet(packet); found { |
| 69 | frequency = network.Dot11Chan2Freq(channel) |
| 70 | } else { |
| 71 | frequency = int(radiotap.ChannelFrequency) |
| 72 | } |
| 73 | |
| 74 | if ap, isNew := mod.Session.WiFi.AddIfNew(ssid, bssid, frequency, radiotap.DBMAntennaSignal); !isNew { |
| 75 | //set beacon packet on the access point station. |
| 76 | //This is for it to be included in the saved handshake file for wifi.assoc |
| 77 | ap.Station.Handshake.Beacon = packet |
| 78 | ap.EachClient(func(mac string, station *network.Station) { |
| 79 | station.Handshake.SetBeacon(packet) |
| 80 | }) |
| 81 | } |
| 82 | } else { |
| 83 | mod.Debug("skipping %s with %d dBm", from.String(), radiotap.DBMAntennaSignal) |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func (mod *WiFiModule) startProbing(staMac net.HardwareAddr, ssid string) error { |
| 90 | // if not already running, temporarily enable the pcap handle |
| 91 | // for packet injection |
| 92 | if !mod.Running() { |
| 93 | if err := mod.Configure(); err != nil { |
| 94 | return err |
| 95 | } |
| 96 | defer mod.handle.Close() |
| 97 | } |
| 98 | |
| 99 | for seq := uint16(0); seq < 5 && mod.Running(); seq++ { |
| 100 | if err, pkt := packets.NewDot11ProbeRequest(staMac, seq, ssid, network.GetInterfaceChannel(mod.iface.Name())); err != nil { |
| 101 | mod.Error("could not create probe packet: %s", err) |
| 102 | continue |
| 103 | } else { |
| 104 | mod.injectPacket(pkt) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | mod.Debug("sent probe frames") |
| 109 | |
| 110 | return nil |
| 111 | } |
| 112 | |
| 113 | func (mod *WiFiModule) discoverProbes(radiotap *layers.RadioTap, dot11 *layers.Dot11, packet gopacket.Packet) { |
| 114 | if dot11.Type != layers.Dot11TypeMgmtProbeReq { |
| 115 | return |
| 116 | } |
| 117 | |
| 118 | reqLayer := packet.Layer(layers.LayerTypeDot11MgmtProbeReq) |
| 119 | if reqLayer == nil { |
| 120 | return |
| 121 | } |
| 122 | |
| 123 | req, ok := reqLayer.(*layers.Dot11MgmtProbeReq) |
| 124 | if !ok { |
| 125 | return |
| 126 | } |
| 127 | |
| 128 | // skip stuff we're sending |
| 129 | if bytes.Equal(mod.probeMac, dot11.Address2) { |
| 130 | return |
| 131 | } |
| 132 | |
| 133 | clientSTA := network.NormalizeMac(dot11.Address2.String()) |
| 134 | if mod.filterProbeSTA != nil && !mod.filterProbeSTA.MatchString(clientSTA) { |
| 135 | return |
| 136 | } |
| 137 | |
| 138 | tot := len(req.Contents) |
| 139 | if tot < 3 { |
| 140 | return |
| 141 | } |
| 142 | |
| 143 | avail := uint32(tot - 2) |
| 144 | if avail < 2 { |
| 145 | return |
| 146 | } |
| 147 | size := uint32(req.Contents[1]) |
| 148 | if size == 0 || size > avail { |
| 149 | return |
| 150 | } |
| 151 | |
| 152 | apSSID := string(req.Contents[2 : 2+size]) |
| 153 | if mod.filterProbeAP != nil && !mod.filterProbeAP.MatchString(apSSID) { |
| 154 | return |
| 155 | } |
| 156 | |
| 157 | mod.Session.Events.Add("wifi.client.probe", ProbeEvent{ |
| 158 | FromAddr: clientSTA, |
| 159 | FromVendor: network.ManufLookup(clientSTA), |
| 160 | FromAlias: mod.Session.Lan.GetAlias(clientSTA), |
| 161 | SSID: apSSID, |
| 162 | RSSI: radiotap.DBMAntennaSignal, |
| 163 | }) |
| 164 | } |
| 165 | |
| 166 | func (mod *WiFiModule) discoverClients(radiotap *layers.RadioTap, dot11 *layers.Dot11, packet gopacket.Packet) { |
| 167 | mod.Session.WiFi.EachAccessPoint(func(bssid string, ap *network.AccessPoint) { |
| 168 | // packet going to this specific BSSID? |
| 169 | if packets.Dot11IsDataFor(dot11, ap.HW) { |
| 170 | bssid := dot11.Address2.String() |
| 171 | freq := int(radiotap.ChannelFrequency) |
| 172 | rssi := radiotap.DBMAntennaSignal |
| 173 | |
| 174 | if station, isNew := ap.AddClientIfNew(bssid, freq, rssi); isNew { |
| 175 | mod.Session.Events.Add("wifi.client.new", ClientEvent{ |
| 176 | AP: ap, |
| 177 | Client: station, |
| 178 | }) |
| 179 | } |
| 180 | } |
| 181 | }) |
| 182 | } |
| 183 | |
| 184 | func (mod *WiFiModule) discoverDeauths(radiotap *layers.RadioTap, dot11 *layers.Dot11, packet gopacket.Packet) { |
| 185 | if dot11.Type != layers.Dot11TypeMgmtDeauthentication { |
| 186 | return |
| 187 | } |
| 188 | |
| 189 | // ignore deauth frames that we sent |
| 190 | if radiotap.ChannelFrequency == 0 { |
| 191 | return |
| 192 | } |
| 193 | |
| 194 | deauthLayer := packet.Layer(layers.LayerTypeDot11MgmtDeauthentication) |
| 195 | if deauthLayer == nil { |
| 196 | return |
| 197 | } |
| 198 | |
| 199 | deauth, ok := deauthLayer.(*layers.Dot11MgmtDeauthentication) |
| 200 | reason := "?" |
| 201 | if ok { |
| 202 | reason = deauth.Reason.String() |
| 203 | } |
| 204 | |
| 205 | // trigger events only if the deauth is coming from an AP we know of |
| 206 | source := dot11.Address1.String() |
| 207 | ap, found := mod.Session.WiFi.Get(source) |
| 208 | if !found { |
| 209 | mod.Debug("skipping deauth frame from %s", source) |
| 210 | return |
| 211 | } |
| 212 | |
| 213 | mod.Debug("deauth radio %#v", radiotap) |
| 214 | |
| 215 | mod.Session.Events.Add("wifi.deauthentication", DeauthEvent{ |
| 216 | RSSI: radiotap.DBMAntennaSignal, |
| 217 | AP: ap, |
| 218 | Address1: source, |
| 219 | Address2: dot11.Address2.String(), |
| 220 | Address3: dot11.Address3.String(), |
| 221 | Reason: reason, |
| 222 | }) |
| 223 | } |