apkpatcher package

Submodules

apkpatcher.cli module

apkpatcher.cli.main() int

cli function

apkpatcher.conf module

conf file

Module contents

class apkpatcher.Patcher(apk: str | Path, version_buildtools: str = None, sdktools: str | Path = None, logger: Logger | None = None, nb_jobs: int = 4)

Bases: object

Raises:

ValueError: _description_ ValueError: _description_ ValueError: _description_ when: _description_ FileNotFoundError: _description_ FileNotFoundError: _description_ ValueError: _description_ FileNotFoundError: _description_ ValueError: _description_

ARCH_ARM = 'arm'
ARCH_ARM64 = 'arm64'
ARCH_X64 = 'x64'
ARCH_X86 = 'x86'
ARCH_X86_64 = 'x86_64'
AUTOLOAD_BIT = 2
CONFIG_BIT = 1
DEFAULT_CONFIG_NAME = 'generatedConfigFile.config'
DEFAULT_GADGET_NAME = 'libfrida-gadget.so'
DEFAULT_HOOKFILE_NAME = 'libhook.js.so'
INTERNET_PERMISSION = 'android.permission.INTERNET'
add_certificate(keycertificate: Path | str, keyalias: str, keypass: str)

Add signature certificate

Args:

keycertificate (Path | str): certificate path keyalias (str): aliasname keypass (str): password

add_network_certificate(cert: Path)

add network certificate

Args:

cert (Path): the certificate to inject

calculate_sha256(file_path: Path)

Calculate the SHA-256 hash of a file.

Parameters:

file_path – Path to the file.

Returns:

SHA-256 hash as a hexadecimal string.

check_libextract() bool

check if extractNativeLibs is enable and active it

Returns:

bool: return False if we didn’t find manifest

compute_directory_hashes(base_directory, hash_dict: dict | None = None) None

Compute SHA-256 hashes for all files in a directory and store them in a nested dictionary.

Parameters:

base_directory – Path to the base directory.

create_lib_arch_folders(arch)

make lib folder in the apk to put native lib

create_security_config_xml(path_network: str) bool

Create security config file for add certificate

Args:

path_network (str): path network

delete_existing_gadget(arch_folder: Path, delete_custom_files: int = 0)

delete existing gadget inside the apk

download_frida() str | None

Download frida binary

Returns:

str | None: return the name of lib

enable_debug_mode() bool

Enable debug mode

Returns:

bool: success to enable debug mode

enable_user_certificates(rsc: ARSC)

Enable user certificate

Args:

rsc (pyaxml.ARSC): return ARSC file return 2 if rsc modified return 1 if success return 0 if failed

enable_v4_signature(file: Path | str)

enable v4 signature

Args:

file (Path | str): v4_signature file

extract_apk(apk: Path, final_dir: Path)

Extract the apk on the temporary folder

extract_dex(dex_file: Path)

Extract Dex

Args:

dex_file (Path): the dexfile to extract

get_entrypoint_class_name() str | None

get the class name of the entrypoint

get_entrypoint_smali_path() Path | None

get the path of apk entrypoint on the smali files

has_permission(permission_name: str) bool

Check if the apk have ‘permission_name’ as permission

Parameters:

permission_name (str): name of the permission with format: android.permission.XXX

Returns:

has_permission (bool): permission is present

inject_custom_network_certificate(rsc, path_network: str) bool

Inject custom network certificate

inject_permission_manifest(permission: str)

Inject permission on the Manifest

inject_user_certificates_label(rsc: ~pyaxml.arscobject.ARSC) -> (<class 'str'>, <class 'int'>)

Inject a proxy certificate directly inside the application

Args:

rsc (pyaxml.ARSC): ARSC file (resource file of Android)

Raises:

FileNotFoundError: raise when manifest is not found

Returns:

str: return the path of network file

insert_frida_lib(gadget_path: str, arch: str, config_file_path=None, auto_load_script_path=None)

Insert native lib inside the apk

Parameters:
  • gadget_path (str): the path of the gadget to insert

insert_frida_loader(frida_lib_name='frida-gadget')

inject snippet to load frida-gadget in smali code

keep_certificate()

keep certificate after use it

missing_sdktools()

raise an error when sdktools is missing

Raises:

ValueError: The error

property need_full_extraction: bool

need full extraction

Returns:

bool: return true if needed full extraction

patching(gadget_to_use: str | None = None, output_file: Path | None = None, user_certificate: bool = False, splits_apk: list[Path] | None = None, entrypoint=None, permissions: list[str] = None) False

patch the apk with gadget ‘gadget_to_use’

property pause: bool

get Pause status

Returns:

bool: pause

repackage_apk(target_file: Path | None = None) Path | None

repackage the apk

Parameters:
  • target_file (str) : the path of the new apk created if none, a new apk will be created with suffix “_patched.apk”

repackage_dex(classes_dir: Path) bool

Repackage Dex

Args:

classes_dir (Path): classes directory

Returns:

bool: _description_

set_arch(arch: str)

set architecture of target phone where apk would be installed

Args:

arch (str): architecture

set_debug()

set debug mode

set_plugin(plugin: str | Path) None

set a plugin

Args:

plugin (str | Path): _description_

set_use_download_frida(frida_version: str)

Specify frida version to download

Args:

frida_version (str): frida version

sign_and_zipalign(apk_path: Path, splits_apk: List[Path])

sign and zipalign file

apkpatcher.download_baksmali(output_file: Path)

Download baksmali

Args:

output_file (Path): output location

apkpatcher.download_smali(output_file: Path)

Download smali

Args:

output_file (Path): output location

apkpatcher.get_latest_version_directory(base_path: Path) str | None

Get latest version directory

Args:

base_path (Path): base path

Returns:

str | None: the latest version directory

apkpatcher.new_logger(level: logging._Level) Logger

Instanciate Logger

Args:

level (logging._Level): level of logger

Returns:

logging.Logger: new logger

apkpatcher.plba(filename: str | Path, arch: str) str

Filename with architecture

Args:

filename (str | Path): base filename arch (str): architecture

Returns:

str: return filename with architecture