Skip to content

Commit

Permalink
new: ble.enum and ble.write now support autocompletion
Browse files Browse the repository at this point in the history
  • Loading branch information
evilsocket committed Feb 18, 2019
1 parent 742e7fd commit 49e2116
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
27 changes: 23 additions & 4 deletions modules/ble/ble_recon.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io/ioutil"
golog "log"
"strings"
"time"

"github.com/bettercap/bettercap/modules/utils"
Expand Down Expand Up @@ -63,7 +64,7 @@ func NewBLERecon(s *session.Session) *BLERecon {
return mod.Show()
}))

mod.AddHandler(session.NewModuleHandler("ble.enum MAC", "ble.enum "+network.BLEMacValidator,
enum := session.NewModuleHandler("ble.enum MAC", "ble.enum "+network.BLEMacValidator,
"Enumerate services and characteristics for the given BLE device.",
func(args []string) error {
if mod.isEnumerating() {
Expand All @@ -74,9 +75,13 @@ func NewBLERecon(s *session.Session) *BLERecon {
mod.writeUUID = nil

return mod.enumAllTheThings(network.NormalizeMac(args[0]))
}))
})

enum.Complete("ble.enum", mod.macCompleter)

mod.AddHandler(enum)

mod.AddHandler(session.NewModuleHandler("ble.write MAC UUID HEX_DATA", "ble.write "+network.BLEMacValidator+" ([a-fA-F0-9]+) ([a-fA-F0-9]+)",
write := session.NewModuleHandler("ble.write MAC UUID HEX_DATA", "ble.write "+network.BLEMacValidator+" ([a-fA-F0-9]+) ([a-fA-F0-9]+)",
"Write the HEX_DATA buffer to the BLE device with the specified MAC address, to the characteristics with the given UUID.",
func(args []string) error {
mac := network.NormalizeMac(args[0])
Expand All @@ -90,7 +95,11 @@ func NewBLERecon(s *session.Session) *BLERecon {
}

return mod.writeBuffer(mac, uuid, data)
}))
})

write.Complete("ble.write", mod.macCompleter)

mod.AddHandler(write)

return mod
}
Expand All @@ -107,6 +116,16 @@ func (mod BLERecon) Author() string {
return "Simone Margaritelli <[email protected]>"
}

func (mod *BLERecon) macCompleter(prefix string) []string {
macs := []string{""}
mod.Session.BLE.EachDevice(func(mac string, dev *network.BLEDevice) {
if prefix == "" || strings.HasPrefix(mac, prefix) {
macs = append(macs, mac)
}
})
return macs
}

func (mod *BLERecon) isEnumerating() bool {
return mod.currDevice != nil
}
Expand Down
9 changes: 9 additions & 0 deletions network/ble.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,12 @@ func (b *BLE) Devices() (devices []*BLEDevice) {
}
return
}

func (b *BLE) EachDevice(cb func(mac string, d *BLEDevice)) {
b.Lock()
defer b.Unlock()

for m, dev := range b.devices {
cb(m, dev)
}
}
11 changes: 11 additions & 0 deletions session/module_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"regexp"
"strconv"

"github.com/evilsocket/islazy/str"
"github.com/evilsocket/islazy/tui"

"github.com/bettercap/readline"
)

const IPv4Validator = `^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$`
Expand All @@ -16,6 +19,7 @@ type ModuleHandler struct {
Description string
Parser *regexp.Regexp
Exec func(args []string) error
Completer *readline.PrefixCompleter
}

func NewModuleHandler(name string, expr string, desc string, exec func(args []string) error) ModuleHandler {
Expand All @@ -33,6 +37,13 @@ func NewModuleHandler(name string, expr string, desc string, exec func(args []st
return h
}

func (h *ModuleHandler) Complete(name string, cb func(prefix string) []string) {
h.Completer = readline.PcItem(name, readline.PcItemDynamic(func(prefix string) []string {
prefix = str.Trim(prefix[len(name):])
return cb(prefix)
}))
}

func (h *ModuleHandler) Help(padding int) string {
return fmt.Sprintf(" "+tui.Bold("%"+strconv.Itoa(padding)+"s")+" : %s\n", h.Name, h.Description)
}
Expand Down
20 changes: 12 additions & 8 deletions session/session_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,21 @@ func (s *Session) setupReadline() (err error) {
tree := make(map[string][]string)
for _, m := range s.Modules {
for _, h := range m.Handlers() {
parts := strings.Split(h.Name, " ")
name := parts[0]
if h.Completer == nil {
parts := strings.Split(h.Name, " ")
name := parts[0]

if _, found := tree[name]; !found {
tree[name] = []string{}
}
if _, found := tree[name]; !found {
tree[name] = []string{}
}

var appendedOption = strings.Join(parts[1:], " ")
var appendedOption = strings.Join(parts[1:], " ")

if len(appendedOption) > 0 && !containsCapitals(appendedOption) {
tree[name] = append(tree[name], appendedOption)
if len(appendedOption) > 0 && !containsCapitals(appendedOption) {
tree[name] = append(tree[name], appendedOption)
}
} else {
prefixCompleters = append(prefixCompleters, h.Completer)
}
}
}
Expand Down

0 comments on commit 49e2116

Please sign in to comment.