hid.go 6.5 KB

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