mirror of
https://github.com/bettercap/bettercap
synced 2025-08-14 02:36:57 -07:00
misc: small fix or general refactoring i did not bother commenting
This commit is contained in:
parent
94ade23da3
commit
44e5803a43
3 changed files with 250 additions and 232 deletions
184
modules/ble_recon_view.go
Normal file
184
modules/ble_recon_view.go
Normal file
|
@ -0,0 +1,184 @@
|
|||
// +build !windows
|
||||
|
||||
package modules
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/bettercap/bettercap/core"
|
||||
"github.com/bettercap/bettercap/log"
|
||||
"github.com/bettercap/bettercap/network"
|
||||
|
||||
"github.com/currantlabs/gatt"
|
||||
)
|
||||
|
||||
var (
|
||||
bleAliveInterval = time.Duration(5) * time.Second
|
||||
blePresentInterval = time.Duration(30) * time.Second
|
||||
)
|
||||
|
||||
func (d *BLERecon) getRow(dev *network.BLEDevice) []string {
|
||||
address := network.NormalizeMac(dev.Device.ID())
|
||||
vendor := dev.Vendor
|
||||
sinceSeen := time.Since(dev.LastSeen)
|
||||
lastSeen := dev.LastSeen.Format("15:04:05")
|
||||
|
||||
if sinceSeen <= bleAliveInterval {
|
||||
lastSeen = core.Bold(lastSeen)
|
||||
} else if sinceSeen > blePresentInterval {
|
||||
lastSeen = core.Dim(lastSeen)
|
||||
address = core.Dim(address)
|
||||
}
|
||||
|
||||
isConnectable := core.Red("no")
|
||||
if dev.Advertisement.Connectable == true {
|
||||
isConnectable = core.Green("yes")
|
||||
}
|
||||
|
||||
return []string{
|
||||
fmt.Sprintf("%d dBm", dev.RSSI),
|
||||
address,
|
||||
dev.Device.Name(),
|
||||
vendor,
|
||||
isConnectable,
|
||||
lastSeen,
|
||||
}
|
||||
}
|
||||
|
||||
func (d *BLERecon) Show() error {
|
||||
devices := d.Session.BLE.Devices()
|
||||
|
||||
sort.Sort(ByBLERSSISorter(devices))
|
||||
|
||||
rows := make([][]string, 0)
|
||||
for _, dev := range devices {
|
||||
rows = append(rows, d.getRow(dev))
|
||||
}
|
||||
nrows := len(rows)
|
||||
|
||||
columns := []string{"RSSI", "Address", "Name", "Vendor", "Connectable", "Last Seen"}
|
||||
|
||||
if nrows > 0 {
|
||||
core.AsTable(os.Stdout, columns, rows)
|
||||
}
|
||||
|
||||
d.Session.Refresh()
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseProperties(ch *gatt.Characteristic) (props []string, isReadable bool) {
|
||||
isReadable = false
|
||||
props = make([]string, 0)
|
||||
mask := ch.Properties()
|
||||
|
||||
if (mask & gatt.CharBroadcast) != 0 {
|
||||
props = append(props, "bcast")
|
||||
}
|
||||
if (mask & gatt.CharRead) != 0 {
|
||||
isReadable = true
|
||||
props = append(props, "read")
|
||||
}
|
||||
if (mask&gatt.CharWriteNR) != 0 || (mask&gatt.CharWrite) != 0 {
|
||||
props = append(props, core.Bold("write"))
|
||||
}
|
||||
if (mask & gatt.CharNotify) != 0 {
|
||||
props = append(props, "notify")
|
||||
}
|
||||
if (mask & gatt.CharIndicate) != 0 {
|
||||
props = append(props, "indicate")
|
||||
}
|
||||
if (mask & gatt.CharSignedWrite) != 0 {
|
||||
props = append(props, core.Yellow("*write"))
|
||||
}
|
||||
if (mask & gatt.CharExtended) != 0 {
|
||||
props = append(props, "x")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func parseRawData(raw []byte) string {
|
||||
s := ""
|
||||
for _, b := range raw {
|
||||
if b != 00 && strconv.IsPrint(rune(b)) == false {
|
||||
return fmt.Sprintf("%x", raw)
|
||||
} else if b == 0 {
|
||||
break
|
||||
} else {
|
||||
s += fmt.Sprintf("%c", b)
|
||||
}
|
||||
}
|
||||
|
||||
return core.Yellow(s)
|
||||
}
|
||||
|
||||
func (d *BLERecon) showServices(p gatt.Peripheral, services []*gatt.Service) {
|
||||
columns := []string{"Handles", "Service > Characteristics", "Properties", "Data"}
|
||||
rows := make([][]string, 0)
|
||||
|
||||
for _, svc := range services {
|
||||
d.Session.Events.Add("ble.device.service.discovered", svc)
|
||||
|
||||
name := svc.Name()
|
||||
if name == "" {
|
||||
name = svc.UUID().String()
|
||||
} else {
|
||||
name = fmt.Sprintf("%s (%s)", core.Green(name), core.Dim(svc.UUID().String()))
|
||||
}
|
||||
|
||||
row := []string{
|
||||
fmt.Sprintf("%04x -> %04x", svc.Handle(), svc.EndHandle()),
|
||||
name,
|
||||
"",
|
||||
"",
|
||||
}
|
||||
|
||||
rows = append(rows, row)
|
||||
|
||||
chars, err := p.DiscoverCharacteristics(nil, svc)
|
||||
if err != nil {
|
||||
log.Error("Error while enumerating chars for service %s: %s", svc.UUID(), err)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, ch := range chars {
|
||||
d.Session.Events.Add("ble.device.characteristic.discovered", ch)
|
||||
|
||||
name = ch.Name()
|
||||
if name == "" {
|
||||
name = " " + ch.UUID().String()
|
||||
} else {
|
||||
name = fmt.Sprintf(" %s (%s)", core.Green(name), core.Dim(ch.UUID().String()))
|
||||
}
|
||||
|
||||
props, isReadable := parseProperties(ch)
|
||||
|
||||
data := ""
|
||||
if isReadable {
|
||||
raw, err := p.ReadCharacteristic(ch)
|
||||
if err != nil {
|
||||
data = core.Red(err.Error())
|
||||
} else {
|
||||
data = parseRawData(raw)
|
||||
}
|
||||
}
|
||||
|
||||
row := []string{
|
||||
fmt.Sprintf("%04x", ch.Handle()),
|
||||
name,
|
||||
strings.Join(props, ", "),
|
||||
data,
|
||||
}
|
||||
|
||||
rows = append(rows, row)
|
||||
}
|
||||
}
|
||||
|
||||
core.AsTable(os.Stdout, columns, rows)
|
||||
d.Session.Refresh()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue