首页
社区
课程
招聘
[原创]Unity逆向获取组件通杀方案
2022-7-11 02:30 27581

[原创]Unity逆向获取组件通杀方案

2022-7-11 02:30
27581

需求起因

由于需要对Adnroid手游游合规检测,即需要识别游戏是否展示“游戏协议声明”,因此需要提取游戏组件内相关原生组件文本。
由于目前Android手游主要以Unity引擎为主,因此首先尝试切入Unity引擎,获取原生组件通杀方案。

实现思路

首先Unity开发语言是C#,
但是由于起初C#生态并没有跨平台支持(真的瓜这决策...),然而Unity又需要跨平台,因此早年间,为了兼容C#和跨平台需求,Unity自己重写了一套C#解释引擎,并且完全支持原生C#语法。
C#作为虚拟机语言,原生是提供了“反射”功能,所以Unity引擎作为C#解释器,也必然支持反射功能,无论是使用mono编译还是il2cpp编译,他都会支持“反射”。

 

因此我们的突破点,便是这个“反射”。
使用反射,即可获取C#中任何类、字段、属性、方法等所有信息,因此也可以获取Unity组件信息,获取到组件实例信息,就可以对其做任何读写操作

 

同时反射机制,相对于使用其他的hook机制,反射可以为我们带来更强的通用性和版本兼容性。

实现案例

案例前言

由于目前很多游戏考虑到安全性问题,一般都使用相对安全点的“il2cpp”编译模式,因此接下来的案例,也是在il2cpp基础上做分析。

 

il2cpp是将C# IL(.net平台一种中间语言)转为C++并编译为native,因此这种模式下,反射接口和直接使用C#的反射接口使用方式不太一样,但是大致流程还是差不多。

 

所以接下来,主要是要找到和整理native模式下,函数的定位以及组合规则。

获取TextMeshProGUI组件内的文本信息

注意:以下有些备注可能不准确,可以作为意会和参考,因为确实有点词穷(理解不够深........),
另外以是伪代码,着重描述核心的底层接口脉络,详细代码,会放在最后。

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
// 假定一个TextMeshProGUI实例对象
let text_mesh_pro_gui_instance;
 
// 获取进程域
// **核心函数:il2cpp_domain_get**
let domain = il2cpp_domain_get()
 
// 获取所有程序集。类似C#获取所有动态链接库(.dll)反射信息
// **核心函数:il2cpp_domain_get_assemblies**
let assmblies = il2cpp_domain_get_assemblies(domain)
let assembly_images=[];
for(let i=0;i<assmblies.size;i++){
    assm = assmblies[i];
    // **核心函数:il2cpp_assembly_get_image**
    images.add(il2cpp_assembly_get_image(assm))
}
 
// 获取指定程序集反射image对象
// **核心函数:il2cpp_image_get_name**
let target_assembly_image = assembly_images
    .filter(assembly_img=>il2cpp_image_get_name(assembly_img)=="Unity.TextMeshPro.dll")
    .pop()
 
// 通过C#内限定名,获取对象class反射信息
// TextMeshProGUI类型组件限定名是"TMPro.TextMeshProGUI",并属于"Unity.TextMeshPro.dll"程序集
// **核心函数:il2cpp_class_from_name**
let TextMeshPro_class = il2cpp_class_from_name(target_assembly_image,"TMPro","TextMeshProGUI")
 
// 获取文本字段反射信息
// TextMeshProGUI存储文本的字段是“m_text”
// **核心函数:il2cpp_class_get_field_from_name**
m_text_field = il2cpp_class_get_field_from_name(TextMeshPro_class,"m_text")
 
// 读取字段内容实例,实例是个结构体
// **核心函数:il2cpp_field_get_value_object**
let str_content_struct = il2cpp_field_get_value_object(m_text_field,text_mesh_pro_gui_instance);
 
// 读取结构体内字符串内容,这里就是获取最终C版本的字符串
// **核心函数:func_il2cpp_string_chars**
content_chars = func_il2cpp_string_chars(str_content_struct)

以上就是读取一个组件,所涉及的核心接口,以及组合方式。
当然不同组件,还会有一些其他接口涉及,但是也是依葫芦画瓢,根据函数名字大概就能推断出什么含义。

应用扩展

既然能获取所有实例对象和信息,并且具备读写能力,那么理论上,可以基于这套思路完成无源码修改游戏,以及基于组件级的脚本点击...............
另外需要提取游戏内资源,此方案也可以绕过大部分加固......

一些想法

这个方案是完全依赖多个关键函数的symbol,目前听说(没实际了解)一些加固厂商,会将这些symbol抹除,如果抹除就通过函数特诊码搜索函数位置,但是这样兼容性有个问题,准确性也是个问题.......

压缩包中相关资源说明

更多il2cpp反射api定义,在il2cpp-api-functions.h

 

il2cpp的结构体定义在il2cpp-class.h

 

完整源码项目文件是_agent.js
_agent.js中的UnityApi类,封装了一部分反射api。

 

_agent.js中的Example类是案例:
图片描述

鸣谢

Zygisk-Il2CppDumper

通过Zygisk-Il2CppDumper源码,偶然间看到相关代码,才获得灵感进一步深挖“反射”这条思路

完整项目源码

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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
/**
 * android框架的so接口
 */
class AndroidApi {
    static #_self;
 
    /**
     * @returns {AndroidApi}
     */
    static get Api() {
        if (!this.#_self) {
            this.#_self = new AndroidApi();
        }
 
        return this.#_self;
    }
 
    /**
     * @param {InvocationListenerCallbacks} callback
     * @constructor
     */
    AttachDlopen(callback) {
        let android_dlopen_ext = Module.findExportByName(null, "android_dlopen_ext");
        if (android_dlopen_ext != null) {
            Interceptor.attach(android_dlopen_ext, callback);
            console.log("找到了 android_dlopen_ext");
        } else {
            console.error("未找到 android_dlopen_ext");
        }
 
        let dlopen = Module.findExportByName(null, "dlopen");
        if (dlopen != null) {
            Interceptor.attach(dlopen, callback);
            console.log("找到了 dlopen");
        } else {
            console.error("未找到 dlopen");
        }
    }
 
 
 
}
 
class UCommonUtil {
    static #_self;
 
    /**
     * @return {UCommonUtil}
     */
    static get Api() {
        if (this.#_self == null) {
            this.#_self = new UCommonUtil();
        }
        return this.#_self;
    }
 
    /**
     * 获取il2cpp里的迭代器
     * @return {NativePointer}
     */
    GetEmptyIter() {
        let iter = Memory.alloc(Process.pointerSize);
        iter.writePointer(NULL);
 
        return iter;
    }
}
 
