Debian Codegraph @

modules/hid/hid_recon.go

raw symbols SCIP
1 package hid
2
3 import (
4 "time"
5
6 "github.com/bettercap/nrf24"
7 "github.com/google/gousb"
8 )
9
10 func (mod *HIDRecon) doHopping() {
11 mod.writeLock.Lock()
12 defer mod.writeLock.Unlock()
13
14 if mod.inPromMode == false {
15 if err := mod.dongle.EnterPromiscMode(); err != nil {
16 mod.Error("error entering promiscuous mode: %v", err)
17 } else {
18 mod.inSniffMode = false
19 mod.inPromMode = true
20 mod.Debug("device entered promiscuous mode")
21 }
22 }
23
24 if time.Since(mod.lastHop) >= mod.hopPeriod {
25 mod.channel++
26 if mod.channel > nrf24.TopChannel {
27 mod.channel = 1
28 }
29 if err := mod.dongle.SetChannel(mod.channel); err != nil {
30 if err == gousb.ErrorNoDevice || err == gousb.TransferStall {
31 mod.Error("device disconnected, stopping module")
32 mod.forceStop()
33 return
34 } else {
35 mod.Warning("error hopping on channel %d: %v", mod.channel, err)
36 }
37 } else {
38 mod.lastHop = time.Now()
39 }
40 }
41 }
42
43 func (mod *HIDRecon) onDeviceDetected(buf []byte) {
44 if sz := len(buf); sz >= 5 {
45 addr, payload := buf[0:5], buf[5:]
46 mod.Debug("detected device %x on channel %d (payload:%x)\n", addr, mod.channel, payload)
47 if isNew, dev := mod.Session.HID.AddIfNew(addr, mod.channel, payload); isNew {
48 // sniff for a while in order to detect the device type
49 go func() {
50 prevSilent := mod.sniffSilent
51
52 if err := mod.setSniffMode(dev.Address, true); err == nil {
53 mod.Debug("detecting device type ...")
54 defer func() {
55 mod.sniffLock.Unlock()
56 mod.setSniffMode("clear", prevSilent)
57 }()
58 // make sure nobody can sniff to another
59 // address until we're not done here...
60 mod.sniffLock.Lock()
61
62 time.Sleep(mod.sniffPeriod)
63 } else {
64 mod.Warning("error while sniffing %s: %v", dev.Address, err)
65 }
66 }()
67 }
68 }
69 }
70
71 func (mod *HIDRecon) devPruner() {
72 mod.waitGroup.Add(1)
73 defer mod.waitGroup.Done()
74
75 maxDeviceTTL := time.Duration(mod.devTTL) * time.Second
76 mod.Debug("devices pruner started with ttl %v", maxDeviceTTL)
77 for mod.Running() {
78 for _, dev := range mod.Session.HID.Devices() {
79 sinceLastSeen := time.Since(dev.LastSeen)
80 if sinceLastSeen > maxDeviceTTL {
81 mod.Debug("device %s not seen in %s, removing.", dev.Address, sinceLastSeen)
82 mod.Session.HID.Remove(dev.Address)
83 }
84 }
85 time.Sleep(30 * time.Second)
86 }
87 }
88
89 func (mod *HIDRecon) Start() error {
90 if err := mod.Configure(); err != nil {
91 return err
92 }
93
94 return mod.SetRunning(true, func() {
95 mod.waitGroup.Add(1)
96 defer mod.waitGroup.Done()
97
98 go mod.devPruner()
99
100 mod.Info("hopping on %d channels every %s", nrf24.TopChannel, mod.hopPeriod)
101 for mod.Running() {
102 if mod.isSniffing() {
103 mod.doPing()
104 } else {
105 mod.doHopping()
106 }
107
108 if mod.isInjecting() {
109 mod.doInjection()
110 mod.setInjectionMode("clear")
111 continue
112 }
113
114 buf, err := mod.dongle.ReceivePayload()
115 if err != nil {
116 if err == gousb.ErrorNoDevice || err == gousb.TransferStall {
117 mod.Error("device disconnected, stopping module")
118 mod.forceStop()
119 return
120 }
121 mod.Warning("error receiving payload from channel %d: %v", mod.channel, err)
122 continue
123 }
124
125 if mod.isSniffing() {
126 mod.onSniffedBuffer(buf)
127 } else {
128 mod.onDeviceDetected(buf)
129 }
130 }
131
132 mod.Debug("stopped")
133 })
134 }