-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfrida_rel.js
More file actions
1283 lines (711 loc) · 37 KB
/
frida_rel.js
File metadata and controls
1283 lines (711 loc) · 37 KB
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
this frida script tries to sniff USB traffic on macOS.
It dumps the data to DUMP_FILE_PATH with some additional ascii markers for further parsing.
OFFSETS_* were dumped on macos bigsur as far as i recall.
- piotr ( piotrbania.com - somewhere in 2021).
**/
// frida -l frida.js --no-pause ./checkra1n --cli
// frida -l frida.js --no-pause ./usb_test
// frida -n checkra1n -l frida.js --no-pause
'use strict'
var connect_ptr;
var classname;
var type;
var total = 0;
var total2 = 0;
// check endianess
// python -c "import sys; print(sys.byteorder)"
// little
var OFFSET_DeviceRequest = 0xd0;
var OFFSET_DeviceRequestTO = 0xf0;
var OFFSET_QueryInterface = 0x8;
var hook_list = Array();
// device
var OFFSET_DeviceRequestAsync = 0xd8;
var OFFSET_DeviceRequestAsyncTO = 0xf8;
// interface
var OFFSET_ControlRequest = 0xc0;
var OFFSET_ControlRequestAsync = 0xc8;
var OFFSET_ControlRequestAsyncTO = 0x130;
var OFFSET_WritePipe = 0x100;
var OFFSET_ControlRequestTO = 0x128;
var OFFSET_ResetDevice = 0xc8;
var OFFSET_USBDeviceReEnumerate = 0x128;
var array_uuids = Array("kIOUSBDeviceInterfaceID320", "874af642e9d0a201cee08c7c05778b8b",
"kIOUSBDeviceInterfaceID300", "93483d94f7046139ebc2f56cbd69f190",
"kIOUSBDeviceInterfaceID245", "3b475a3b2fd52ffeedb31e0099ad7b97",
"kIOUSBDeviceInterfaceID197", "d7118408d8b809c83e3e3e93030096bb",
"kIOUSBDeviceInterfaceID182", "d511914896c42f15861e80270a00529d",
"kIOUSBDeviceInterfaceID500", "e2485b4b47f03ca33be1eafc07027db5",
"kIOUSBDeviceInterfaceID650", "6a47c2242e1bac4accf2343533914d96",
"kIOUSBDeviceInterfaceID", "d411f39ed087815c612805270a00458b",
"kIOUSBInterfaceInterfaceID300", "274f4d88dcadeabcf690ab9fd6364083",
"kIOUSBInterfaceInterfaceID245", "4f4b6b0fd2bdba64ad87690436dc3e8e",
"kIOUSBInterfaceInterfaceID220", "d811e82f0ce60d77d0b1dc93030082a5",
"kIOUSBInterfaceInterfaceID197", "d7118408923c3dc63e3e3e9303009296",
"kIOUSBInterfaceInterfaceID182", "d51196484cac2349861e80270a000892",
"kIOUSBInterfaceInterfaceID500", "a74e93b0c3380d6c16acdd5dfb099b80",
"kIOUSBInterfaceInterfaceID550", "7f4845eb3f4de46a9eeaf8993bb98e8e",
"kIOUSBInterfaceInterfaceID650", "87408180891a15089f5ddbdffe0a9e8f",
"kIOUSBInterfaceInterfaceID700", "1d40a1b09ce5f9177e04c67ae28dc09a",
"kIOUSBInterfaceInterfaceID800", "28433b0cb05da8334c7f111ba8fd028f",
"kIOUSBInterfaceInterfaceID", "d411f39ee87ac973612805270a00d0b1",
"kIOCFPlugInInterfaceID", "d4119c1058e844c26f42c6e45000d491",
"", "");
Process.setExceptionHandler(function(exp) {
console.warn(JSON.stringify(Object.assign(exp, { _lr: DebugSymbol.fromAddress(exp.context.lr), _pc: DebugSymbol.fromAddress(exp.context.pc) }), null, 2));
Memory.protect(exp.memory.address, Process.pointerSize, 'rw-');
return true;
});
function GetUUIDText(raw)
{
for (var i = 0; array_uuids[i] != ""; i++)
{
if (array_uuids[i] == raw) return array_uuids[i-1];
}
return "UNKNOWN " + raw;
}
/*
public var bmRequestType: UInt8
public var bRequest: UInt8
public var wValue: UInt16
public var wIndex: UInt16
public var wLength: UInt16
public var pData: UnsafeMutableRawPointer!
public var wLenDone: UInt32
*/
function htonl(n)
{
return [
(n & 0xFF000000) >>> 24,
(n & 0x00FF0000) >>> 16,
(n & 0x0000FF00) >>> 8,
(n & 0x000000FF) >>> 0,
];
}
function byteArrayToLong(/*byte[]*/byteArray) {
var value = 0;
for ( var i = byteArray.length - 1; i >= 0; i--) {
value = (value * 256) + parseInt(byteArray[i]);
console.log("value = " + value + " ( added = " + parseInt(byteArray[i]));
}
return value;
};
function swap16(val) {
return val;
//return new UInt64(val).toNumber(); //.toString(16);
//console.log("swap16 val=" + val);
//return val;
return ((val & 0xFF) << 8)
| ((val >> 8) & 0xFF);
}
var gPACKET_COUNTER = 0;
var gTRACE = 0;
var DUMP_FILE_PATH = "/Users/piotr/Desktop/wireshark_checkrain/frida/dump.txt";
var DUMP_ENTRY_MARKER_CR = "III1";
var DUMP_ENTRY_MARKER_CRTO = "III2";
var DUMP_ENTRY_MARKER_CRASYNC = "III3";
var DUMP_ENTRY_MARKER_CRASYNCTO = "III4";
var DUMP_ENTRY_MARKER_WRITEPIPE = "III5";
var DUMP_ENTRY_MARKER_DR = "DDD1";
var DUMP_ENTRY_MARKER_DRTO = "DDD2";
var DUMP_ENTRY_MARKER_DRASYNC = "DDD3";
var DUMP_ENTRY_MARKER_DRASYNCTO = "DDD4";
var DUMP_ENTRY_MARKER_DRESET = "RRR1"; // reset device
var DUMP_ENTRY_MARKER_ENUM = "RRR2"; // USBDeviceReEnumerate is also used for reset device (see darwin_usb.c)
var DUMP_ENTRY_MARKER_QUERYINTERFACE = "QQQQ";
var DUMP_ENTRY_MARKER_DATA_START = "DATA";
var DUMP_ENTRY_MARKER_DATA_END = "BBBB";
var DUMP_ENTRY_MARKER_RET_END = "REND";
var IOUSBDevRequest_size = 0x18;
var IOUSBDevRequestTO_size = 0x20;
var ENTRY_CONTROLREQUEST = 0;
var ENTRY_CONTROLREQUEST_TO = 1;
var ENTRY_CONTROLREQUEST_ASYNC = 2;
var ENTRY_WRITEPIPE = 3;
var ENTRY_CONTROLREQUEST_DEVICEREQUEST = 5;
var ENTRY_CONTROLREQUEST_DEVICEREQUEST_TO = 6;
var ENTRY_CONTROLREQUEST_DEVICEREQUEST_ASYNC = 7;
var ENTRY_CONTROLREQUEST_DEVICEREQUEST_ASYNC_TO = 8;
var ENTRY_CONTROLREQUEST_DEVICE_RESET = 10;
var ENTRY_CONTROLREQUEST_DEVICE_ENUMERATE = 11;
var ENTRY_CONTROLREQUEST_ASYNC_TO = 12;
var ENTRY_QUERYINTERFACE = 13;
var gDumpFile = 0;
// which_api = 0 (ControlRequest), 1 (ControlRequestTO), 2 (ControlRequestAsync), 3 (WritePipe)
// DeviceRequest 5
// DeviceRequestTO 6
// DeviceRequestAsync 7
// DeviceRequestAsync 8
function DumpControlRequestToFile(_this, _pipe, req, ret_code, which_api, data, len)
{
gTRACE++;
var ret_code_num = ret_code.toString(16).padStart(8, "0"); // i don't know how to create bytes from js number, so i use this fucking string version (max 64bits)
ret_code_num = ret_code_num.substring(0, 8);
if (!gDumpFile) gDumpFile = new File(DUMP_FILE_PATH, "wb")
if (which_api == ENTRY_QUERYINTERFACE)
{
console.log("!!! FRIDA: " +gTRACE + " DumpControlRequestToFile: QUERY INTERFACE\r\n");
gDumpFile.write(DUMP_ENTRY_MARKER_QUERYINTERFACE);
gDumpFile.write(ret_code_num); gDumpFile.write(DUMP_ENTRY_MARKER_RET_END);
gDumpFile.write(DUMP_ENTRY_MARKER_DATA_END);
return;
}
if (which_api == ENTRY_WRITEPIPE)
{
// WritePipe
console.log("!!! FRIDA: " +gTRACE + " DumpControlRequestToFile: WritePipe pData=" + data + " len = " + len);
gDumpFile.write(DUMP_ENTRY_MARKER_WRITEPIPE);
gDumpFile.write(ret_code_num); gDumpFile.write(DUMP_ENTRY_MARKER_RET_END);
gDumpFile.write(Memory.readByteArray(data, len));
gDumpFile.write(DUMP_ENTRY_MARKER_DATA_END);
console.log("RETURN\r\n");
return;
}
if (which_api == ENTRY_CONTROLREQUEST_DEVICE_RESET)
{
gDumpFile.write(DUMP_ENTRY_MARKER_DRESET);
gDumpFile.write(ret_code_num); gDumpFile.write(DUMP_ENTRY_MARKER_RET_END);
gDumpFile.write(DUMP_ENTRY_MARKER_DATA_END);
return;
}
if (which_api == ENTRY_CONTROLREQUEST_DEVICE_ENUMERATE)
{
gDumpFile.write(DUMP_ENTRY_MARKER_ENUM);
gDumpFile.write(ret_code_num); gDumpFile.write(DUMP_ENTRY_MARKER_RET_END);
gDumpFile.write(DUMP_ENTRY_MARKER_DATA_END);
return;
}
try
{
var _bmRequestType = req.readU8().toString(16);
} catch(e)
{
console.log("!!! FRIDA: " +gTRACE + " DumpControlRequestToFile: unable to read memory: req = " + req);
return;
}
var _bmRequestType = req.readU8().toString(16);
var _bRequest = req.add(1).readU8().toString(16);
var _wValue = req.add(2).readU16().toString(16); //uint64( swap16(req.add(2).readU16()) );
var _wIndex = req.add(4).readU16().toString(16); //uint64( swap16(req.add(4).readU16()) ); // wValue needs to be converted
var _pData = ptr(Memory.readPointer(req.add(8)));
var _wLength = req.add(6).readU16().toString(16); //uint64( swap16(req.add(6).readU16()) ); // wlength needs to be converted
if (ret_code != 0)
{
console.log("!!! FRIDA: " +gTRACE + " FAILURE STATUS DumpControlRequestToFile which_api=" + which_api + " req=" + req + " bmRequestType=" + _bmRequestType + " bRequest = " + _bRequest + " wValue = " + _wValue + " wIndex=" + _wIndex + " wLength=" + _wLength.toString(16) + " pData=" + _pData + " ret_code=0x" + ret_code +" was a failure skipping\r\n");
//return;
}
console.log("!!! FRIDA: " +gTRACE + " DumpControlRequestToFile: which_api=" + which_api + "this=" + _this + " pipe=" + _pipe + " req=" + req + " bmRequestType=" + _bmRequestType + " bRequest = " + _bRequest + " wValue = " + _wValue + " wIndex=" + _wIndex + " wLength=" + _wLength.toString(16) + " pData=" + _pData);
// this shit is a must for frida, otherwise in readMemory it will use wrong wLength
_wLength = new UInt64("0x"+_wLength);
console.log("!!! FRIDA: " +gTRACE + " wLength=" + _wLength + " pData=" + _pData);
if ((_wLength == 0x800) || (_wLength == 2048) || (_wLength == "0x800"))
{
console.log("!!! FRIDA: " +gTRACE + " INCREASING COUNTER=" + gPACKET_COUNTER);
gPACKET_COUNTER++;
}
if (which_api == ENTRY_CONTROLREQUEST)
{
// controlrequest
// IOUSBDevRequest(byte bmRequestType, byte bRequest, short wValue, short wIndex, short wLength, Pointer pData, int wLenDone)
// sizeof(IOUSBDevRequest) = 0x18
console.log("!!! FRIDA: " +gTRACE + " DumpControlRequestToFile: _pData = " + _pData + " end: " + ptr(_pData).add(_wLength));
gDumpFile.write(DUMP_ENTRY_MARKER_CR);
gDumpFile.write(ret_code_num); gDumpFile.write(DUMP_ENTRY_MARKER_RET_END);
// dump binary header
gDumpFile.write(Memory.readByteArray(req, IOUSBDevRequest_size));
// dump pData
gDumpFile.write(DUMP_ENTRY_MARKER_DATA_START);
if ((_pData != 0) && (_wLength != 0)) gDumpFile.write(Memory.readByteArray(_pData, _wLength));
gDumpFile.write(DUMP_ENTRY_MARKER_DATA_END);
}
else if (which_api == ENTRY_CONTROLREQUEST_TO)
{
// controlrequest to
// IOUSBDevRequestTO(byte bmRequestType, byte bRequest, short wValue, short wIndex, short wLength, Pointer pData, int wLenDone, int noDataTimeout, int completionTimeout)
// sizeof(IOUSBDevRequestTO) = 0x20
gDumpFile.write(DUMP_ENTRY_MARKER_CRTO);
gDumpFile.write(ret_code_num); gDumpFile.write(DUMP_ENTRY_MARKER_RET_END);
// dump binary header
gDumpFile.write(Memory.readByteArray(req, IOUSBDevRequestTO_size));
// dump pData
gDumpFile.write(DUMP_ENTRY_MARKER_DATA_START);
if ((_pData != 0) && (_wLength != 0)) gDumpFile.write(Memory.readByteArray(_pData, _wLength));
gDumpFile.write(DUMP_ENTRY_MARKER_DATA_END);
}
else if (which_api == ENTRY_CONTROLREQUEST_ASYNC)
{
// ControlRequestAsync
gDumpFile.write(DUMP_ENTRY_MARKER_CRASYNC);
gDumpFile.write(ret_code_num); gDumpFile.write(DUMP_ENTRY_MARKER_RET_END);
// dump binary header
gDumpFile.write(Memory.readByteArray(req, IOUSBDevRequest_size));
// dump pData
gDumpFile.write(DUMP_ENTRY_MARKER_DATA_START);
if ((_pData != 0) && (_wLength != 0)) gDumpFile.write(Memory.readByteArray(_pData, _wLength));
gDumpFile.write(DUMP_ENTRY_MARKER_DATA_END);
}
else if (which_api == ENTRY_CONTROLREQUEST_ASYNC_TO )
{
// ControlRequestAsyncTO
gDumpFile.write(DUMP_ENTRY_MARKER_CRASYNCTO);
gDumpFile.write(ret_code_num); gDumpFile.write(DUMP_ENTRY_MARKER_RET_END);
// dump binary header
gDumpFile.write(Memory.readByteArray(req, IOUSBDevRequest_size));
// dump pData
gDumpFile.write(DUMP_ENTRY_MARKER_DATA_START);
if ((_pData != 0) && (_wLength != 0)) gDumpFile.write(Memory.readByteArray(_pData, _wLength));
gDumpFile.write(DUMP_ENTRY_MARKER_DATA_END);
}
else if (which_api == ENTRY_CONTROLREQUEST_DEVICEREQUEST)
{
// DeviceRequest
// IOUSBDevRequest(byte bmRequestType, byte bRequest, short wValue, short wIndex, short wLength, Pointer pData, int wLenDone)
// sizeof(IOUSBDevRequest) = 0x18
console.log("!!! FRIDA: " +gTRACE + " DumpControlRequestToFile: _pData = " + _pData + " end: " + ptr(_pData).add(_wLength));
gDumpFile.write(DUMP_ENTRY_MARKER_DR);
gDumpFile.write(ret_code_num); gDumpFile.write(DUMP_ENTRY_MARKER_RET_END);
// dump binary header
gDumpFile.write(Memory.readByteArray(req, IOUSBDevRequest_size));
// dump pData
gDumpFile.write(DUMP_ENTRY_MARKER_DATA_START);
if ((_pData != 0) && (_wLength != 0)) gDumpFile.write(Memory.readByteArray(_pData, _wLength));
gDumpFile.write(DUMP_ENTRY_MARKER_DATA_END);
}
else if (which_api == ENTRY_CONTROLREQUEST_DEVICEREQUEST_TO)
{
// DeviceRequestTO
// IOUSBDevRequestTO(byte bmRequestType, byte bRequest, short wValue, short wIndex, short wLength, Pointer pData, int wLenDone, int noDataTimeout, int completionTimeout)
// sizeof(IOUSBDevRequestTO) = 0x20
gDumpFile.write(DUMP_ENTRY_MARKER_DRTO);
gDumpFile.write(ret_code_num); gDumpFile.write(DUMP_ENTRY_MARKER_RET_END);
// dump binary header
gDumpFile.write(Memory.readByteArray(req, IOUSBDevRequestTO_size));
// dump pData
console.log("!!! FRIDA: " +gTRACE + " DUMPING IOUSBDevRequestTO_size=" + IOUSBDevRequestTO_size + " _wLength=" + _wLength + "_pData = " + _pData + " req = " + req);
gDumpFile.write(DUMP_ENTRY_MARKER_DATA_START);
if ((_pData != 0) && (_wLength != 0)) gDumpFile.write(Memory.readByteArray(_pData, _wLength));
gDumpFile.write(DUMP_ENTRY_MARKER_DATA_END);
}
else if (which_api == ENTRY_CONTROLREQUEST_DEVICEREQUEST_ASYNC)
{
// DeviceRequestAsync
// IOUSBDevRequest(byte bmRequestType, byte bRequest, short wValue, short wIndex, short wLength, Pointer pData, int wLenDone)
// sizeof(IOUSBDevRequest) = 0x18
console.log("!!! FRIDA: " +gTRACE + " DumpControlRequestToFile: _pData = " + _pData + " end: " + ptr(_pData).add(_wLength));
gDumpFile.write(DUMP_ENTRY_MARKER_DRASYNC);
gDumpFile.write(ret_code_num); gDumpFile.write(DUMP_ENTRY_MARKER_RET_END);
// dump binary header
gDumpFile.write(Memory.readByteArray(req, IOUSBDevRequest_size));
// dump pData
gDumpFile.write(DUMP_ENTRY_MARKER_DATA_START);
if ((_pData != 0) && (_wLength != 0)) gDumpFile.write(Memory.readByteArray(_pData, _wLength));
gDumpFile.write(DUMP_ENTRY_MARKER_DATA_END);
}
else if (which_api == ENTRY_CONTROLREQUEST_DEVICEREQUEST_ASYNC_TO)
{
// DeviceRequestAsyncTO
// IOUSBDevRequestTO(byte bmRequestType, byte bRequest, short wValue, short wIndex, short wLength, Pointer pData, int wLenDone, int noDataTimeout, int completionTimeout)
// sizeof(IOUSBDevRequestTO) = 0x20
gDumpFile.write(DUMP_ENTRY_MARKER_DRASYNCTO);
gDumpFile.write(ret_code_num); gDumpFile.write(DUMP_ENTRY_MARKER_RET_END);
// dump binary header
gDumpFile.write(Memory.readByteArray(req, IOUSBDevRequestTO_size));
// dump pData
gDumpFile.write(DUMP_ENTRY_MARKER_DATA_START);
if ((_pData != 0) && (_wLength != 0)) gDumpFile.write(Memory.readByteArray(_pData, _wLength));
gDumpFile.write(DUMP_ENTRY_MARKER_DATA_END);
}
gDumpFile.flush();
}
function DumpIORequest(req, Variant2)
{
/*
typedef struct {
UInt8 bmRequestType;
UInt8 bRequest;
UInt16 wValue;
UInt16 wIndex;
UInt16 wLength;
void * pData; // data pointer
UInt32 wLenDone; // # bytes transferred
} IOUSBDevRequest;
*/
if (req == 0)
{
console.log("!!! FRIDA: " +gTRACE + " DumpIORequest: NO REQUEST TO DUMP, REQ=0\r\n");
return;
}
console.log("!!! FRIDA: " +gTRACE + " DumpIORequest: req=" + req);
try {
var _bmRequestType = req.readU8().toString(16);
var _bRequest = req.add(1).readU8().toString(16);
var _wValue = req.add(2).readU16().toString(16);
var _wIndex = req.add(4).readU16().toString(16);
var _wLength = req.add(6).readU16().toString(16);
} catch(e)
{
console.log("!!! FRIDA: " +gTRACE + " DumpIORequest: unable to read memory: req = " + req);
return;
}
console.log("!!! FRIDA: " +gTRACE + " DumpIORequest: req=" + req + " bmRequestType=" + _bmRequestType + " bRequest = " + _bRequest + " wValue = " + _wValue + " wIndex=" + _wIndex + " wLength=" + _wLength);
return;
}
Interceptor.attach(Module.findExportByName("IOKit", "IOCreatePlugInInterfaceForService"), {
onEnter: function(args) {
var ret_addr = this.returnAddress;
var plug_ptr = args[3];
this.plug_ptr_out = ptr(plug_ptr); //.toString();
console.log("!!! FRIDA: " +gTRACE + " IOCreatePlugInInterfaceForService called, plug_ptr_out=" + plug_ptr + " ret_addr = " + ret_addr);
},
onLeave: function(retval) {
// If we have a valid connection
console.log("!!! FRIDA: " +gTRACE + " RET IOCreatePlugInInterfaceForService retval == " + retval + " (WE ARE EXPECTING 0)");
if (retval == 0) {
var _p1 = Memory.readPointer(this.plug_ptr_out);
_p1 = Memory.readPointer(_p1);
//console.log("!!! FRIDA: " +gTRACE + " p1 = " +_p1);
var QueryInterface_addr = Memory.readPointer(_p1.add(OFFSET_QueryInterface));
var QueryInterface_addr2 = Instruction.parse(QueryInterface_addr); //Memory.readPointer(QueryInterface_addr2);
// we need to hook this somehow
// (*plug)->QueryInterface(plug, CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (void *)&dev);
console.log("!!! FRIDA: " +gTRACE + " HOOKING QueryInterface = " + QueryInterface_addr + " func addr = " + QueryInterface_addr2);
if (QueryInterface_addr in hook_list)
{
console.log("FUNCTION ALREAD HOOKED - SKIPPING");
return retval;
}
var old_QueryInterface = new NativeFunction(QueryInterface_addr, 'int', ['pointer', 'pointer', 'pointer', 'pointer']);
hook_list[ QueryInterface_addr ] = 1;
//
// query interface replacement
//
Interceptor.replace(QueryInterface_addr, new NativeCallback((_this, _id1, _id2, _dev_out) => {
var ret_addr = this.returnAddress;
var ret_bytes = ret_addr.readU64().toString(16);
var uuid = _id1.toString(16) + _id2.toString(16);
var uuid_text = GetUUIDText(uuid);
var hook_interface = 0;
console.log("!!! FRIDA: " + gTRACE + " in hooked QueryInterface IN dev_out=" + _dev_out + " return=" + ret_addr + " bytes=" + ret_bytes );
console.log("!!! FRIDA: " + gTRACE + " in hooked QueryInterface IN CFUIID="+uuid_text);
if (uuid_text.includes("InterfaceInterface"))
{
console.log("!!! FRIDA: " +gTRACE + " hooking InterfaceInterface");
hook_interface = 1;
}
DumpControlRequestToFile(_this, 0, 0, 0, ENTRY_QUERYINTERFACE, 0, 0);
var res = old_QueryInterface(_this, _id1, _id2, _dev_out);
var _dev_out_real = Memory.readPointer(_dev_out);
if (_dev_out_real == 0)
{
console.log("!!! FRIDA: " +gTRACE + " error _dev_out_real == 0\r\n");
return res;
}
_dev_out_real = Memory.readPointer(_dev_out_real);
console.log("!!! FRIDA: " +gTRACE + " out hooked QueryInterface OUT res=" + res +" dev_out_real=" + _dev_out_real);
//Memory.writePointer(_dev_out, ptr("0x112233445566"));
//return res;
if (res == 0)
{
if (hook_interface == 1)
{
//return res;
var _addr_ControlRequest = Memory.readPointer(_dev_out_real.add(OFFSET_ControlRequest));
var _addr_ControlRequestTO = Memory.readPointer(_dev_out_real.add(OFFSET_ControlRequestTO));
var _addr_ControlRequestAsync = Memory.readPointer(_dev_out_real.add(OFFSET_ControlRequestAsync));
var _addr_ControlRequestAsyncTO = Memory.readPointer(_dev_out_real.add(OFFSET_ControlRequestAsyncTO));
var _addr_WritePipe = Memory.readPointer(_dev_out_real.add(OFFSET_WritePipe));
console.log("!!! FRIDA: " +gTRACE + " HOOKING INTERFACE _ControlRequest=" + _addr_ControlRequest + " WritePipe = " + _addr_WritePipe);
//
// HOOK ControlRequest
// IOReturn (*ControlRequest)(void *self, UInt8 pipeRef, IOUSBDevRequest *req);
if (!(_addr_ControlRequest in hook_list))
{
console.log("!!! FRIDA: " +gTRACE + " hooking INTERFACE ControlRequest");
hook_list[_addr_ControlRequest] = 1;
var old_ControlRequest = new NativeFunction(_addr_ControlRequest, 'int', ['pointer', 'pointer', 'pointer']);
Interceptor.replace(_addr_ControlRequest, new NativeCallback((_this, _pipeRef, _req) => {
var old_req = ptr(_req);
console.log("!!! FRIDA: " +gTRACE + " in hooked ControlRequest IN req=" + _req);
//DumpIORequest(ptr(_req), 0);
var res = 0;
DumpControlRequestToFile(_this, _pipeRef, old_req, res, ENTRY_CONTROLREQUEST, 0, 0);
res = old_ControlRequest(_this, _pipeRef, _req);
//DumpControlRequestToFile(_this, _pipeRef, old_req, res, ENTRY_CONTROLREQUEST, 0, 0);
return res;
}, 'int', ['pointer', 'pointer', 'pointer']));
}
//
// HOOK ControlRequestTO
// IOReturn (*ControlRequestTO)(void *self, UInt8 pipeRef, IOUSBDevRequestTO *req);
if (!(_addr_ControlRequestTO in hook_list))
{
console.log("!!! FRIDA: " +gTRACE + " hooking INTERFACE ControlRequestTO");
hook_list[_addr_ControlRequestTO] = 1;
var old_ControlRequestTO = new NativeFunction(_addr_ControlRequestTO, 'int', ['pointer', 'pointer', 'pointer']);
Interceptor.replace(_addr_ControlRequestTO, new NativeCallback((_this, _pipeRef, _req) => {
var old_req = ptr(_req);
console.log("!!! FRIDA: " +gTRACE + " in hooked ControlRequestTO IN req=" + _req);
//DumpIORequest(ptr(_req), 1);
var res = 0;
DumpControlRequestToFile(_this, _pipeRef, old_req, res, ENTRY_CONTROLREQUEST_TO, 0, 0);
res = old_ControlRequestTO(_this, _pipeRef, _req);
//DumpControlRequestToFile(_this, _pipeRef, old_req, res, ENTRY_CONTROLREQUEST_TO, 0, 0);
return res;
}, 'int', ['pointer', 'pointer', 'pointer']));
}
//
// ControlRequestAsync
// IOReturn (*ControlRequestAsync)(void *self, UInt8 pipeRef, IOUSBDevRequest *req, IOAsyncCallback1 callback, void *refCon);
if (!(_addr_ControlRequestAsync in hook_list))
{
// DeviceRequest(void *_this, IOUSBDevRequestTO *req)
console.log("!!! FRIDA: " +gTRACE + " hooking INTERFACE ControlRequestAsync");
hook_list[_addr_ControlRequestAsync] = 1;
var old_ControlRequestAsync = new NativeFunction(_addr_ControlRequestAsync, 'int', ['pointer', 'pointer', 'pointer', 'pointer', 'pointer']);
Interceptor.replace(_addr_ControlRequestAsync, new NativeCallback((_this, _pipeRef, _req, _callback, _refCon) => {
var old_req = ptr(_req);
console.log("!!! FRIDA: " +gTRACE + " in hooked ControlRequestAsync IN req=" + _req);
//DumpIORequest(ptr(_req), 0);
var res = 0;
DumpControlRequestToFile(_this, _pipeRef, old_req, res, ENTRY_CONTROLREQUEST_ASYNC, 0, 0);
res = old_ControlRequestAsync(_this, _pipeRef, _req, _callback, _refCon);
//DumpControlRequestToFile(_this, _pipeRef, old_req, res, ENTRY_CONTROLREQUEST_ASYNC, 0, 0);
return res;
}, 'int', ['pointer', 'pointer', 'pointer', 'pointer', 'pointer']));
}
//
// ControlRequestAsyncTO
// IOReturn (*ControlRequestAsyncTO)(void *self, UInt8 pipeRef, IOUSBDevRequestTO *req, IOAsyncCallback1 callback, void *refCon);
if (!(_addr_ControlRequestAsyncTO in hook_list))
{
// DeviceRequest(void *_this, IOUSBDevRequestTO *req)
console.log("!!! FRIDA: " +gTRACE + " hooking INTERFACE ControlRequestAsyncTO");
hook_list[_addr_ControlRequestAsyncTO] = 1;
var old_ControlRequestAsyncTO = new NativeFunction(_addr_ControlRequestAsyncTO, 'int', ['pointer', 'pointer', 'pointer', 'pointer', 'pointer']);
Interceptor.replace(_addr_ControlRequestAsyncTO, new NativeCallback((_this, _pipeRef, _req, _callback, _refCon) => {
var old_req = ptr(_req);
console.log("!!! FRIDA: " +gTRACE + " in hooked ControlRequestAsyncTO IN req=" + _req);
//DumpIORequest(ptr(_req), 0);
var res = 0;
DumpControlRequestToFile(_this, _pipeRef, old_req, res, ENTRY_CONTROLREQUEST_ASYNC_TO, 0, 0);
res = old_ControlRequestAsyncTO(_this, _pipeRef, _req, _callback, _refCon);
//DumpControlRequestToFile(_this, _pipeRef, old_req, res, ENTRY_CONTROLREQUEST_ASYNC_TO, 0, 0);
return res;
}, 'int', ['pointer', 'pointer', 'pointer', 'pointer', 'pointer']));
}
// HOOK WritePipe
// IOReturn (*WritePipe)(void *self, UInt8 pipeRef, void *buf, UInt32 size);
/*
if (!(_addr_WritePipe in hook_list))
{
console.log("!!! FRIDA: " +gTRACE + " hooking INTERFACE WritePipe");
hook_list[_addr_WritePipe] = 1;
var old_WritePipe = new NativeFunction(_addr_WritePipe, 'int', ['pointer', 'pointer', 'pointer', 'int']);
Interceptor.replace(_addr_WritePipe, new NativeCallback((_this, _pipeRef, _buf, _size) => {
console.log("!!! FRIDA: " +gTRACE + " in hooked WritePipe IN size=" + _size + " _buf=" + _buf);
var res = old_WritePipe(_this, _pipeRef, _buf, _size);
DumpControlRequestToFile(0, res, ENTRY_WRITEPIPE, _buf, _size);
return res;
}, 'int', ['pointer', 'pointer', 'pointer', 'int']));
}
*/
return res;
}
//
//
//
// hooking Device Interface APIS
//
//
var _addr_DeviceRequest = Memory.readPointer(_dev_out_real.add(OFFSET_DeviceRequest));
var _addr_DeviceRequestTO = Memory.readPointer(_dev_out_real.add(OFFSET_DeviceRequestTO));
var _addr_DeviceRequestAsync = Memory.readPointer(_dev_out_real.add(OFFSET_DeviceRequestAsync));
var _addr_DeviceRequestAsyncTO = Memory.readPointer(_dev_out_real.add(OFFSET_DeviceRequestAsyncTO));
var _addr_ResetDevice = Memory.readPointer(_dev_out_real.add(OFFSET_ResetDevice));
var _addr_USBDeviceReEnumerate = Memory.readPointer(_dev_out_real.add(OFFSET_USBDeviceReEnumerate));
// IOReturn (*ControlRequest)(void *self, UInt8 pipeRef, IOUSBDevRequest *req);
// IOReturn (*ControlRequestAsync)(void *self, UInt8 pipeRef, IOUSBDevRequest *req, IOAsyncCallback1 callback, void *refCon);
// IOReturn (*WritePipe)(void *self, UInt8 pipeRef, void *buf, UInt32 size);
//Memory.writePointer(_dev_out_real.add(OFFSET_DeviceRequestAsync), ptr("0x112233445566"));
//Memory.writePointer(_dev_out_real.add(OFFSET_DeviceRequestAsyncTO), ptr("0x112233445566"));
console.log("!!! FRIDA: " +gTRACE + " HOOKING DeviceRequest=" + _addr_DeviceRequest + " DeviceRequesTO = " + _addr_DeviceRequestTO);
//
// HOOK DeviceRequest
//
if (!(_addr_DeviceRequest in hook_list))
{
// DeviceRequest(void *_this, IOUSBDevRequestTO *req)
hook_list[_addr_DeviceRequest] = 1;
var old_DeviceRequest = new NativeFunction(_addr_DeviceRequest, 'int', ['pointer', 'pointer']);
Interceptor.replace(_addr_DeviceRequest, new NativeCallback((_this, _req) => {
var old_req = ptr(_req);
console.log("!!! FRIDA: " +gTRACE + " in hooked DeviceRequest IN req=" + _req);