class CommonUtil {
    static #_self;
 
    /**
     * @return {CommonUtil}
     */
    static get Api() {
        if (this.#_self == null) {
            this.#_self = new CommonUtil();
        }
        return this.#_self;
    }
 
    /**
     * 创建NativeFunction封装,用于少写两行代码,方便使用
     * @returns {NativeFunction}
     */
    NewNativeFunction(module, exportName, retType, argTypes) {
        let ptr = module.findExportByName(exportName);
        if (ptr == null) {
            console.error(`没有找到【${exportName}】`);
            return null;
        }
 
        console.log(`成功找到【${exportName}】`);
 
        return new NativeFunction(ptr, retType, argTypes);
    }
}
 
/**
 * unity il2cpp中的so接口
 */
 
class UnityApi {
    static #_il2cppName = "libil2cpp.so";
 
    static #_self;
 
    /**
     * @type {Module}
     */
    #_il2cppMoudle;
 
    #func_il2cpp_get_corlib = null;
 
    // class
    #func_il2cpp_class_from_name = null;
    #func_il2cpp_class_get_method_from_name = null;
    #func_il2cpp_class_get_namespace = null;
    #func_il2cpp_class_get_name = null;
    #func_il2cpp_class_get_type = null;
    #func_il2cpp_class_from_type = null;
    #func_il2cpp_class_get_field_from_name = null;
    #func_il2cpp_class_get_fields = null;
    #func_il2cpp_class_get_properties = null;
    #func_il2cpp_class_get_property_from_name = null;
    #func_il2cpp_class_get_image = null;
 
    // domain
    #func_il2cpp_domain_get = null;
    #func_il2cpp_domain_get_assemblies = null;
 
    // assembly
    #func_il2cpp_assembly_get_image = null;
 
    // image
    #func_il2cpp_image_get_name = null;
    #func_il2cpp_image_get_class_count = null;
    #func_il2cpp_image_get_class = null;
 
    // runtime
    #func_il2cpp_runtime_invoke = null;
 
    // type
    #func_il2cpp_type_get_type = null;
    #func_il2cpp_type_get_object = null;
    #func_il2cpp_type_get_class_or_element_class = null;
 
    // field
    #func_il2cpp_field_get_value_object = null;
    #func_il2cpp_field_get_name = null;
    #func_il2cpp_field_get_type = null;
 
    // property
    #func_il2cpp_property_get_get_method = null;
    #func_il2cpp_property_get_name = null;
 
    // object
    #func_il2cpp_object_get_size = null;
    #func_il2cpp_object_get_class = null;
 
    // string
    #func_il2cpp_string_chars = null;
 
    constructor() {
        this.#init();
    }
 
    /**
     * 由于Unity Il2cpp相关接口,需要在il2cpp处加载后,
     * 才能正常发现,所以需要先在合适的地方初始化,再后续使用
     * @constructor
     */
    static Init() {
        if (!this.#_self) {
            this.#_self = new UnityApi();
        }
    }
 
    /**
     * 静态暴露接口
     * @returns {UnityApi}
     */
    static get Api() {
        if (!this.#_self) {
            throw "UnityApi还未初始化"
        }
        return this.#_self;
    }
 
    //-----------------------------------------------------【API】-------------------------------------------------------------//
 
    /**
     * @return {NativePointer}
     */
    il2cpp_get_corlib() {
        return this.#func_il2cpp_get_corlib();
    }
 
    /**
     * 根据类的限定名,获取Type信息
     * @param image_p .dll基址
     * @param namespace 命名空间
     * @param name 类名
     * @returns {NativePointer}
     */
    il2cpp_class_from_name(image_p, namespace, name) {
        let ns = Memory.allocUtf8String(namespace);
        let className = Memory.allocUtf8String(name);
        return this.#func_il2cpp_class_from_name(image_p, ns, className);
    }
 
    /**
     * 通过class获取type
     * @param {NativePointer} klass
     * @return {NativePointer}
     */
    il2cpp_class_get_type(klass) {
        return this.#func_il2cpp_class_get_type(klass);
    }
 
    /**
     * 反射获取指定方法
     * @param {NativePointer} klass class句柄
     * @param {string} methodName 指定方法名
     * @param {number} argCount 参数个数
     * @return {NativePointer}
     */
    il2cpp_class_get_method_from_name(klass, methodName, argCount) {
        let methodNameP = Memory.allocUtf8String(methodName);
        return this.#func_il2cpp_class_get_method_from_name(klass, methodNameP, argCount);
    }
 
    /**
     * 获取class下所有字段
     * @param {NativePointer} klass
     * @return {Generator<*, void, *>}
     */
    * il2cpp_class_get_fields(klass) {
        let iter = UCommonUtil.Api.GetEmptyIter();
        while (true) {
            let result = this.#func_il2cpp_class_get_fields(klass, iter);
 
            if (result.valueOf() == 0) {
                break;
            }
 
            yield  result;
        }
    }
 
    /**
     * 获取进程域
     * @returns {NativePointer}
     */
    il2cpp_domain_get() {
        return this.#func_il2cpp_domain_get();
    }
 
    /**
     * 获取所有程序集的基址
     * @param domain
     * @return {{size:number,assemblies:any}} .dll数组(const Il2CppAssembly**)
     */
    il2cpp_domain_get_assemblies(domain) {
        let size = Memory.alloc(4);
        size.writeInt(0);
        let assemblies = this.#func_il2cpp_domain_get_assemblies(domain, size);
        return {
            size: size.readInt(),
            assemblies: assemblies
        }
    }
 
    /**
     * 获取.dll的句柄
     * @param assembly .dll信息指针
     * @return {NativePointer} .dll句柄
     */
    il2cpp_assembly_get_image(assembly) {
        return this.#func_il2cpp_assembly_get_image(assembly);
    }
 
    /**
     * 获取image的name
     * @param {NativePointer} image
     * @return {string} image的名字
     */
    il2cpp_image_get_name(image) {
        let result = this.#func_il2cpp_image_get_name(image);
        return result.readPointer().readCString();
    }
 
    /**
     * 获取.dll下有多少个class
     * @param {NativePointer} image .dll句柄
     * @return {number} class个数
     */
    il2cpp_image_get_class_count(image) {
        let result = this.#func_il2cpp_image_get_class_count(image);
        return result;
    }
 
    /**
     * 获取class
     * @param {NativePointer} image
     * @param {number} index
     * @return {NativePointer}
     */
    il2cpp_image_get_class(image, index) {
        return this.#func_il2cpp_image_get_class(image, index);
    }
 
