| 1 | package wifi |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "sort" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/bettercap/bettercap/modules/net_recon" |
| 11 | "github.com/bettercap/bettercap/network" |
| 12 | "github.com/bettercap/bettercap/session" |
| 13 | |
| 14 | "github.com/dustin/go-humanize" |
| 15 | |
| 16 | "github.com/evilsocket/islazy/ops" |
| 17 | "github.com/evilsocket/islazy/tui" |
| 18 | ) |
| 19 | |
| 20 | func (mod *WiFiModule) isApSelected() bool { |
| 21 | return mod.ap != nil |
| 22 | } |
| 23 | |
| 24 | func (mod *WiFiModule) getRow(station *network.Station) ([]string, bool) { |
| 25 | rssi := network.ColorRSSI(int(station.RSSI)) |
| 26 | bssid := station.HwAddress |
| 27 | sinceStarted := time.Since(mod.Session.StartedAt) |
| 28 | sinceFirstSeen := time.Since(station.FirstSeen) |
| 29 | if sinceStarted > (net_recon.JustJoinedTimeInterval*2) && sinceFirstSeen <= net_recon.JustJoinedTimeInterval { |
| 30 | // if endpoint was first seen in the last 10 seconds |
| 31 | bssid = tui.Bold(bssid) |
| 32 | } |
| 33 | |
| 34 | seen := station.LastSeen.Format("15:04:05") |
| 35 | sinceLastSeen := time.Since(station.LastSeen) |
| 36 | if sinceStarted > net_recon.AliveTimeInterval && sinceLastSeen <= net_recon.AliveTimeInterval { |
| 37 | // if endpoint seen in the last 10 seconds |
| 38 | seen = tui.Bold(seen) |
| 39 | } else if sinceLastSeen > net_recon.PresentTimeInterval { |
| 40 | // if endpoint not seen in the last 60 seconds |
| 41 | seen = tui.Dim(seen) |
| 42 | } |
| 43 | |
| 44 | ssid := ops.Ternary(station.ESSID() == "<hidden>", tui.Dim(station.ESSID()), station.ESSID()).(string) |
| 45 | |
| 46 | encryption := station.Encryption |
| 47 | if len(station.Cipher) > 0 { |
| 48 | encryption = fmt.Sprintf("%s (%s, %s)", station.Encryption, station.Cipher, station.Authentication) |
| 49 | } |
| 50 | |
| 51 | if encryption == "OPEN" || encryption == "" { |
| 52 | encryption = tui.Green("OPEN") |
| 53 | ssid = tui.Green(ssid) |
| 54 | bssid = tui.Green(bssid) |
| 55 | } else { |
| 56 | // this is ugly, but necessary in order to have this |
| 57 | // method handle both access point and clients |
| 58 | // transparently |
| 59 | if ap, found := mod.Session.WiFi.Get(station.HwAddress); found && ap.HasKeyMaterial() { |
| 60 | encryption = tui.Red(encryption) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | sent := ops.Ternary(station.Sent > 0, humanize.Bytes(station.Sent), "").(string) |
| 65 | recvd := ops.Ternary(station.Received > 0, humanize.Bytes(station.Received), "").(string) |
| 66 | |
| 67 | include := false |
| 68 | if mod.source == "" { |
| 69 | for _, frequencies := range mod.frequencies { |
| 70 | if frequencies == station.Frequency { |
| 71 | include = true |
| 72 | break |
| 73 | } |
| 74 | } |
| 75 | } else { |
| 76 | include = true |
| 77 | } |
| 78 | |
| 79 | if int(station.RSSI) < mod.minRSSI { |
| 80 | include = false |
| 81 | } |
| 82 | |
| 83 | if mod.isApSelected() { |
| 84 | if mod.showManuf { |
| 85 | return []string{ |
| 86 | rssi, |
| 87 | bssid, |
| 88 | tui.Dim(station.Vendor), |
| 89 | strconv.Itoa(station.Channel), |
| 90 | sent, |
| 91 | recvd, |
| 92 | seen, |
| 93 | }, include |
| 94 | } else { |
| 95 | return []string{ |
| 96 | rssi, |
| 97 | bssid, |
| 98 | strconv.Itoa(station.Channel), |
| 99 | sent, |
| 100 | recvd, |
| 101 | seen, |
| 102 | }, include |
| 103 | } |
| 104 | } else { |
| 105 | // this is ugly, but necessary in order to have this |
| 106 | // method handle both access point and clients |
| 107 | // transparently |
| 108 | clients := "" |
| 109 | if ap, found := mod.Session.WiFi.Get(station.HwAddress); found { |
| 110 | if ap.NumClients() > 0 { |
| 111 | clients = strconv.Itoa(ap.NumClients()) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | wps := "" |
| 116 | if station.HasWPS() { |
| 117 | if ver, found := station.WPS["Version"]; found { |
| 118 | wps = ver |
| 119 | } else { |
| 120 | wps = "✔" |
| 121 | } |
| 122 | |
| 123 | if state, found := station.WPS["State"]; found { |
| 124 | if state == "Not Configured" { |
| 125 | wps += " (not configured)" |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | wps = tui.Dim(tui.Yellow(wps)) |
| 130 | } |
| 131 | |
| 132 | if mod.showManuf { |
| 133 | return []string{ |
| 134 | rssi, |
| 135 | bssid, |
| 136 | tui.Dim(station.Vendor), |
| 137 | ssid, |
| 138 | encryption, |
| 139 | wps, |
| 140 | strconv.Itoa(station.Channel), |
| 141 | clients, |
| 142 | sent, |
| 143 | recvd, |
| 144 | seen, |
| 145 | }, include |
| 146 | } else { |
| 147 | return []string{ |
| 148 | rssi, |
| 149 | bssid, |
| 150 | ssid, |
| 151 | encryption, |
| 152 | wps, |
| 153 | strconv.Itoa(station.Channel), |
| 154 | clients, |
| 155 | sent, |
| 156 | recvd, |
| 157 | seen, |
| 158 | }, include |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | func (mod *WiFiModule) doFilter(station *network.Station) bool { |
| 164 | if mod.selector.Expression == nil { |
| 165 | return true |
| 166 | } |
| 167 | return mod.selector.Expression.MatchString(station.BSSID()) || |
| 168 | mod.selector.Expression.MatchString(station.ESSID()) || |
| 169 | mod.selector.Expression.MatchString(station.Alias) || |
| 170 | mod.selector.Expression.MatchString(station.Vendor) || |
| 171 | mod.selector.Expression.MatchString(station.Encryption) |
| 172 | } |
| 173 | |
| 174 | func (mod *WiFiModule) doSelection() (err error, stations []*network.Station) { |
| 175 | if err = mod.selector.Update(); err != nil { |
| 176 | return |
| 177 | } |
| 178 | |
| 179 | apSelected := mod.isApSelected() |
| 180 | if apSelected { |
| 181 | if ap, found := mod.Session.WiFi.Get(mod.ap.HwAddress); found { |
| 182 | stations = ap.Clients() |
| 183 | } else { |
| 184 | err = fmt.Errorf("Could not find station %s", mod.ap.HwAddress) |
| 185 | return |
| 186 | } |
| 187 | } else { |
| 188 | stations = mod.Session.WiFi.Stations() |
| 189 | } |
| 190 | |
| 191 | filtered := []*network.Station{} |
| 192 | for _, station := range stations { |
| 193 | if mod.doFilter(station) { |
| 194 | filtered = append(filtered, station) |
| 195 | } |
| 196 | } |
| 197 | stations = filtered |
| 198 | |
| 199 | switch mod.selector.SortField { |
| 200 | case "seen": |
| 201 | sort.Sort(ByWiFiSeenSorter(stations)) |
| 202 | case "essid": |
| 203 | sort.Sort(ByEssidSorter(stations)) |
| 204 | case "bssid": |
| 205 | sort.Sort(ByBssidSorter(stations)) |
| 206 | case "channel": |
| 207 | sort.Sort(ByChannelSorter(stations)) |
| 208 | case "clients": |
| 209 | sort.Sort(ByClientsSorter(stations)) |
| 210 | case "encryption": |
| 211 | sort.Sort(ByEncryptionSorter(stations)) |
| 212 | case "sent": |
| 213 | sort.Sort(ByWiFiSentSorter(stations)) |
| 214 | case "rcvd": |
| 215 | sort.Sort(ByWiFiRcvdSorter(stations)) |
| 216 | case "rssi": |
| 217 | sort.Sort(ByRSSISorter(stations)) |
| 218 | default: |
| 219 | sort.Sort(ByRSSISorter(stations)) |
| 220 | } |
| 221 | |
| 222 | // default is asc |
| 223 | if mod.selector.Sort == "desc" { |
| 224 | // from https://github.com/golang/go/wiki/SliceTricks |
| 225 | for i := len(stations)/2 - 1; i >= 0; i-- { |
| 226 | opp := len(stations) - 1 - i |
| 227 | stations[i], stations[opp] = stations[opp], stations[i] |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | if mod.selector.Limit > 0 { |
| 232 | limit := mod.selector.Limit |
| 233 | max := len(stations) |
| 234 | if limit > max { |
| 235 | limit = max |
| 236 | } |
| 237 | stations = stations[0:limit] |
| 238 | } |
| 239 | |
| 240 | return |
| 241 | } |
| 242 | |
| 243 | func (mod *WiFiModule) colDecorate(colNames []string, name string, dir string) { |
| 244 | for i, c := range colNames { |
| 245 | if c == name { |
| 246 | colNames[i] += " " + dir |
| 247 | break |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | func (mod *WiFiModule) colNames(nrows int) []string { |
| 253 | columns := []string(nil) |
| 254 | |
| 255 | if !mod.isApSelected() { |
| 256 | if mod.showManuf { |
| 257 | columns = []string{"RSSI", "BSSID", "Manufacturer", "SSID", "Encryption", "WPS", "Ch", "Clients", "Sent", "Recvd", "Seen"} |
| 258 | } else { |
| 259 | columns = []string{"RSSI", "BSSID", "SSID", "Encryption", "WPS", "Ch", "Clients", "Sent", "Recvd", "Seen"} |
| 260 | } |
| 261 | } else if nrows > 0 { |
| 262 | if mod.showManuf { |
| 263 | columns = []string{"RSSI", "BSSID", "Manufacturer", "Ch", "Sent", "Recvd", "Seen"} |
| 264 | } else { |
| 265 | columns = []string{"RSSI", "BSSID", "Ch", "Sent", "Recvd", "Seen"} |
| 266 | } |
| 267 | mod.Printf("\n%s clients:\n", mod.ap.HwAddress) |
| 268 | } else { |
| 269 | mod.Printf("\nNo authenticated clients detected for %s.\n", mod.ap.HwAddress) |
| 270 | } |
| 271 | |
| 272 | if columns != nil { |
| 273 | switch mod.selector.SortField { |
| 274 | case "seen": |
| 275 | mod.colDecorate(columns, "Seen", mod.selector.SortSymbol) |
| 276 | case "essid": |
| 277 | mod.colDecorate(columns, "SSID", mod.selector.SortSymbol) |
| 278 | case "bssid": |
| 279 | mod.colDecorate(columns, "BSSID", mod.selector.SortSymbol) |
| 280 | case "channel": |
| 281 | mod.colDecorate(columns, "Ch", mod.selector.SortSymbol) |
| 282 | case "clients": |
| 283 | mod.colDecorate(columns, "Clients", mod.selector.SortSymbol) |
| 284 | case "encryption": |
| 285 | mod.colDecorate(columns, "Encryption", mod.selector.SortSymbol) |
| 286 | case "sent": |
| 287 | mod.colDecorate(columns, "Sent", mod.selector.SortSymbol) |
| 288 | case "rcvd": |
| 289 | mod.colDecorate(columns, "Recvd", mod.selector.SortSymbol) |
| 290 | case "rssi": |
| 291 | mod.colDecorate(columns, "RSSI", mod.selector.SortSymbol) |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | return columns |
| 296 | } |
| 297 | |
| 298 | func (mod *WiFiModule) showStatusBar() { |
| 299 | parts := []string{ |
| 300 | fmt.Sprintf("%s (ch. %d)", mod.iface.Name(), network.GetInterfaceChannel(mod.iface.Name())), |
| 301 | fmt.Sprintf("%s %s", tui.Red("↑"), humanize.Bytes(mod.Session.Queue.Stats.Sent)), |
| 302 | fmt.Sprintf("%s %s", tui.Green("↓"), humanize.Bytes(mod.Session.Queue.Stats.Received)), |
| 303 | fmt.Sprintf("%d pkts", mod.Session.Queue.Stats.PktReceived), |
| 304 | } |
| 305 | |
| 306 | if nErrors := mod.Session.Queue.Stats.Errors; nErrors > 0 { |
| 307 | parts = append(parts, fmt.Sprintf("%d errs", nErrors)) |
| 308 | } |
| 309 | |
| 310 | if nHandshakes := mod.Session.WiFi.NumHandshakes(); nHandshakes > 0 { |
| 311 | parts = append(parts, fmt.Sprintf("%d handshakes", nHandshakes)) |
| 312 | } |
| 313 | |
| 314 | mod.Printf("\n%s\n\n", strings.Join(parts, " / ")) |
| 315 | } |
| 316 | |
| 317 | func (mod *WiFiModule) Show() (err error) { |
| 318 | if mod.Running() == false { |
| 319 | return session.ErrAlreadyStopped(mod.Name()) |
| 320 | } |
| 321 | |
| 322 | var stations []*network.Station |
| 323 | if err, stations = mod.doSelection(); err != nil { |
| 324 | return |
| 325 | } |
| 326 | |
| 327 | if err, mod.showManuf = mod.BoolParam("wifi.show.manufacturer"); err != nil { |
| 328 | return err |
| 329 | } |
| 330 | |
| 331 | rows := make([][]string, 0) |
| 332 | for _, s := range stations { |
| 333 | if row, include := mod.getRow(s); include { |
| 334 | rows = append(rows, row) |
| 335 | } |
| 336 | } |
| 337 | nrows := len(rows) |
| 338 | if nrows > 0 { |
| 339 | tui.Table(mod.Session.Events.Stdout, mod.colNames(nrows), rows) |
| 340 | } |
| 341 | |
| 342 | mod.showStatusBar() |
| 343 | |
| 344 | mod.Session.Refresh() |
| 345 | |
| 346 | return nil |
| 347 | } |
| 348 | |
| 349 | func (mod *WiFiModule) ShowWPS(bssid string) (err error) { |
| 350 | if mod.Running() == false { |
| 351 | return session.ErrAlreadyStopped(mod.Name()) |
| 352 | } |
| 353 | |
| 354 | toShow := []*network.Station{} |
| 355 | if bssid == network.BroadcastMac { |
| 356 | for _, station := range mod.Session.WiFi.List() { |
| 357 | if station.HasWPS() { |
| 358 | toShow = append(toShow, station.Station) |
| 359 | } |
| 360 | } |
| 361 | } else { |
| 362 | if station, found := mod.Session.WiFi.Get(bssid); found { |
| 363 | if station.HasWPS() { |
| 364 | toShow = append(toShow, station.Station) |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | if len(toShow) == 0 { |
| 370 | return fmt.Errorf("no WPS enabled access points matched the criteria") |
| 371 | } |
| 372 | |
| 373 | sort.Sort(ByBssidSorter(toShow)) |
| 374 | |
| 375 | colNames := []string{"Name", "Value"} |
| 376 | |
| 377 | for _, station := range toShow { |
| 378 | ssid := ops.Ternary(station.ESSID() == "<hidden>", tui.Dim(station.ESSID()), station.ESSID()).(string) |
| 379 | |
| 380 | rows := [][]string{ |
| 381 | {tui.Green("essid"), ssid}, |
| 382 | {tui.Green("bssid"), station.BSSID()}, |
| 383 | } |
| 384 | |
| 385 | keys := []string{} |
| 386 | for name := range station.WPS { |
| 387 | keys = append(keys, name) |
| 388 | } |
| 389 | sort.Strings(keys) |
| 390 | |
| 391 | for _, name := range keys { |
| 392 | rows = append(rows, []string{ |
| 393 | tui.Green(name), |
| 394 | tui.Yellow(station.WPS[name]), |
| 395 | }) |
| 396 | } |
| 397 | |
| 398 | tui.Table(mod.Session.Events.Stdout, colNames, rows) |
| 399 | } |
| 400 | |
| 401 | return nil |
| 402 | } |