1
1
import contextlib
2
+ import functools
2
3
import lzma
3
4
import os
4
5
import re
@@ -803,6 +804,30 @@ def _h():
803
804
804
805
return patched_smali
805
806
807
+ @functools .cache
808
+ def _find_libs_path (self ):
809
+ """
810
+ Find the libraries path for the target architecture within the APK.
811
+ """
812
+ base_libs_path = os .path .join (self .apk_temp_directory , 'lib' )
813
+ available_libs_arch = os .listdir (base_libs_path )
814
+ if self .architecture in available_libs_arch :
815
+ # Exact match with arch
816
+ return os .path .join (base_libs_path , self .architecture )
817
+ else :
818
+ # Try to use prefix search
819
+ try :
820
+ matching_arch = next (
821
+ item for item in available_libs_arch if item .startswith (self .architecture )
822
+ )
823
+ click .secho ('Using matching architecture {0} from provided architecture {1}.' .format (
824
+ matching_arch , self .architecture
825
+ ), dim = True )
826
+ return os .path .join (base_libs_path , matching_arch )
827
+ except StopIteration :
828
+ # Might create the arch folder inside the APK tree
829
+ return os .path .join (base_libs_path , self .architecture )
830
+
806
831
def inject_load_library (self , target_class : str = None ):
807
832
"""
808
833
Injects a loadLibrary call into a class.
@@ -828,7 +853,7 @@ def inject_load_library(self, target_class: str = None):
828
853
# Inspired by https://fadeevab.com/frida-gadget-injection-on-android-no-root-2-methods/
829
854
if not self .architecture or not self .libfridagadget_name :
830
855
raise Exception ('Frida-gadget should have been copied prior to injecting!' )
831
- libs_path = os . path . join ( self .apk_temp_directory , 'lib' , self . architecture )
856
+ libs_path = self ._find_libs_path ( )
832
857
existing_libs_in_apk = [
833
858
lib
834
859
for lib in os .listdir (libs_path )
@@ -889,7 +914,7 @@ def add_gadget_to_apk(self, architecture: str,
889
914
self .libfridagadget_name = libfridagadget_name
890
915
self .libfridagadgetconfig_name = libfridagadget_name .replace ('.so' , '.config.so' )
891
916
892
- libs_path = os . path . join ( self .apk_temp_directory , 'lib' , architecture )
917
+ libs_path = self ._find_libs_path ( )
893
918
894
919
# check if the libs path exists
895
920
if not os .path .exists (libs_path ):
0 commit comments