Skip to content

Commit c74796b

Browse files
committed
Fuzzy match of architectures in APK tree
1 parent 8ddf760 commit c74796b

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

objection/utils/patchers/android.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import contextlib
2+
import functools
23
import lzma
34
import os
45
import re
@@ -803,6 +804,30 @@ def _h():
803804

804805
return patched_smali
805806

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+
806831
def inject_load_library(self, target_class: str = None):
807832
"""
808833
Injects a loadLibrary call into a class.
@@ -828,7 +853,7 @@ def inject_load_library(self, target_class: str = None):
828853
# Inspired by https://fadeevab.com/frida-gadget-injection-on-android-no-root-2-methods/
829854
if not self.architecture or not self.libfridagadget_name:
830855
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()
832857
existing_libs_in_apk = [
833858
lib
834859
for lib in os.listdir(libs_path)
@@ -889,7 +914,7 @@ def add_gadget_to_apk(self, architecture: str,
889914
self.libfridagadget_name = libfridagadget_name
890915
self.libfridagadgetconfig_name = libfridagadget_name.replace('.so', '.config.so')
891916

892-
libs_path = os.path.join(self.apk_temp_directory, 'lib', architecture)
917+
libs_path = self._find_libs_path()
893918

894919
# check if the libs path exists
895920
if not os.path.exists(libs_path):

0 commit comments

Comments
 (0)