-
Notifications
You must be signed in to change notification settings - Fork 6
/
iOS_Trace.js
executable file
·1534 lines (1296 loc) · 62.1 KB
/
iOS_Trace.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
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
// 检查任意指针是否是有效的Objective-C对象
// 参考https://codeshare.frida.re/@mrmacete/objc-method-observer/
// 导入必要的模块和函数
// const { spawnSync } = require('child_process');
// const fs = require('fs');
var NSData = ObjC.classes.NSData;
var NSString = ObjC.classes.NSString;
function unicode2str(str) {
var ret = "";
var ustr = "";
for (var i = 0; i < str.length; i++) {
var code = str.charCodeAt(i);
var code16 = code.toString(16);
if (code < 0xf) {
ustr = "\\u" + "000" + code16;
} else if (code < 0xff) {
ustr = "\\u" + "00" + code16;
} else if (code < 0xfff) {
ustr = "\\u" + "0" + code16;
} else {
ustr = "\\u" + code16;
}
ret += ustr;
}
return ret;
}
/* JavaScript String -> NSString */
function str(s) {
return Memory.allocUtf8String(s);
}
function nsstr(str) {
return ObjC.classes.NSString.stringWithUTF8String_(Memory.allocUtf8String(str));
}
/* NSString -> NSData */
function nsstr2nsdata(nsstr) {
return nsstr.dataUsingEncoding_(4);
}
/* NSData -> NSString */
function nsdata2nsstr(nsdata) {
return ObjC.classes.NSString.alloc().initWithData_encoding_(nsdata, 4);
}
/* Print Native Callstack */
function callstack() {
console.log(Thread.backtrace(this.context, Backtracer.ACCURATE).map(DebugSymbol.fromAddress).join("\n") + "\n");
}
function callstack_() {
console.log(ObjC.classes.NSThread.callStackSymbols().toString());
}
// color着色
var Color = {
RESET: "\x1b[39;49;00m", Black: "0;01", Blue: "4;01", Cyan: "6;01", Gray: "7;01", Green: "2;01", Purple: "5;01", Red: "1;01", Yellow: "3;01",
Light: {
Black: "0;11", Blue: "4;11", Cyan: "6;11", Gray: "7;01", Green: "2;11", Purple: "5;11", Red: "1;11", Yellow: "3;11"
}
};
var LOG = function (input, kwargs) {
kwargs = kwargs || {};
var logLevel = kwargs['l'] || 'log', colorPrefix = '\x1b[3', colorSuffix = 'm';
if (typeof input === 'object')
input = JSON.stringify(input, null, kwargs['i'] ? 2 : null);
if (kwargs['c'])
input = colorPrefix + kwargs['c'] + colorSuffix + input + Color.RESET;
console[logLevel](input);
};
/* Get all modules */
function getmodule() {
var modules = Process.enumerateModulesSync();
return modules.map(function (item) {
return item['path'];
});
}
/* Get all class in module */
function getmoduleclass(module) {
if (module == null) {
module = Process.enumerateModulesSync()[0]['path'];
}
var pcount = Memory.alloc(4);
Memory.writeU32(pcount, 0);
var objc_copyClassNamesForImage = new NativeFunction(Module.findExportByName(null, "objc_copyClassNamesForImage"), "pointer", ["pointer", "pointer"]);
var classptrarr = objc_copyClassNamesForImage(Memory.allocUtf8String(module), pcount);
var count = Memory.readU32(pcount);
var result = Array();
for (var i = 0; i < count; i++) {
var classptr = Memory.readPointer(classptrarr.add(Process.pointerSize * i));
result.push(Memory.readUtf8String(classptr));
}
return result;
}
function getclassmodule(classname) {
var objc_getClass = new NativeFunction(Module.findExportByName(null, "objc_getClass"), "pointer", ["pointer"]);
var class_getImageName = new NativeFunction(Module.findExportByName(null, "class_getImageName"), "pointer",
["pointer"]);
var class_ = objc_getClass(Memory.allocUtf8String(classname));
return Memory.readUtf8String(class_getImageName(class_));
}
function getsymbolmodule(symbolname) {
var dladdr = new NativeFunction(Module.findExportByName(null, "dladdr"), "int", ["pointer", "pointer"]);
var info = Memory.alloc(Process.pointerSize * 4);
dladdr(Module.findExportByName(null, symbolname), info);
return {
"fname": Memory.readUtf8String(Memory.readPointer(info)),
"fbase": Memory.readPointer(info.add(Process.pointerSize)),
"sname": Memory.readUtf8String(Memory.readPointer(info.add(Process.pointerSize * 2))),
"saddr": Memory.readPointer(info.add(Process.pointerSize * 3)),
}
}
function getaddressmodule(address) {
var dladdr = new NativeFunction(Module.findExportByName(null, "dladdr"), "int", ["pointer", "pointer"]);
var info = Memory.alloc(Process.pointerSize * 4);
dladdr(ptr(address), info);
return {
"fname": Memory.readUtf8String(Memory.readPointer(info)),
"fbase": Memory.readPointer(info.add(Process.pointerSize)),
"sname": Memory.readUtf8String(Memory.readPointer(info.add(Process.pointerSize * 2))),
"saddr": Memory.readPointer(info.add(Process.pointerSize * 3)),
}
}
/* Get Objective-C Method of class */
function getclassmethod(classname) {
var objc_getClass = new NativeFunction(Module.findExportByName(null, "objc_getClass"), "pointer", ["pointer"]);
var objc_getMetaClass = new NativeFunction(Module.findExportByName(null, "objc_getMetaClass"), "pointer", ["pointer"]);
var class_ = objc_getClass(Memory.allocUtf8String(classname));
var metaclass_ = objc_getMetaClass(Memory.allocUtf8String(classname));
var pcount = Memory.alloc(4);
Memory.writeU32(pcount, 0);
var class_copyMethodList = new NativeFunction(Module.findExportByName(null, "class_copyMethodList"), "pointer", ["pointer", "pointer"]);
var method_getName = new NativeFunction(Module.findExportByName(null, "method_getName"), "pointer", ["pointer"]);
var method_getImplementation = new NativeFunction(Module.findExportByName(null, "method_getImplementation"), "pointer", ["pointer"]);
var methodptrarr = class_copyMethodList(class_, pcount);
var count = Memory.readU32(pcount);
var result = new Array();
for (var i = 0; i < count; i++) {
var method = Memory.readPointer(methodptrarr.add(Process.pointerSize * i));
var name = Memory.readUtf8String(method_getName(method));
var imp = method_getImplementation(method);
result.push({ "name": "- " + name, "imp": imp });
//console.log("-[" + classname + " " + name + "] -> " + imp);
}
Memory.writeU32(pcount, 0);
methodptrarr = class_copyMethodList(metaclass_, pcount);
count = Memory.readU32(pcount);
for (var i = 0; i < count; i++) {
var method = Memory.readPointer(methodptrarr.add(Process.pointerSize * i));
var name = Memory.readUtf8String(method_getName(method));
var imp = method_getImplementation(method);
result.push({ "name": "+ " + name, "imp": imp });
//console.log("+[" + classname + " " + name + "] -> " + imp);
}
return result;
}
// 强制过证书校验
function forcetrustcert() {
Interceptor.replace(Module.findExportByName(null, 'SecTrustEvaluate'),
new NativeCallback(function (trust, result) {
Memory.writePointer(result, ptr('0x1'));
console.log('pass SecTrustEvaluate');
return 0;
}, 'int', ['pointer', 'pointer'])
);
/* 获取app路径下的可执行模块 hook存在以下方法的类
- evaluateServerTrust:forDomain:
- allowInvalidCertificates
- shouldContinueWithInvalidCertificate
*/
var apppath = Process.enumerateModulesSync()[0]['path'];
apppath = apppath.slice(0, apppath.lastIndexOf('/'));
getmodule().forEach(function (module, i) {
if (module.indexOf(apppath) != 0) return;
getmoduleclass(module).forEach(function (classname, j) {
getclassmethod(classname).forEach(function (methodinfo, k) {
var name = methodinfo['name'];
if (name == '- evaluateServerTrust:forDomain:' ||
name == '- allowInvalidCertificates' ||
name == '- shouldContinueWithInvalidCertificate') {
console.log("forcetrustcert " + classname + " " + name);
Interceptor.attach(methodinfo['imp'], {
onEnter: function (args) {
console.log("forcetrustcert " + classname + " " + name);
},
onLeave: function (retval) {
retval.replace(ptr('0x1'));
}
});
}
});
});
});
}
// 随机字符串
function randomString(len) {
len = len || 32;
var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
var maxPos = $chars.length;
var pwd = '';
for (i = 0; i < len; i++) {
pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
}
return pwd;
}
// 生成二进制NSData
function rawnsdata() {
var buf = Memory.alloc(256);
for (var i = 0; i < 256; i++) {
Memory.writeU8(buf.add(i), i);
}
return ObjC.classes.NSData.dataWithBytes_length_(buf, 256);
}
function dump_alertview() {
ObjC.chooseSync(ObjC.classes.UIAlertController).forEach(
function (alertcontroller) {
var actions = alertcontroller.actions();
for (var i = 0; i < actions.count(); i++) {
var action = actions.objectAtIndex_(i);
var handler = action.handler();
if (handler != null) handler = handler.handle;
console.log(action.title(), handler);
}
}
);
}
function get_object_method_address(object, action) {
var class_getMethodImplementation = new NativeFunction(Module.findExportByName(null, "class_getMethodImplementation"), "pointer", ["pointer", "pointer"]);
var imp = class_getMethodImplementation(object.class(), ObjC.selector(action));
var mod = Process.getModuleByAddress(imp);
return mod['name'] + '!' + imp.sub(mod['base'])
}
function get_function_address(address) {
address = ptr(address);
var symbol = getaddressmodule(address);
var sympath = symbol['fname'];
var symname = sympath.split('/')[sympath.split('/').length - 1];
return symname + '!' + address.sub(symbol['fbase']);
}
// 遍历界面元素
function tranverse_view() {
var appCls = ObjC.classes["NSApplication"] || ObjC.classes["UIApplication"];
var mainwin = appCls.sharedApplication().keyWindow();
var arr = Array();
function find_subviews_internal(view, depth) {
if (view == null) {
return;
}
var space = '';
for (var i = 0; i < depth; i++) {
space += '-';
}
var text = '';
var ctrlname = view.class().toString();
if (view.respondsToSelector_(ObjC.selector('text'))) {
text = view.text() == null ? "" : view.text().toString();
}
var responder = '';
var iter = true;
if (view.respondsToSelector_(ObjC.selector('allTargets'))) {
var targets = view.allTargets().allObjects();
if (targets != null) {
var targetcount = targets.count();
var events = view.allControlEvents();
for (var i = 0; i < targetcount; i++) {
var target = targets.objectAtIndex_(i);
var actions = view.actionsForTarget_forControlEvent_(target, events);
if (actions != null) {
var actioncount = actions.count();
for (var j = 0; j < actioncount; j++) {
var clsname = target.class().toString();
var action = actions.objectAtIndex_(j).toString();
var addr = get_object_method_address(target, action)
responder += '[' + clsname + ' ' + action + ']=' + addr + ',';
}
}
}
}
}
var msg = space + ctrlname + " " + view.handle;
if (text.length > 0) {
msg += " => " + unicode2str(text);
}
if (responder != '') {
msg += " selectors= " + responder;
}
console.log((msg));
if (view.respondsToSelector_(ObjC.selector('actions'))) {
// UIAlertView
iter = false;
var actions = view.actions();
var actioncount = actions.count();
for (var j = 0; j < actioncount; j++) {
var action = actions.objectAtIndex_(j);
var title = action.title().UTF8String();
if (action.handler() == null) {
console.log((space + ' action= ' + unicode2str(title)));
} else {
var block = action.handler().handle;
var funcaddr = Memory.readPointer(block.add(Process.pointerSize * 2));
var types = action.handler().types;
var addr = get_function_address(funcaddr);
console.log((space + ' action= ' + unicode2str(title) + ' ' + types + ' ' + addr));
}
}
}
if (view.respondsToSelector_(ObjC.selector('gestureRecognizers'))) {
var gestures = view.gestureRecognizers();
if (gestures != null) {
var gesturecount = gestures.count();
for (var k = 0; k < gesturecount; k++) {
var gesture = gestures.objectAtIndex_(k);
var targets = ObjC.Object(gesture.handle.add(16).readPointer());
if (targets.handle.isNull()) {
continue;
}
var targetcount = targets.count();
for (var l = 0; l < targetcount; l++) {
var target = targets.objectAtIndex_(l);
var addr = get_function_address(target.action());
console.log(space + 'action', addr);
}
}
}
/*for (var j = 0; j < gesturecount; j++) {
var gesture = gestures.objectAtIndex_(j);
}*/
}
var subviews = view.subviews();
if (subviews != null && iter) {
var subviewcount = subviews.count();
for (var i = 0; i < subviewcount; i++) {
var subview = subviews.objectAtIndex_(i);
find_subviews_internal(subview, depth + 1);
}
}
}
find_subviews_internal(mainwin, 0);
}
function trace_view() {
var UIApplication = ObjC.classes.UIApplication;
Interceptor.attach(UIApplication["- sendAction:to:from:forEvent:"].implementation, {
onEnter: function (args) {
var action = Memory.readUtf8String(args[2]);
var toobj = ObjC.Object(args[3]);
var fromobj = ObjC.Object(args[4]);
var event = ObjC.Object(args[5]);
console.log('SendAction:' + action + ' to:' + toobj.toString() +
' from:' + fromobj.toString() + ' forEvent:' + event.toString() + ']');
}
});
}
/* c function wrapper */
function getExportFunction(type, name, ret, args) {
var nptr;
nptr = Module.findExportByName(null, name);
if (nptr === null) {
console.log("cannot find " + name);
return null;
} else {
if (type === "f") {
var funclet = new NativeFunction(nptr, ret, args);
if (typeof funclet === "undefined") {
console.log("parse error " + name);
return null;
}
return funclet;
} else if (type === "d") {
var datalet = Memory.readPointer(nptr);
if (typeof datalet === "undefined") {
console.log("parse error " + name);
return null;
}
return datalet;
}
}
}
function modload(modpath) {
var dlopen = getExportFunction("f", "dlopen", "pointer", ["pointer", "int"]);
dlopen(str(modpath), 1);
}
function getscreensize() {
var UIScreen = ObjC.classes.UIScreen;
return UIScreen.mainScreen().bounds()[1];
}
function click(x, y) {
// https://github.com/zjjno/PTFakeTouchDemo.git 编译为dylib
modload("/Library/MobileSubstrate/DynamicLibraries/PTFakeTouch.dylib")
var touchxy = getExportFunction("f", "touchxy", "void", ["int", "int"]);
touchxy(x, y);
}
function _utf8_encode(string) {
string = string.replace(/\r\n/g, "\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
} else if ((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
} else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
}
// 获取keychain数据
function getkeychain() {
var NSMutableDictionary = ObjC.classes.NSMutableDictionary;
var kCFBooleanTrue = ObjC.Object(getExportFunction("d", "kCFBooleanTrue"));
var kSecReturnAttributes = ObjC.Object(getExportFunction("d", "kSecReturnAttributes"));
var kSecMatchLimitAll = ObjC.Object(getExportFunction("d", "kSecMatchLimitAll"));
var kSecMatchLimit = ObjC.Object(getExportFunction("d", "kSecMatchLimit"));
var kSecClassGenericPassword = ObjC.Object(getExportFunction("d", "kSecClassGenericPassword"));
var kSecClassInternetPassword = ObjC.Object(getExportFunction("d", "kSecClassInternetPassword"));
var kSecClassCertificate = ObjC.Object(getExportFunction("d", "kSecClassCertificate"));
var kSecClassKey = ObjC.Object(getExportFunction("d", "kSecClassKey"));
var kSecClassIdentity = ObjC.Object(getExportFunction("d", "kSecClassIdentity"));
var kSecClass = ObjC.Object(getExportFunction("d", "kSecClass"));
var query = NSMutableDictionary.alloc().init();
var SecItemCopyMatching = getExportFunction("f", "SecItemCopyMatching", "int", ["pointer", "pointer"]);
[kSecClassGenericPassword, kSecClassInternetPassword, kSecClassCertificate, kSecClassKey,
kSecClassIdentity].forEach(function (secItemClass) {
query.setObject_forKey_(kCFBooleanTrue, kSecReturnAttributes);
query.setObject_forKey_(kSecMatchLimitAll, kSecMatchLimit);
query.setObject_forKey_(secItemClass, kSecClass);
var result = Memory.alloc(8);
Memory.writePointer(result, ptr("0"));
SecItemCopyMatching(query.handle, result);
var pt = Memory.readPointer(result);
if (!pt.isNull()) {
console.log(ObjC.Object(pt).toString());
}
}
)
}
/* Base64 Encode */
function base64(input) {
var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = _utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output + _keyStr.charAt(enc1) + _keyStr.charAt(enc2) + _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
}
return output;
}
function CheckObjc(p) {
var klass = getObjCClassPtr(p);
return !klass.isNull();
}
function getObjCClassPtr(p) {
/*
* Loosely based on:
* https://blog.timac.org/2016/1124-testing-if-an-arbitrary-pointer-is-a-valid-objective-c-object/
*/
var ISA_MASK = ptr('0x0000000ffffffff8');
var ISA_MAGIC_MASK = ptr('0x000003f000000001');
var ISA_MAGIC_VALUE = ptr('0x000001a000000001');
if (!isReadable(p))
return NULL;
var isa = p.readPointer();
var classP = isa;
if (classP.and(ISA_MAGIC_MASK).equals(ISA_MAGIC_VALUE))
classP = isa.and(ISA_MASK);
if (isReadable(classP))
return classP;
return NULL;
}
function isReadable(p) {
try {
p.readU8();
return true;
} catch (e) {
return false;
}
}
// https://codeshare.frida.re/@lichao890427/ios-utils/
// var NSData = ObjC.classes.NSData;
// var NSString = ObjC.classes.NSString;
// /* NSData -> NSString */
// function NSData2NSString(NSData) {
// return ObjC.classes.NSString.alloc().initWithData_encoding_(NSData, 4);
// }
// generic trace
function trace(pattern) {
var type = (pattern.indexOf(" ") !== -1) ? "objc" : "module";
var res = new ApiResolver(type);
var matches = res.enumerateMatchesSync(pattern);
var targets = uniqBy(matches, JSON.stringify);
targets.forEach(function (target) {
if (type === "objc") {
var filter = [ // 自定义过滤条件,方法名称中不含以下关键词
"SDK",
"Monitor",
"_"
];
for (var i = 0, Traceflag = 0; i < filter.length; i++) {
if (target.name.indexOf(filter[i]) != -1) {
Traceflag = 1;
}
}
if (Traceflag === 0) {
LOG("Tracing - Name: " + target.name + " address: " + target.address, { c: Color.Gray });
// console.log("Tracing " + target.name +" "+ target.address);
traceObjC(target.address, target.name);
}
}
else if (type === "module") {
traceModule(target.address, target.name);
}
});
}
// remove duplicates from array
function uniqBy(array, key) {
var seen = {};
return array.filter(function (item) {
var k = key(item);
return seen.hasOwnProperty(k) ? false : (seen[k] = true);
});
}
// trace ObjC methods
function traceObjC(impl, name) {
Interceptor.attach(impl, {
onEnter: function (args) {
// debug only the intended calls
console.log("Tracing " + name);
console.log("[+] ---------------------------------------------------------------");
LOG("*** entering " + name, { c: Color.Green });
// console.log("*** entered " + name);
// print full backtrace
// console.log('\tACCURATE Backtrace:\n\t' + Thread.backtrace(this.context,Backtracer.ACCURATE).map(DebugSymbol.fromAddress).join('\n\t'));
// console.log('\tFUZZY Backtrace:\n\t' + Thread.backtrace(this.context,Backtracer.FUZZY).map(DebugSymbol.fromAddress).join('\n\t'));
// print caller
console.log("[+] Caller: " + DebugSymbol.fromAddress(this.returnAddress));
// print args
if (name.indexOf(":") !== -1) { // 有参数的逻辑处理
var param = name.split(":");
param[0] = param[0].split(" ")[1];
for (var i = 0; i < param.length - 1; i++) {
// console.log("[+] args"+"["+ (i+2) +"] objc: " + CheckObjc(args[i + 2]));
if (CheckObjc(args[i + 2])) {
printArg("arg" + (i + 2) + " " + param[i] + ": ", args[i + 2]);
}
}
// 防止遗漏Receiver对象
if (CheckObjc(args[0])) {
// 无参数的Objective-C方法,打印args[0]
var param1 = new ObjC.Object(args[0]);
LOG("[+++++] 类 args[0]: " + param1, { c: Color.Gray });
// console.log("[+] args[0]: " + param1);
console.log("[++++] type: " + param1.$className);
// if(param1.$className == "RCTScrollView"){
// console.log(param1.$className+" setContentOffset_animated");
// //args[0]: <RCTScrollView: 0x106045a00; reactTag: 725; frame = (0 0; 347 173.5); clipsToBounds = YES; layer = <CALayer: 0x28103bee0>>
// // var offset = param1.contentOffset();
// // //param1.setContentOffset_animated_({x: offset.x, y: offset.y - 1000}, 0.5);
// // var bottomOffset = {x: 0, y: contentSize.height - param1.bounds().size.height};
// // if (bottomOffset.y < 0) {
// // bottomOffset.y = 0;
// // }
// // param1.setContentOffset_animated_(bottomOffset, true);
// }
}
} else { // 无参数的逻辑处理,如-[NSString md5]
if (CheckObjc(args[0])) {
// 无参数的Objective-C方法,打印args[0]
var param1 = new ObjC.Object(args[0]);
LOG("[+] 类 args[0]: " + param1, { c: Color.Gray });
// console.log("[+] args[0]: " + param1);
console.log("[+] type: " + param1.$className);
}
}
},
onLeave: function (retval) {
// console.log("[+] retval objc: " + CheckObjc(retval));
if (CheckObjc(retval)) {
printArg("retval: ", retval);
// var objcParam = ObjC.Object(retval);
// var objcType = objcParam.$className;
// if(objcType == "__NSCFString"){
// //objcParam = "https://res.exexm.com/cw_145225549855002";
// //retval.replace("https://video.dcgvc.com/video/m3u8/2023/03/27/6df8ee4e/diba.ts");
// }
}
LOG("*** exited " + name, { c: Color.Green });
// console.log("*** exiting " + name);
console.log("[-] ---------------------------------------------------------------\n");
}
});
}
// print helper
function printArg(desc, arg) {
try {
var objcParam = ObjC.Object(arg);
var objcType = objcParam.$className;
// [+] arg3: {length = 36, bytes = 0x37374131 30324232 2d323736 462d3435 ... 34333430 43313143 }
// [+] type: NSConcreteMutableData
// ==>
// [+] arg3: 77A102B2-276F-4542-8F33-0DF84340C11C
// [+] type: __NSCFString
if (desc.indexOf("arg") != -1) { // 区分参数与返回值着色
if (objcType == "NSConcreteMutableData" || objcType == "OS_dispatch_data" || objcType == "_NSInlineData" || objcType == "NSConcreteData") { // 将NSConcreteMutableData等类型转化为
LOG("[+] 参数为NSData 不显示", { c: Color.Gray });
// objcParam = objcParam.CKHexString();
// 将数据转换为字符串
// const jsonString = objcParam.toString();
// 非可见字符, 打印hex
// console.log("DATA Base64数据: "+base64(jsonString));
} else {
LOG("[+] " + desc + objcParam, { c: Color.Gray });
}
} else {
//LOG("[+] " + desc + objcParam, { c: Color.Cyan });
if (objcType == "NSConcreteMutableData" || objcType == "OS_dispatch_data" || objcType == "_NSInlineData" || objcType == "NSConcreteData") { // 将NSConcreteMutableData等类型转化为NSString打印
console.log("[返回值]" + objcType + " NSData 类型数据,不显示");
try {
// objcParam = NSData2NSString(objcParam); // 非可见字符不会报异常
if (objcParam.length() == 16 ){
// 获取 NSData 对象中的字节数组
//var bytes = Memory.readByteArray(objcParam.bytes(), objcParam.length());
//LOG("返回值解密16位IV打印hex ====== " + objcParam);
}else{
var newobjcParam = objcParam.bytes().readUtf8String(objcParam.length());
// let jsonResponse = JSON.parse(newobjcParam);
// console.log("NSData 返回值解密:" + jsonResponse);
//send(newobjcParam);
LOG("返回值解密长度:"+newobjcParam.length, { c: Color.Yellow });
if(newobjcParam.length>100){
send(newobjcParam);
}
}
} catch (e) {
LOG("解析ERROR: " + e, { c: Color.Red });
//加密随机IV
//if (objcParam.length() == 16 ){
// 转换为 Base64
// var encoder = new ObjC.Base64Encoder();
// encoder.write(bytes, bytes.length);
// var base64Str = encoder.finish();
//objcParam.bytes()
// var array = Memory.fromHex(objcParam.CKHexString()).readByteArray(objcParam.length());
// 将 NSInlineData 对象转换为 JavaScript 中的 Uint8Array 类型
//if(objcParam.$kind == "instance"){
// 计算 inline data 的开始地址
// var startAddr = ptr(objcParam.bytes().toString()).add(Process.pointerSize * 2); // 跳过 isa 和 flags 字段
// }
// console.log("=====Base64: " + base64encode(bin2string(array)));
// }
//打印hex:<9d452cf0 d6146289 8e82b85b dff7a311>
//const jsonString = objcParam.toString();
// objcParam = objcParam.CKHexString();
// // 将数据转换为字符串
// 非可见字符, 打印hex
// var array = new Uint8Array(Memory.readByteArray(objcParam.bytes(), Memory.readUInt(objcParam.length())));
// LOG("[+] 打印hex Base64后: " + base64encode(bin2string(array)), { c: Color.Yellow });
// console.log("加密Base64数据: "+base64(jsonString));
}
// var objcParam22 = NSData2NSString(objcParam); // 非可见字符不会报异常
//console.log(">> 解密数据: >>> "+nsstr(objcParam));
let jsonResponse;
try {
// const jsonString = objcParam.toString();
//jsonResponse = JSON.parse(jsonString);
//send("\t- Attributes : " + jsonString);
// send(jsonString);
// LOG(jsonString);
// 0x10212c800
// console.log("getclassmodule : " + getclassmodule("RCTCustomScrollView"));
// 通过对象地址获取对象
// const address = 0x10212c800/* 对象地址 */;
// const obj = getaddressmodule(address);
// console.log("scrollView: "+obj.class);
//scrolltobottom();
// var scrollview = 0x116178c00;
// 获取 Objective-C 运行时模块
// const objc = ObjC;
// // 通过对象地址获取对象
// const address = 0x1050f2c00; /* RCTCustomScrollView: 0x1050f2c00; 对象地址 */;
// const object = new ObjC.Object(address);
// // 将对象转换为 Objective-C 视图对象
// const view = new objc.classes.UIView(object);
// console.log(view);
} catch (e) {
console.error("Failed:", e);
// console.error(jsonString);
return;
}
/*// 解析 JSON 数据
let jsonResponse;
try {
jsonResponse = JSON.parse(jsonString);
//解析返回数据 //这里处理每个tab 类目的所有JSON信息
if (jsonResponse.code == 0 || jsonResponse.data != null){
const categoryName = jsonResponse.data.category_name;
const categoryId = jsonResponse.data.category_id;
const videos = jsonResponse.data.videos;
const next_start_offset = jsonResponse.data.next_start_offset;
console.log("categoryId : " + categoryId + " categoryName: " + categoryName);
if(nsstr(categoryId).length()>10){
//说明就是按照类目拉取的数据
const fileName = '/Users/nantian/Documents/yueyu/ios/frida_ios_android_script/frida-script-ios/dbjson/'+categoryId+'_'+next_start_offset+'.json';
console.log("Json写入文件"+fileName);
const outputStr = `${JSON.stringify(videos)}\n`;
// fs.writeFileSync(fileName, outputStr, { flag: 'a' });
// console.log("outputStr: "+outputStr);
const str = ObjC.classes.NSString.stringWithString_(outputStr);
// const nsdata = str.dataUsingEncoding_(4);
// nsdata.writeToFile_atomically_(fileName, true);
//send("message",categoryId,categoryName,next_start_offset,str);
//send(''+jsonString);
// send(objcParam);
// var obj = ObjC.Object(args[4]);
send("\t- Attributes : " + jsonString);
console.log("Json写入文件OK...");
}
}
} catch (e) {
console.error("Failed:", e);
return;
}
*/
} else {
LOG("[返回值] " + objcType + " " + desc + " " + objcParam, { c: Color.Cyan });
}
}
// console.log("[+] " + desc + objcParam);
console.log("[+] type: " + objcType);
} catch (err) {
console.log("error: " + desc + arg);
}
}
function base64Encode(data) {
var encoder = new Base64Encoder();
encoder.write(data.bytes(), data.length());
return encoder.finish();
}
function scrolltobottom() {
// 查找 UIScrollView 实例
// 导入 UIKit 库
var UIKit = ObjC.classes.UIKit;
// 导入 Foundation 库
var Foundation = ObjC.classes.Foundation;
var scrollView = ObjC.classes.UIScrollView.alloc().init();
// 获取当前的 UIWindow
var appCls = ObjC.classes["NSApplication"] || ObjC.classes["UIApplication"];
var window = appCls.sharedApplication().keyWindow();
// var controller = window.rootViewController;
// while (controller.presentedViewController) {
// controller = controller.presentedViewController;
// }
if (window != null) {
// 定义悬浮按钮类
class FloatingButton {
constructor(imageName, onClick) {
this.imageName = imageName;
this.onClick = onClick;
this.button = null;
}
// 创建并配置按钮
createButton() {
if (this.button == null) {
this.button = ObjC.class.UIButton.buttonWithType(ObjC.UIButtonTypeCustom);
//this.button.setImage(UIImage.imageNamed(this.imageName), UIControlStateNormal);
this.button.addTarget_action_forControlEvents_(this, ObjC.selector("handleClick:"), ObjC.UIControlEvents.TouchUpInside);
}
return this.button;
}
// 按钮点击事件处理函数
handleClick(sender) {
console.log("Floating button clicked!");
this.onClick(sender);
}
}
// 定义开始任务按钮类
class StartTaskButton extends FloatingButton {
constructor(onClick) {
super("start_task_button.png", onClick);
this.timer = null;
}
// 开始任务
startTask() {
this.stopTimer();
this.timer = ObjC.class.NSTimer.scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(1.0, this, ObjC.selector("findCurrentViewController"), null, true);
console.log("Task started.");
}
// 停止定时器
stopTimer() {
if (this.timer != null) {
this.timer.invalidate();
this.timer = null;
}
}
// 查找当前显示的视图控制器
findCurrentViewController() {
// var controller = UIApplication.sharedApplication().keyWindow.rootViewController;
// while (controller.presentedViewController) {
// controller = controller.presentedViewController;
// }
// console.log("Current view controller:", controller.class().toString());
}
}
// 定义暂停任务按钮类
class PauseTaskButton extends FloatingButton {
constructor(onClick) {
super("pause_task_button.png", onClick);
}
// 停止任务
stopTask() {
console.log("Task stopped.");
}
}
// 创建开始任务按钮和暂停任务按钮
var startTaskButton = new StartTaskButton(function (sender) {
startTaskButton.startTask();
});
var pauseTaskButton = new PauseTaskButton(function (sender) {
startTaskButton.stopTimer();
pauseTaskButton.stopTask();
});
// 添加开始任务按钮到窗口上
// var button = ObjC.classes.UIButton.buttonWithType(ObjC.UIButtonTypeCustom);
var defaultManager = ObjC.classes.NSFileManager.defaultManager();
//this.button.setImage(UIImage.imageNamed(this.imageName), UIControlStateNormal);
//button.addTarget_action_forControlEvents_(this, ObjC.selector("handleClick:"), ObjC.UIControlEvents.TouchUpInside);
console.log("button: "+defaultManager);
// var startButton = startTaskButton.createButton();
//startButton.frame = CGRectMake(
// UIScreen.mainScreen().bounds.size.width - 80,
// (UIScreen.mainScreen().bounds.size.height / 2) - 40,
// 60, 80);
//console.log("button: "+button);
//var window = UIApplication.sharedApplication().keyWindow;
// window.addSubview(startButton);
// // 添加暂停任务按钮到窗口上
// var pauseButton = pauseTaskButton.createButton();
// pauseButton.frame = CGRectMake(
// UIScreen.mainScreen().bounds.size.width - 80,
// (UIScreen.mainScreen().bounds.size.height / 2) + 20,
// 60, 80);
// window.addSubview(pauseButton);
}
/*
// console.log("subviews(): "+window.subviews());
if (scrollView != undefined ) {
var contentSize = scrollView.contentSize();
// ...
// 自动滚动 UIScrollView 到底部
var contentSize = scrollView.contentSize();
if(contentSize.height > 0){