new: ble.write implemented

This commit is contained in:
evilsocket 2018-02-27 03:07:59 +01:00
parent 608c0d7349
commit b4cd5f2a6d
2 changed files with 64 additions and 5 deletions

View file

@ -3,6 +3,7 @@
package modules
import (
"encoding/hex"
"fmt"
"io/ioutil"
golog "log"
@ -15,10 +16,16 @@ import (
"github.com/currantlabs/gatt"
)
const (
macRegexp = "([a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2})"
)
type BLERecon struct {
session.SessionModule
gattDevice gatt.Device
currDevice *network.BLEDevice
writeUUID *gatt.UUID
writeData []byte
connected bool
connTimeout time.Duration
quit chan bool
@ -54,16 +61,35 @@ func NewBLERecon(s *session.Session) *BLERecon {
return d.Show()
}))
d.AddHandler(session.NewModuleHandler("ble.enum MAC", "ble.enum ([a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2})",
d.AddHandler(session.NewModuleHandler("ble.enum MAC", "ble.enum "+macRegexp,
"Enumerate services and characteristics for the given BLE device.",
func(args []string) error {
if d.isEnumerating() == true {
return fmt.Errorf("An enumeration for %s is already running, please wait.", d.currDevice.Device.ID())
}
d.writeData = nil
d.writeUUID = nil
return d.enumAllTheThings(network.NormalizeMac(args[0]))
}))
d.AddHandler(session.NewModuleHandler("ble.write MAC UUID HEX_DATA", "ble.write "+macRegexp+" ([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])
uuid, err := gatt.ParseUUID(args[1])
if err != nil {
return fmt.Errorf("Error parsing %s: %s", args[1], err)
}
data, err := hex.DecodeString(args[2])
if err != nil {
return fmt.Errorf("Error parsing %s: %s", args[2], err)
}
return d.writeBuffer(mac, uuid, data)
}))
return d
}
@ -139,6 +165,12 @@ func (d *BLERecon) Start() error {
})
}
func (d *BLERecon) writeBuffer(mac string, uuid gatt.UUID, data []byte) error {
d.writeUUID = &uuid
d.writeData = data
return d.enumAllTheThings(mac)
}
func (d *BLERecon) enumAllTheThings(mac string) error {
dev, found := d.Session.BLE.Get(mac)
if found == false || dev == nil {

View file

@ -71,8 +71,10 @@ func (d *BLERecon) Show() error {
return nil
}
func parseProperties(ch *gatt.Characteristic) (props []string, isReadable bool) {
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()
@ -85,6 +87,8 @@ func parseProperties(ch *gatt.Characteristic) (props []string, isReadable bool)
}
if (mask&gatt.CharWriteNR) != 0 || (mask&gatt.CharWrite) != 0 {
props = append(props, core.Bold("write"))
isWritable = true
withResponse = (mask & gatt.CharWriteNR) == 0
}
if (mask & gatt.CharNotify) != 0 {
props = append(props, "notify")
@ -94,6 +98,8 @@ func parseProperties(ch *gatt.Characteristic) (props []string, isReadable bool)
}
if (mask & gatt.CharSignedWrite) != 0 {
props = append(props, core.Yellow("*write"))
isWritable = true
withResponse = true
}
if (mask & gatt.CharExtended) != 0 {
props = append(props, "x")
@ -121,6 +127,9 @@ func (d *BLERecon) showServices(p gatt.Peripheral, services []*gatt.Service) {
columns := []string{"Handles", "Service > Characteristics", "Properties", "Data"}
rows := make([][]string, 0)
wantsToWrite := d.writeUUID != nil
foundToWrite := false
for _, svc := range services {
d.Session.Events.Add("ble.device.service.discovered", svc)
@ -156,7 +165,21 @@ func (d *BLERecon) showServices(p gatt.Peripheral, services []*gatt.Service) {
name = fmt.Sprintf(" %s (%s)", core.Green(name), core.Dim(ch.UUID().String()))
}
props, isReadable := parseProperties(ch)
props, isReadable, isWritable, withResponse := parseProperties(ch)
if wantsToWrite && d.writeUUID.Equal(ch.UUID()) == true {
foundToWrite = true
if isWritable {
log.Info("Writing %d bytes to characteristics %s ...", len(d.writeData), d.writeUUID)
} else {
log.Warning("Attempt to write %d bytes to non writable characteristics %s ...", len(d.writeData), d.writeUUID)
}
err := p.WriteCharacteristic(ch, d.writeData, !withResponse)
if err != nil {
log.Error("Error while writing: %s", err)
}
}
data := ""
if isReadable {
@ -179,6 +202,10 @@ func (d *BLERecon) showServices(p gatt.Peripheral, services []*gatt.Service) {
}
}
core.AsTable(os.Stdout, columns, rows)
d.Session.Refresh()
if wantsToWrite && foundToWrite == false {
log.Error("Writable characteristics %s not found.", d.writeUUID)
} else {
core.AsTable(os.Stdout, columns, rows)
d.Session.Refresh()
}
}