mirror of
https://github.com/bettercap/bettercap
synced 2025-08-19 04:59:25 -07:00
misc: small fix or general refactoring i did not bother commenting
This commit is contained in:
parent
d6c406cb73
commit
7cc9e5b0b6
4 changed files with 52 additions and 14 deletions
|
@ -19,7 +19,6 @@ func NewFrame(buf []byte, delay int) Frame {
|
||||||
type Command struct {
|
type Command struct {
|
||||||
Mode byte
|
Mode byte
|
||||||
HID byte
|
HID byte
|
||||||
Char string
|
|
||||||
Sleep int
|
Sleep int
|
||||||
Frames []Frame
|
Frames []Frame
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/bettercap/bettercap/network"
|
||||||
|
|
||||||
"github.com/evilsocket/islazy/tui"
|
"github.com/evilsocket/islazy/tui"
|
||||||
|
|
||||||
"github.com/dustin/go-humanize"
|
"github.com/dustin/go-humanize"
|
||||||
|
@ -24,42 +26,66 @@ func (mod *HIDRecon) setInjectionMode(address string) error {
|
||||||
return nil
|
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)
|
dev, found := mod.Session.HID.Get(mod.sniffAddr)
|
||||||
if found == false {
|
if found == false {
|
||||||
mod.Warning("could not find HID device %s", mod.sniffAddr)
|
return errNoDevice(mod.sniffAddr), nil, nil
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
builder, found := FrameBuilders[dev.Type]
|
builder, found := FrameBuilders[dev.Type]
|
||||||
if found == false {
|
if found == false {
|
||||||
mod.Warning("HID frame injection is not supported for device type %s", dev.Type.String())
|
if dev.Type == network.HIDTypeUnknown {
|
||||||
return
|
return errNoType(mod.sniffAddr), nil, nil
|
||||||
|
}
|
||||||
|
return errNotSupported(dev), nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
keyLayout := KeyMapFor(mod.keyLayout)
|
keyLayout := KeyMapFor(mod.keyLayout)
|
||||||
if keyLayout == nil {
|
if keyLayout == nil {
|
||||||
mod.Warning("could not find keymap for '%s' layout", mod.keyLayout)
|
return errNoKeyMap(mod.keyLayout), nil, nil
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
str := "hello world from bettercap ^_^"
|
str := "hello world from bettercap ^_^"
|
||||||
cmds := make([]*Command, 0)
|
cmds := make([]*Command, 0)
|
||||||
for _, c := range str {
|
for _, c := range str {
|
||||||
ch := fmt.Sprintf("%c", c)
|
if m, found := keyLayout[string(c)]; found {
|
||||||
if m, found := keyLayout[ch]; found {
|
|
||||||
cmds = append(cmds, &Command{
|
cmds = append(cmds, &Command{
|
||||||
Char: ch,
|
|
||||||
HID: m.HID,
|
HID: m.HID,
|
||||||
Mode: m.Mode,
|
Mode: m.Mode,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
mod.Warning("could not find HID command for '%c'", ch)
|
return fmt.Errorf("could not find HID command for '%c'", c), nil, nil
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.BuildFrames(cmds)
|
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
|
numFrames := 0
|
||||||
szFrames := 0
|
szFrames := 0
|
||||||
for _, cmd := range cmds {
|
for _, cmd := range cmds {
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
package hid_recon
|
package hid_recon
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
type KeyMap map[string]Command
|
type KeyMap map[string]Command
|
||||||
|
|
||||||
var BaseMap = KeyMap{
|
var BaseMap = KeyMap{
|
||||||
|
@ -2111,3 +2115,12 @@ func KeyMapFor(lang string) KeyMap {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SupportedLayouts() []string {
|
||||||
|
maps := []string{}
|
||||||
|
for lang, _ := range KeyMaps {
|
||||||
|
maps = append(maps, lang)
|
||||||
|
}
|
||||||
|
sort.Strings(maps)
|
||||||
|
return maps
|
||||||
|
}
|
||||||
|
|
|
@ -114,7 +114,7 @@ func (mod *HIDRecon) Show() (err error) {
|
||||||
if mod.sniffAddrRaw == nil {
|
if mod.sniffAddrRaw == nil {
|
||||||
fmt.Printf("\nchannel:%d\n\n", mod.channel)
|
fmt.Printf("\nchannel:%d\n\n", mod.channel)
|
||||||
} else {
|
} 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 {
|
if len(rows) > 0 {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue