hid_inject.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package hid
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/bettercap/bettercap/network"
  6. "github.com/evilsocket/islazy/tui"
  7. "github.com/dustin/go-humanize"
  8. )
  9. func (mod *HIDRecon) isInjecting() bool {
  10. return mod.inInjectMode
  11. }
  12. func (mod *HIDRecon) setInjectionMode(address string) error {
  13. if err := mod.setSniffMode(address, true); err != nil {
  14. return err
  15. } else if address == "clear" {
  16. mod.inInjectMode = false
  17. } else {
  18. mod.inInjectMode = true
  19. }
  20. return nil
  21. }
  22. func errNoDevice(addr string) error {
  23. return fmt.Errorf("HID device %s not found, make sure that hid.recon is on and that this device has been discovered", addr)
  24. }
  25. func errNoType(addr string) error {
  26. return fmt.Errorf("HID frame injection requires the device type to be detected, try to 'hid.sniff %s' for a few seconds.", addr)
  27. }
  28. func errNotSupported(dev *network.HIDDevice) error {
  29. return fmt.Errorf("HID frame injection is not supported for device type %s", dev.Type.String())
  30. }
  31. func errNoKeyMap(layout string) error {
  32. return fmt.Errorf("could not find keymap for '%s' layout, supported layouts are: %s", layout, SupportedLayouts())
  33. }
  34. func (mod *HIDRecon) prepInjection() (error, *network.HIDDevice, []*Command) {
  35. var err error
  36. if err, mod.sniffType = mod.StringParam("hid.force.type"); err != nil {
  37. return err, nil, nil
  38. }
  39. dev, found := mod.Session.HID.Get(mod.sniffAddr)
  40. if found == false {
  41. mod.Warning("device %s is not visible, will use HID type %s", mod.sniffAddr, tui.Yellow(mod.sniffType))
  42. } else if dev.Type == network.HIDTypeUnknown {
  43. mod.Warning("device %s type has not been detected yet, falling back to '%s'", mod.sniffAddr, tui.Yellow(mod.sniffType))
  44. }
  45. var builder FrameBuilder
  46. if found && dev.Type != network.HIDTypeUnknown {
  47. // get the device specific protocol handler
  48. builder, found = FrameBuilders[dev.Type]
  49. if found == false {
  50. return errNotSupported(dev), nil, nil
  51. }
  52. } else {
  53. // get the device protocol handler from the hid.force.type parameter
  54. builder = builderFromName(mod.sniffType)
  55. }
  56. // get the keymap from the selected layout
  57. keyMap := KeyMapFor(mod.keyLayout)
  58. if keyMap == nil {
  59. return errNoKeyMap(mod.keyLayout), nil, nil
  60. }
  61. // parse the script into a list of Command objects
  62. cmds, err := mod.parser.Parse(keyMap, mod.scriptPath)
  63. if err != nil {
  64. return err, nil, nil
  65. }
  66. mod.Info("%s loaded ...", mod.scriptPath)
  67. // build the protocol specific frames to send
  68. if err := builder.BuildFrames(dev, cmds); err != nil {
  69. return err, nil, nil
  70. }
  71. return nil, dev, cmds
  72. }
  73. func (mod *HIDRecon) doInjection() {
  74. mod.writeLock.Lock()
  75. defer mod.writeLock.Unlock()
  76. err, dev, cmds := mod.prepInjection()
  77. if err != nil {
  78. mod.Error("%v", err)
  79. return
  80. }
  81. numFrames := 0
  82. szFrames := 0
  83. for _, cmd := range cmds {
  84. for _, frame := range cmd.Frames {
  85. numFrames++
  86. szFrames += len(frame.Data)
  87. }
  88. }
  89. devType := mod.sniffType
  90. if dev != nil {
  91. devType = dev.Type.String()
  92. }
  93. mod.Info("sending %d (%s) HID frames to %s (type:%s layout:%s) ...",
  94. numFrames,
  95. humanize.Bytes(uint64(szFrames)),
  96. tui.Bold(mod.sniffAddr),
  97. tui.Yellow(devType),
  98. tui.Yellow(mod.keyLayout))
  99. for i, cmd := range cmds {
  100. for j, frame := range cmd.Frames {
  101. for attempt := 0; attempt < 3; attempt++ {
  102. if err := mod.dongle.TransmitPayload(frame.Data, 500, 5); err != nil {
  103. if attempt < 2 {
  104. mod.Debug("error sending frame #%d of HID command #%d: %v, retrying ...", j, i, err)
  105. } else {
  106. mod.Error("error sending frame #%d of HID command #%d: %v", j, i, err)
  107. }
  108. } else {
  109. break
  110. }
  111. }
  112. if frame.Delay > 0 {
  113. mod.Debug("sleeping %dms after frame #%d of command #%d ...", frame.Delay, j, i)
  114. time.Sleep(frame.Delay)
  115. }
  116. }
  117. if cmd.Sleep > 0 {
  118. mod.Debug("sleeping %dms after command #%d ...", cmd.Sleep, i)
  119. time.Sleep(time.Duration(cmd.Sleep) * time.Millisecond)
  120. }
  121. }
  122. }