Debian Codegraph @

modules/hid/hid_inject.go

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