-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
740 lines (608 loc) · 17.8 KB
/
index.js
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
'use strict';
/**
* Node.js BtleJuice bindings.
**/
const io = require('socket.io-client');
const events = require('events');
const util = require('util');
/**
* HookModify
*
* Hooking related class.
**/
class HookModify {
constructor(data) {
this.data = data;
}
getData() {
return this.data;
}
}
/**
* HookForceResponse
*
* Hooking related class.
**/
class HookForceResponse {
constructor(data, error) {
this.data = data;
this.error = error;
}
getData(){
return this.data;
}
getError(){
return this.error;
}
}
/**
* UnhandledException
**/
class UnhandledException {
}
/**
* Btlejuice
*
* Provides all the features required to communicate with the BtleJuice
* MitM service.
**/
class BtleJuice {
constructor(host, port) {
/* BtleJuice is an event emitter, call its parent constructor. */
events.EventEmitter.call(this);
/* Create the webservice URL from host and port. */
this.url = util.format('http://%s:%d/', host, port);
/* Create the socket.io client from this url. */
this.client = new io(this.url);
/* Connection failed handler. */
this.client.on('connect_error', function(){
console.log('[!] Unable to connect to BtleJuice core.');
process.exit(1);
});
/* Setup handlers. */
this.installEventHandlers();
};
/**
* installEventHandlers()
*
* Setup event handlers for the current socket.io client.
**/
installEventHandlers(){
/* Call onConnected() once connected to the remote service. */
this.client.on('connect', function(){
this._onConnected();
}.bind(this));
/* Call onDisconnected() if disconnected from the remote service. */
this.client.on('disconnect', function(){
this._onDisconnected();
}.bind(this));
/* Call onStatusUpdated() when status update is received. */
this.client.on('app.status', function(status){
this._onStatusUpdated(status);
}.bind(this));
/* Call onTargetSelected() when a target has been selected. */
this.client.on('app.target', function(target){
this._onTargetSelected(target);
}.bind(this));
/* Call onClientConnected() when a BT client connects to our proxy. */
this.client.on('app.connect', function(client){
this._onClientConnected(client);
}.bind(this));
/* Call onClientLeft() when a BT client disconnects from our proxy. */
this.client.on('app.disconnect', function(client){
this._onClientLeft(client);
}.bind(this));
/* Call onDeviceFound() when a device is found during scanning. */
this.client.on('peripheral', function(peripheral, name, rssi){
this._onDeviceFound(peripheral, name, rssi);
}.bind(this));
/* Call onProxyReady() when our proxy is ready to forward. */
this.client.on('ready', function(){
this._onProxyReady();
}.bind(this));
/* Call onDataNotification() when proxy gets a notification from the remote
device */
this.client.on('data', function(service, characteristic, data){
this._onDataNotification(service, characteristic, data);
}.bind(this));
/* Call onWriteResponse() when proxy gets a response from a write operation. */
this.client.on('ble_write_resp', function(service, characteristic, error){
this._onWriteResponse(service, characteristic, error);
}.bind(this));
/* Call onReadResponse() when proxy gets a response from a read operation. */
this.client.on('ble_read_resp', function(service, characteristic, data){
this._onReadResponse(service, characteristic, data);
}.bind(this));
/* Call onNotifyResponse() when proxy gets a response from a notification. */
this.client.on('ble_notify_resp', function(service, characteristic){
this._onNotifyResponse(service, characteristic);
}.bind(this));
/* Call onProfile() when proxy gets target profile. */
this.client.on('profile', function(profile){
this._onProfile(profile);
}.bind(this));
/* Call onWriteRequest() when proxy gets a write request. */
this.client.on('proxy_write', function(service, characteristic, data, offset, withoutResponse){
this._onWriteRequest(service, characteristic, data, offset, withoutResponse);
}.bind(this));
/* Call onReadRequest() when proxy gets a read request. */
this.client.on('proxy_read', function(service, characteristic, offset){
this._onReadRequest(service, characteristic, offset);
}.bind(this));
/* Call onNotifyRequest() when proxy gets a notification request. */
this.client.on('proxy_notify', function(service, characteristic, enabled){
this._onNotifyRequest(service, characteristic, enabled);
}.bind(this));
};
/**************************************
* Available actions
**************************************/
/**
* selectTarget()
*
* Select a specific target.
**/
selectTarget(target){
this.client.emit('target', target);
}
/**
* startScan()
*
* Start BTLE scanning. Stops proxy if already active.
**/
startScan(){
this.client.emit('scan_devices');
}
/**
* status()
*
* Get BtleJuice core status.
**/
status(){
this.client.emit('status');
}
/**
* stop()
*
* Stop proxy.
**/
stop(){
this.client.emit('stop');
}
/**
* deviceWrite()
*
* Performs a write operation on the target device.
**/
deviceWrite(service, characteristic, data, offset, withoutResponse){
if (offset == null)
offset = 0;
if (withoutResponse == null)
withoutResponse = false;
this.client.emit(
'ble_write',
service,
characteristic,
data,
offset,
withoutResponse
);
}
/**
* deviceRead()
*
* Performs a read operation on the target device.
**/
deviceRead(service, characteristic){
this.client.emit('ble_read', service, characteristic);
}
/**
* deviceNotify()
*
* Register for notification for a given characteristic.
**/
deviceNotify(service, characteristic, enabled){
this.client.emit('ble_notify', service, characteristic, enabled);
}
/**
* sendWriteResponse()
*
* Send a write response to the BLE master device (client app)
**/
sendWriteResponse(service, characteristic, error){
this.client.emit('proxy_write_resp', service, characteristic, error);
}
/**
* sendReadResponse()
*
* Send a read response to the BLE master device (client app)
**/
sendReadResponse(service, characteristic, data){
this.client.emit('proxy_read_resp', service, characteristic, data);
}
/**
* sendNotificationResponse()
*
* Send a notification response to the master.
**/
sendNotificationResponse(service, characteristic){
this.client.emit('proxy_notify_resp', service, characteristic);
}
/**
* sendDataNotification()
*
* Notify a master of incoming data.
**/
sendDataNotification(service, characteristic, data){
this.client.emit('proxy_data', service, characteristic, data);
}
/**************************************
* Callbacks (must be overriden)
*************************************/
_onConnected(){}
_onDisconnected(){}
_onClientConnected(client){}
_onClientLeft(client){}
_onStatusUpdated(status){}
_onProfile(profile){}
_onTargetSelected(target){}
_onDeviceFound(peripheral, name, rssi){}
_onProxyReady(){}
_onDataNotification(service, characteristic, data){}
_onWriteResponse(service, characteristic, error){}
_onReadResponse(service, characteristic, data){}
_onNotifyResponse(service, characteristic){}
_onWriteRequest(service, characteristic, data, offset, withoutResponse){}
_onReadRequest(service, characteristic, offset){}
_onNotifyRequest(service, characteristic, enabled){}
}
class BtleJuiceProxy extends BtleJuice {
/**
* Constructor.
**/
constructor(target, host, port) {
/* Connect to Btlejuice core. */
super(host, port);
this.target = target;
}
/**
* Connect handler.
**/
_onConnected(){
this.stop();
this.selectTarget(this.target);
}
/**
* _onReadRequest()
*
* Forward the read request to the target device.
**/
_onReadRequest(service, characteristic, offset){
this.deviceRead(service, characteristic, offset);
}
/**
* _onReadResponse()
*
* Forward the read response to the dummy device.
**/
_onReadResponse(service, characteristic, data){
this.sendReadResponse(service, characteristic, data);
}
/**
* _onWriteRequest()
*
* Forward the write request to the target device.
**/
_onWriteRequest(service, characteristic, data, offset, withoutResponse) {
this.deviceWrite(
service,
characteristic,
data,
offset,
withoutResponse
);
}
/**
* _onWriteResponse()
*
* Forward the write response to the dummy device.
**/
_onWriteResponse(service, characteristic, error){
this.sendWriteResponse(service, characteristic, error);
}
/**
* _onNotifyRequest()
*
* Forward the notification subscription request to the target device.
**/
_onNotifyRequest(service, characteristic, enabled){
this.deviceNotify(service, characteristic, enabled);
}
/**
* _onNotifyResponse()
*
* Forward the notification subscription response to the dummy device.
**/
_onNotifyResponse(service, characteristic){
this.sendNotificationResponse(service, characteristic);
}
/**
* _onDataNotification()
*
* Forward the data notification to the dummy device.
**/
_onDataNotification(service, characteristic, data){
this.sendDataNotification(service, characteristic, data);
}
}
/**
* SniffingInterface
*
* This class provides all the required callbacks to sniff data
* exchanged through GATT read, write and notification operations.
**/
class SniffingInterface extends BtleJuiceProxy {
constructor(host, port, target) {
super(target, host, port);
}
/**
* _onConnected()
*
* Calls the ̀̀ onConnected̀̀ callback when connected to BtleJuice core.
**/
_onConnected(){
super._onConnected();
this.onConnected();
}
/**
* _onProxyReady()
*
* Calls the ̀̀ onProxyReadỳ callback when BtleJuice's proxy is set up.
**/
_onProxyReady(){
this.onProxyReady();
}
/**
* _onClientConnected()
*
* Calls the onClientConnected callback each time a peripheral connects to
* the dummy device.
**/
_onClientConnected(client){
this.onClientConnected(client);
}
/**
* _onClientLeft()
*
* Calls the onClientLeft callback each time a peripheral disconnects from
* the dummy device.
**/
_onClientLeft(client){
this.onClientLeft(client);
}
/**
* _onReadResponse()
*
* Calls the onRead callback when data is read from a characteristic.
**/
_onReadResponse(service, characteristic, data) {
super._onReadResponse(service, characteristic, data);
this.onRead(service, characteristic, data);
}
/**
* _onWriteRequest()
*
* Calls the onWrite callback when data is read from a characteristic.
**/
_onWriteRequest(service, characteristic, data, offset, withoutResponse) {
super._onWriteRequest(service, characteristic, data, offset, withoutResponse);
this.onWrite(service, characteristic, data, offset, withoutResponse);
}
/**
* _onDataNotification()
*
* Calls the onNotification callback when data is updated.
**/
_onDataNotification(service, characteristic, data) {
super._onDataNotification(service, characteristic, data);
this.onNotification(service, characteristic, data);
}
/**
* _onNotifyRequest()
*
* Calls the onSubscribe callback on notification (un)subscription.
**/
_onNotifyRequest(service, characteristic, enabled) {
super._onNotifyRequest(service, characteristic, enabled);
this.onSubscribe(service, characteristic, enabled);
}
/* Hexiify helper. */
hexiify(data){
var output = '';
for (var i=0; i<data.length; i++) {
var b = data.readUInt8(i);
if ((b>=48) && (b<=122))
output += '.'+String.fromCharCode(b);
else {
var b_ =b.toString(16)
if (b<0x10)
b_ = '0'+b;
output += b_+' ';
}
}
return output;
}
/* Callbacks (must be overriden) */
onConnected(){}
onProxyReady(){}
onClientConnected(client){}
onClientLeft(client){}
onRead(service, characteristic, data){}
onWrite(service, characteristic, data, offset, withoutResponse){}
onNotification(service, characteristic, data){}
onSubscribe(service, characteristic, enabled){}
}
/**
* HookingInterface
*
* Provides callbacks for handling GATT read, write and notification ops.
*
* Throw HookForceResponse to force a response to be sent (requests will not be
* forwarded if thrown in onBefore* callbacks).
* Throw HookModify to return a modified data to the dummy device.
**/
class HookingInterface extends BtleJuiceProxy {
constructor(host, port, target) {
super(target, host, port)
}
_onConnected(){
super._onConnected();
this.onConnected();
}
_onProxyReady(){
this.onProxyReady();
}
_onClientConnected(client){
this.onClientConnected(client);
}
_onClientLeft(client){
this.onClientLeft();
}
/**
* _onReadRequest()
*
* Calls onBeforeRead() to process the read request.
*
* If HookForceResponse is thrown, then do not forward request to target
* device but return the provided response to the dummy instead.
**/
_onReadRequest(service, characteristic, offset) {
try {
/* Call the `onBeforeRead` callback. */
this.onBeforeRead(service, characteristic, offset);
/* Forward request to the device. */
this.deviceRead(service, characteristic, offset);
} catch (e) {
if (e instanceof HookForceResponse) {
/* Send response to the proxy and do not send request to device. */
this.sendReadResponse(service, characteristic, e.data);
} else {
throw new UnhandledException();
}
}
}
/**
* _onReadResponse()
*
* Calls onAfterRead() to process the read response.
*
* If HookModify is thrown, then the modified response is forwarded to
* the dummy device instead of the original one.
**/
_onReadResponse(service, characteristic, data) {
try {
/* Call `onAfterRead` callback. */
this.onAfterRead(service, characteristic, data);
/* Forward response to client application. */
this.sendReadResponse(service, characteristic, data);
} catch (e) {
if (e instanceof HookModify) {
/* Send modified response. */
this.sendReadResponse(service, characteristic, e.data);
} else {
throw new UnhandledException();
}
}
}
/**
* _onWriteRequest()
*
* Calls onBeforeWrite() to process the write request.
*
* If HookModify is thrown, then the modified data is forwarded to
* the target device instead of the original one.
*
* If HookForceResponse is thrown, then the write request is not performed
* but a success message will be sent to the dummy device.
**/
_onWriteRequest(service, characteristic, data, offset, withoutResponse) {
try {
/* Call `onBeforeWrite` callback. */
this.onBeforeWrite(service, characteristic, data, offset, withoutResponse);
/* Forward request to device. */
this.deviceWrite(service, characteristic, data, offset, withoutResponse);
} catch (e) {
if (e instanceof HookForceResponse) {
/* Send forced response. */
this.sendWriteResponse(service, characteristic, e.error);
} else if (e instanceof HookModify) {
/* Send modified data. */
this.deviceWrite(service, characteristic, e.data, offset, withoutResponse);
} else {
throw new UnhandledException();
}
}
}
/**
* _onWriteRequest()
*
* Calls onBeforeSubscribe() to process the subscription request.
*
* If HookForceResponse is thrown, the subscription is not forwarded to the
* target device but a success message is sent to the dummy device.
**/
_onNotifyRequest(service, characteristic, enabled) {
try {
/* Call `onBeforeSubscribe` callback. */
this.onBeforeSubscribe(service, characteristic, enabled);
/* Forward to device. */
this.deviceNotify(service, characteristic, enabled);
} catch(e) {
if (e instanceof HookForceResponse) {
/* Send notification subscription response. */
this.sendNotificationResponse(service, characteristic);
} else {
throw new UnhandledException();
}
}
}
/**
* _onDataNotification()
*
* Calls onBeforeNotification() to process the notification.
*
* If HookForceResponse is thrown, the notification is discarded.
* If HookModify is thrown, the modified data is notified.
**/
_onDataNotification(service, characteristic, data) {
try {
/* Call `onBeforeNotification` callback. */
this.onBeforeNotification(service, characteristic, data);
/* Forward to client application. */
this.sendDataNotification(service, characteristic, data);
} catch (e) {
if (e instanceof HookModify) {
this.sendDataNotification(service, characteristic, e.data);
} else if (e instanceof HookForceResponse) {
} else {
throw new UnhandledException();
}
}
}
/* Callbacks. */
onConnected(){}
onProxyReady(){}
onClientConnected(client){}
onClientLeft(client){}
onBeforeRead(service, characteristic, offset){}
onAfterRead(service, characteristic, data){}
onBeforeWrite(service, characteristic, data, offset, withoutResponse){}
onBeforeSubscribe(service, characteristic, enabled){}
onBeforeNotification(service, characteristic, data){}
}
/* Module exports. */
exports.BtleJuiceProxy = BtleJuiceProxy;
exports.SniffingInterface = SniffingInterface;
exports.HookingInterface = HookingInterface;
exports.HookModify = HookModify;
exports.HookForceResponse = HookForceResponse;