new: parsing BLE privacy flag

This commit is contained in:
evilsocket 2019-02-22 14:06:54 +01:00
parent 5da615f968
commit 774413fad7
No known key found for this signature in database
GPG key ID: 1564D7F30393A456

View file

@ -205,19 +205,36 @@ func parseProperties(ch *gatt.Characteristic) (props []string, isReadable bool,
return return
} }
func parseRawData(raw []byte) string { func isMostlyPrintable(raw []byte) bool {
s := "" if raw == nil {
return false
}
tot := len(raw)
if tot == 0 {
return false
}
pr := 0
for _, b := range raw { for _, b := range raw {
if b != 00 && !strconv.IsPrint(rune(b)) { if strconv.IsPrint(rune(b)) {
return fmt.Sprintf("%x", raw) pr++
} else if b == 0 {
break
} else {
s += fmt.Sprintf("%c", b)
} }
} }
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 // 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) { func (mod *BLERecon) showServices(p gatt.Peripheral, services []*gatt.Service) {
columns := []string{"Handles", "Service > Characteristics", "Properties", "Data"} columns := []string{"Handles", "Service > Characteristics", "Properties", "Data"}
rows := make([][]string, 0) 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) 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 := p.WriteCharacteristic(ch, mod.writeData, !withResponse); err != nil {
if err != nil {
mod.Error("error while writing: %s", err) mod.Error("error while writing: %s", err)
} }
} }
data := "" data := ""
raw := ([]byte)(nil) raw := ([]byte)(nil)
multi := ([]string)(nil)
sz := 0
err := error(nil) err := error(nil)
if isReadable { if isReadable {
if raw, err = p.ReadCharacteristic(ch); err != nil { raw, err = p.ReadCharacteristic(ch)
data = tui.Red(err.Error())
} else {
data = parseRawData(raw)
}
}
sz := 0
if raw != nil { if raw != nil {
sz = len(raw) 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) data = parseAppearance(raw)
} else if ch.Name() == "PnP ID" && sz >= 7 { } else if ch.Name() == "PnP ID" && sz >= 7 {
multi = parsePNPID(raw) multi = parsePNPID(raw)
} else if ch.Name() == "Peripheral Preferred Connection Parameters" && sz >= 8 { } else if ch.Name() == "Peripheral Preferred Connection Parameters" && sz >= 8 {
multi = parseConnectionParams(raw) multi = parseConnectionParams(raw)
} else if ch.Name() == "Peripheral Privacy Flag" && sz >= 1 {
data = parsePrivacyFlag(raw)
} else {
data = parseRawData(raw)
} }
if multi == nil { if multi == nil {