diff --git a/modules/hid_recon/hid_command.go b/modules/hid_recon/hid_command.go index 6bc0387f..d0c99c59 100644 --- a/modules/hid_recon/hid_command.go +++ b/modules/hid_recon/hid_command.go @@ -19,7 +19,6 @@ func NewFrame(buf []byte, delay int) Frame { type Command struct { Mode byte HID byte - Char string Sleep int Frames []Frame } diff --git a/modules/hid_recon/hid_inject.go b/modules/hid_recon/hid_inject.go index d46b175b..f752f631 100644 --- a/modules/hid_recon/hid_inject.go +++ b/modules/hid_recon/hid_inject.go @@ -4,6 +4,8 @@ import ( "fmt" "time" + "github.com/bettercap/bettercap/network" + "github.com/evilsocket/islazy/tui" "github.com/dustin/go-humanize" @@ -24,42 +26,66 @@ func (mod *HIDRecon) setInjectionMode(address string) error { return nil } -func (mod *HIDRecon) doInjection() { +func errNoDevice(addr string) error { + return fmt.Errorf("HID device %s not found, make sure that hid.recon is on and that device has been discovered", addr) +} + +func errNoType(addr string) error { + return fmt.Errorf("HID frame injection requires the device type to be detected, try to 'hid.sniff %s' for a few seconds.", addr) +} + +func errNotSupported(dev *network.HIDDevice) error { + return fmt.Errorf("HID frame injection is not supported for device type %s", dev.Type.String()) +} + +func errNoKeyMap(layout string) error { + return fmt.Errorf("could not find keymap for '%s' layout, supported layouts are: %s", layout, SupportedLayouts()) +} + +func (mod *HIDRecon) prepInjection() (error, *network.HIDDevice, []*Command) { dev, found := mod.Session.HID.Get(mod.sniffAddr) if found == false { - mod.Warning("could not find HID device %s", mod.sniffAddr) - return + return errNoDevice(mod.sniffAddr), nil, nil } builder, found := FrameBuilders[dev.Type] if found == false { - mod.Warning("HID frame injection is not supported for device type %s", dev.Type.String()) - return + if dev.Type == network.HIDTypeUnknown { + return errNoType(mod.sniffAddr), nil, nil + } + return errNotSupported(dev), nil, nil } keyLayout := KeyMapFor(mod.keyLayout) if keyLayout == nil { - mod.Warning("could not find keymap for '%s' layout", mod.keyLayout) - return + return errNoKeyMap(mod.keyLayout), nil, nil } str := "hello world from bettercap ^_^" cmds := make([]*Command, 0) for _, c := range str { - ch := fmt.Sprintf("%c", c) - if m, found := keyLayout[ch]; found { + if m, found := keyLayout[string(c)]; found { cmds = append(cmds, &Command{ - Char: ch, HID: m.HID, Mode: m.Mode, }) } else { - mod.Warning("could not find HID command for '%c'", ch) - return + return fmt.Errorf("could not find HID command for '%c'", c), nil, nil } } builder.BuildFrames(cmds) + + return nil, dev, cmds +} + +func (mod *HIDRecon) doInjection() { + err, dev, cmds := mod.prepInjection() + if err != nil { + mod.Error("%v", err) + return + } + numFrames := 0 szFrames := 0 for _, cmd := range cmds { diff --git a/modules/hid_recon/hid_keymaps.go b/modules/hid_recon/hid_keymaps.go index 459e275f..0c296d0b 100644 --- a/modules/hid_recon/hid_keymaps.go +++ b/modules/hid_recon/hid_keymaps.go @@ -1,5 +1,9 @@ package hid_recon +import ( + "sort" +) + type KeyMap map[string]Command var BaseMap = KeyMap{ @@ -2111,3 +2115,12 @@ func KeyMapFor(lang string) KeyMap { } return nil } + +func SupportedLayouts() []string { + maps := []string{} + for lang, _ := range KeyMaps { + maps = append(maps, lang) + } + sort.Strings(maps) + return maps +} diff --git a/modules/hid_recon/hid_show.go b/modules/hid_recon/hid_show.go index 986cf379..cb4cbe6e 100644 --- a/modules/hid_recon/hid_show.go +++ b/modules/hid_recon/hid_show.go @@ -114,7 +114,7 @@ func (mod *HIDRecon) Show() (err error) { if mod.sniffAddrRaw == nil { fmt.Printf("\nchannel:%d\n\n", mod.channel) } else { - fmt.Printf("\nchannel:%d sniffing:%s\n\n", mod.channel, mod.sniffAddr) + fmt.Printf("\nchannel:%d sniffing:%s\n\n", mod.channel, tui.Red(mod.sniffAddr)) } if len(rows) > 0 {