forked from bettercap/bettercap
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ble_recon.go
209 lines (167 loc) · 4.78 KB
/
ble_recon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// +build !windows
// +build !darwin
package modules
import (
"encoding/hex"
"fmt"
"io/ioutil"
golog "log"
"time"
"github.com/bettercap/bettercap/log"
"github.com/bettercap/bettercap/network"
"github.com/bettercap/bettercap/session"
"github.com/bettercap/gatt"
)
type BLERecon struct {
session.SessionModule
gattDevice gatt.Device
currDevice *network.BLEDevice
writeUUID *gatt.UUID
writeData []byte
connected bool
connTimeout time.Duration
quit chan bool
done chan bool
}
func NewBLERecon(s *session.Session) *BLERecon {
d := &BLERecon{
SessionModule: session.NewSessionModule("ble.recon", s),
gattDevice: nil,
quit: make(chan bool),
done: make(chan bool),
connTimeout: time.Duration(10) * time.Second,
currDevice: nil,
connected: false,
}
d.AddHandler(session.NewModuleHandler("ble.recon on", "",
"Start Bluetooth Low Energy devices discovery.",
func(args []string) error {
return d.Start()
}))
d.AddHandler(session.NewModuleHandler("ble.recon off", "",
"Stop Bluetooth Low Energy devices discovery.",
func(args []string) error {
return d.Stop()
}))
d.AddHandler(session.NewModuleHandler("ble.show", "",
"Show discovered Bluetooth Low Energy devices.",
func(args []string) error {
return d.Show()
}))
d.AddHandler(session.NewModuleHandler("ble.enum MAC", "ble.enum "+network.BLEMacValidator,
"Enumerate services and characteristics for the given BLE device.",
func(args []string) error {
if d.isEnumerating() {
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 "+network.BLEMacValidator+" ([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
}
func (d BLERecon) Name() string {
return "ble.recon"
}
func (d BLERecon) Description() string {
return "Bluetooth Low Energy devices discovery."
}
func (d BLERecon) Author() string {
return "Simone Margaritelli <evilsocket@protonmail.com>"
}
func (d *BLERecon) isEnumerating() bool {
return d.currDevice != nil
}
func (d *BLERecon) Configure() (err error) {
if d.Running() {
return session.ErrAlreadyStarted
} else if d.gattDevice == nil {
log.Info("Initializing BLE device ...")
// hey Paypal GATT library, could you please just STFU?!
golog.SetOutput(ioutil.Discard)
if d.gattDevice, err = gatt.NewDevice(defaultBLEClientOptions...); err != nil {
return err
}
d.gattDevice.Handle(
gatt.PeripheralDiscovered(d.onPeriphDiscovered),
gatt.PeripheralConnected(d.onPeriphConnected),
gatt.PeripheralDisconnected(d.onPeriphDisconnected),
)
d.gattDevice.Init(d.onStateChanged)
}
return nil
}
func (d *BLERecon) Start() error {
if err := d.Configure(); err != nil {
return err
}
return d.SetRunning(true, func() {
go d.pruner()
<-d.quit
log.Info("Stopping BLE scan ...")
d.gattDevice.StopScanning()
d.done <- true
})
}
func (d *BLERecon) Stop() error {
return d.SetRunning(false, func() {
d.quit <- true
<-d.done
})
}
func (d *BLERecon) pruner() {
log.Debug("Started BLE devices pruner ...")
for d.Running() {
for _, dev := range d.Session.BLE.Devices() {
if time.Since(dev.LastSeen) > blePresentInterval {
d.Session.BLE.Remove(dev.Device.ID())
}
}
time.Sleep(5 * time.Second)
}
}
func (d *BLERecon) setCurrentDevice(dev *network.BLEDevice) {
d.connected = false
d.currDevice = dev
}
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 || dev == nil {
return fmt.Errorf("BLE device with address %s not found.", mac)
} else if d.Running() {
d.gattDevice.StopScanning()
}
d.setCurrentDevice(dev)
if err := d.Configure(); err != nil && err != session.ErrAlreadyStarted {
return err
}
log.Info("Connecting to %s ...", mac)
go func() {
time.Sleep(d.connTimeout)
if d.isEnumerating() && !d.connected {
d.Session.Events.Add("ble.connection.timeout", d.currDevice)
d.onPeriphDisconnected(nil, nil)
}
}()
d.gattDevice.Connect(dev.Device)
return nil
}