Skip to content

Commit

Permalink
Enable architecture auto-detection
Browse files Browse the repository at this point in the history
Signed-off-by: ksg97031 <ksg97031@gmail.com>
  • Loading branch information
ksg97031 committed Dec 24, 2024
1 parent e4bb24c commit 5c089ec
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
24 changes: 13 additions & 11 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,17 @@ Usage
Patch an APK with the Frida gadget library
Options:
--arch TEXT Target architecture of the device. (options: arm64, x86_64, arm, x86)
--config TEXT Upload the Frida configuration file.
--no-res Do not decode resources.
--main-activity TEXT Specify the main activity if desired. (e.g., com.example.MainActivity)
--sign Automatically sign the APK using uber-apk-signer.
--skip-decompile Skip decompilation if desired.
--skip-recompile Skip recompilation if desired.
--use-aapt2 Use aapt2 instead of aapt.
--version Show version and exit.
--help Show this message and exit.
--arch TEXT Target architecture of the device. (options: arm64, x86_64, arm, x86)
--config TEXT Upload the Frida configuration file.
--custom-gadget-name TEXT Custom name for the Frida gadget.
--no-res Do not decode resources.
--main-activity TEXT Specify the main activity if desired. (e.g., com.example.MainActivity)
--sign Automatically sign the APK using uber-apk-signer.
--skip-decompile Skip decompilation if desired.
--skip-recompile Skip recompilation if desired.
--use-aapt2 Use aapt2 instead of aapt.
--version Show version and exit.
--help Show this message and exit.
How do I begin?
~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -77,9 +78,10 @@ How do I begin?
.. code:: sh
$ frida-gadget handtrackinggpu.apk --arch arm64 --sign
$ frida-gadget handtrackinggpu.apk --sign
[INFO] Auto-detected frida version: 16.1.3
[INFO] APK: '[REDACTED]\demo-apk\handtrackinggpu.apk'
[INFO] Auto-detected architecture via ADB: arm64-v8a # Alternatively, specify the architecture with --arch arm64
[INFO] Gadget Architecture(--arch): arm64(default)
[DEBUG] Decompiling the target APK using apktool
[DEBUG] Downloading the frida gadget library for arm64
Expand Down
45 changes: 42 additions & 3 deletions scripts/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,42 @@ def sign_apk(apk_path:str):
return True


def detect_adb_arch():
"""Detect the architecture of the currently connected device via ADB.
This function communicates with a connected Android device over ADB
to determine its CPU architecture (e.g., arm64-v8a, armeabi-v7a, x86).
Returns:
str: The detected architecture of the connected device.
Defaults to 'arm64' if detection fails.
"""
pipe = subprocess.PIPE
cmd = ['adb', 'shell', 'getprop', 'ro.product.cpu.abi']
default_arch = "arm64"

try:
with subprocess.Popen(cmd, stdin=pipe, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as process:
stdout, stderr = process.communicate()
if process.returncode != 0:
logger.warning("Failed to execute ADB command. Error: %s", stderr.decode().strip())
logger.warning("Falling back to default architecture: %s", default_arch)
return default_arch

arch = stdout.decode().strip()
if not arch:
logger.warning("Architecture detection failed: no output received. Falling back to default: %s", default_arch)
return default_arch

logger.info("Auto-detected architecture via ADB: %s", arch)
return arch

except FileNotFoundError:
logger.warning("ADB is not installed or not found in the system PATH. Falling back to default: %s", default_arch)
return default_arch
except Exception as e:
logger.warning("An unexpected error occurred during architecture detection: %s. Falling back to default: %s", str(e), default_arch)
return default_arch

def print_version(ctx, _, value):
"""Print version and exit"""
Expand All @@ -300,7 +336,7 @@ def print_version(ctx, _, value):

# pylint: disable=too-many-arguments
@click.command()
@click.option('--arch', default="arm64", help="Target architecture of the device. (options: arm64, x86_64, arm, x86)")
@click.option('--arch', default=None, help="Target architecture of the device. (options: arm64, x86_64, arm, x86)")
@click.option('--config', help="Upload the Frida configuration file.")
@click.option('--custom-gadget-name', default=None, help="Custom name for the Frida gadget.")
@click.option('--no-res', is_flag=True, help="Do not decode resources.")
Expand All @@ -318,13 +354,16 @@ def run(apk_path: str, arch: str, config: str, no_res:bool, main_activity: str,
apk_path = Path(apk_path)

logger.info("APK: '%s'", apk_path)
logger.info("Gadget Architecture(--arch): %s%s", arch, "(default)" if arch == "arm64" else "")
if arch is None:
arch = detect_adb_arch()

arch = arch.lower()
if arch == 'arm64-v8a':
arch = 'arm64'
elif arch == 'armeabi-v7a':
arch = 'arm'
logger.info("Gadget Architecture(--arch): %s%s", arch, "(default)" if arch == "arm64" else "")

arch = arch.lower()
supported_archs = ['arm', 'arm64', 'x86', 'x86_64']
if arch not in supported_archs:
logger.error(
Expand Down

0 comments on commit 5c089ec

Please sign in to comment.