    /**
     * 通过class获取作用域名
     * @param {NativePointer} klass
     * @return {string};
     */
    il2cpp_class_get_namespace(klass) {
        let result = this.#func_il2cpp_class_get_namespace(klass);
        let ns = ptr(result).readCString();
        return ns;
    }
 
    /**
     * 通过class获取名字
     * @param {NativePointer} klass
     * @return {string}
     */
    il2cpp_class_get_name(klass) {
        let result = this.#func_il2cpp_class_get_name(klass);
        return ptr(result).readCString();
    }
 
    /**
     * @param {NativePointer} klass
     * @return {NativePointer}
     */
    il2cpp_class_get_image(klass) {
        return this.#func_il2cpp_class_get_image(klass);
    }
 
    /**
     * 反射调用方法
     * @param {NativePointer} methodInfo 反射方法对象
     * @param {NativePointer} obj 方法所属实例,static为NULL
     * @param {NativePointer} params 参数数组
     * @param {NativePointer} exception 报错
     * @return {NativePointer}
     */
    il2cpp_runtime_invoke(methodInfo, obj, params, exception) {
        return this.#func_il2cpp_runtime_invoke(methodInfo, obj, params, exception);
    }
 
    /**
     * 获取class的TypeInfo信息
     * @param {NativePointer} type
     * @return {NativePointer}
     */
    il2cpp_class_from_type(type) {
        return this.#func_il2cpp_class_from_type(type);
    }
 
    /**
     * @param {NativePointer} klass
     * @param {string} name
     * @return {NativePointer}
     */
    il2cpp_class_get_field_from_name(klass, name) {
        let nameP = Memory.allocUtf8String(name);
        return this.#func_il2cpp_class_get_field_from_name(klass, nameP);
    }
 
    /**
     * 读取字段的名字
     * @param {NativePointer} field
     * @return {string}
     */
    il2cpp_field_get_name(field) {
        let result = this.#func_il2cpp_field_get_name(field);
        return ptr(result).readCString();
    }
 
    /**
     * @param {NativePointer} field
     * @return {NativePointer}
     */
    il2cpp_field_get_type(field) {
        return this.#func_il2cpp_field_get_type(field);
    }
 
    /**
     * 获取class所有属性
     * @param {NativePointer} klass
     * @return {*}
     */
    * il2cpp_class_get_properties(klass) {
        let iter = UCommonUtil.Api.GetEmptyIter();
        while (true) {
            let result = this.#func_il2cpp_class_get_properties(klass, iter);
 
            if (result.valueOf() == 0) {
                break;
            }
 
            yield  result;
        }
    }
 
 
    /**
     * 通过名字,获取class指定属性
     * @param {NativePointer} klass
     * @param {string} name
     * @return {NativePointer}
     */
    il2cpp_class_get_property_from_name(klass, name) {
        let nameP = Memory.allocUtf8String(name);
        return this.#func_il2cpp_class_get_property_from_name(klass, nameP);
    }
 
    /**
     * @param {NativePointer} type
     * @return {number}
     */
    il2cpp_type_get_type(type) {
        return this.#func_il2cpp_type_get_type(type);
    }
 
    /**
     * 根据反射信息,创建Type对象(即C#中反射的核心对象类)
     * @param {NativePointer} type
     * @return {NativePointer}
     */
    il2cpp_type_get_object(type) {
        return this.#func_il2cpp_type_get_object(type);
    }
 
    /**
     * @param {NativePointer} type
     * @return {NativePointer}
     */
    il2cpp_type_get_class_or_element_class(type) {
        return this.#func_il2cpp_type_get_class_or_element_class(type);
    }
 
    /**
     * 反射读取字段值
     * @param {NativePointer} field
     * @param {NativePointer} obj
     * @return {NativePointer}
     */
    il2cpp_field_get_value_object(field, obj) {
        return this.#func_il2cpp_field_get_value_object(field, obj);
    }
 
    /**
     * 获取属性的get方法(及读取属性值行为)
     * @param {NativePointer} prop
     * @return {NativePointer}
     */
    il2cpp_property_get_get_method(prop) {
        return this.#func_il2cpp_property_get_get_method(prop);
    }
 
    /**
     * 获取属性名字
     * @param {NativePointer} prop
     * @return {NativePointer}
     */
    il2cpp_property_get_name(prop) {
        let result = this.#func_il2cpp_property_get_name(prop);
        return ptr(result).readCString();
    }
 
    /**
     * 获取对象大小
     * @param {NativePointer} obj
     * @return {number}
     */
    il2cpp_object_get_size(obj) {
        return this.#func_il2cpp_object_get_size(obj);
    }
 
    /**
     * @param {NativePointer} obj
     * @return {NativePointer}
     */
    il2cpp_object_get_class(obj) {
        return this.#func_il2cpp_object_get_class(obj);
    }
 
    /**
     * @param {NativePointer} str
     * @return {string}
     */
    func_il2cpp_string_chars(str) {
        let result = this.#func_il2cpp_string_chars(str);
        return ptr(result).readUtf16String();
    }
 
    //-----------------------------------------------------【Attach】-------------------------------------------------------------//
 
    /**
     * @param {InvocationListenerCallbacks} callback
     */
    Attach_il2cpp_get_corlib(callback) {
        let func = this.#_il2cppMoudle.findExportByName("il2cpp_get_corlib");
        Interceptor.attach(func, callback);
    }
 
    /**
     * @param {InvocationListenerCallbacks} callback
     */
    Attach_il2cpp_class_from_name(callback) {
        let func = this.#_il2cppMoudle.findExportByName("il2cpp_class_from_name");
        Interceptor.attach(func, callback);
    }
 
 
    /**
     * 初始化so函数
     */
    #init() {
        this.#_il2cppMoudle = Process.findModuleByName(UnityApi.#_il2cppName);
 
        if (!this.#_il2cppMoudle) {
            console.error("未发现il2cppModule");
            return;
        }
        // ---------------------------------------------------------------------------------//
 
