forked from facebook/redex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunpacker.py
1121 lines (978 loc) · 39.3 KB
/
unpacker.py
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
#!/usr/bin/env python3
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
# pyre-strict
import fnmatch
import hashlib
import itertools
import json
import logging
import lzma
import mmap
import os
import re
import shutil
import subprocess
import tarfile
import typing
import zipfile
from abc import ABC, abstractmethod
from os.path import basename, dirname, getsize, isdir, isfile, join, normpath
from pyredex.logger import log
from pyredex.utils import (
abs_glob,
ensure_libs_dir,
make_temp_dir,
remove_signature_files,
)
class BaseDexMode(ABC):
def __init__(
self,
primary_dir: str,
dex_prefix: str,
canary_prefix: typing.Optional[str],
store_id: typing.Optional[str],
dependencies: typing.Optional[typing.List[str]],
) -> None:
self._primary_dir = primary_dir
self._dex_prefix = dex_prefix
self._canary_prefix = canary_prefix
self._store_id = store_id
self._dependencies = dependencies
@abstractmethod
def detect(self, extracted_apk_dir: str) -> bool:
return False
@abstractmethod
def unpackage(self, extracted_apk_dir: str, dex_dir: str) -> None:
primary_dex = join(
extracted_apk_dir, self._primary_dir, self._dex_prefix + ".dex"
)
if os.path.exists(primary_dex):
shutil.move(primary_dex, dex_dir)
@abstractmethod
def repackage(
self,
extracted_apk_dir: str,
dex_dir: str,
have_locators: bool,
locator_store_id: int,
fast_repackage: bool,
reset_timestamps: bool,
) -> None:
primary_dex = join(dex_dir, self._dex_prefix + ".dex")
if os.path.exists(primary_dex):
shutil.move(primary_dex, join(extracted_apk_dir, self._primary_dir))
def get_canary(self, i: int) -> str:
canary_prefix = self._canary_prefix
assert canary_prefix is not None
return canary_prefix + ".dex%02d.Canary" % i
class ApplicationModule(object):
def __init__(
self,
extracted_apk_dir: str,
name: str,
canary_prefix: typing.Optional[str],
dependencies: typing.List[str],
split: str = "",
) -> None:
self.name = name
self.path: str = join(split, "assets", name)
self.canary_prefix = canary_prefix
self.dependencies = dependencies
self.dex_mode: typing.Optional[BaseDexMode] = None
@staticmethod
def detect(
extracted_apk_dir: str, is_bundle: bool = False
) -> typing.List["ApplicationModule"]:
modules = []
pattern = "*/assets/*/metadata.txt" if is_bundle else "assets/*/metadata.txt"
for candidate in abs_glob(extracted_apk_dir, pattern):
with open(candidate) as metadata:
name = None
dependencies: typing.List[str] = []
canary_match = None
canary_prefix = None
for line in metadata.read().splitlines():
tokens = line.split()
if tokens[0] == ".id":
name = tokens[1]
if tokens[0] == ".requires":
dependencies.append(tokens[1])
if tokens[0][0] != ".":
canary_match = re.search(
"([A-Za-z0-9]*)[.]dex[0-9][0-9_]*[.]Canary", tokens[2]
)
if canary_match is not None:
canary_prefix = canary_match.group(1)
if name is not None:
split = (
basename(normpath(join(candidate, "../../../")))
if is_bundle
else ""
)
modules.append(
ApplicationModule(
extracted_apk_dir, name, canary_prefix, dependencies, split
)
)
modules.sort(key=lambda m: m.path)
return modules
def get_name(self) -> str:
return self.name
def get_canary_prefix(self) -> typing.Optional[str]:
return self.canary_prefix
def write_redex_metadata(self, path: str, metadata_file: str) -> None:
files = []
for x in abs_glob(path, "*.dex"):
files.append(x)
metadata = {"id": self.name, "requires": self.dependencies, "files": files}
with open(metadata_file, "w") as store_metadata:
json.dump(metadata, store_metadata)
def unpackage(
self, extracted_apk_dir: str, dex_dir: str, unpackage_metadata: bool = False
) -> None:
dex_mode = XZSDexMode(
secondary_dir=self.path,
store_name=self.name,
dex_prefix=self.name,
canary_prefix=self.canary_prefix,
store_id=self.name,
dependencies=self.dependencies,
)
if dex_mode.detect(extracted_apk_dir):
self.dex_mode = dex_mode
log("module " + self.name + " is XZSDexMode")
dex_mode.unpackage(extracted_apk_dir, dex_dir, unpackage_metadata)
return
dex_mode = SubdirDexMode(
secondary_dir=self.path,
store_name=self.name,
dex_prefix=self.name,
canary_prefix=self.canary_prefix,
store_id=self.name,
dependencies=self.dependencies,
)
if dex_mode.detect(extracted_apk_dir):
self.dex_mode = dex_mode
log("module " + self.name + " is SubdirDexMode")
dex_mode.unpackage(extracted_apk_dir, dex_dir, unpackage_metadata)
return
dex_mode = Api21ModuleDexMode(
secondary_dir=self.path,
store_name=self.name,
canary_prefix=self.canary_prefix,
store_id=self.name,
dependencies=self.dependencies,
)
self.dex_mode = dex_mode
log("module " + self.name + " is Api21ModuleDexMode")
dex_mode.unpackage(extracted_apk_dir, dex_dir, unpackage_metadata)
def repackage(
self,
extracted_apk_dir: str,
dex_dir: str,
have_locators: bool,
locator_store_id: int,
fast_repackage: bool,
reset_timestamps: bool,
) -> None:
dex_mode = self.dex_mode
assert dex_mode is not None
dex_mode.repackage(
extracted_apk_dir,
dex_dir,
have_locators,
locator_store_id,
fast_repackage,
reset_timestamps,
)
class DexMetadata(object):
def __init__(
self,
store: typing.Optional[str] = None,
dependencies: typing.Optional[typing.List[str]] = None,
have_locators: bool = False,
is_root_relative: bool = False,
locator_store_id: int = 0,
superpack_files: int = 0,
) -> None:
self._have_locators = False
self._store = store
self._dependencies = dependencies
self._have_locators = have_locators
self._is_root_relative = is_root_relative
self._dexen: typing.List[typing.Tuple[str, str, str]] = []
self._locator_store_id = locator_store_id
self.superpack_files = superpack_files
def add_dex(
self, dex_path: str, canary_class: str, hash: typing.Optional[str] = None
) -> None:
if hash is None:
with open(dex_path, "rb") as dex:
sha1hash = hashlib.sha1(dex.read()).hexdigest()
else:
sha1hash = hash
self._dexen.append((os.path.basename(dex_path), sha1hash, canary_class))
def write(self, path: str) -> None:
with open(path, "w") as meta:
store = self._store
if store is not None:
meta.write(".id " + store + "\n")
deps = self._dependencies
if deps is not None:
for dependency in deps:
meta.write(".requires " + dependency + "\n")
if self._is_root_relative:
meta.write(".root_relative\n")
if self._have_locators:
meta.write(".locators\n")
if self._locator_store_id > 0:
meta.write(".locator_id " + str(self._locator_store_id) + "\n")
if self.superpack_files > 0:
meta.write(".superpack_files " + str(self.superpack_files) + "\n")
for dex in self._dexen:
meta.write(" ".join(dex) + "\n")
class Api21DexMode(BaseDexMode):
"""
On API 21+, secondary dex files are in the root of the apk and are named
classesN.dex for N in [2, 3, 4, ... ]
Note that this mode will also be used for apps that don't have any
secondary dex files.
"""
def __init__(
self,
primary_dir: str = "",
secondary_dir: str = "assets/secondary-program-dex-jars",
dex_prefix: str = "classes",
canary_prefix: typing.Optional[str] = "secondary",
is_root_relative: bool = True,
store_id: typing.Optional[str] = None,
dependencies: typing.Optional[typing.List[str]] = None,
) -> None:
BaseDexMode.__init__(
self, primary_dir, dex_prefix, canary_prefix, store_id, dependencies
)
self._secondary_dir = secondary_dir
self._is_root_relative = is_root_relative
def detect(self, extracted_apk_dir: str) -> bool:
# Note: This mode is the fallback and we only check for it after
# checking for the other modes. This should return true for any
# apk.
return isfile(
join(extracted_apk_dir, self._primary_dir, self._dex_prefix + ".dex")
)
def unpackage(
self, extracted_apk_dir: str, dex_dir: str, unpackage_metadata: bool = False
) -> None:
BaseDexMode.unpackage(self, extracted_apk_dir, dex_dir)
metadata_dir = join(extracted_apk_dir, self._secondary_dir)
if self._is_root_relative:
extracted_dex_dir = join(extracted_apk_dir, self._primary_dir)
else:
extracted_dex_dir = metadata_dir
for path in abs_glob(extracted_dex_dir, "*.dex"):
shutil.move(path, dex_dir)
def repackage(
self,
extracted_apk_dir: str,
dex_dir: str,
have_locators: bool,
locator_store_id: int = 0,
fast_repackage: bool = False,
reset_timestamps: bool = True,
) -> None:
BaseDexMode.repackage(
self,
extracted_apk_dir,
dex_dir,
have_locators,
locator_store_id,
fast_repackage,
reset_timestamps,
)
metadata_dir = join(extracted_apk_dir, self._secondary_dir)
metadata = DexMetadata(
is_root_relative=self._is_root_relative,
have_locators=have_locators,
store=self._store_id,
dependencies=self._dependencies,
locator_store_id=locator_store_id,
)
for i in itertools.count(2):
dex_path = join(dex_dir, self._dex_prefix + "%d.dex" % i)
if not isfile(dex_path):
break
metadata.add_dex(dex_path, BaseDexMode.get_canary(self, i - 1))
if self._is_root_relative:
shutil.move(dex_path, join(extracted_apk_dir, self._primary_dir))
else:
shutil.move(dex_path, metadata_dir)
if os.path.exists(metadata_dir):
metadata.write(join(metadata_dir, "metadata.txt"))
class Api21ModuleDexMode(Api21DexMode):
"""
modules built in Api21 builds will just have <store_name><i>.dex files in
the module directory. This should only be used by modules.
"""
def __init__(
self,
secondary_dir: str,
store_name: str = "secondary",
canary_prefix: typing.Optional[str] = "secondary",
store_id: typing.Optional[str] = None,
dependencies: typing.Optional[typing.List[str]] = None,
) -> None:
Api21DexMode.__init__(
self,
primary_dir="",
secondary_dir=secondary_dir,
dex_prefix=store_name,
canary_prefix=canary_prefix,
store_id=store_id,
dependencies=dependencies,
is_root_relative=False,
)
self._store_name = store_name
def detect(self, extracted_apk_dir: str) -> bool:
secondary_dex_dir = join(extracted_apk_dir, self._secondary_dir)
return len(list(abs_glob(secondary_dex_dir, "*.dex"))) > 0
class SubdirDexMode(BaseDexMode):
"""
`buck build katana` places secondary dexes in a subdir with no compression
"""
def __init__(
self,
primary_dir: str = "",
secondary_dir: str = "assets/secondary-program-dex-jars",
store_name: str = "secondary",
dex_prefix: str = "classes",
canary_prefix: typing.Optional[str] = "secondary",
store_id: typing.Optional[str] = None,
dependencies: typing.Optional[typing.List[str]] = None,
) -> None:
BaseDexMode.__init__(
self, primary_dir, dex_prefix, canary_prefix, store_id, dependencies
)
self._secondary_dir = secondary_dir
self._store_name = store_name
def detect(self, extracted_apk_dir: str) -> bool:
secondary_dex_dir = join(extracted_apk_dir, self._secondary_dir)
# pyre-fixme[7]: Expected `bool` but got `int`.
return isdir(secondary_dex_dir) and len(
list(abs_glob(secondary_dex_dir, "*.dex.jar"))
)
def unpackage(
self, extracted_apk_dir: str, dex_dir: str, unpackage_metadata: bool = False
) -> None:
jars = abs_glob(join(extracted_apk_dir, self._secondary_dir), "*.dex.jar")
for jar in jars:
dexpath = join(dex_dir, basename(jar))[:-4]
extract_dex_from_jar(jar, dexpath)
os.remove(jar + ".meta")
os.remove(jar)
metadata_txt = join(extracted_apk_dir, self._secondary_dir, "metadata.txt")
if unpackage_metadata:
shutil.copy(metadata_txt, dex_dir)
os.remove(metadata_txt)
BaseDexMode.unpackage(self, extracted_apk_dir, dex_dir)
def repackage(
self,
extracted_apk_dir: str,
dex_dir: str,
have_locators: bool,
locator_store_id: int = 0,
fast_repackage: bool = False,
reset_timestamps: bool = True,
) -> None:
BaseDexMode.repackage(
self,
extracted_apk_dir,
dex_dir,
have_locators,
locator_store_id,
fast_repackage,
reset_timestamps,
)
metadata = DexMetadata(
have_locators=have_locators,
store=self._store_id,
dependencies=self._dependencies,
locator_store_id=locator_store_id,
)
for i in itertools.count(1):
oldpath = join(dex_dir, self._dex_prefix + "%d.dex" % (i + 1))
dexpath = join(dex_dir, self._store_name + "-%d.dex" % i)
if not isfile(oldpath):
break
shutil.move(oldpath, dexpath)
jarpath = dexpath + ".jar"
create_dex_jar(jarpath, dexpath, reset_timestamps=reset_timestamps)
metadata.add_dex(jarpath, BaseDexMode.get_canary(self, i))
dex_meta_base = jarpath + ".meta"
dex_meta_path = join(dex_dir, dex_meta_base)
with open(dex_meta_path, "w") as dex_meta:
dex_meta.write("jar:%d dex:%d\n" % (getsize(jarpath), getsize(dexpath)))
shutil.move(dex_meta_path, join(extracted_apk_dir, self._secondary_dir))
shutil.move(jarpath, join(extracted_apk_dir, self._secondary_dir))
jar_meta_path = join(dex_dir, "metadata.txt")
metadata.write(jar_meta_path)
shutil.move(jar_meta_path, join(extracted_apk_dir, self._secondary_dir))
warned_about_xz = False
def _warn_xz() -> None:
global warned_about_xz
if not warned_about_xz:
logging.warning(
"Falling back to python lzma. For increased performance, install xz."
)
warned_about_xz = True
def unpack_xz(input: str, output: str) -> None:
# See whether the `xz` binary exists. It may be faster because of multithreaded decoding.
if shutil.which("xz"):
cmd = 'cat "{}" | xz -d --threads 6 > "{}"'.format(input, output)
subprocess.check_call(cmd, shell=True) # noqa: P204
return
_warn_xz()
with lzma.open(input, "rb") as input_f:
with open(output, "wb") as output_f:
BUF_SIZE = 4 * 1024 * 1024
while True:
buf = input_f.read(BUF_SIZE)
if not buf:
break
output_f.write(buf)
def pack_xz(
input: str,
output: str,
compression_level: typing.Union[int, str] = 9,
threads: int = 6,
check: int = lzma.CHECK_CRC32,
) -> None:
# See whether the `xz` binary exists. It may be faster because of multithreaded encoding.
if shutil.which("xz"):
check_map = {
lzma.CHECK_CRC32: "crc32",
lzma.CHECK_CRC64: "crc64",
lzma.CHECK_SHA256: "sha256",
lzma.CHECK_NONE: None,
None: None,
}
check_str = check_map[check]
subprocess.check_call( # noqa(P204)
f"xz -z{compression_level} --threads={threads} -c"
+ (f" --check={check_str}" if check_str else "")
+ f" {input} > {output}",
shell=True,
)
return
_warn_xz()
assert isinstance(compression_level, int)
c = lzma.LZMACompressor(
format=lzma.FORMAT_XZ, check=check, preset=compression_level
)
with open(output, "wb") as output_f:
with open(input, "rb") as input_f:
BUF_SIZE = 4 * 1024 * 1024
while True:
buf = input_f.read(BUF_SIZE)
if not buf:
break
c_buf = c.compress(buf)
output_f.write(c_buf)
end_buf = c.flush()
output_f.write(end_buf)
def unpack_tar_xz(input: str, output_dir: str) -> None:
# See whether the `xz` binary exists. It may be faster because of multithreaded decoding.
if shutil.which("xz") and shutil.which("tar"):
cmd = f'XZ_OPT=-T6 tar xf "{input}" -C "{output_dir}"'
subprocess.check_call(cmd, shell=True) # noqa: P204
return
_warn_xz()
with tarfile.open(name=input, mode="r:xz") as t:
os.makedirs(output_dir, exist_ok=True)
t.extractall(output_dir)
class XZSDexMode(BaseDexMode):
"""
Secondary dex files are packaged in individual jar files where are then
concatenated together and compressed with xz.
... This format is completely insane.
"""
def __init__(
self,
primary_dir: str = "",
secondary_dir: str = "assets/secondary-program-dex-jars",
store_name: str = "secondary",
dex_prefix: str = "classes",
canary_prefix: typing.Optional[str] = "secondary",
store_id: typing.Optional[str] = None,
dependencies: typing.Optional[typing.List[str]] = None,
) -> None:
BaseDexMode.__init__(
self, primary_dir, dex_prefix, canary_prefix, store_id, dependencies
)
self._xzs_dir = secondary_dir
self._xzs_filename: str = store_name + ".dex.jar.xzs"
self._store_name = store_name
def detect(self, extracted_apk_dir: str) -> bool:
path = join(extracted_apk_dir, self._xzs_dir, self._xzs_filename)
return isfile(path)
def unpackage(
self, extracted_apk_dir: str, dex_dir: str, unpackage_metadata: bool = False
) -> None:
src = join(extracted_apk_dir, self._xzs_dir, self._xzs_filename)
dest = join(dex_dir, self._xzs_filename)
# Move secondary dexen
shutil.move(src, dest)
# concat_jar is a bunch of .dex.jar files concatenated together.
concat_jar = join(dex_dir, self._xzs_filename[:-4])
unpack_xz(dest, concat_jar)
if unpackage_metadata:
shutil.copy(join(extracted_apk_dir, self._xzs_dir, "metadata.txt"), dex_dir)
dex_order = []
with open(
join(extracted_apk_dir, self._xzs_dir, "metadata.txt")
) as dex_metadata:
for line in dex_metadata.read().splitlines():
if line[0] != ".":
tokens = line.split()
search_pattern = self._store_name + r"-(\d+)\.dex\.jar\.xzs\.tmp~"
match = re.search(search_pattern, tokens[0])
if match is None:
raise Exception(
"unable to find match in "
+ tokens[0]
+ " for "
+ search_pattern
)
dex_order.append(int(match.group(1)))
# Sizes of the concatenated .dex.jar files are stored in .meta files.
# Read the sizes of each .dex.jar file and un-concatenate them.
jar_size_regex = r"jar:(\d+)"
secondary_dir = join(extracted_apk_dir, self._xzs_dir)
jar_sizes = {}
for i in dex_order:
filename = self._store_name + "-%d.dex.jar.xzs.tmp~.meta" % i
metadata_path = join(secondary_dir, filename)
if isfile(metadata_path):
with open(metadata_path) as f:
match = re.match(jar_size_regex, f.read())
assert match is not None
jar_sizes[i] = int(match.group(1))
os.remove(metadata_path)
log("found jar " + filename + " of size " + str(jar_sizes[i]))
else:
break
with open(concat_jar, "rb") as cj:
for i in dex_order:
jarpath = join(dex_dir, self._store_name + "-%d.dex.jar" % i)
with open(jarpath, "wb") as jar:
jar.write(cj.read(jar_sizes[i]))
for j in list(jar_sizes.keys()):
jar_size = getsize(
dex_dir + "/" + self._store_name + "-" + str(j) + ".dex.jar"
)
log(
"validating "
+ self._store_name
+ "-"
+ str(j)
+ ".dex.jar size="
+ str(jar_size)
+ " expecting="
+ str(jar_sizes[j])
)
assert jar_sizes[j] == jar_size
assert sum(jar_sizes.values()) == getsize(concat_jar)
# Clean up everything other than dexen in the dex directory
os.remove(concat_jar)
os.remove(dest)
# Lastly, unzip all the jar files and delete them
for jarpath in abs_glob(dex_dir, "*.jar"):
extract_dex_from_jar(jarpath, jarpath[:-4])
os.remove(jarpath)
BaseDexMode.unpackage(self, extracted_apk_dir, dex_dir)
def repackage(
self,
extracted_apk_dir: str,
dex_dir: str,
have_locators: bool,
locator_store_id: int = 0,
fast_repackage: bool = False,
reset_timestamps: bool = True,
) -> None:
BaseDexMode.repackage(
self,
extracted_apk_dir,
dex_dir,
have_locators,
locator_store_id,
fast_repackage,
reset_timestamps,
)
dex_sizes = {}
jar_sizes = {}
concat_jar_path = join(dex_dir, self._store_name + ".dex.jar")
concat_jar_meta = join(dex_dir, "metadata.txt")
dex_metadata = DexMetadata(
have_locators=have_locators,
store=self._store_id,
dependencies=self._dependencies,
locator_store_id=locator_store_id,
)
with open(concat_jar_path, "wb") as concat_jar:
for i in itertools.count(1):
oldpath = join(dex_dir, self._dex_prefix + "%d.dex" % (i + 1))
if not isfile(oldpath):
break
dexpath = join(dex_dir, self._store_name + "-%d.dex" % i)
# Package each dex into a jar
shutil.move(oldpath, dexpath)
jarpath = dexpath + ".jar"
create_dex_jar(jarpath, dexpath, reset_timestamps=reset_timestamps)
dex_sizes[jarpath] = getsize(dexpath)
jar_sizes[jarpath] = getsize(jarpath)
# Concatenate the jar files and create corresponding metadata files
with open(jarpath + ".xzs.tmp~.meta", "w") as metadata:
sizes = "jar:{} dex:{}".format(
jar_sizes[jarpath], dex_sizes[jarpath]
)
metadata.write(sizes)
with open(jarpath, "rb") as jar:
contents = jar.read()
concat_jar.write(contents)
sha1hash = hashlib.sha1(contents).hexdigest()
dex_metadata.add_dex(
jarpath + ".xzs.tmp~",
BaseDexMode.get_canary(self, i),
hash=sha1hash,
)
dex_metadata.write(concat_jar_meta)
assert getsize(concat_jar_path) == sum(
getsize(x) for x in abs_glob(dex_dir, self._store_name + "-*.dex.jar")
)
# XZ-compress the result
pack_xz(
input=concat_jar_path,
output=f"{concat_jar_path}.xz",
compression_level=0 if fast_repackage else 9,
)
# Delete the original.
os.remove(concat_jar_path)
# Copy all the archive and metadata back to the apk directory
secondary_dex_dir = join(extracted_apk_dir, self._xzs_dir)
for path in abs_glob(dex_dir, self._store_name + "*.meta"):
shutil.copy(path, secondary_dex_dir)
shutil.copy(concat_jar_meta, join(secondary_dex_dir, "metadata.txt"))
shutil.copy(
concat_jar_path + ".xz", join(secondary_dex_dir, self._xzs_filename)
)
# These are checked in order from top to bottom. The first one to have detect()
# return true will be used.
SECONDARY_DEX_MODES = [XZSDexMode(), SubdirDexMode(), Api21DexMode()]
BUNDLE_SECONDARY_DEX_MODES = [
XZSDexMode(
primary_dir="base/dex",
secondary_dir="base/assets/secondary-program-dex-jars",
),
SubdirDexMode(
primary_dir="base/dex",
secondary_dir="base/assets/secondary-program-dex-jars",
),
Api21DexMode(
primary_dir="base/dex",
secondary_dir="base/assets/secondary-program-dex-jars",
),
]
class UnknownSecondaryDexModeException(Exception):
pass
def detect_secondary_dex_mode(
extracted_apk_dir: str, is_bundle: bool = False
) -> BaseDexMode:
modes = BUNDLE_SECONDARY_DEX_MODES if is_bundle else SECONDARY_DEX_MODES
for mode in modes:
if mode.detect(extracted_apk_dir):
return mode
raise UnknownSecondaryDexModeException()
def extract_dex_from_jar(jarpath: str, dexpath: str) -> None:
dest_directory = dirname(dexpath)
with zipfile.ZipFile(jarpath) as jar:
contents = jar.namelist()
dexfiles = [name for name in contents if name.endswith(".dex")]
assert len(dexfiles) == 1, "Expected a single dex file"
dexname = jar.extract(dexfiles[0], dest_directory)
os.rename(join(dest_directory, dexname), dexpath)
def create_dex_jar(
jarpath: str,
dexpath: str,
compression: int = zipfile.ZIP_STORED,
reset_timestamps: bool = True,
) -> None:
with zipfile.ZipFile(jarpath, mode="w") as zf:
zf.write(dexpath, "classes.dex", compress_type=compression)
zf.writestr(
"/META-INF/MANIFEST.MF",
b"Manifest-Version: 1.0\n"
b"Dex-Location: classes.dex\n"
b"Created-By: redex\n\n",
)
if reset_timestamps:
ZipReset.reset_file(jarpath)
class ZipManager:
"""
__enter__: Unzips input_apk into extracted_apk_dir
__exit__: Zips extracted_apk_dir into output_apk
"""
per_file_compression: typing.Dict[str, int] = {}
renamed_files_to_original: typing.Dict[str, str] = {}
def __init__(self, input_apk: str, extracted_apk_dir: str, output_apk: str) -> None:
self.input_apk = input_apk
self.extracted_apk_dir = extracted_apk_dir
self.output_apk = output_apk
def __enter__(self) -> None:
log("Extracting apk...")
with zipfile.ZipFile(self.input_apk) as z:
for info in z.infolist():
self.per_file_compression[info.filename] = info.compress_type
z.extractall(self.extracted_apk_dir)
def set_resource_file_mapping(self, path: str) -> None:
with open(path) as f:
mapping = json.load(f)
for k, v in mapping.items():
self.renamed_files_to_original[v] = k
def __exit__(self, *args: typing.Any) -> None:
remove_signature_files(self.extracted_apk_dir)
if isfile(self.output_apk):
os.remove(self.output_apk)
log("Creating output apk")
with zipfile.ZipFile(self.output_apk, "w") as new_apk:
# Need sorted output for deterministic zip file. Sorting `dirnames` will
# ensure the tree walk order. Sorting `filenames` will ensure the files
# inside the tree.
# This scheme uses less memory than collecting all files first.
for dirpath, dirnames, filenames in os.walk(self.extracted_apk_dir):
dirnames.sort()
for filename in sorted(filenames):
filepath = join(dirpath, filename)
archivepath = filepath[len(self.extracted_apk_dir) + 1 :]
try:
original_path = self.renamed_files_to_original.get(
archivepath, archivepath
)
compress = self.per_file_compression[original_path]
except KeyError:
compress = zipfile.ZIP_DEFLATED
new_apk.write(filepath, archivepath, compress_type=compress)
class UnpackManager:
"""
__enter__: Unpacks dexes and application modules from extracted_apk_dir into dex_dir
__exit__: Repacks the dexes and application modules in dex_dir back into extracted_apk_dir
"""
application_modules: typing.List[ApplicationModule] = []
def __init__(
self,
input_apk: str,
extracted_apk_dir: str,
dex_dir: str,
have_locators: bool = False,
debug_mode: bool = False,
fast_repackage: bool = False,
reset_timestamps: bool = True,
is_bundle: bool = False,
) -> None:
self.input_apk = input_apk
self.extracted_apk_dir = extracted_apk_dir
self.dex_dir = dex_dir
self.have_locators = have_locators
self.debug_mode = debug_mode
self.fast_repackage = fast_repackage
self.reset_timestamps: bool = reset_timestamps or debug_mode
self.is_bundle = is_bundle
self.dex_mode: typing.Optional[BaseDexMode] = None
def __enter__(self) -> typing.List[str]:
dex_mode = detect_secondary_dex_mode(self.extracted_apk_dir, self.is_bundle)
self.dex_mode = dex_mode
log("Unpacking dex files")
dex_mode.unpackage(self.extracted_apk_dir, self.dex_dir)
log("Detecting Application Modules")
store_metadata_dir = make_temp_dir(
".application_module_metadata", self.debug_mode
)
self.application_modules = ApplicationModule.detect(
self.extracted_apk_dir, self.is_bundle
)
store_files = []
for module in self.application_modules:
canary_prefix = module.get_canary_prefix()
log(
"found module: "
+ module.get_name()
+ " "
+ (canary_prefix if canary_prefix is not None else "(no canary prefix)")
)
store_path = os.path.join(self.dex_dir, module.get_name())
os.mkdir(store_path)
module.unpackage(self.extracted_apk_dir, store_path)
store_metadata = os.path.join(
store_metadata_dir, module.get_name() + ".json"
)
module.write_redex_metadata(store_path, store_metadata)
store_files.append(store_metadata)
return store_files
def __exit__(self, *args: typing.Any) -> None:
log("Repacking dex files")
log("Emit Locator Strings: %s" % self.have_locators)
dex_mode = self.dex_mode
assert dex_mode is not None
dex_mode.repackage(
self.extracted_apk_dir,
self.dex_dir,
self.have_locators,
locator_store_id=0,
fast_repackage=self.fast_repackage,
reset_timestamps=self.reset_timestamps,
)
locator_store_id = 1
for module in self.application_modules:
log(
"repacking module: "
+ module.get_name()
+ " with id "
+ str(locator_store_id)
)
module.repackage(
self.extracted_apk_dir,
self.dex_dir,
self.have_locators,
locator_store_id,
fast_repackage=self.fast_repackage,
reset_timestamps=self.reset_timestamps,
)
locator_store_id = locator_store_id + 1
class LibraryManager:
"""
__enter__: Unpacks additional libraries in extracted_apk_dirs so library class files can be found
__exit__: Cleanup temp directories used by the class
"""
temporary_libs_dir: typing.Optional[str] = None
def __init__(self, extracted_apk_dir: str, is_bundle: bool = False) -> None:
self.extracted_apk_dir = extracted_apk_dir
self.is_bundle = is_bundle
def __enter__(self) -> None:
# Some of the native libraries can be concatenated together into one
# xz-compressed file. We need to decompress that file so that we can scan
# through it looking for classnames.
libs_to_extract = []
xz_lib_name = "libs.xzs"
zstd_lib_name = "libs.zstd"
for root, _, filenames in os.walk(self.extracted_apk_dir):
for filename in fnmatch.filter(filenames, xz_lib_name):
libs_to_extract.append(join(root, filename))
for filename in fnmatch.filter(filenames, zstd_lib_name):
fullpath = join(root, filename)
# For voltron modules BUCK creates empty zstd files for each module
if os.path.getsize(fullpath) > 0:
libs_to_extract.append(fullpath)
if len(libs_to_extract) > 0: