finished the duckyscript parser

This commit is contained in:
evilsocket 2019-02-20 17:54:44 +01:00
parent 055ba917a1
commit 037d5cea22
No known key found for this signature in database
GPG key ID: 1564D7F30393A456
11 changed files with 249 additions and 42 deletions

View file

@ -1,4 +1,4 @@
package hid_recon package hid
const ( const (
frameDelay = 12 frameDelay = 12

View file

@ -1,4 +1,4 @@
package hid_recon package hid
import ( import (
"github.com/bettercap/bettercap/network" "github.com/bettercap/bettercap/network"

View file

@ -1,4 +1,4 @@
package hid_recon package hid
import ( import (
"time" "time"

187
modules/hid/duckyparser.go Normal file
View file

@ -0,0 +1,187 @@
package hid
import (
"fmt"
"strconv"
"strings"
"github.com/evilsocket/islazy/fs"
)
type DuckyParser struct {
mod *HIDRecon
}
func (p DuckyParser) parseLiteral(what string, kmap KeyMap) (*Command, error) {
// get reference command from the layout
ref, found := kmap[what]
if found == false {
return nil, fmt.Errorf("can't find '%s' in current keymap", what)
}
return &Command{
HID: ref.HID,
Mode: ref.Mode,
}, nil
}
func (p DuckyParser) parseModifier(line string, kmap KeyMap, modMask byte) (*Command, error) {
// get optional key after the modifier
ch := ""
if idx := strings.IndexRune(line, ' '); idx != -1 {
ch = line[idx+1:]
}
cmd, err := p.parseLiteral(ch, kmap)
if err != nil {
return nil, err
}
// apply modifier mask
cmd.Mode |= modMask
return cmd, nil
}
func (p DuckyParser) parseNumber(from string) (int, error) {
idx := strings.IndexRune(from, ' ')
if idx == -1 {
return 0, fmt.Errorf("can't parse number from '%s'", from)
}
num, err := strconv.Atoi(from[idx+1:])
if err != nil {
return 0, fmt.Errorf("can't parse number from '%s': %v", from, err)
}
return num, nil
}
func (p DuckyParser) parseString(from string) (string, error) {
idx := strings.IndexRune(from, ' ')
if idx == -1 {
return "", fmt.Errorf("can't parse string from '%s'", from)
}
return from[idx+1:], nil
}
func (p DuckyParser) lineIs(line string, tokens ...string) bool {
for _, tok := range tokens {
if strings.HasPrefix(line, tok) {
return true
}
}
return false
}
func (p DuckyParser) Parse(kmap KeyMap, path string) (cmds []*Command, err error) {
lines := []string{}
source := []string{}
reader := (chan string)(nil)
if reader, err = fs.LineReader(path); err != nil {
return
} else {
for line := range reader {
lines = append(lines, line)
}
}
// preprocessing
for lineno, line := range lines {
if p.lineIs(line, "REPEAT") {
if lineno == 0 {
err = fmt.Errorf("error on line %d: REPEAT instruction at the beginning of the script", lineno+1)
return
}
times := 1
times, err = p.parseNumber(line)
if err != nil {
return
}
for i := 0; i < times; i++ {
source = append(source, lines[lineno-1])
}
} else {
source = append(source, line)
}
}
cmds = make([]*Command, 0)
for lineno, line := range source {
cmd := &Command{}
if p.lineIs(line, "CTRL", "CONTROL") {
if cmd, err = p.parseModifier(line, kmap, 1); err != nil {
return
}
} else if p.lineIs(line, "SHIFT") {
if cmd, err = p.parseModifier(line, kmap, 2); err != nil {
return
}
} else if p.lineIs(line, "ALT") {
if cmd, err = p.parseModifier(line, kmap, 4); err != nil {
return
}
} else if p.lineIs(line, "GUI", "WINDOWS", "COMMAND") {
if cmd, err = p.parseModifier(line, kmap, 8); err != nil {
return
}
} else if p.lineIs(line, "CTRL-ALT", "CONTROL-ALT") {
if cmd, err = p.parseModifier(line, kmap, 4|1); err != nil {
return
}
} else if p.lineIs(line, "CTRL-SHIFT", "CONTROL-SHIFT") {
if cmd, err = p.parseModifier(line, kmap, 1|2); err != nil {
return
}
} else if p.lineIs(line, "ESC", "ESCAPE", "APP") {
if cmd, err = p.parseLiteral("ESCAPE", kmap); err != nil {
return
}
} else if p.lineIs(line, "ENTER") {
if cmd, err = p.parseLiteral("ENTER", kmap); err != nil {
return
}
} else if p.lineIs(line, "UP", "UPARROW") {
if cmd, err = p.parseLiteral("UP", kmap); err != nil {
return
}
} else if p.lineIs(line, "DOWN", "DOWNARROW") {
if cmd, err = p.parseLiteral("DOWN", kmap); err != nil {
return
}
} else if p.lineIs(line, "LEFT", "LEFTARROW") {
if cmd, err = p.parseLiteral("LEFT", kmap); err != nil {
return
}
} else if p.lineIs(line, "RIGHT", "RIGHTARROW") {
if cmd, err = p.parseLiteral("RIGHT", kmap); err != nil {
return
}
} else if p.lineIs(line, "DELAY", "SLEEP") {
secs := 0
if secs, err = p.parseNumber(line); err != nil {
return
}
cmd = &Command{Sleep: secs}
} else if p.lineIs(line, "STRING", "STR") {
str := ""
if str, err = p.parseString(line); err != nil {
return
}
for _, c := range str {
if cmd, err = p.parseLiteral(string(c), kmap); err != nil {
return
}
cmds = append(cmds, cmd)
}
continue
} else {
err = fmt.Errorf("can't parse line %d ('%s') of %s", lineno+1, line, path)
return
}
cmds = append(cmds, cmd)
}
return
}

View file

@ -1,4 +1,4 @@
package hid_recon package hid
import ( import (
"fmt" "fmt"
@ -43,11 +43,13 @@ func errNoKeyMap(layout string) error {
} }
func (mod *HIDRecon) prepInjection() (error, *network.HIDDevice, []*Command) { func (mod *HIDRecon) prepInjection() (error, *network.HIDDevice, []*Command) {
// we can only inject onto visible connections
dev, found := mod.Session.HID.Get(mod.sniffAddr) dev, found := mod.Session.HID.Get(mod.sniffAddr)
if found == false { if found == false {
return errNoDevice(mod.sniffAddr), nil, nil return errNoDevice(mod.sniffAddr), nil, nil
} }
// get the device specific protocol handler
builder, found := FrameBuilders[dev.Type] builder, found := FrameBuilders[dev.Type]
if found == false { if found == false {
if dev.Type == network.HIDTypeUnknown { if dev.Type == network.HIDTypeUnknown {
@ -56,24 +58,21 @@ func (mod *HIDRecon) prepInjection() (error, *network.HIDDevice, []*Command) {
return errNotSupported(dev), nil, nil return errNotSupported(dev), nil, nil
} }
keyLayout := KeyMapFor(mod.keyLayout) // get the keymap from the selected layout
if keyLayout == nil { keyMap := KeyMapFor(mod.keyLayout)
if keyMap == nil {
return errNoKeyMap(mod.keyLayout), nil, nil return errNoKeyMap(mod.keyLayout), nil, nil
} }
str := "hello world from bettercap ^_^" // parse the script into a list of Command objects
cmds := make([]*Command, 0) cmds, err := mod.parser.Parse(keyMap, mod.scriptPath)
for _, c := range str { if err != nil {
if m, found := keyLayout[string(c)]; found { return err, nil, nil
cmds = append(cmds, &Command{
HID: m.HID,
Mode: m.Mode,
})
} else {
return fmt.Errorf("could not find HID command for '%c'", c), nil, nil
}
} }
mod.Info("%s loaded ...", mod.scriptPath)
// build the protocol specific frames to send
if err := builder.BuildFrames(cmds); err != nil { if err := builder.BuildFrames(cmds); err != nil {
return err, nil, nil return err, nil, nil
} }

View file

@ -1,6 +1,7 @@
package hid_recon package hid
import ( import (
"fmt"
"sync" "sync"
"time" "time"
@ -29,12 +30,14 @@ type HIDRecon struct {
inPromMode bool inPromMode bool
inInjectMode bool inInjectMode bool
keyLayout string keyLayout string
scriptPath string
parser DuckyParser
selector *utils.ViewSelector selector *utils.ViewSelector
} }
func NewHIDRecon(s *session.Session) *HIDRecon { func NewHIDRecon(s *session.Session) *HIDRecon {
mod := &HIDRecon{ mod := &HIDRecon{
SessionModule: session.NewSessionModule("hid.recon", s), SessionModule: session.NewSessionModule("hid", s),
waitGroup: &sync.WaitGroup{}, waitGroup: &sync.WaitGroup{},
sniffLock: &sync.Mutex{}, sniffLock: &sync.Mutex{},
hopPeriod: 100 * time.Millisecond, hopPeriod: 100 * time.Millisecond,
@ -51,20 +54,27 @@ func NewHIDRecon(s *session.Session) *HIDRecon {
inInjectMode: false, inInjectMode: false,
pingPayload: []byte{0x0f, 0x0f, 0x0f, 0x0f}, pingPayload: []byte{0x0f, 0x0f, 0x0f, 0x0f},
keyLayout: "US", keyLayout: "US",
scriptPath: "",
} }
mod.AddHandler(session.NewModuleHandler("hid.recon on", "", mod.AddHandler(session.NewModuleHandler("hid.recon on", "",
"Start HID recon.", "Start scanning for HID devices on the 2.4Ghz spectrum.",
func(args []string) error { func(args []string) error {
return mod.Start() return mod.Start()
})) }))
mod.AddHandler(session.NewModuleHandler("hid.recon off", "", mod.AddHandler(session.NewModuleHandler("hid.recon off", "",
"Stop HID recon.", "Stop scanning for HID devices on the 2.4Ghz spectrum.",
func(args []string) error { func(args []string) error {
return mod.Stop() return mod.Stop()
})) }))
mod.AddParam(session.NewBoolParameter("hid.lna",
"true",
"If true, enable the LNA power amplifier for CrazyRadio devices."))
/*
pretty useless until i don't implement the microsoft specific keylogger
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)$`, 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)$`,
"TODO TODO", "TODO TODO",
func(args []string) error { func(args []string) error {
@ -74,47 +84,58 @@ func NewHIDRecon(s *session.Session) *HIDRecon {
sniff.Complete("hid.sniff", s.HIDCompleter) sniff.Complete("hid.sniff", s.HIDCompleter)
mod.AddHandler(sniff) mod.AddHandler(sniff)
*/
mod.AddHandler(session.NewModuleHandler("hid.show", "", mod.AddHandler(session.NewModuleHandler("hid.show", "",
"TODO TODO", "Show a list of detected HID devices on the 2.4Ghz spectrum.",
func(args []string) error { func(args []string) error {
return mod.Show() return mod.Show()
})) }))
inject := session.NewModuleHandler("hid.inject ADDRESS", `(?i)^hid\.inject ([a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2})$`, 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+(.+)$`,
"TODO TODO", "Parse the duckyscript FILENAME and inject it as HID frames spoofing the device ADDRESS, using the LAYOUT keyboard mapping.",
func(args []string) error { func(args []string) error {
return mod.setInjectionMode(args[0]) if err := mod.setInjectionMode(args[0]); err != nil {
return err
}
mod.keyLayout = args[1]
mod.scriptPath = args[2]
return nil
}) })
inject.Complete("hid.inject", s.HIDCompleter) inject.Complete("hid.inject", s.HIDCompleter)
mod.AddHandler(inject) mod.AddHandler(inject)
mod.parser = DuckyParser{mod}
mod.selector = utils.ViewSelectorFor(&mod.SessionModule, "hid.show", []string{"mac", "seen"}, "mac desc") mod.selector = utils.ViewSelectorFor(&mod.SessionModule, "hid.show", []string{"mac", "seen"}, "mac desc")
return mod return mod
} }
func (mod HIDRecon) Name() string { func (mod HIDRecon) Name() string {
return "hid.recon" return "hid"
} }
func (mod HIDRecon) Description() string { func (mod HIDRecon) Description() string {
return "TODO TODO" 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."
} }
func (mod HIDRecon) Author() string { func (mod HIDRecon) Author() string {
return "Simone Margaritelli <evilsocket@gmail.com>" 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."
} }
func (mod *HIDRecon) Configure() error { func (mod *HIDRecon) Configure() error {
var err error var err error
if mod.dongle, err = nrf24.Open(); err != nil { if err, mod.useLNA = mod.BoolParam("hid.lna"); err != nil {
return err return err
} }
if mod.dongle, err = nrf24.Open(); err != nil {
return fmt.Errorf("make sure that a nRF24LU1+ based USB dongle is connected and running the rfstorm firmware: %s", err)
}
mod.Debug("using device %s", mod.dongle.String()) mod.Debug("using device %s", mod.dongle.String())
if mod.useLNA { if mod.useLNA {

View file

@ -1,4 +1,4 @@
package hid_recon package hid
import ( import (
"fmt" "fmt"

View file

@ -1,4 +1,4 @@
package hid_recon package hid
import ( import (
"github.com/bettercap/bettercap/network" "github.com/bettercap/bettercap/network"

View file

@ -1,4 +1,4 @@
package hid_recon package hid
import ( import (
"time" "time"

View file

@ -1,4 +1,4 @@
package hid_recon package hid
import ( import (
"sort" "sort"

View file

@ -10,7 +10,7 @@ import (
"github.com/bettercap/bettercap/modules/dns_spoof" "github.com/bettercap/bettercap/modules/dns_spoof"
"github.com/bettercap/bettercap/modules/events_stream" "github.com/bettercap/bettercap/modules/events_stream"
"github.com/bettercap/bettercap/modules/gps" "github.com/bettercap/bettercap/modules/gps"
"github.com/bettercap/bettercap/modules/hid_recon" "github.com/bettercap/bettercap/modules/hid"
"github.com/bettercap/bettercap/modules/http_proxy" "github.com/bettercap/bettercap/modules/http_proxy"
"github.com/bettercap/bettercap/modules/http_server" "github.com/bettercap/bettercap/modules/http_server"
"github.com/bettercap/bettercap/modules/https_proxy" "github.com/bettercap/bettercap/modules/https_proxy"
@ -57,5 +57,5 @@ func LoadModules(sess *session.Session) {
sess.Register(update.NewUpdateModule(sess)) sess.Register(update.NewUpdateModule(sess))
sess.Register(wifi.NewWiFiModule(sess)) sess.Register(wifi.NewWiFiModule(sess))
sess.Register(wol.NewWOL(sess)) sess.Register(wol.NewWOL(sess))
sess.Register(hid_recon.NewHIDRecon(sess)) sess.Register(hid.NewHIDRecon(sess))
} }