        this.#func_il2cpp_get_corlib = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_get_corlib",
            "pointer",
            []);
 
        // class
        this.#func_il2cpp_class_from_name = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_class_from_name",
            "pointer",
            ["pointer", "pointer", "pointer"]);
        this.#func_il2cpp_class_get_method_from_name = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_class_get_method_from_name",
            "pointer",
            ["pointer", "pointer", "int"]);
        this.#func_il2cpp_class_get_namespace = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_class_get_namespace",
            "pointer",
            ["pointer"]);
        this.#func_il2cpp_class_get_name = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_class_get_name",
            "pointer",
            ["pointer"]);
        this.#func_il2cpp_class_get_type = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_class_get_type",
            "pointer",
            ["pointer"]);
        this.#func_il2cpp_class_from_type = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_class_from_type",
            "pointer",
            ["pointer"]);
        this.#func_il2cpp_class_get_field_from_name = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_class_get_field_from_name",
            "pointer",
            ["pointer", "pointer"]);
        this.#func_il2cpp_class_get_fields = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_class_get_fields",
            "pointer",
            ["pointer", "pointer"]);
        this.#func_il2cpp_class_get_properties = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_class_get_properties",
            "pointer",
            ["pointer", "pointer"]);
        this.#func_il2cpp_class_get_property_from_name = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_class_get_property_from_name",
            "pointer",
            ["pointer", "pointer"]);
        this.#func_il2cpp_object_get_class = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_object_get_class",
            "pointer",
            ["pointer"]);
        this.#func_il2cpp_class_get_image = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_class_get_image",
            "pointer",
            ["pointer"]);
 
        // domain
        this.#func_il2cpp_domain_get = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_domain_get",
            "pointer",
            []);
        this.#func_il2cpp_domain_get_assemblies = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_domain_get_assemblies",
            "pointer",
            ["pointer", "pointer"]);
 
        // assembly
        this.#func_il2cpp_assembly_get_image = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_assembly_get_image",
            "pointer",
            ["pointer"]);
 
        // image
        this.#func_il2cpp_image_get_name = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_image_get_name",
            "pointer",
            ["pointer"]);
        this.#func_il2cpp_image_get_class_count = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_image_get_class_count",
            "uint32",
            ["pointer"]);
        this.#func_il2cpp_image_get_class = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_image_get_class",
            "pointer",
            ["pointer", "uint32"]);
 
        // runtime
        this.#func_il2cpp_runtime_invoke = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_runtime_invoke",
            "pointer",
            ["pointer", "pointer", "pointer", "pointer"]);
 
        // type
        this.#func_il2cpp_type_get_type = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_type_get_type",
            "int",
            ["pointer"]);
        this.#func_il2cpp_type_get_object = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_type_get_object",
            "pointer",
            ["pointer"]);
        this.#func_il2cpp_field_get_type = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_field_get_type",
            "pointer",
            ["pointer"]);
        this.#func_il2cpp_type_get_class_or_element_class = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_type_get_class_or_element_class",
            "pointer",
            ["pointer"]);
 
        // field
        this.#func_il2cpp_field_get_value_object = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_field_get_value_object",
            "pointer",
            ["pointer", "pointer"]);
        this.#func_il2cpp_field_get_name = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_field_get_name",
            "pointer",
            ["pointer"]);
 
        // property
        this.#func_il2cpp_property_get_get_method = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_property_get_get_method",
            "pointer",
            ["pointer"]);
        this.#func_il2cpp_property_get_name = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_property_get_name",
            "pointer",
            ["pointer"]);
 
        // object
        this.#func_il2cpp_object_get_size = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_object_get_size",
            "uint32",
            ["pointer"]);
 
        // string
        this.#func_il2cpp_string_chars = CommonUtil.Api.NewNativeFunction(this.#_il2cppMoudle, "il2cpp_string_chars",
            "pointer",
            ["pointer"]);
        // ---------------------------------------------------------------------------------//
    }
 
 
}
 
/**
 * 懒加载包装类
 */
class Lazy {
    #_value = null;
 
    /**
     * @type {function ():*} value
     */
    #_lazyValue = null;
 
    /**
     * @param {*} this上下文
     * @param {function ():*} value 实例化对象的方法回调
     * @return {Lazy}
     */
    static New(context,value) {
        let lazy = new Lazy();
        lazy.Value = value.bind(context);
        return lazy;
    }
 
    /**
     * @param {function ():*} value
     */
    set Value(value) {
        this.#_lazyValue = value;
    }
 
    /**
     * @return {*}
     */
    get Value() {
        if (this.#_value == null) {
            this.#_value = this.#_lazyValue();
        }
 
        return this.#_value;
    }
}
 
class UMethodInfo {
    /**
     * @type {Lazy}
     */
    #_methodInfo = null;
 
    /**
     * @type {UnityApi}
     */
    #_unityApi = UnityApi.Api;
 
    /**
     * @param {
     *     {
     *         kalss:NativePointer,
     *         methodName:string
     *         argCount:number
     *     }
     * } input
     */
    constructor(input) {
        this.#_methodInfo = Lazy.New(this,() => {
            return this.#_unityApi.il2cpp_class_get_method_from_name(input.kalss,
                input.methodName,
                input.argCount);
        });
    }
 
    /**
     *
     * @param {NativePointer} args 参数数组
     * @param {NativePointer} obj 实例对象
     * @return {(NativePointer | *)}
     * @constructor
     */
    Invoke(obj, args) {
        let result = this.#_unityApi.il2cpp_runtime_invoke(this.#_methodInfo.Value,
            obj, args, NULL);
 
        return result;
    }
}
 
class UDomain {
    static #_self;
 
    /**
     * @type {NativePointer}
     */
    #_domain = null;
    /**
     * @type {{size:number,assemblies:any}}
     */
    #_assmblies = null;
    /**
     * @type {NativePointer[]}
     */
    #_assemblyImages = null;
 
    /**
     * @type {UnityApi}
     */
    #_unityApi = null;
 
    constructor() {
        this.#_unityApi = UnityApi.Api;
 
        this.#LoadDomain();
        this.#LoadAssemblies();
        this.#LoadAssemblyImages();
    }
 
    /**
     * @return {UDomain}
     */
    static get Api() {
        if (UDomain.#_self == null) {
            UDomain.#_self = new UDomain();
        }
 
        return UDomain.#_self;
    }
 
    /**
     * @return {NativePointer[]}
     */
    get AllAssemblyImages() {
        return this.#_assemblyImages;
    }
 
    /**
     * @param {string} name
     * @return {NativePointer}
     */
    GetAssemblyImageByName(name) {
        return this.#_assemblyImages
            .filter(image => this.#_unityApi.il2cpp_image_get_name(image) == name)
            .pop()
            .readPointer();
    }
 
    #LoadDomain() {
        this.#_domain = this.#_unityApi.il2cpp_domain_get();
    }
 
