new: parsing BLE flags and company identifiers from advertisements

This commit is contained in:
evilsocket 2019-02-14 12:26:28 +01:00
commit f72dac0c95
No known key found for this signature in database
GPG key ID: 1564D7F30393A456
253 changed files with 37143 additions and 487 deletions

View file

@ -4,17 +4,13 @@
package ble
import (
"fmt"
"os"
"sort"
"strconv"
"strings"
"time"
"github.com/bettercap/bettercap/network"
"github.com/bettercap/gatt"
"github.com/evilsocket/islazy/ops"
"github.com/evilsocket/islazy/tui"
)
@ -24,20 +20,11 @@ var (
)
func (mod *BLERecon) getRow(dev *network.BLEDevice) []string {
// ref. https://www.metageek.com/training/resources/understanding-rssi-2.html
rssi := fmt.Sprintf("%d dBm", dev.RSSI)
if dev.RSSI >= -67 {
rssi = tui.Green(rssi)
} else if dev.RSSI >= -70 {
rssi = tui.Dim(tui.Green(rssi))
} else if dev.RSSI >= -80 {
rssi = tui.Yellow(rssi)
} else {
rssi = tui.Dim(tui.Red(rssi))
}
rssi := network.ColorRSSI(dev.RSSI)
name := ops.Ternary(dev.Device.Name() == "", dev.Advertisement.LocalName, dev.Device.Name()).(string)
address := network.NormalizeMac(dev.Device.ID())
vendor := tui.Dim(dev.Vendor)
vendor := tui.Dim(ops.Ternary(dev.Vendor == "", dev.Advertisement.Company, dev.Vendor).(string))
isConnectable := ops.Ternary(dev.Advertisement.Connectable, tui.Green("✔"), tui.Red("✖")).(string)
sinceSeen := time.Since(dev.LastSeen)
lastSeen := dev.LastSeen.Format("15:04:05")
@ -48,16 +35,12 @@ func (mod *BLERecon) getRow(dev *network.BLEDevice) []string {
address = tui.Dim(address)
}
isConnectable := tui.Red("✖")
if dev.Advertisement.Connectable {
isConnectable = tui.Green("✔")
}
return []string{
rssi,
address,
dev.Device.Name(),
name,
vendor,
dev.Advertisement.Flags.String(),
isConnectable,
lastSeen,
}
@ -117,14 +100,14 @@ func (mod *BLERecon) doSelection() (err error, devices []*network.BLEDevice) {
}
func (mod *BLERecon) colNames() []string {
colNames := []string{"RSSI", "MAC", "Name", "Vendor", "Connectable", "Seen"}
colNames := []string{"RSSI", "MAC", "Name", "Vendor", "Flags", "Connect", "Seen"}
switch mod.selector.SortField {
case "rssi":
colNames[0] += " " + mod.selector.SortSymbol
case "mac":
colNames[1] += " " + mod.selector.SortSymbol
case "seen":
colNames[5] += " " + mod.selector.SortSymbol
colNames[6] += " " + mod.selector.SortSymbol
}
return colNames
}
@ -147,142 +130,3 @@ func (mod *BLERecon) Show() error {
return nil
}
func parseProperties(ch *gatt.Characteristic) (props []string, isReadable bool, isWritable bool, withResponse bool) {
isReadable = false
isWritable = false
withResponse = 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, tui.Bold("write"))
isWritable = true
withResponse = (mask & gatt.CharWriteNR) == 0
}
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, tui.Yellow("*write"))
isWritable = true
withResponse = true
}
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)) {
return fmt.Sprintf("%x", raw)
} else if b == 0 {
break
} else {
s += fmt.Sprintf("%c", b)
}
}
return tui.Yellow(s)
}
func (mod *BLERecon) showServices(p gatt.Peripheral, services []*gatt.Service) {
columns := []string{"Handles", "Service > Characteristics", "Properties", "Data"}
rows := make([][]string, 0)
wantsToWrite := mod.writeUUID != nil
foundToWrite := false
for _, svc := range services {
mod.Session.Events.Add("ble.device.service.discovered", svc)
name := svc.Name()
if name == "" {
name = svc.UUID().String()
} else {
name = fmt.Sprintf("%s (%s)", tui.Green(name), tui.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 {
mod.Error("error while enumerating chars for service %s: %s", svc.UUID(), err)
continue
}
for _, ch := range chars {
mod.Session.Events.Add("ble.device.characteristic.discovered", ch)
name = ch.Name()
if name == "" {
name = " " + ch.UUID().String()
} else {
name = fmt.Sprintf(" %s (%s)", tui.Green(name), tui.Dim(ch.UUID().String()))
}
props, isReadable, isWritable, withResponse := parseProperties(ch)
if wantsToWrite && mod.writeUUID.Equal(ch.UUID()) {
foundToWrite = true
if isWritable {
mod.Info("writing %d bytes to characteristics %s ...", len(mod.writeData), mod.writeUUID)
} else {
mod.Warning("attempt to write %d bytes to non writable characteristics %s ...", len(mod.writeData), mod.writeUUID)
}
err := p.WriteCharacteristic(ch, mod.writeData, !withResponse)
if err != nil {
mod.Error("error while writing: %s", err)
}
}
data := ""
if isReadable {
raw, err := p.ReadCharacteristic(ch)
if err != nil {
data = tui.Red(err.Error())
} else {
data = parseRawData(raw)
}
}
row := []string{
fmt.Sprintf("%04x", ch.Handle()),
name,
strings.Join(props, ", "),
data,
}
rows = append(rows, row)
}
}
if wantsToWrite && !foundToWrite {
mod.Error("writable characteristics %s not found.", mod.writeUUID)
} else {
tui.Table(os.Stdout, columns, rows)
mod.Session.Refresh()
}
}

View file

@ -0,0 +1,154 @@
// +build !windows
// +build !darwin
package ble
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/bettercap/gatt"
"github.com/evilsocket/islazy/tui"
)
func parseProperties(ch *gatt.Characteristic) (props []string, isReadable bool, isWritable bool, withResponse bool) {
isReadable = false
isWritable = false
withResponse = 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, tui.Bold("write"))
isWritable = true
withResponse = (mask & gatt.CharWriteNR) == 0
}
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, tui.Yellow("*write"))
isWritable = true
withResponse = true
}
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)) {
return fmt.Sprintf("%x", raw)
} else if b == 0 {
break
} else {
s += fmt.Sprintf("%c", b)
}
}
return tui.Yellow(s)
}
func (mod *BLERecon) showServices(p gatt.Peripheral, services []*gatt.Service) {
columns := []string{"Handles", "Service > Characteristics", "Properties", "Data"}
rows := make([][]string, 0)
wantsToWrite := mod.writeUUID != nil
foundToWrite := false
for _, svc := range services {
mod.Session.Events.Add("ble.device.service.discovered", svc)
name := svc.Name()
if name == "" {
name = svc.UUID().String()
} else {
name = fmt.Sprintf("%s (%s)", tui.Green(name), tui.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 {
mod.Error("error while enumerating chars for service %s: %s", svc.UUID(), err)
continue
}
for _, ch := range chars {
mod.Session.Events.Add("ble.device.characteristic.discovered", ch)
name = ch.Name()
if name == "" {
name = " " + ch.UUID().String()
} else {
name = fmt.Sprintf(" %s (%s)", tui.Green(name), tui.Dim(ch.UUID().String()))
}
props, isReadable, isWritable, withResponse := parseProperties(ch)
if wantsToWrite && mod.writeUUID.Equal(ch.UUID()) {
foundToWrite = true
if isWritable {
mod.Info("writing %d bytes to characteristics %s ...", len(mod.writeData), mod.writeUUID)
} else {
mod.Warning("attempt to write %d bytes to non writable characteristics %s ...", len(mod.writeData), mod.writeUUID)
}
err := p.WriteCharacteristic(ch, mod.writeData, !withResponse)
if err != nil {
mod.Error("error while writing: %s", err)
}
}
data := ""
if isReadable {
raw, err := p.ReadCharacteristic(ch)
if err != nil {
data = tui.Red(err.Error())
} else {
data = parseRawData(raw)
}
}
row := []string{
fmt.Sprintf("%04x", ch.Handle()),
name,
strings.Join(props, ", "),
data,
}
rows = append(rows, row)
}
}
if wantsToWrite && !foundToWrite {
mod.Error("writable characteristics %s not found.", mod.writeUUID)
} else {
tui.Table(os.Stdout, columns, rows)
mod.Session.Refresh()
}
}

View file

@ -22,18 +22,7 @@ func (mod *WiFiModule) isApSelected() bool {
}
func (mod *WiFiModule) getRow(station *network.Station) ([]string, bool) {
// ref. https://www.metageek.com/training/resources/understanding-rssi-2.html
rssi := fmt.Sprintf("%d dBm", station.RSSI)
if station.RSSI >= -67 {
rssi = tui.Green(rssi)
} else if station.RSSI >= -70 {
rssi = tui.Dim(tui.Green(rssi))
} else if station.RSSI >= -80 {
rssi = tui.Yellow(rssi)
} else {
rssi = tui.Dim(tui.Red(rssi))
}
rssi := network.ColorRSSI(int(station.RSSI))
bssid := station.HwAddress
sinceStarted := time.Since(mod.Session.StartedAt)
sinceFirstSeen := time.Since(station.FirstSeen)