Debian Codegraph @

modules/hid/hid.go

raw symbols SCIP
1 package hid
2
3 import (
4 "fmt"
5 "strings"
6 "sync"
7 "time"
8
9 "github.com/bettercap/bettercap/modules/utils"
10 "github.com/bettercap/bettercap/session"
11
12 "github.com/bettercap/nrf24"
13 )
14
15 type HIDRecon struct {
16 session.SessionModule
17 dongle *nrf24.Dongle
18 waitGroup *sync.WaitGroup
19 channel int
20 devTTL int
21 hopPeriod time.Duration
22 pingPeriod time.Duration
23 sniffPeriod time.Duration
24 lastHop time.Time
25 lastPing time.Time
26 useLNA bool
27 sniffLock *sync.Mutex
28 writeLock *sync.Mutex
29 sniffAddrRaw []byte
30 sniffAddr string
31 sniffType string
32 pingPayload []byte
33 inSniffMode bool
34 sniffSilent bool
35 inPromMode bool
36 inInjectMode bool
37 keyLayout string
38 scriptPath string
39 parser DuckyParser
40 selector *utils.ViewSelector
41 }
42
43 func NewHIDRecon(s *session.Session) *HIDRecon {
44 mod := &HIDRecon{
45 SessionModule: session.NewSessionModule("hid", s),
46 waitGroup: &sync.WaitGroup{},
47 sniffLock: &sync.Mutex{},
48 writeLock: &sync.Mutex{},
49 devTTL: 1200,
50 hopPeriod: 100 * time.Millisecond,
51 pingPeriod: 100 * time.Millisecond,
52 sniffPeriod: 500 * time.Millisecond,
53 lastHop: time.Now(),
54 lastPing: time.Now(),
55 useLNA: true,
56 channel: 1,
57 sniffAddrRaw: nil,
58 sniffAddr: "",
59 inSniffMode: false,
60 inPromMode: false,
61 inInjectMode: false,
62 sniffSilent: true,
63 pingPayload: []byte{0x0f, 0x0f, 0x0f, 0x0f},
64 keyLayout: "US",
65 scriptPath: "",
66 }
67
68 mod.State.Store("sniffing", &mod.sniffAddr)
69 mod.State.Store("injecting", &mod.inInjectMode)
70 mod.State.Store("layouts", SupportedLayouts())
71
72 mod.AddHandler(session.NewModuleHandler("hid.recon on", "",
73 "Start scanning for HID devices on the 2.4Ghz spectrum.",
74 func(args []string) error {
75 return mod.Start()
76 }))
77
78 mod.AddHandler(session.NewModuleHandler("hid.recon off", "",
79 "Stop scanning for HID devices on the 2.4Ghz spectrum.",
80 func(args []string) error {
81 return mod.Stop()
82 }))
83
84 mod.AddHandler(session.NewModuleHandler("hid.clear", "",
85 "Clear all devices collected by the HID discovery module.",
86 func(args []string) error {
87 mod.Session.HID.Clear()
88 return nil
89 }))
90
91 sniff := session.NewModuleHandler("hid.sniff ADDRESS", `(?i)^hid\.sniff ([a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2}|clear)$`,
92 "Start sniffing a specific ADDRESS in order to collect payloads, use 'clear' to stop collecting.",
93 func(args []string) error {
94 return mod.setSniffMode(args[0], false)
95 })
96
97 sniff.Complete("hid.sniff", s.HIDCompleter)
98
99 mod.AddHandler(sniff)
100
101 mod.AddHandler(session.NewModuleHandler("hid.show", "",
102 "Show a list of detected HID devices on the 2.4Ghz spectrum.",
103 func(args []string) error {
104 return mod.Show()
105 }))
106
107 inject := session.NewModuleHandler("hid.inject ADDRESS LAYOUT FILENAME", `(?i)^hid\.inject ([a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2})\s+(.+)\s+(.+)$`,
108 "Parse the duckyscript FILENAME and inject it as HID frames spoofing the device ADDRESS, using the LAYOUT keyboard mapping.",
109 func(args []string) error {
110 if err := mod.setInjectionMode(args[0]); err != nil {
111 return err
112 }
113 mod.keyLayout = args[1]
114 mod.scriptPath = args[2]
115 return nil
116 })
117
118 inject.Complete("hid.inject", s.HIDCompleter)
119
120 mod.AddHandler(inject)
121
122 mod.AddParam(session.NewIntParameter("hid.ttl",
123 fmt.Sprintf("%d", mod.devTTL),
124 "Seconds of inactivity to consider a device as not in range."))
125
126 mod.AddParam(session.NewBoolParameter("hid.lna",
127 "true",
128 "If true, enable the LNA power amplifier for CrazyRadio devices."))
129
130 mod.AddParam(session.NewIntParameter("hid.hop.period",
131 "100",
132 "Time in milliseconds to stay on each channel before hopping to the next one."))
133
134 mod.AddParam(session.NewIntParameter("hid.ping.period",
135 "100",
136 "Time in milliseconds to attempt to ping a device on a given channel while in sniffer mode."))
137
138 mod.AddParam(session.NewIntParameter("hid.sniff.period",
139 "500",
140 "Time in milliseconds to automatically sniff payloads from a device, once it's detected, in order to determine its type."))
141
142 builders := availBuilders()
143
144 mod.AddParam(session.NewStringParameter("hid.force.type",
145 "logitech",
146 fmt.Sprintf("(%s)", strings.Join(builders, "|")),
147 fmt.Sprintf("If the device is not visible or its type has not being detected, force the device type to this value. Accepted values: %s", strings.Join(builders, ", "))))
148
149 mod.parser = DuckyParser{mod}
150 mod.selector = utils.ViewSelectorFor(&mod.SessionModule, "hid.show", []string{"mac", "seen"}, "mac desc")
151
152 return mod
153 }
154
155 func (mod HIDRecon) Name() string {
156 return "hid"
157 }
158
159 func (mod HIDRecon) Description() string {
160 return "A scanner and frames injection module for HID devices on the 2.4Ghz spectrum, using Nordic Semiconductor nRF24LU1+ based USB dongles and Bastille Research RFStorm firmware."
161 }
162
163 func (mod HIDRecon) Author() string {
164 return "Simone Margaritelli <[email protected]> (this module and the nrf24 client library), Bastille Research (the rfstorm firmware and original research), phikshun and infamy for JackIt."
165 }
166
167 func (mod *HIDRecon) Configure() error {
168 var err error
169 var n int
170
171 if mod.Running() {
172 return session.ErrAlreadyStarted(mod.Name())
173 }
174
175 if err, mod.useLNA = mod.BoolParam("hid.lna"); err != nil {
176 return err
177 }
178
179 if err, mod.devTTL = mod.IntParam("hid.ttl"); err != nil {
180 return err
181 }
182
183 if err, n = mod.IntParam("hid.hop.period"); err != nil {
184 return err
185 } else {
186 mod.hopPeriod = time.Duration(n) * time.Millisecond
187 }
188
189 if err, n = mod.IntParam("hid.ping.period"); err != nil {
190 return err
191 } else {
192 mod.pingPeriod = time.Duration(n) * time.Millisecond
193 }
194
195 if err, n = mod.IntParam("hid.sniff.period"); err != nil {
196 return err
197 } else {
198 mod.sniffPeriod = time.Duration(n) * time.Millisecond
199 }
200
201 if mod.dongle, err = nrf24.Open(); err != nil {
202 return fmt.Errorf("make sure that a nRF24LU1+ based USB dongle is connected and running the rfstorm firmware: %s", err)
203 }
204
205 mod.Debug("using device %s", mod.dongle.String())
206
207 if mod.useLNA {
208 if err = mod.dongle.EnableLNA(); err != nil {
209 return fmt.Errorf("make sure your device supports LNA, otherwise set hid.lna to false and retry: %s", err)
210 }
211 mod.Debug("LNA enabled")
212 }
213
214 return nil
215 }
216
217 func (mod *HIDRecon) forceStop() error {
218 return mod.SetRunning(false, func() {
219 if mod.dongle != nil {
220 mod.dongle.Close()
221 mod.Debug("device closed")
222 }
223 })
224 }
225 func (mod *HIDRecon) Stop() error {
226 return mod.SetRunning(false, func() {
227 mod.waitGroup.Wait()
228 if mod.dongle != nil {
229 mod.dongle.Close()
230 mod.Debug("device closed")
231 }
232 })
233 }