    #LoadAssemblies() {
        this.#_assmblies = this.#_unityApi.il2cpp_domain_get_assemblies(this.#_domain);
    }
 
    #LoadAssemblyImages() {
        let images = [...Array(this.#_assmblies.size).keys()]
            .map(value => ptr(this.#_assmblies.assemblies).add(Process.pointerSize * value))
            .map(assm => this.#_unityApi.il2cpp_assembly_get_image(assm));
 
        this.#_assemblyImages = images;
    }
}
 
/**
 * @readonly
 * @enum {string}
 */
const AssemblyName={
    UnityEngine:"UnityEngine.dll",
    UnityEngineUI:"UnityEngine.UI.dll",
    UnityTextMeshPro:"Unity.TextMeshPro.dll",
    UnityEngineCoreModule:"UnityEngine.CoreModule.dll"
};
 
class UnityUtil {
    static #_self = null;
 
    /**
     * @return {UnityUtil}
     */
    static get Api() {
        if (UnityUtil.#_self == null) {
            UnityUtil.#_self = new UnityUtil();
        }
 
        return UnityUtil.#_self;
    }
 
    /**
     * 获取Class
     * @param {
     *     {
     *         domain:UDomain,
     *         assemblyName:string,
     *         namespace:string,
     *         klassName:string,
     *     }
     * } input
     * @return {NativePointer}
     */
    GetClass(input) {
        let assemblyImage = input.domain.GetAssemblyImageByName(input.assemblyName);
        let klass = UnityApi.Api.il2cpp_class_from_name(assemblyImage, input.namespace, input.klassName);
        return klass;
    }
 
    /**
     * 获取指定类的Type实例
     * @param {
     *     {
     *         klass:NativePointer
     *     }
     * } input
     * @return {NativePointer}
     */
    GetTypeObj(input) {
        let type = UnityApi.Api.il2cpp_class_get_type(input.klass);
        let typeObj = UnityApi.Api.il2cpp_type_get_object(type);
        return typeObj;
    }
 
    /**
     * 反射读取字符串类型字段的值
     * @param {NativePointer} field
     * @param {NativePointer} obj
     * @return {string}
     */
    ReadFieldWithString(field, obj) {
        let valueObj = UnityApi.Api.il2cpp_field_get_value_object(field, obj);
        let valueReal = UnityApi.Api.func_il2cpp_string_chars(valueObj);
        return valueReal;
    }
 
    /**
     * 反射读取字符串类型属性值
     * @param {NativePointer} property
     * @param {NativePointer} obj
     */
    ReadPropertyWithString(property, obj) {
        let getFunc = UnityApi.Api.il2cpp_property_get_get_method(property);
        let valueObj = UnityApi.Api.il2cpp_runtime_invoke(getFunc, obj, NULL, NULL);
        let valueReal = UnityApi.Api.func_il2cpp_string_chars(valueObj);
        return valueReal;
    }
 
}
 
class ReflectionInfoInput {
    dllName;
    namespace;
    className;
    constructor(dllName,namespace,className) {
        this.dllName = dllName;
        this.namespace = namespace;
        this.className = className;
    }
}
 
class ReflectionBase {
    get UApi(){
        return UnityApi.Api;
    }
 
    /**
     *
     * @return {function(): UDomain}
     * @constructor
     */
    get Domain(){
        return UDomain.Api;
    }
}
 
class UReflectionInfo extends ReflectionBase {
    /**
     * @type {ReflectionInfoInput}
     */
    #_input = null;
 
    /**
     * 实例对象
     * @type {NativePointer}
     */
    #_instanceObj = null;
 
    /**
     * @type {Lazy}
     */
    #_assemblyImage = null;
 
    /**
     * @type {Lazy}
     */
    #_class = null;
 
    /**
     * @type {Lazy}
     */
    #_type = null;
 
    /**
     * @type {Lazy}
     */
    #_fields = null;
 
    /**
     * @type {Lazy}
     */
    #_properties = null;
 
    /**
     * @param {ReflectionInfoInput | NativePointer} input
     */
    constructor(input) {
        super();
 
        if (input instanceof ReflectionInfoInput) {
            this.#_input = input;
        }
 
        if (input instanceof NativePointer) {
            this.#_instanceObj = input;
        }
 
        this.#LoadInit();
    }
 
    /**
     * @return {NativePointer}
     */
    get Class() {
        return this.#_class.Value;
    }
 
    get Type() {
        return this.#_type.Value;
    }
 
    get Fields() {
        return this.#_fields.Value;
    }
 
    get Properties() {
        return this.#_properties.Value;
    }
 
    PropertyByName(name) {
        return this.UApi.il2cpp_class_get_property_from_name(this.Class, name);
    }
 
    /**
     * @param {string} name
     * @return {NativePointer}
     */
    FieldByName(name) {
        return this.UApi.il2cpp_class_get_field_from_name(this.Class, name);
    }
 
    /**
     * @param name 方法名字
     * @param argCount 方法个数
     * @return {UMethodInfo}
     */
    MethodByName(name, argCount) {
        return new UMethodInfo({
            kalss: this.#_class.Value,
            methodName: name,
            argCount: argCount
        })
    }
 
 
    #LoadInit() {
        this.#Switch({
            objNotNull: this.#LoadClassByObj,
            inputNotNull: this.#LoadClassByName
        });
 
        this.#LoadAssemblyImage();
        this.#LoadType();
        this.#LoadFields();
        this.#LoadProperties();
    }
 
    /**
     * @param {
     *     {
     *         objNotNull:function ():*,
     *         inputNotNull:function ():*
     *     }
     * } input
     * @return {*}
     */
    #Switch(input) {
        if (this.#_instanceObj != null) {
            return input.objNotNull.call(this);
        }
 
        if (this.#_input != null) {
            return input.inputNotNull.call(this);
        }
    }
 
    #LoadAssemblyImage() {
        this.#_assemblyImage = this.#Switch({
            objNotNull: () => Lazy.New(this, () => this.UApi.il2cpp_class_get_image(this.Class)),
            inputNotNull: () => Lazy.New(this, () => this.Domain.GetAssemblyImageByName(this.#_input.dllName)),
        });
    }
 
    #LoadClassByObj() {
        this.#_class = Lazy.New(this, () => this.UApi.il2cpp_object_get_class(this.#_instanceObj));
    }
 
    #LoadClassByName() {
        this.#_class = Lazy.New(this, () => this.UApi.il2cpp_class_from_name(this.#_assemblyImage.Value,
            this.#_input.namespace,
            this.#_input.className)
        );
    }
 
    #LoadType() {
        this.#_type = Lazy.New(this, () => {
            let type = this.UApi.il2cpp_class_get_type(this.Class);
            return this.UApi.il2cpp_type_get_object(type);
        });
    }
 
    #LoadFields() {
        this.#_fields = Lazy.New(this, () => [...this.UApi.il2cpp_class_get_fields(this.Class)]);
    }
 
    #LoadProperties() {
        this.#_properties = Lazy.New(this, () => [...this.UApi.il2cpp_class_get_properties(this.Class)]);
    }
}
 
