| 1 |
import argparse |
| 2 |
import io |
| 3 |
import os |
| 4 |
|
| 5 |
from apkInspector import __version__ as version |
| 6 |
from apkInspector.extract import extract_file_based_on_header_info, extract_all_files_from_central_directory |
| 7 |
from apkInspector.headers import print_headers_of_filename, ZipEntry, show_and_save_info_of_headers |
| 8 |
from apkInspector.helpers import save_data_to_file, pretty_print_header |
| 9 |
from apkInspector.indicators import apk_tampering_check |
| 10 |
from apkInspector.axml import get_manifest |
| 11 |
|
| 12 |
|
| 13 |
def print_nested_dict(dictionary, parent_key=''): |
| 14 |
for key, value in dictionary.items(): |
| 15 |
if isinstance(value, dict): |
| 16 |
print_nested_dict(value, parent_key=f"{parent_key}") |
| 17 |
else: |
| 18 |
full_key = f"{parent_key}->{key}" if parent_key else key |
| 19 |
print(f"{full_key}: {value}") |
| 20 |
|
| 21 |
|
| 22 |
def get_apk_files(path): |
| 23 |
|
| 24 |
if os.path.isfile(path): |
| 25 |
return [path] |
| 26 |
|
| 27 |
|
| 28 |
elif os.path.isdir(path): |
| 29 |
return [os.path.join(path, f) for f in os.listdir(path) if |
| 30 |
f.endswith('.apk') and os.path.isfile(os.path.join(path, f))] |
| 31 |
|
| 32 |
|
| 33 |
return [] |
| 34 |
|
| 35 |
|
| 36 |
def main(): |
| 37 |
parser = argparse.ArgumentParser(description='apkInspector is a tool designed to provide detailed insights into ' |
| 38 |
'the zip structure of APK files, offering the ' |
| 39 |
'capability to extract content and decode the AndroidManifest.xml ' |
| 40 |
'file.') |
| 41 |
parser.add_argument('-apk', help='A single APK to inspect or a directory where multiple APKs may reside.') |
| 42 |
parser.add_argument('-f', '--filename', help='Filename to provide info for') |
| 43 |
parser.add_argument('-ll', '--list-local', action='store_true', help='List all files by name from local headers') |
| 44 |
parser.add_argument('-lc', '--list-central', action='store_true', help='List all files by name from central ' |
| 45 |
'directory header') |
| 46 |
parser.add_argument('-la', '--list-all', action='store_true', |
| 47 |
help='List all files from both central directory and local headers') |
| 48 |
parser.add_argument('-e', '--export', action='store_true', |
| 49 |
help='Export to JSON. What you list from the other flags, will be exported') |
| 50 |
parser.add_argument('-x', '--extract', action='store_true', help='Attempt to extract the file specified by the -f ' |
| 51 |
'flag') |
| 52 |
parser.add_argument('-xa', '--extract-all', action='store_true', help='Attempt to extract all files detected in ' |
| 53 |
'the central directory header -- skipping collisions!') |
| 54 |
parser.add_argument('-m', '--manifest', action='store_true', |
| 55 |
help='Extract and decode the AndroidManifest.xml') |
| 56 |
parser.add_argument('-sm', '--specify-manifest', help='Pass an encoded AndroidManifest.xml file to be decoded') |
| 57 |
parser.add_argument('-a', '--analyze', action='store_true', |
| 58 |
help='Check an APK for static analysis evasion techniques') |
| 59 |
parser.add_argument('-v', '--version', action='store_true', help='Retrieves version information') |
| 60 |
args = parser.parse_args() |
| 61 |
|
| 62 |
if args.version: |
| •63 |
mm = r""" |
| 64 |
# _ _____ _ |
| 65 |
# | | |_ _| | | |
| 66 |
# __ _ _ __ | | __ | | _ __ ___ _ __ ___ ___ | |_ ___ _ __ |
| 67 |
# / _` || '_ \ | |/ / | | | '_ \ / __|| '_ \ / _ \ / __|| __| / _ \ | '__| |
| 68 |
# | (_| || |_) || < _| |_ | | | |\__ \| |_) || __/| (__ | |_ | (_) || | |
| 69 |
# \__,_|| .__/ |_|\_\ \___/ |_| |_||___/| .__/ \___| \___| \__| \___/ |_| |
| 70 |
# | | | | |
| 71 |
# |_| |_| |
| 72 |
""" |
| 73 |
print(mm) |
| 74 |
print(f"apkInspector Library Version: {version}") |
| 75 |
print(f"Copyright 2025 erev0s <[email protected]>\n") |
| 76 |
return |
| 77 |
print(f"apkInspector Version: {version}") |
| 78 |
print(f"Copyright 2025 erev0s <[email protected]>\n") |
| 79 |
if args.apk is None and args.specify_manifest is None: |
| 80 |
parser.error('APK file or AndroidManifest.xml file is required') |
| 81 |
if not (args.specify_manifest is None) != (args.apk is None): |
| 82 |
parser.error( |
| 83 |
'Please specify an apk file with flag "-apk" or an AndroidManifest.xml file with flag "-sm", but not both.') |
| 84 |
if args.apk: |
| 85 |
apk_files = get_apk_files(args.apk) |
| 86 |
if not apk_files: |
| 87 |
print(f"No APK files found at: {args.apk}") |
| 88 |
return |
| 89 |
for apk in apk_files: |
| 90 |
pretty_print_header(f"Results for {apk}:") |
| 91 |
apk_name = os.path.splitext(apk)[0] |
| 92 |
with open(apk, 'rb') as apk_file: |
| 93 |
zipentry = ZipEntry.parse(apk_file) |
| 94 |
if args.filename and args.extract: |
| 95 |
cd_h_of_file = zipentry.get_central_directory_entry_dict(args.filename) |
| 96 |
if cd_h_of_file is None: |
| 97 |
print(f"It appears that file: {args.filename} is not among the entries of the central directory!") |
| 98 |
return |
| 99 |
local_header_of_file = zipentry.get_local_header_dict(args.filename) |
| 100 |
print_headers_of_filename(cd_h_of_file, local_header_of_file) |
| 101 |
extracted_data = extract_file_based_on_header_info(apk_file, local_header_of_file, cd_h_of_file)[0] |
| 102 |
save_data_to_file(f"EXTRACTED_{args.filename}", extracted_data) |
| 103 |
elif args.filename: |
| 104 |
cd_h_of_file = zipentry.get_central_directory_entry_dict(args.filename) |
| 105 |
if cd_h_of_file is None: |
| 106 |
print(f"It appears that file: {args.filename} is not among the entries of the central directory!") |
| 107 |
return |
| 108 |
local_header_of_file = zipentry.get_local_header_dict(args.filename) |
| 109 |
print_headers_of_filename(cd_h_of_file, local_header_of_file) |
| 110 |
elif args.extract_all: |
| 111 |
print(f"Number of entries: {len(zipentry.central_directory.entries)}") |
| 112 |
if not extract_all_files_from_central_directory(apk_file, zipentry.to_dict()["central_directory"], zipentry.to_dict()["local_headers"], apk_name): |
| 113 |
print(f"Extraction successful for: {apk_name}") |
| 114 |
elif args.list_local: |
| 115 |
show_and_save_info_of_headers(zipentry.to_dict()["local_headers"], apk_name, "local", args.export, True) |
| 116 |
print(f"Local headers list complete. Export: {args.export}") |
| 117 |
elif args.list_central: |
| 118 |
show_and_save_info_of_headers(zipentry.to_dict()["central_directory"], apk_name, "central", args.export, True) |
| 119 |
print(f"Central header list complete. Export: {args.export}") |
| 120 |
elif args.list_all: |
| 121 |
show_and_save_info_of_headers(zipentry.to_dict()["central_directory"], apk_name, "local", args.export, True) |
| 122 |
show_and_save_info_of_headers(zipentry.to_dict()["local_headers"], apk_name, "local", args.export, True) |
| 123 |
print(f"Central and local headers list complete. Export: {args.export}") |
| 124 |
elif args.manifest: |
| 125 |
cd_h_of_file = zipentry.get_central_directory_entry_dict("AndroidManifest.xml") |
| 126 |
local_header_of_file = zipentry.get_local_header_dict("AndroidManifest.xml") |
| 127 |
extracted_data = io.BytesIO( |
| 128 |
extract_file_based_on_header_info(apk_file, local_header_of_file, cd_h_of_file)[0]) |
| 129 |
manifest = get_manifest(extracted_data) |
| 130 |
with open("decoded_AndroidManifest.xml", "w", encoding="utf-8") as xml_file: |
| 131 |
xml_file.write(manifest) |
| 132 |
print("AndroidManifest was saved as: decoded_AndroidManifest.xml") |
| 133 |
elif args.analyze: |
| 134 |
tamperings = apk_tampering_check(zipentry.zip, False) |
| 135 |
if tamperings['zip tampering']: |
| 136 |
print( |
| 137 |
f"\nThe zip structure was tampered with using the following patterns:\n") |
| 138 |
print_nested_dict(tamperings['zip tampering']) |
| 139 |
else: |
| 140 |
print(f"No files were detected were a tampering in the zip structure was present.") |
| 141 |
if tamperings['manifest tampering']: |
| 142 |
print(f"\n\nThe AndroidManifest.xml file was tampered using the following patterns:\n") |
| 143 |
print_nested_dict(tamperings['manifest tampering']) |
| 144 |
else: |
| 145 |
print(f"The AndroidManifest.xml file does not seem to be tampered structurally.") |
| 146 |
else: |
| 147 |
parser.print_help() |
| 148 |
elif args.specify_manifest: |
| 149 |
with open(args.specify_manifest, 'rb') as enc_manifest: |
| 150 |
manifest = get_manifest(io.BytesIO(enc_manifest.read())) |
| 151 |
with open("decoded_AndroidManifest.xml", "w", encoding="utf-8") as xml_file: |
| 152 |
xml_file.write(manifest) |
| 153 |
print("AndroidManifest was saved as: decoded_AndroidManifest.xml") |
| 154 |
|
| 155 |
|
| 156 |
if __name__ == '__main__': |
| 157 |
main() |