From 774413fad771e609c48dbda70a48b326ef3c537d Mon Sep 17 00:00:00 2001 From: evilsocket Date: Fri, 22 Feb 2019 14:06:54 +0100 Subject: [PATCH] new: parsing BLE privacy flag --- modules/ble/ble_show_services.go | 69 ++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 22 deletions(-) diff --git a/modules/ble/ble_show_services.go b/modules/ble/ble_show_services.go index e2ce8886..e6ef706e 100644 --- a/modules/ble/ble_show_services.go +++ b/modules/ble/ble_show_services.go @@ -205,19 +205,36 @@ func parseProperties(ch *gatt.Characteristic) (props []string, isReadable bool, return } -func parseRawData(raw []byte) string { - s := "" +func isMostlyPrintable(raw []byte) bool { + if raw == nil { + return false + } + + tot := len(raw) + if tot == 0 { + return false + } + + pr := 0 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) + if strconv.IsPrint(rune(b)) { + pr++ } } - return tui.Yellow(s) + return (float32(pr) / float32(tot)) >= 0.5 +} + +func parseRawData(raw []byte) string { + s := "" + for _, b := range raw { + if strconv.IsPrint(rune(b)) { + s += tui.Yellow(string(b)) + } else { + s += tui.Dim(fmt.Sprintf("%x", b)) + } + } + return s } // org.bluetooth.characteristic.gap.appearance @@ -264,6 +281,14 @@ func parseConnectionParams(raw []byte) []string { } } +// org.bluetooth.characteristic.gap.peripheral_privacy_flag +func parsePrivacyFlag(raw []byte) string { + if raw[0] == 0x0 { + return tui.Green("Privacy Diabled") + } + return tui.Red("Privacy Enabled") +} + func (mod *BLERecon) showServices(p gatt.Peripheral, services []*gatt.Service) { columns := []string{"Handles", "Service > Characteristics", "Properties", "Data"} rows := make([][]string, 0) @@ -316,35 +341,35 @@ func (mod *BLERecon) showServices(p gatt.Peripheral, services []*gatt.Service) { 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 { + if err := p.WriteCharacteristic(ch, mod.writeData, !withResponse); err != nil { mod.Error("error while writing: %s", err) } } data := "" raw := ([]byte)(nil) + multi := ([]string)(nil) + sz := 0 err := error(nil) if isReadable { - if raw, err = p.ReadCharacteristic(ch); err != nil { - data = tui.Red(err.Error()) - } else { - data = parseRawData(raw) + raw, err = p.ReadCharacteristic(ch) + if raw != nil { + sz = len(raw) } } - sz := 0 - if raw != nil { - sz = len(raw) - } - multi := ([]string)(nil) - - if ch.Name() == "Appearance" && sz >= 2 { + if err != nil { + data = tui.Red(err.Error()) + } else if ch.Name() == "Appearance" && sz >= 2 { data = parseAppearance(raw) } else if ch.Name() == "PnP ID" && sz >= 7 { multi = parsePNPID(raw) } else if ch.Name() == "Peripheral Preferred Connection Parameters" && sz >= 8 { multi = parseConnectionParams(raw) + } else if ch.Name() == "Peripheral Privacy Flag" && sz >= 1 { + data = parsePrivacyFlag(raw) + } else { + data = parseRawData(raw) } if multi == nil {