/*
    类型反射信息定义
    (在此处定义好类型的反射信息,需要某类型反射信息可方便使用)
 */
class UTypeDefinition {
    static #_self;
 
    #_u_object = new UReflectionInfo(new ReflectionInfoInput(
        AssemblyName.UnityEngine,
        "UnityEngine",
        "Object"));
 
    #_u_Behaviour = new UReflectionInfo(new ReflectionInfoInput(
        AssemblyName.UnityEngine,
        "UnityEngine",
        "Behaviour"
    ));
 
    #_u_UI_Text = new UReflectionInfo(new ReflectionInfoInput(
        AssemblyName.UnityEngineUI,
        "UnityEngine.UI",
        "Text"
    ));
 
    #_u_TextMeshProUGUI = new UReflectionInfo(new ReflectionInfoInput(
        AssemblyName.UnityTextMeshPro,
        "TMPro",
        "TextMeshProUGUI"
    ));
 
    #_u_Resources = new UReflectionInfo(new ReflectionInfoInput(
        AssemblyName.UnityEngineCoreModule,
        "UnityEngine",
        "Resources"
    ));
 
    /**
     * @return {UTypeDefinition}
     */
    static get Api() {
        if (UTypeDefinition.#_self == null) {
            UTypeDefinition.#_self = new UTypeDefinition();
        }
        return UTypeDefinition.#_self;
    }
 
    /**
     * @return {UReflectionInfo}
     */
    get U_Object() {
        return this.#_u_object;
    }
 
    /**
     * @return {UReflectionInfo}
     */
    get U_Behaviour() {
        return this.#_u_Behaviour;
    }
 
    /**
     * @return {UReflectionInfo}
     */
    get U_UI_Text() {
        return this.#_u_UI_Text;
    }
 
    /**
     * @return {UReflectionInfo}
     */
    get U_TextMeshProUGUI() {
        return this.#_u_TextMeshProUGUI;
    }
 
    /**
     * @return {UReflectionInfo}
     */
    get U_Resources() {
        return this.#_u_Resources;
    }
}
 
class UObject {
    /**
     * @type {Domain}
     */
     #_domain = UDomain.Api;
 
    /**
     * @type {UnityApi}
     */
    #_unityApi = UnityApi.Api;
 
    /**
     * @type {NativePointer}
     */
    #_obj = null;
 
    /**
     * 实例对象的class
     * @type {UReflectionInfo}
     */
    #_instanceReflection = null;
 
    /**
     * @param {
     *     {
     *         obj:NativePointer,
     *     }
     * } input
     */
    constructor(input) {
        this.#_obj = input.obj;
        this.#LoadInstanceKlass(input.obj);
    }
 
    /**
     * 获取组件自定义ID名字
     * @return {string}
     */
    get IDName() {
        let property = this.#_unityApi.il2cpp_class_get_property_from_name(UTypeDefinition.Api.U_Object.Class, "name");
        let name = UnityUtil.Api.ReadPropertyWithString(property, this.#_obj);
        return name;
    }
 
    /**
     * 获取类型限定名字
     * @return {string}
     */
    get TypeName() {
        let namespace = this.#_unityApi.il2cpp_class_get_namespace(this.#_instanceReflection.Class);
        let className = this.#_unityApi.il2cpp_class_get_name(this.#_instanceReflection.Class);
        return `${namespace}.${className}`;
    }
 
    /**
     * @return {UReflectionInfo}
     */
    get InstanceReflection() {
        return this.#_instanceReflection;
    }
 
    /**
     * @return {UnityApi}
     */
    get UApi() {
        return this.#_unityApi;
    }
 
    /**
     * @return {Domain}
     */
    get Domain() {
        return this.#_domain;
    }
 
    /**
     * @return {NativePointer}
     */
    get ObjInstance() {
        return this.#_obj;
    }
 
    #LoadInstanceKlass() {
        this.#_instanceReflection = new UReflectionInfo(this.#_obj);
    }
}
 
class UBehavior extends UObject {
    /**
     *
     * @param {
     *     {
     *         obj:NativePointer
     *     }
     * } input
     */
    constructor(input) {
        super(input);
    }
}
 
class UTextMeshProUGUI extends UObject {
    /**
     * @param {
     *     {
     *         obj:NativePointer
     *     }
     * } input
     */
    constructor(input) {
        super(input);
    }
 
    /**
     * @return {string}
     */
    get TextContent() {
        let field = UTypeDefinition.Api.U_TextMeshProUGUI.FieldByName("m_text");
        let content = UnityUtil.Api.ReadFieldWithString(field, this.ObjInstance);
        return content;
    }
}
 
class UServiceBase {
    /**
     * @type {UDomain}
     */
    #_domain = UDomain.Api;
 
    /**
     * @type {UnityApi}
     */
    #_UApi = UnityApi.Api;
 
    get UApi() {
        return this.#_UApi;
    }
 
    get Domain() {
        return this.#_domain;
    }
}
 
class UIText extends UObject {
    /**
     * @param {
     *     {
     *         obj:NativePointer,
     *     }
     * } input
     */
    constructor(input) {
        super(input);
    }
 
    /**
     * @return {string}
     */
    get TextContent(){
        let property = UTypeDefinition.Api.U_UI_Text.PropertyByName("text");
        let content = UnityUtil.Api.ReadPropertyWithString(property, this.ObjInstance);
        return content;
    }
}
 
/**
 * 搜索功能封装
 */
class UFind extends UServiceBase {
    /**
     * @type {UMethodInfo}
     */
    #_findObjectsOfType = null
 
    constructor() {
        super();
        this.#LoadFindObjectsOfType();
    }
 
    /**
     * 获取当前界面的所有控件元素
     * @return {UBehavior[]}
     */
    GetCurrentAllComponent() {
        let components = this.#FindObjectsOfType(UTypeDefinition.Api.U_Behaviour.Type);
        let behaviors = components
            .map(obj => new UBehavior({
                obj: obj
            }));
 
        // for (const behavior of behaviors) {
        //     console.log(`behavior.TypeName:${behavior.TypeName}`);
        // }
        //
        // console.log("----------------------")
        //
        // for (let behavior of behaviors) {
        //     console.log(`ID Name:${behavior.IDName}--`);
        // }
 
        return behaviors;
    }
 
    /**
     * @return {UTextMeshProUGUI[]}
     */
    GetCurrentComponentByTextMeshProUGUI() {
        let objs = this.#FindObjectsOfType(UTypeDefinition.Api.U_TextMeshProUGUI.Type);
        let tmps = objs.map(value => new UTextMeshProUGUI({
            obj: value
        }));
 
        // console.log(`tmps个数:${tmps.length}`)
        //
        // for (let elem of tmps) {
        //     console.log(`tmp text:${elem.TextContent}`);
        // }
 
        return tmps;
    }
 
