FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
UnityAndroidIl2cppPatchDemo-libboostrap/bootstrap.cpp at master · noodle1983/UnityAndroidIl2cppPatchDemo-libboostrap · GitHub
noodle1983
/
UnityAndroidIl2cppPatchDemo-libboostrap
Public
Notifications
You must be signed in to change notification settings
Fork
59
Star
127
Code
Issues
6
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
UnityAndroidIl2cppPatchDemo-libboostrap
/
bootstrap.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
833 lines (720 loc) · 23 KB
Breadcrumbs
UnityAndroidIl2cppPatchDemo-libboostrap
/
bootstrap.cpp
Copy path
File metadata and controls
833 lines (720 loc) · 23 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
il2cpp.h
"
#
include
<
fstream
>
#
include
<
sstream
>
#
include
<
string.h
>
#
include
<
sys/types.h
>
#
include
<
sys/stat.h
>
#
include
<
fcntl.h
>
#
include
"
log.h
"
#
include
"
zip/shadow_zip.h
"
#
include
"
xhook/xhook.h
"
#
include
"
io_github_noodle1983_Boostrap.h
"
#
include
"
mymap32.h
"
#
include
"
file_mapping.h
"
#
include
"
serial_utils.h
"
static
inline
char
*
dupstr
(
const
char
*
const
str)
{
if
(str ==
NULL
){
return
NULL
; }
int
len =
strlen
(str) +
1
;
char
* ret =
new
char
[len];
memset
(ret,
0
, len);
strncpy
(ret, str, len);
return
ret;
}
const
char
*
SPLITER
=
"
;
"
;
const
char
* g_data_file_path =
NULL
;
const
char
* g_apk_file_path =
NULL
;
const
char
* g_bundle_id =
NULL
;
static
void
bootstrap
();
std::string
get_apk_path
(
const
std::string& bundle_id);
__attribute__
((visibility (
"
default
"
)))
JNIEXPORT void JNICALL Java_io_github_noodle1983_Boostrap_init
(JNIEnv * jenv, jclass cls, jstring path)
{
const
char
* data_file_path = jenv->
GetStringUTFChars
(path,
NULL
);
g_data_file_path =
dupstr
(data_file_path);
//
never delete, ok with only one
jenv->
ReleaseStringUTFChars
(path, data_file_path);
MY_INFO
(
"
data file path:%s
"
, g_data_file_path);
bootstrap
();
}
static
std::string
get_bundle_id
()
{
pid_t
pid =
getpid
();
char
bundle_id_filename[
256
] = {
0
};
snprintf
(bundle_id_filename,
sizeof
(bundle_id_filename),
"
/proc/%d/cmdline
"
, pid);
std::fstream
bundle_id_file
(bundle_id_filename, std::fstream::in);
if
(!bundle_id_file.
is_open
())
{
MY_ERROR
(
"
failed to open %s
"
, bundle_id_filename);
exit
(-
1
);
return
"
"
;
}
char
bundle_id[
256
] = {
0
};
bundle_id_file.
getline
(bundle_id,
sizeof
(bundle_id));
bundle_id_file.
close
();
return
std::string
(bundle_id);
}
static
bool
copy_file
(
const
char
*
const
from_path,
const
char
*
const
to_path)
{
MY_LOG
(
"
copy %s to %s
"
, from_path, to_path);
int
fd_to, fd_from;
char
buf[
4096
];
ssize_t
nread;
int
saved_errno;
fd_from =
open
(from_path,
O_RDONLY
);
if
(fd_from <
0
){
MY_ERROR
(
"
can't access to %s.
"
, from_path);
return
false
;
}
fd_to =
open
(to_path,
O_WRONLY
|
O_CREAT
|
O_EXCL
,
0755
);
if
(fd_to <
0
){
MY_ERROR
(
"
can't access to %s.
"
, to_path);
close
( fd_from );
return
false
;
}
while
((nread =
read
(fd_from, buf,
sizeof
buf))>
0
) {
char
*out_ptr = buf;
ssize_t
nwritten;
do
{
nwritten =
write
(fd_to, out_ptr, nread);
if
(nwritten >=
0
) {
nread -= nwritten;
out_ptr += nwritten;
}
else
if
(errno !=
EINTR
) {
MY_ERROR
(
"
can't write to %s. errno:%d
"
, to_path, errno);
close
( fd_from );
close
( fd_to );
return
false
;
}
}
while
(nread >
0
);
}
close
( fd_from );
close
( fd_to );
return
true
;
}
static
bool
prepare_so_lib
(
const
char
*
const
data_path,
const
std::string& bundle_id)
{
DIR
* dir =
opendir
(data_path);
if
(dir ==
NULL
)
{
MY_ERROR
(
"
can't access data path:%s
"
, data_path);
return
false
;
}
const
char
*
NEW_SO_POSTFIX
=
"
.new
"
;
int
new_file_postfix_len =
strlen
(
NEW_SO_POSTFIX
);
struct
dirent
*ent =
NULL
;
while
((ent =
readdir
(dir)) !=
NULL
)
{
if
(ent->
d_type
&
DT_DIR
) {
continue
; }
const
char
* filename = ent->
d_name
;
int
len =
strlen
(filename);
if
(len > new_file_postfix_len &&
memcmp
(filename + len - new_file_postfix_len,
NEW_SO_POSTFIX
, new_file_postfix_len) ==
0
)
{
//
copy patch_dir/xxx.new -> data/files/xxx.new
char
from_filepath[
256
] = {
0
};
snprintf
(from_filepath,
sizeof
(from_filepath),
"
%s/%s
"
, data_path, filename);
char
to_filepath[
256
] = {
0
};
snprintf
(to_filepath,
sizeof
(to_filepath),
"
%s/%s
"
, g_data_file_path, filename);
copy_file
(from_filepath, to_filepath);
//
rename patch_dir/xxx.new -> data/files/xxx
//
if rollback, rename back before prepare_so_lib
char
new_filepath[
256
] = {
0
};
strncpy
(new_filepath, from_filepath,
sizeof
(new_filepath));
new_filepath[
strlen
(from_filepath) - new_file_postfix_len] =
'
\0
'
;
unlink
(new_filepath);
rename
(from_filepath, new_filepath);
continue
;
}
}
closedir
(dir);
return
true
;
}
__attribute__
((visibility (
"
default
"
)))
char* get_arch_abi()
{
return
dupstr
(
TARGET_ARCH_ABI
);
}
char
* g_use_data_path =
NULL
;
__attribute__
((visibility (
"
default
"
)))
char* use_data_dir(
const
char
* data_path,
const
char
* useless)
{
char
formal_patch_info_path[
256
] = {
0
};
snprintf
(formal_patch_info_path,
sizeof
(formal_patch_info_path),
"
%s/user.db
"
, g_data_file_path);
if
(
strlen
(data_path) ==
0
){
unlink
(formal_patch_info_path);
return
NULL
;
}
if
(
strlen
(data_path) >
0
){
DIR
* dir =
opendir
(data_path);
if
(dir ==
NULL
)
{
MY_ERROR
(
"
can't access data path:%s
"
, data_path);
return
strdup
(
"
can't access data dir!
"
);
}
closedir
(dir);
}
std::string bundle_id =
get_bundle_id
();
//
1. prepare so lib
prepare_so_lib
(data_path, bundle_id);
//
2. pre-find apk path
std::string apk_path_string =
get_apk_path
(bundle_id);
//
3. pre-calc partition
ShadowZipGlobalData global_data;
if
(
0
!=
ShadowZip::init
(data_path, apk_path_string.
c_str
(), &global_data)){
char
error_str[
256
] = {
0
};
snprintf
(error_str,
sizeof
(error_str),
"
failed to do ShadowZip::init(%s, %s)
"
, data_path, apk_path_string.
c_str
());
MY_ERROR
(
"
%s
"
, error_str);
return
dupstr
(error_str);
}
char
patch_info_path[
256
] = {
0
};
snprintf
(patch_info_path,
sizeof
(patch_info_path),
"
%s/user.db.tmp
"
, g_data_file_path);
FILE
* fw = ::
fopen
(patch_info_path,
"
wb
"
);
if
(fw ==
NULL
)
{
char
error_str[
256
] = {
0
};
snprintf
(error_str,
sizeof
(error_str),
"
can't open:%s, errno:%d
"
, patch_info_path, errno);
MY_ERROR
(
"
can't open:%s, errno:%d
"
, patch_info_path, errno);
return
dupstr
(error_str);
}
serial_uint32
(fw,
5460
);
serial_string
(fw, data_path);
serial_string
(fw, apk_path_string);
serial_string
(fw, bundle_id);
serial_string_vector
(fw, global_data.
all_files_
);
serial_partition_vector
(fw, global_data.
patch_partitions_
);
serial_uint64
(fw, global_data.
end_of_file_
);
::fclose
(fw);
unlink
(formal_patch_info_path);
rename
(patch_info_path, formal_patch_info_path);
return
NULL
;
}
static
bool
pre_process_all_so_lib
(
const
std::string& bundle_id)
{
DIR
* dir =
opendir
(g_data_file_path);
if
(dir ==
NULL
)
{
MY_ERROR
(
"
can't access data path:%s
"
, g_data_file_path);
return
false
;
}
const
char
*
NEW_SO_POSTFIX
=
"
.new
"
;
int
new_file_postfix_len =
strlen
(
NEW_SO_POSTFIX
);
char
filepath_buffer[
256
] = {
0
};
struct
dirent
*ent =
NULL
;
while
((ent =
readdir
(dir)) !=
NULL
)
{
if
(ent->
d_type
&
DT_DIR
) {
continue
; }
const
char
* filename = ent->
d_name
;
int
len =
strlen
(filename);
snprintf
(filepath_buffer,
sizeof
(filepath_buffer),
"
%s/%s
"
, g_data_file_path, filename);
if
(len > new_file_postfix_len &&
memcmp
(filename + len - new_file_postfix_len,
NEW_SO_POSTFIX
, new_file_postfix_len) ==
0
)
{
std::string so_name =
std::string
(filename).
substr
(
0
, len - new_file_postfix_len);
std::string new_filepath = filepath_buffer;
std::string old_filepath = new_filepath.
substr
(
0
, new_filepath.
length
() - new_file_postfix_len );
unlink
(old_filepath.
c_str
());
rename
(new_filepath.
c_str
(), old_filepath.
c_str
());
chmod
(old_filepath.
c_str
(),
0755
);
MY_LOG
(
"
rename %s to %s
"
, new_filepath.
c_str
(), old_filepath.
c_str
());
continue
;
}
}
closedir
(dir);
return
true
;
}
static
dev_t
g_apk_device_id = -
1
;
static
ino_t
g_apk_ino = -
1
;
static
bool
extract_patch_info
(std::string& default_path, std::string& patch_path)
{
char
default_il2cpp_path[
256
] = {
0
};
snprintf
(default_il2cpp_path,
sizeof
(default_il2cpp_path),
"
%s/../lib/libil2cpp.so
"
, g_data_file_path);
default_path =
std::string
(default_il2cpp_path);
patch_path.
clear
();
char
patch_info_path[
256
] = {
0
};
snprintf
(patch_info_path,
sizeof
(patch_info_path),
"
%s/user.db
"
, g_data_file_path);
//
get patch date
struct
stat
user_stat;
memset
(&user_stat,
0
,
sizeof
(
struct
stat
));
if
(
stat
(patch_info_path, &user_stat) !=
0
) {
MY_LOG
(
"
can't read file:%d[%s]
"
, errno, patch_info_path);
return
false
;
}
FILE
* fp = ::
fopen
(patch_info_path,
"
rb
"
);
if
(fp ==
NULL
)
{
MY_ERROR
(
"
can't open:%s, errno:%d
"
, patch_info_path, errno);
return
false
;
}
uint32_t
magic =
unserial_uint32
(fp);
if
(magic !=
5460
){
MY_ERROR
(
"
wrong user.db format
"
);
return
false
;
}
std::string data_path_string =
unserial_string
(fp);
MY_INFO
(
"
cached data path:%s
"
, data_path_string.
c_str
());
std::string apk_path_string =
unserial_string
(fp);
MY_INFO
(
"
cached apk_path_string:%s
"
, apk_path_string.
c_str
());
std::string bundle_id =
unserial_string
(fp);
MY_INFO
(
"
cached bundle_id:%s
"
, bundle_id.
c_str
());
LeakSingleton<ShadowZipGlobalData,
0
>::
init
();
unserial_string_vector
(fp, g_shadowzip_global_data->
all_files_
);
unserial_partition_vector
(fp, g_shadowzip_global_data->
patch_partitions_
);
g_shadowzip_global_data->
end_of_file_
=
unserial_uint64
(fp);
::fclose
(fp);
ShadowZip::log
(g_shadowzip_global_data);
const
char
* data_path = data_path_string.
c_str
();
DIR
* dir =
opendir
(data_path);
if
(dir ==
NULL
)
{
MY_ERROR
(
"
can't access data path:%s
"
, data_path);
return
false
;
}
closedir
(dir);
//
get and save apk file id
const
char
* apk_path = apk_path_string.
c_str
();
struct
stat
apk_stat;
memset
(&apk_stat,
0
,
sizeof
(
struct
stat
));
if
(
stat
(apk_path, &apk_stat) !=
0
) {
MY_ERROR
(
"
can't read file:%d[%s]
"
, errno, apk_path);
return
false
;
}
g_apk_device_id = apk_stat.
st_dev
;
g_apk_ino = apk_stat.
st_ino
;
//
if we have newer apk file, then no need to load patch
if
(apk_stat.
st_mtime
> user_stat.
st_mtime
){
MY_ERROR
(
"
newer apk file:%s, no need to patch
"
, apk_path);
unlink
(patch_info_path);
return
false
;
}
if
(!
pre_process_all_so_lib
(bundle_id))
{
MY_ERROR
(
"
can't pre_process_all_so_lib
"
);
return
false
;
}
char
link_file[
256
] = {
0
};
snprintf
(link_file,
sizeof
(link_file),
"
%s/libil2cpp.so
"
, g_data_file_path);
std::string
il2cpp_path
(link_file);
FILE
*fi =
fopen
(il2cpp_path.
c_str
(),
"
r
"
);
if
(fi ==
NULL
)
{
MY_ERROR
(
"
can't libil2cpp:%s
"
, il2cpp_path.
c_str
());
return
false
;
}
fclose
(fi);
if
(g_use_data_path !=
NULL
){
delete[]
g_use_data_path;}
g_use_data_path =
dupstr
(data_path);
if
(g_apk_file_path !=
NULL
){
delete[]
g_apk_file_path;}
g_apk_file_path =
dupstr
(apk_path);
if
(g_bundle_id !=
NULL
){
delete[]
g_bundle_id;}
g_bundle_id =
dupstr
(bundle_id.
c_str
());
patch_path = il2cpp_path;
return
true
;
}
static
void
*
get_module_base
(
const
char
* module_name )
{
FILE
*fp;
size_t
addr =
0
;
char
*pch;
char
filename[
32
];
char
line[
1024
];
char
* store_ptr =
NULL
;
fp =
fopen
(
"
/proc/self/maps
"
,
"
r
"
);
if
( fp !=
NULL
){
while
(
fgets
( line,
sizeof
(line), fp ) ){
if
(
strstr
( line, module_name ) ){
pch =
strtok_r
( line,
"
-
"
, &store_ptr);
addr =
strtoull
( pch,
NULL
,
16
);
break
;
}
}
fclose
( fp ) ;
}
return
(
void
*)addr;
}
static
void
*
get_module_base
(
char
* module_buffer,
int
len )
{
FILE
*fp;
size_t
addr =
0
;
char
*pch;
char
line[
1024
];
char
* matched_pos =
NULL
;
char
* store_ptr =
NULL
;
fp =
fopen
(
"
/proc/self/maps
"
,
"
r
"
);
if
( fp !=
NULL
){
while
(
fgets
( line,
sizeof
(line), fp ) ){
if
((matched_pos =
strstr
( line, module_buffer )) !=
NULL
){
pch =
strtok_r
( line,
"
-
"
, &store_ptr );
addr =
strtoull
( pch,
NULL
,
16
);
char
* fullpath = matched_pos;
while
(fullpath > line && (*fullpath) !=
'
'
&& (*fullpath) !=
'
\t
'
){ fullpath--; }
strncpy
(module_buffer, fullpath +
1
, len);
for
(
int
i =
0
; i < len; i++){
if
(module_buffer[i] ==
'
'
|| module_buffer[i] ==
'
\r
'
|| module_buffer[i] ==
'
\n
'
|| module_buffer[i] ==
'
\t
'
|| module_buffer[i] ==
'
\0
'
) {
module_buffer[i] =
0
;
break
;
}
}
break
;
}
}
fclose
( fp ) ;
}
return
(
void
*)addr;
}
static
void
dump_maps
( )
{
FILE
*fp;
size_t
addr =
0
;
char
*pch;
char
filename[
32
];
char
line[
1024
];
fp =
fopen
(
"
/proc/self/maps
"
,
"
r
"
);
if
( fp !=
NULL
){
while
(
fgets
( line,
sizeof
(line), fp ) ){
MY_ERROR
(
"
%s
"
, line);
}
fclose
( fp ) ;
}
}
std::string
get_apk_path
(
const
std::string& bundle_id)
{
FILE
*fp;
char
*found_path =
NULL
;
char
filename[
32
];
char
line[
1024
];
const
char
*
TAIL
=
"
.apk
"
;
int
TAIL_LEN
=
strlen
(
TAIL
);
std::string ret;
fp =
fopen
(
"
/proc/self/maps
"
,
"
r
"
);
if
( fp !=
NULL
){
while
(
fgets
( line,
sizeof
(line), fp ) ){
int
line_len =
strlen
(line);
while
(line[line_len -
1
] ==
'
'
|| line[line_len -
1
] ==
'
\r
'
|| line[line_len -
1
] ==
'
\n
'
|| line[line_len -
1
] ==
'
\t
'
) {line[line_len -
1
] =
'
\0
'
; line_len--;}
if
(
strstr
( line, bundle_id.
c_str
()) && (
memcmp
(line +
strlen
(line) -
TAIL_LEN
,
TAIL
,
TAIL_LEN
) ==
0
) ){
found_path =
strchr
( line,
'
/
'
);
if
(
ShadowZip::contains_path
(found_path,
"
assets/bin/Data/
"
))
{
ret =
std::string
(found_path);
break
;
}
}
}
fclose
( fp ) ;
}
return
ret;
}
typedef
int
(* StatType)(
const
char
*path,
struct
stat
*file_stat);
static
int
my_stat
(
const
char
*path,
struct
stat
*file_stat)
{
memset
(file_stat,
0
,
sizeof
(
struct
stat
));
int
ret =
stat
(path, file_stat);
if
(ret !=
0
) {
return
ret;
}
if
(g_apk_device_id == file_stat->
st_dev
&& g_apk_ino == file_stat->
st_ino
)
{
uint64_t
size =
ShadowZip::get_eof_pos
();
if
(size >
0
){
file_stat->
st_size
= (
off_t
)size;
}
return
ret;
}
return
ret;
}
static
ShadowZip*
get_cached_shadowzip
(
FILE
*stream)
{
FileExtraData* file_extra_data =
get_mapping
(stream);
return
file_extra_data ==
NULL
?
NULL
: file_extra_data->
shadow_zip
;
}
static
ShadowZip*
get_cached_shadowzip
(
int
fd)
{
FileExtraData* file_extra_data =
get_mapping
(fd);
return
file_extra_data ==
NULL
?
NULL
: file_extra_data->
shadow_zip
;
}
typedef
FILE
*(* FOpenType)(
const
char
*path,
const
char
*mode);
static
FILE
*
my_fopen
(
const
char
*path,
const
char
*mode)
{
MY_METHOD
(
"
fopen([%s],[%s])
"
, path, mode);
struct
stat
file_stat;
memset
(&file_stat,
0
,
sizeof
(
struct
stat
));
if
(
stat
(path, &file_stat) !=
0
) {
MY_METHOD
(
"
not exist([%s],[%s])
"
, path, mode);
return
fopen
(path, mode);
}
if
(g_apk_device_id == file_stat.
st_dev
&& g_apk_ino == file_stat.
st_ino
)
{
ShadowZip* shadow_zip =
new
ShadowZip
();
FILE
* fp = shadow_zip->
fopen
();
if
(fp ==
NULL
){
MY_ERROR
(
"
something bad happens!
"
);
delete
shadow_zip;
return
fopen
(path, mode);
}
FileExtraData* file_extra_data =
save_mapping
(shadow_zip);
MY_LOG
(
"
shadow apk in fopen: %s, fd:0x%08x, file*: 0x%08llx
"
, path, file_extra_data->
fd
, (
unsigned
long
long
)file_extra_data->
file
);
return
file_extra_data->
file
;
}
MY_METHOD
(
"
not apk([%s],[%s])
"
, path, mode);
return
fopen
(path, mode);
}
typedef
int
(*FseekType)(
FILE
*stream,
long
offset,
int
whence);
static
int
my_fseek
(
FILE
*stream,
long
offset,
int
whence)
{
ShadowZip* shadow_zip =
get_cached_shadowzip
(stream);
if
(shadow_zip ==
NULL
){
return
fseek
(stream, offset, whence);
}
else
{
return
shadow_zip->
fseek
(stream, offset, whence);
}
}
typedef
long
(*FtellType)(
FILE
*stream);
static
int
my_ftell
(
FILE
*stream)
{
ShadowZip* shadow_zip =
get_cached_shadowzip
(stream);
if
(shadow_zip ==
NULL
){
return
ftell
(stream);
}
else
{
return
shadow_zip->
ftell
(stream);
}
}
typedef
void
(*RewindType)(
FILE
*stream);
static
void
myrewind
(
FILE
*stream)
{
ShadowZip* shadow_zip =
get_cached_shadowzip
(stream);
if
(shadow_zip ==
NULL
){
rewind
(stream);
}
else
{
shadow_zip->
rewind
(stream);
}
}
typedef
size_t
(*FreadType)(
void
*ptr,
size_t
size,
size_t
nmemb,
FILE
*stream);
static
size_t
my_fread
(
void
*ptr,
size_t
size,
size_t
nmemb,
FILE
*stream)
{
ShadowZip* shadow_zip =
get_cached_shadowzip
(stream);
if
(shadow_zip ==
NULL
){
return
fread
(ptr, size, nmemb, stream);
}
else
{
return
shadow_zip->
fread
(ptr, size, nmemb, stream);
}
}
typedef
char
* (*Fgets)(
char
*s,
int
size,
FILE
*stream);
static
char
*
my_fgets
(
char
*s,
int
size,
FILE
*stream)
{
ShadowZip* shadow_zip =
get_cached_shadowzip
(stream);
if
(shadow_zip ==
NULL
){
return
fgets
(s, size, stream);
}
else
{
return
shadow_zip->
fgets
(s, size, stream);
}
}
typedef
int
(*FcloseType)(
FILE
* _fp);
static
int
my_fclose
(
FILE
* stream)
{
MY_METHOD
(
"
my_fclose: file*: 0x%08llx
"
, (
unsigned
long
long
)stream);
FileExtraData* file_extra_data =
get_mapping
(stream);
if
(file_extra_data !=
NULL
)
{
clean_mapping_data
(file_extra_data);
return
0
;
}
else
{
return
fclose
(stream);
}
}
typedef
void
*(*DlopenType)(
const
char
*filename,
int
flags);
static
void
*
my_dlopen
(
const
char
*filename,
int
flags)
{
int
il2cpp_postfix_len =
strlen
(
"
libil2cpp.so
"
);
int
len =
strlen
(filename);
if
(len > il2cpp_postfix_len &&
memcmp
(filename + len - il2cpp_postfix_len,
"
libil2cpp.so
"
, il2cpp_postfix_len) ==
0
)
{
char
link_file[
256
] = {
0
};
snprintf
(link_file,
sizeof
(link_file),
"
%s/libil2cpp.so
"
, g_data_file_path);
MY_LOG
(
"
redirect to %s
"
, link_file);
return
dlopen
(link_file, flags);
}
return
dlopen
(filename, flags);
}
typedef
int
(*OpenType)(
const
char
*path,
int
flags, ...);
static
int
my_open
(
const
char
*path,
int
flags, ...)
{
mode_t
mode = -
1
;
int
has_mode = ((flags &
O_CREAT
) ==
O_CREAT
) || ((flags &
020000000
) ==
020000000
);
if
(has_mode)
{
va_list args;
va_start
(args, flags);
mode =
static_cast
<
mode_t
>(
va_arg
(args,
int
));
va_end
(args);
}
struct
stat
file_stat;
memset
(&file_stat,
0
,
sizeof
(
struct
stat
));
if
(
stat
(path, &file_stat) !=
0
) {
int
ret = has_mode ?
open
(path, flags, mode) :
open
(path, flags);
MY_METHOD
(
"
open(can't access): %s -> fd:0x%08x
"
, path, ret);
return
ret;
}
if
(g_apk_device_id == file_stat.
st_dev
&& g_apk_ino == file_stat.
st_ino
)
{
ShadowZip* shadow_zip =
new
ShadowZip
();
FILE
* fp = shadow_zip->
fopen
();
if
(fp ==
NULL
){
MY_ERROR
(
"
something bad happens!
"
);
delete
shadow_zip;
int
ret = has_mode ?
open
(path, flags, mode) :
open
(path, flags);
MY_METHOD
(
"
open(rollback): %s -> fd:0x%08x
"
, path, ret);
return
ret;
}
FileExtraData* file_extra_data =
save_mapping
(shadow_zip);
MY_LOG
(
"
shadow apk in open: %s, fd:0x%08x, file*: 0x%08llx
"
, path, file_extra_data->
fd
, (
unsigned
long
long
)file_extra_data->
file
);
return
file_extra_data->
fd
;
}
int
ret = has_mode ?
open
(path, flags, mode) :
open
(path, flags);
MY_METHOD
(
"
not apk open: %s -> fd:0x%08x
"
, path, ret);
return
ret;
}
typedef
ssize_t
(*ReadType)(
int
fd,
void
*buf,
size_t
nbyte);
static
ssize_t
my_read
(
int
fd,
void
*buf,
size_t
nbyte)
{
MY_METHOD
(
"
read: 0x%08x, %zu
"
, fd, nbyte);
ssize_t
ret =
0
;
ShadowZip* shadow_zip =
get_cached_shadowzip
(fd);
if
(shadow_zip ==
NULL
){
ret =
read
(fd, buf, nbyte);
}
else
{
ret = shadow_zip->
fread
(buf,
1
, nbyte, (
FILE
*)(
size_t
)fd);
}
MY_METHOD
(
"
readed: 0x%08x, %zd
"
, fd, ret);
return
ret;
}
typedef
off_t
(*LseekType)(
int
fd,
off_t
offset,
int
whence);
off_t
my_lseek
(
int
fd,
off_t
offset,
int
whence)
{
MY_METHOD
(
"
lseek: 0x%08x, offset: 0x%08lx, whence: %d
"
, fd, offset, whence);
off_t
ret =
0
;
ShadowZip* shadow_zip =
get_cached_shadowzip
(fd);
if
(shadow_zip ==
NULL
){
ret =
lseek
(fd, offset, whence);
}
else
{
ret = shadow_zip->
fseek
((
FILE
*)(
size_t
)fd, offset, whence);
if
(ret ==
0
){ret = shadow_zip->
ftell
((
FILE
*)(
size_t
)fd);}
}
MY_METHOD
(
"
lseek: 0x%08x, return: %ld
"
, fd, ret);
return
ret;
}
typedef
off64_t
(*Lseek64Type)(
int
fd,
off64_t
offset,
int
whence);
off64_t
my_lseek64
(
int
fd,
off64_t
offset,
int
whence)
{
MY_METHOD
(
"
lseek64: 0x%08x, offset: 0x%08llx, whence: %d
"
, fd, (
unsigned
long
long
)offset, whence);
off64_t
ret =
0
;
ShadowZip* shadow_zip =
get_cached_shadowzip
(fd);
if
(shadow_zip ==
NULL
){
ret =
lseek64
(fd, offset, whence);
}
else
{
ret = shadow_zip->
fseek
((
FILE
*)(
size_t
)fd, offset, whence);
if
(ret ==
0
){ret = shadow_zip->
ftell
((
FILE
*)(
size_t
)fd);}
}
MY_METHOD
(
"
lseek64: 0x%08x, return: %lld
"
, fd, (
unsigned
long
long
)ret);
return
ret;
}
typedef
int
(*CloseType)(
int
fd);
static
int
my_close
(
int
fd)
{
MY_METHOD
(
"
my_close: 0x%08x
"
, fd);
FileExtraData* file_extra_data =
get_mapping
(fd);
if
(file_extra_data !=
NULL
)
{
clean_mapping_data
(file_extra_data);
return
0
;
}
else
{
return
close
(fd);
}
}
static
int
init_hook
()
{
#
define
HOOK
(
lib, symbol
) \
if
(
0
!=
xhook_register
(lib, #symbol, (
void
*)my_##symbol,
NULL
)){ \
MY_ERROR
(
"
failed to find function:%s in %s
"
, #symbol, lib); \
return
-
1
; \
}
//
xhook_enable_debug(1);
HOOK
(
"
libunity.so
"
, fopen);
HOOK
(
"
libunity.so
"
, fseek);
HOOK
(
"
libunity.so
"
, ftell);
HOOK
(
"
libunity.so
"
, fread);
HOOK
(
"
libunity.so
"
, fgets);
HOOK
(
"
libunity.so
"
, fclose);
HOOK
(
"
libunity.so
"
, stat);
HOOK
(
"
libunity.so
"
, dlopen);
HOOK
(
"
libunity.so
"
, open);
HOOK
(
"
libunity.so
"
, read);
HOOK
(
"
libunity.so
"
, lseek);
HOOK
(
"
libunity.so
"
, lseek64);
HOOK
(
"
libunity.so
"
, close);
if
(
0
!=
xhook_refresh
(
1
)){
MY_ERROR
(
"
failed to find replace function
"
);
return
-
1
;
}
return
0
;
}
static
int
init_art_hook
()
{
//
char path[512] = {0};
//
snprintf((char*)path, sizeof(path), "libart.so");
//
void const* addr = get_module_base(path, sizeof(path)-1);
//
MY_LOG("libart addr:0x%zx, path:%s", (size_t)addr, path);
//
if (addr == NULL){
//
MY_ERROR("failed to load libart info");
//
return -1;
//
}
//
//
void *handle = dlopen(path, RTLD_LAZY);
//
if (!handle) {
//
MY_ERROR("failed to load libart:%s", dlerror());
//
return -1;
//
}
//
//
std::vector<const char*> symbol_names;
//
symbol_names.push_back("dlopen");
//
//
std::vector<void const *> new_func;
//
new_func.push_back((void const *)my_art_dlopen);
//
//
std::vector<void **> old_func;
//
old_func.push_back((void**)&art_old_dlopen);
//
//
elf_hook(path, addr, symbol_names, new_func, old_func);
//
//
//dlclose(handle);
return
0
;
}
static
void
bootstrap
()
{
std::string default_il2cpp_path;
std::string patch_il2cpp_path;
bool
use_patch =
extract_patch_info
(default_il2cpp_path, patch_il2cpp_path);
if
(use_patch){
MY_INFO
(
"
bootstrap running %s with apk_path:[%s], patch_so:[%s], patch_dir:[%s]
"
,
TARGET_ARCH_ABI
,
g_apk_file_path, patch_il2cpp_path.
c_str
(), g_use_data_path);
bool
success = (
0
==
init_hook
()) && (
0
==
init_mapping_data
());
if
(success)
{
MY_INFO
(
"
bootstrap running with patch:%s
"
, patch_il2cpp_path.
c_str
());
//
ShadowZip::output_apk(g_use_data_path);
}
//
else we still do so hook
else
{
MY_INFO
(
"
bootstrap running failed with patch
"
);
}
}
else
{
MY_INFO
(
"
bootstrap running without patch
"
);
}
}
//
static void entrance() __attribute__((constructor));
//
void entrance() {
//
}
Back
|
FazBrowse Home
|
New Git URL