Debian Codegraph @

modules/ble/ble_show_services.go

raw symbols SCIP
1 // +build !windows
2
3 package ble
4
5 import (
6 "encoding/binary"
7 "fmt"
8 "strconv"
9 "strings"
10
11 "github.com/bettercap/bettercap/network"
12 "github.com/bettercap/gatt"
13
14 "github.com/evilsocket/islazy/tui"
15 )
16
17 var appearances = map[uint16]string{
18 0: "Unknown",
19 64: "Generic Phone",
20 128: "Generic Computer",
21 192: "Generic Watch",
22 193: "Watch: Sports Watch",
23 256: "Generic Clock",
24 320: "Generic Display",
25 384: "Generic Remote Control",
26 448: "Generic Eye-glasses",
27 512: "Generic Tag",
28 576: "Generic Keyring",
29 640: "Generic Media Player",
30 704: "Generic Barcode Scanner",
31 768: "Generic Thermometer",
32 769: "Thermometer: Ear",
33 832: "Generic Heart rate Sensor",
34 833: "Heart Rate Sensor: Heart Rate Belt",
35 896: "Generic Blood Pressure",
36 897: "Blood Pressure: Arm",
37 898: "Blood Pressure: Wrist",
38 960: "Human Interface Device (HID)",
39 961: "Keyboard",
40 962: "Mouse",
41 963: "Joystick",
42 964: "Gamepad",
43 965: "Digitizer Tablet",
44 966: "Card Reader",
45 967: "Digital Pen",
46 968: "Barcode Scanner",
47 1024: "Generic Glucose Meter",
48 1088: "Generic: Running Walking Sensor",
49 1089: "Running Walking Sensor: In-Shoe",
50 1090: "Running Walking Sensor: On-Shoe",
51 1091: "Running Walking Sensor: On-Hip",
52 1152: "Generic: Cycling",
53 1153: "Cycling: Cycling Computer",
54 1154: "Cycling: Speed Sensor",
55 1155: "Cycling: Cadence Sensor",
56 1156: "Cycling: Power Sensor",
57 1157: "Cycling: Speed and Cadence Sensor",
58 1216: "Generic Control Device",
59 1217: "Switch",
60 1218: "Multi-switch",
61 1219: "Button",
62 1220: "Slider",
63 1221: "Rotary",
64 1222: "Touch-panel",
65 1280: "Generic Network Device",
66 1281: "Access Point",
67 1344: "Generic Sensor",
68 1345: "Motion Sensor",
69 1346: "Air Quality Sensor",
70 1347: "Temperature Sensor",
71 1348: "Humidity Sensor",
72 1349: "Leak Sensor",
73 1350: "Smoke Sensor",
74 1351: "Occupancy Sensor",
75 1352: "Contact Sensor",
76 1353: "Carbon Monoxide Sensor",
77 1354: "Carbon Dioxide Sensor",
78 1355: "Ambient Light Sensor",
79 1356: "Energy Sensor",
80 1357: "Color Light Sensor",
81 1358: "Rain Sensor",
82 1359: "Fire Sensor",
83 1360: "Wind Sensor",
84 1361: "Proximity Sensor",
85 1362: "Multi-Sensor",
86 1408: "Generic Light Fixtures",
87 1409: "Wall Light",
88 1410: "Ceiling Light",
89 1411: "Floor Light",
90 1412: "Cabinet Light",
91 1413: "Desk Light",
92 1414: "Troffer Light",
93 1415: "Pendant Light",
94 1416: "In-ground Light",
95 1417: "Flood Light",
96 1418: "Underwater Light",
97 1419: "Bollard with Light",
98 1420: "Pathway Light",
99 1421: "Garden Light",
100 1422: "Pole-top Light",
101 1423: "Spotlight",
102 1424: "Linear Light",
103 1425: "Street Light",
104 1426: "Shelves Light",
105 1427: "High-bay / Low-bay Light",
106 1428: "Emergency Exit Light",
107 1472: "Generic Fan",
108 1473: "Ceiling Fan",
109 1474: "Axial Fan",
110 1475: "Exhaust Fan",
111 1476: "Pedestal Fan",
112 1477: "Desk Fan",
113 1478: "Wall Fan",
114 1536: "Generic HVAC",
115 1537: "Thermostat",
116 1600: "Generic Air Conditioning",
117 1664: "Generic Humidifier",
118 1728: "Generic Heating",
119 1729: "Radiator",
120 1730: "Boiler",
121 1731: "Heat Pump",
122 1732: "Infrared Heater",
123 1733: "Radiant Panel Heater",
124 1734: "Fan Heater",
125 1735: "Air Curtain",
126 1792: "Generic Access Control",
127 1793: "Access Door",
128 1794: "Garage Door",
129 1795: "Emergency Exit Door",
130 1796: "Access Lock",
131 1797: "Elevator",
132 1798: "Window",
133 1799: "Entrance Gate",
134 1856: "Generic Motorized Device",
135 1857: "Motorized Gate",
136 1858: "Awning",
137 1859: "Blinds or Shades",
138 1860: "Curtains",
139 1861: "Screen",
140 1920: "Generic Power Device",
141 1921: "Power Outlet",
142 1922: "Power Strip",
143 1923: "Plug",
144 1924: "Power Supply",
145 1925: "LED Driver",
146 1926: "Fluorescent Lamp Gear",
147 1927: "HID Lamp Gear",
148 1984: "Generic Light Source",
149 1985: "Incandescent Light Bulb",
150 1986: "LED Bulb",
151 1987: "HID Lamp",
152 1988: "Fluorescent Lamp",
153 1989: "LED Array",
154 1990: "Multi-Color LED Array",
155 3136: "Generic: Pulse Oximeter",
156 3137: "Fingertip",
157 3138: "Wrist Worn",
158 3200: "Generic: Weight Scale",
159 3264: "Generic",
160 3265: "Powered Wheelchair",
161 3266: "Mobility Scooter",
162 3328: "Generic",
163 5184: "Generic: Outdoor Sports Activity",
164 5185: "Location Display Device",
165 5186: "Location and Navigation Display Device",
166 5187: "Location Pod",
167 5188: "Location and Navigation Pod",
168 }
169
170 func parseProperties(ch *gatt.Characteristic) (props []string, isReadable bool, isWritable bool, withResponse bool) {
171 isReadable = false
172 isWritable = false
173 withResponse = false
174 props = make([]string, 0)
175 mask := ch.Properties()
176
177 if (mask & gatt.CharBroadcast) != 0 {
178 props = append(props, "BCAST")
179 }
180 if (mask & gatt.CharRead) != 0 {
181 isReadable = true
182 props = append(props, "READ")
183 }
184 if (mask&gatt.CharWriteNR) != 0 || (mask&gatt.CharWrite) != 0 {
185 props = append(props, tui.Bold("WRITE"))
186 isWritable = true
187 withResponse = (mask & gatt.CharWriteNR) == 0
188 }
189 if (mask & gatt.CharNotify) != 0 {
190 props = append(props, "NOTIFY")
191 }
192 if (mask & gatt.CharIndicate) != 0 {
193 props = append(props, "INDICATE")
194 }
195 if (mask & gatt.CharSignedWrite) != 0 {
196 props = append(props, tui.Yellow("SIGN WRITE"))
197 isWritable = true
198 withResponse = true
199 }
200 if (mask & gatt.CharExtended) != 0 {
201 props = append(props, "X")
202 }
203
204 return
205 }
206
207 func parseRawData(raw []byte) string {
208 s := ""
209 for _, b := range raw {
210 if strconv.IsPrint(rune(b)) {
211 s += tui.Yellow(string(b))
212 } else {
213 s += tui.Dim(fmt.Sprintf("%02x", b))
214 }
215 }
216 return s
217 }
218
219 // org.bluetooth.characteristic.gap.appearance
220 func parseAppearance(raw []byte) string {
221 app := binary.LittleEndian.Uint16(raw[0:2])
222 if appName, found := appearances[app]; found {
223 return tui.Green(appName)
224 }
225 return fmt.Sprintf("0x%x", app)
226 }
227
228 // org.bluetooth.characteristic.pnp_id
229 func parsePNPID(raw []byte) []string {
230 vendorIdSrc := byte(raw[0])
231 vendorId := binary.LittleEndian.Uint16(raw[1:3])
232 prodId := binary.LittleEndian.Uint16(raw[3:5])
233 prodVer := binary.LittleEndian.Uint16(raw[5:7])
234
235 src := ""
236 if vendorIdSrc == 1 {
237 src = " (Bluetooth SIG assigned Company Identifier)"
238 } else if vendorIdSrc == 2 {
239 src = " (USB Implementer’s Forum assigned Vendor ID value)"
240 }
241
242 return []string{
243 tui.Green("Vendor ID") + fmt.Sprintf(": 0x%04x%s", vendorId, tui.Dim(src)),
244 tui.Green("Product ID") + fmt.Sprintf(": 0x%04x", prodId),
245 tui.Green("Product Version") + fmt.Sprintf(": 0x%04x", prodVer),
246 }
247 }
248
249 // org.bluetooth.characteristic.gap.peripheral_preferred_connection_parameters
250 func parseConnectionParams(raw []byte) []string {
251 minConInt := binary.LittleEndian.Uint16(raw[0:2])
252 maxConInt := binary.LittleEndian.Uint16(raw[2:4])
253 slaveLat := binary.LittleEndian.Uint16(raw[4:6])
254 conTimeMul := binary.LittleEndian.Uint16(raw[6:8])
255
256 return []string{
257 tui.Green("Connection Interval") + fmt.Sprintf(": %d -> %d", minConInt, maxConInt),
258 tui.Green("Slave Latency") + fmt.Sprintf(": %d", slaveLat),
259 tui.Green("Connection Supervision Timeout Multiplier") + fmt.Sprintf(": %d", conTimeMul),
260 }
261 }
262
263 // org.bluetooth.characteristic.gap.peripheral_privacy_flag
264 func parsePrivacyFlag(raw []byte) string {
265 if raw[0] == 0x0 {
266 return tui.Green("Privacy Disabled")
267 }
268 return tui.Red("Privacy Enabled")
269 }
270
271 func (mod *BLERecon) showServices(p gatt.Peripheral, services []*gatt.Service) {
272 columns := []string{"Handles", "Service > Characteristics", "Properties", "Data"}
273 rows := make([][]string, 0)
274
275 wantsToWrite := mod.writeUUID != nil
276 foundToWrite := false
277
278 mod.currDevice.Services = make([]network.BLEService, 0)
279
280 for _, svc := range services {
281 service := network.BLEService{
282 UUID: svc.UUID().String(),
283 Name: svc.Name(),
284 Handle: svc.Handle(),
285 EndHandle: svc.EndHandle(),
286 Characteristics: make([]network.BLECharacteristic, 0),
287 }
288
289 mod.Session.Events.Add("ble.device.service.discovered", svc)
290
291 name := svc.Name()
292 if name == "" {
293 name = svc.UUID().String()
294 } else {
295 name = fmt.Sprintf("%s (%s)", tui.Green(name), tui.Dim(svc.UUID().String()))
296 }
297
298 row := []string{
299 fmt.Sprintf("%04x -> %04x", svc.Handle(), svc.EndHandle()),
300 name,
301 "",
302 "",
303 }
304
305 rows = append(rows, row)
306
307 chars, err := p.DiscoverCharacteristics(nil, svc)
308 if err != nil {
309 mod.Error("error while enumerating chars for service %s: %s", svc.UUID(), err)
310 } else {
311 for _, ch := range chars {
312 props, isReadable, isWritable, withResponse := parseProperties(ch)
313
314 char := network.BLECharacteristic{
315 UUID: ch.UUID().String(),
316 Name: ch.Name(),
317 Handle: ch.VHandle(),
318 Properties: props,
319 }
320
321 mod.Session.Events.Add("ble.device.characteristic.discovered", ch)
322
323 name = ch.Name()
324 if name == "" {
325 name = " " + ch.UUID().String()
326 } else {
327 name = fmt.Sprintf(" %s (%s)", tui.Green(name), tui.Dim(ch.UUID().String()))
328 }
329
330 if wantsToWrite && mod.writeUUID.Equal(ch.UUID()) {
331 foundToWrite = true
332 if isWritable {
333 mod.Debug("writing %d bytes to characteristics %s ...", len(mod.writeData), mod.writeUUID)
334 } else {
335 mod.Warning("attempt to write %d bytes to non writable characteristics %s ...", len(mod.writeData), mod.writeUUID)
336 }
337
338 if err := p.WriteCharacteristic(ch, mod.writeData, !withResponse); err != nil {
339 mod.Error("error while writing: %s", err)
340 }
341 }
342
343 sz := 0
344 raw := ([]byte)(nil)
345 err := error(nil)
346 if isReadable {
347 if raw, err = p.ReadCharacteristic(ch); raw != nil {
348 sz = len(raw)
349 }
350 }
351
352 data := ""
353 multi := ([]string)(nil)
354 if err != nil {
355 data = tui.Red(err.Error())
356 } else if ch.Name() == "Appearance" && sz >= 2 {
357 data = parseAppearance(raw)
358 } else if ch.Name() == "PnP ID" && sz >= 7 {
359 multi = parsePNPID(raw)
360 } else if ch.Name() == "Peripheral Preferred Connection Parameters" && sz >= 8 {
361 multi = parseConnectionParams(raw)
362 } else if ch.Name() == "Peripheral Privacy Flag" && sz >= 1 {
363 data = parsePrivacyFlag(raw)
364 } else {
365 data = parseRawData(raw)
366 }
367
368 if ch.Name() == "Device Name" && data != "" && mod.currDevice.DeviceName == "" {
369 mod.currDevice.DeviceName = data
370 }
371
372 if multi == nil {
373 char.Data = data
374 rows = append(rows, []string{
375 fmt.Sprintf("%04x", ch.VHandle()),
376 name,
377 strings.Join(props, ", "),
378 data,
379 })
380 } else {
381 char.Data = multi
382 for i, m := range multi {
383 if i == 0 {
384 rows = append(rows, []string{
385 fmt.Sprintf("%04x", ch.VHandle()),
386 name,
387 strings.Join(props, ", "),
388 m,
389 })
390 } else {
391 rows = append(rows, []string{"", "", "", m})
392 }
393 }
394 }
395
396 service.Characteristics = append(service.Characteristics, char)
397 }
398 // blank row after every service, bleah style
399 rows = append(rows, []string{"", "", "", ""})
400 }
401
402 mod.currDevice.Services = append(mod.currDevice.Services, service)
403 }
404
405 if wantsToWrite && !foundToWrite {
406 mod.Error("writable characteristics %s not found.", mod.writeUUID)
407 } else {
408 tui.Table(mod.Session.Events.Stdout, columns, rows)
409 mod.Session.Refresh()
410 }
411 }