    /**
     * @return {UIText[]}
     */
    GetCurrentComponentByText() {
        let objs = this.#FindObjectsOfType(UTypeDefinition.Api.U_UI_Text.Type);
        let texts = objs.map(value => new UIText({
            obj: value
        }));
 
        // console.log(`UI_Text个数:${texts.length}`)
        //
        // for (let elem of texts) {
        //     console.log(`ui text:${elem.TextContent}`);
        // }
 
        return texts;
    }
 
    /**
     * 获取指定类型的所有子类元素
     * @param {NativePointer} obj 实例对象
     * @param {NativePointer} type 类型Type信息
     * @return {NativePointer[]}
     */
    #FindObjectsOfType(type) {
        // 构建装参数指针的数组
        let argArr = Memory.alloc(Process.pointerSize);
        argArr.writePointer(type);
 
        let result = this.#_findObjectsOfType.Invoke(NULL, argArr);
 
        // 读取数组长度
        let length = result.add(Process.pointerSize * 3).readInt();
        // 读取元素数组头
        let elemArr = result.add(Process.pointerSize * 4);
 
        let objArr = [...Array(length).keys()]
            // 读取数组元素
            .map(value => ptr(elemArr).add(Process.pointerSize * value).readPointer());
 
        return objArr;
    }
 
    #LoadFindObjectsOfType() {
        this.#_findObjectsOfType= UTypeDefinition.Api.U_Resources.MethodByName("FindObjectsOfTypeAll",
            1);
 
    }
}
 
class AndroidApplicationContext {
    static #_self = null;
 
    #_context = null;
 
 
    constructor() {
        this.#LoadContext();
    }
 
    /**
     * @return {AndroidApplicationContext}
     */
    static get Api() {
        if (AndroidApplicationContext.#_self == null) {
            AndroidApplicationContext.#_self = new AndroidApplicationContext();
        }
 
        return AndroidApplicationContext.#_self;
    }
 
    /**
     * 获取路径结果:/data/app/包名+随机数/xx.apk
     * @return {string}
     */
    get getPackageCodePath() {
        return this.#_context.getPackageCodePath().value;
    }
 
    /**
     * @return {string}
     */
    get nativeLibraryDir() {
        return this.#_context.getApplicationInfo().nativeLibraryDir.value;
    }
 
 
    #LoadContext() {
        let activityThread = Java.use("android.app.ActivityThread");
        let currentApplication = activityThread.currentApplication();
        this.#_context = currentApplication.getApplicationContext();
    }
}
 
class AndroidFile {
    #_file = null;
    #_absPath = null;
 
    constructor(absPath) {
        this.#_absPath = absPath;
 
        let fileClass = Java.use("java.io.File");
        this.#_file = fileClass.$new(absPath);
    }
 
    /**
     * @return {boolean}
     */
    exists() {
        return this.#_file.exists();
    }
}
 
class UnityEngine {
    /**
     * @type {Find}
     */
    #_find;
 
    /**
     * 判断是否是Unity引擎的游戏
     * @param {function (boolean)} callback
     */
    IsUnity(callback) {
        // 此处如果不使用延迟加载,会很大概率出现无法找到context
        setTimeout(() => {
            // 直接读取apk文件夹是否有libil2cpp.so判断是否unity游戏
            let nativeLibraryDir = AndroidApplicationContext.Api.nativeLibraryDir;
            let file = new AndroidFile(`${nativeLibraryDir}/libil2cpp.so`);
            callback(file.exists());
        }, 1000);
    }
 
    /**
     * Unity引擎初始化
     * @param {function (UnityEngine)} onComplete 初始化完成后回调
     */
    Init(onComplete) {
        let that = this;
 
        let modules = Process.enumerateModules();
        let haveIl2Cpp = modules.find(value => value.name == "libil2cpp.so");
 
        // 先遍历内存,是否有il2cpp.so,没有就hook dlopen
        if (haveIl2Cpp) {
            this.#Load(onComplete);
        } else {
            // hook so加载节点
            AndroidApi.Api.AttachDlopen({
                onEnter(args) {
                    let soPath = args[0].readCString();
                    // 判断加载文件是否存在libil2cpp.so,如果存在,接下来hook该so相关东西
                    this.haveIl2cpp = soPath.endsWith("libil2cpp.so");
                },
                onLeave(rv) {
                    if (!this.haveIl2cpp)
                        return;
 
                    // 发现il2cpp.so加载,延迟一小会再加载,避免so无法找到
                    setTimeout(() => {
                        that.#Load(onComplete);
                    }, 2000);
                }
            });
        }
    }
 
    /**
     * @return {UFind}
     */
    get Find() {
        return this.#_find;
    }
 
    #Load(onComplete) {
        UnityApi.Init();
        this.#_find = new UFind();
 
        onComplete(this);
    }
}
 
class EngineManager {
    /**
     * @return {UnityEngine}
     */
    static GetUnityEngine(){
        return new UnityEngine();
    }
}
 
// 初始化成功后,在frida cli中调用样例
// Example.Print_CurrentAllComponent()
// Example.Print_CurrentComponentByTextMeshProUGUI()
// Example.Print_CurrentComponentByText()
 
class Example {
    static #_unityEngine = null;
 
    /**
     * 方便明亮行调用,所以此处设置一个静态属性
     * @return {UnityEngine}
     */
    static get UnityEngine() {
        return Example.#_unityEngine;
    }
 
    /**
     * 初始化成功后,在frida cli中调用样例
     * Example.Print_CurrentAllComponent()
     */
    static Print_CurrentAllComponent(){
        let uBehaviors = Example.UnityEngine.Find.GetCurrentAllComponent();
        for (let item of uBehaviors) {
            console.log(`组件类型名字:${item.TypeName}---组件ID名:${item.IDName}`);
        }
        console.log(`Behavior子类个数:${uBehaviors.length}`);
    }
 
    /**
     * 初始化成功后,在frida cli中调用样例
     * Example.Print_CurrentComponentByTextMeshProUGUI()
     */
    static Print_CurrentComponentByTextMeshProUGUI(){
        let uTextMeshProUGUIS = Example.UnityEngine.Find.GetCurrentComponentByTextMeshProUGUI();
        for (let item of uTextMeshProUGUIS) {
            console.log(`TextMeshProUGUI文本内容:【${item.TextContent}】`);
        }
        console.log(`TextMeshProUGUI子类个数:${uTextMeshProUGUIS.length}`);
 
    }
 
