| 1 | package hid |
| 2 | |
| 3 | import ( |
| 4 | "sort" |
| 5 | "time" |
| 6 | |
| 7 | "github.com/bettercap/bettercap/network" |
| 8 | |
| 9 | "github.com/dustin/go-humanize" |
| 10 | |
| 11 | "github.com/evilsocket/islazy/tui" |
| 12 | ) |
| 13 | |
| 14 | var ( |
| 15 | AliveTimeInterval = time.Duration(5) * time.Minute |
| 16 | PresentTimeInterval = time.Duration(1) * time.Minute |
| 17 | JustJoinedTimeInterval = time.Duration(10) * time.Second |
| 18 | ) |
| 19 | |
| 20 | func (mod *HIDRecon) getRow(dev *network.HIDDevice) []string { |
| 21 | sinceLastSeen := time.Since(dev.LastSeen) |
| 22 | seen := dev.LastSeen.Format("15:04:05") |
| 23 | |
| 24 | if sinceLastSeen <= JustJoinedTimeInterval { |
| 25 | seen = tui.Bold(seen) |
| 26 | } else if sinceLastSeen > PresentTimeInterval { |
| 27 | seen = tui.Dim(seen) |
| 28 | } |
| 29 | |
| 30 | return []string{ |
| 31 | dev.Address, |
| 32 | dev.Type.String(), |
| 33 | dev.Channels(), |
| 34 | humanize.Bytes(dev.PayloadsSize()), |
| 35 | seen, |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func (mod *HIDRecon) doFilter(dev *network.HIDDevice) bool { |
| 40 | if mod.selector.Expression == nil { |
| 41 | return true |
| 42 | } |
| 43 | return mod.selector.Expression.MatchString(dev.Address) |
| 44 | } |
| 45 | |
| 46 | func (mod *HIDRecon) doSelection() (err error, devices []*network.HIDDevice) { |
| 47 | if err = mod.selector.Update(); err != nil { |
| 48 | return |
| 49 | } |
| 50 | |
| 51 | devices = mod.Session.HID.Devices() |
| 52 | filtered := []*network.HIDDevice{} |
| 53 | for _, dev := range devices { |
| 54 | if mod.doFilter(dev) { |
| 55 | filtered = append(filtered, dev) |
| 56 | } |
| 57 | } |
| 58 | devices = filtered |
| 59 | |
| 60 | switch mod.selector.SortField { |
| 61 | case "mac": |
| 62 | sort.Sort(ByHIDMacSorter(devices)) |
| 63 | case "seen": |
| 64 | sort.Sort(ByHIDSeenSorter(devices)) |
| 65 | } |
| 66 | |
| 67 | // default is asc |
| 68 | if mod.selector.Sort == "desc" { |
| 69 | // from https://github.com/golang/go/wiki/SliceTricks |
| 70 | for i := len(devices)/2 - 1; i >= 0; i-- { |
| 71 | opp := len(devices) - 1 - i |
| 72 | devices[i], devices[opp] = devices[opp], devices[i] |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if mod.selector.Limit > 0 { |
| 77 | limit := mod.selector.Limit |
| 78 | max := len(devices) |
| 79 | if limit > max { |
| 80 | limit = max |
| 81 | } |
| 82 | devices = devices[0:limit] |
| 83 | } |
| 84 | |
| 85 | return |
| 86 | } |
| 87 | |
| 88 | func (mod *HIDRecon) colNames() []string { |
| 89 | colNames := []string{"MAC", "Type", "Channels", "Data", "Seen"} |
| 90 | switch mod.selector.SortField { |
| 91 | case "mac": |
| 92 | colNames[0] += " " + mod.selector.SortSymbol |
| 93 | case "seen": |
| 94 | colNames[4] += " " + mod.selector.SortSymbol |
| 95 | } |
| 96 | return colNames |
| 97 | } |
| 98 | |
| 99 | func (mod *HIDRecon) Show() (err error) { |
| 100 | var devices []*network.HIDDevice |
| 101 | if err, devices = mod.doSelection(); err != nil { |
| 102 | return |
| 103 | } |
| 104 | |
| 105 | rows := make([][]string, 0) |
| 106 | for _, dev := range devices { |
| 107 | rows = append(rows, mod.getRow(dev)) |
| 108 | } |
| 109 | |
| 110 | tui.Table(mod.Session.Events.Stdout, mod.colNames(), rows) |
| 111 | |
| 112 | if mod.sniffAddrRaw == nil { |
| 113 | mod.Printf("\nchannel:%d\n\n", mod.channel) |
| 114 | } else { |
| 115 | mod.Printf("\nchannel:%d sniffing:%s\n\n", mod.channel, tui.Red(mod.sniffAddr)) |
| 116 | } |
| 117 | |
| 118 | if len(rows) > 0 { |
| 119 | mod.Session.Refresh() |
| 120 | } |
| 121 | |
| 122 | return nil |
| 123 | } |