    /**
     * 初始化成功后,在frida cli中调用样例
     * Example.Print_CurrentComponentByText()
     */
    static Print_CurrentComponentByText(){
        let uiTexts =  Example.UnityEngine.Find.GetCurrentComponentByText();
        for (let item of uiTexts) {
            console.log(`Text组件内容:【${item.TextContent}】`);
        }
        console.log(`UIText子类个数:${uiTexts.length}`);
    }
 
    Entry() {
        // 使用样例
        let unityEngine = EngineManager.GetUnityEngine();
 
        unityEngine.IsUnity(isUnity=>{
            if (!isUnity)
            {
                console.error("此游戏非Unity引擎游戏");
                return ;
            }
            console.log("此游戏是Unity引擎游戏");
 
            unityEngine.Init(engine => {
                console.log("初始化成功!!");
                Example.#_unityEngine = engine;
            });
        });
    }
}
 
/**
 * 编译脚本入口。
 * 所有js脚本,全部自动编译整合进入"_agent.js"
 */
function main(){
    console.log("frida  main代码快进入执行了");
    Java.perform(() => {
        console.log("frida  perform代码快进入执行了");
 
        if (!Java.available) {
            console.log("Frida服务没有启动,取消执行");
        }
 
        let example = new Example();
        example.Entry();
    });
}
 
main();

相关源码资源下载链接

百度网盘:

1
2
链接:https://pan.baidu.com/s/11VzgG8qd2WcwZAT-U24ANQ
提取码:veew

[CTF入门培训]顶尖高校博士及硕士团队亲授《30小时教你玩转CTF》,视频+靶场+题目!助力进入CTF世界

最后于 2022-7-14 19:03 被憨豆爆发编辑 ,原因: 调整标题
收藏
点赞7
打赏
分享
最新回复 (17)
雪    币: 3676
活跃值: (3858)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
caolinkai 2022-7-11 08:50
2
1
感谢分享
雪    币: 1954
活跃值: (3653)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
method 2022-7-11 10:13
3
1
感谢分享
雪    币: 6571
活跃值: (3823)
能力值: (RANK:200 )
在线值:
发帖
回帖
粉丝
LowRebSwrd 4 2022-7-20 09:30
4
0
感谢分享
雪    币: 5112
活跃值: (4419)
能力值: ( LV5,RANK:65 )
在线值:
发帖
回帖
粉丝
gamehack 2022-7-20 10:11
5
0
感谢分享
雪    币: 8283
活跃值: (4811)
能力值: ( LV4,RANK:45 )
在线值:
发帖
回帖
粉丝
v0id_ 2022-7-20 12:20
6
0
感谢分享
雪    币: 733
活跃值: (570)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
我想你了 2022-7-22 10:13
7
0
感谢分享
雪    币: 61
活跃值: (200)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
anuora 2022-7-22 11:02
8
0
感谢分享
雪    币: 242
活跃值: (418)
能力值: ( LV11,RANK:188 )
在线值:
发帖
回帖
粉丝
XPoy 3 2022-8-13 14:04
9
0
新鲜思路! 感谢分享! 
雪    币:
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
wx_suzuke 2022-8-17 19:21
10
0

想請教,我已經有正常初始化成功,但是Example.Print_CurrentAllComponent()時遇到以下錯誤,求解
Error: access violation accessing 0x18
    at il2cpp_runtime_invoke (/frida/repl-2.js:363)  
    at Invoke (/frida/repl-2.js:807)
    at #FindObjectsOfType (/frida/repl-2.js:1517)    
    at GetCurrentAllComponent (/frida/repl-2.js:1451)
    at Print_CurrentAllComponent (/frida/repl-2.js:1699)
    at <eval> (<input>:1)
    at eval (native)

有嘗試先il2cpp_thread_attach(il2cpp_domain_get())了還是不行,謝謝!

最后于 2022-8-17 20:05 被wx_suzuke编辑 ,原因:
雪    币: 217
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
憨豆爆发 2022-8-18 03:27
11
0
wx_suzuke 想請教,我已經有正常初始化成功,但是Example.Print_CurrentAllComponent()時遇到以下錯誤,求解Error:&nbsp;access&nbsp;viola ...
可能是版本不兼容,这个方案也不是百分百兼容所有版本,不同版本引擎有一点细微调整。
没记错的话,我当时测试用的2021。
雪    币: 182
活跃值: (116)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
higherlin 2022-9-21 22:48
12
0

请教对于泛型方法应该怎么获取并执行?例如:public T GetNetObject<T>() { },用il2cpp_class_get_method_from_name虽然可以获取到方法信息,但是执行地址是NULL,而且这类方法好像是有约束的,il2cpp_class_get_method_from_name获取的方法用il2cpp_runtime_invoke调用并不会返回正确的数据。

最后于 2022-9-21 22:52 被higherlin编辑 ,原因:
雪    币: 217
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
憨豆爆发 2022-9-22 13:40
13
0
higherlin 请教对于泛型方法应该怎么获取并执行?例如:public&nbsp;T&nbsp;GetNetObject&lt;T&gt;()&nbsp;{&nbsp; ...
建议用unity写个demo对比看下,太久了确实记不太清清楚
雪    币: 182
活跃值: (116)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
higherlin 2022-9-23 16:26
14
0
憨豆爆发 建议用unity写个demo对比看下,太久了确实记不太清清楚
通过CodeRegistration和MetadataRegistration倒是可以获取到methodPointer,但是如果不能获取到不同泛型的这个方法的MethodInfo,执行这个方法会直接闪退,MethodInfo->methodDefinition要和对应的泛型对应才能正确执行,如果不能通反射获取泛型MethodInfo,那Unity这错误有点低级啊。
雪    币: 182
活跃值: (116)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
higherlin 2022-9-23 22:12
15
1
憨豆爆发 建议用unity写个demo对比看下,太久了确实记不太清清楚
终于搞定泛型方法调用,要通过反射调用MakeGenericMethod。
雪    币: 217
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
憨豆爆发 2022-10-2 02:29
16
0
higherlin 终于搞定泛型方法调用,要通过反射调用MakeGenericMethod。
哦~感谢佬分享经验  奶心奶心~~~
雪    币: 224
活跃值: (1773)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
wx_嗨 2022-12-21 11:56
17
0
大佬请教下,MakeGenericMethod方法的参数是Type类型的列表Type[],这个要怎么在C或者js里面复现
雪    币: 224
活跃值: (1773)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
wx_嗨 2022-12-21 22:23
18
0
higherlin 终于搞定泛型方法调用,要通过反射调用MakeGenericMethod。
老哥,最后调用MakeGenericMethod的时候参数是怎么构建的,我也frida一直报错
游客
登录 | 注册 方可回帖
返回