From f56b88e7e20996746cab5a29b7074b9ffae7f4af Mon Sep 17 00:00:00 2001 From: tkmru Date: Wed, 11 Dec 2024 14:35:45 +0900 Subject: [PATCH 1/3] fix error handling --- apkutil/cli.py | 64 ++++++++++++++++++++++++++++++++++++------------- apkutil/util.py | 29 +++++++++++++++------- setup.py | 2 +- 3 files changed, 70 insertions(+), 25 deletions(-) diff --git a/apkutil/cli.py b/apkutil/cli.py index 887aade..59a8e81 100644 --- a/apkutil/cli.py +++ b/apkutil/cli.py @@ -44,7 +44,9 @@ def cmd_set_debuggable(args): print('Aligning APK by zipalign...') try: - util.align(apk_path) + result = util.align(apk_path) + if not result: + raise Exception() except Exception as e: print(e) print(Fore.RED + 'Failed') @@ -52,7 +54,9 @@ def cmd_set_debuggable(args): print('Signing APK by apksigner...') try: - util.sign(apk_path) + result = util.sign(apk_path) + if not result: + raise Exception() except Exception as e: print(e) print(Fore.RED + 'Failed') @@ -97,7 +101,9 @@ def cmd_set_network(args): print('Aligning APK by zipalign...') try: - util.align(apk_path) + result = util.align(apk_path) + if not result: + raise Exception() except Exception as e: print(e) print(Fore.RED + 'Failed') @@ -105,7 +111,9 @@ def cmd_set_network(args): print('Signing APK by apksigner...') try: - util.sign(apk_path) + result = util.sign(apk_path) + if not result: + raise Exception() except Exception as e: print(e) print(Fore.RED + 'Failed') @@ -117,7 +125,9 @@ def cmd_set_network(args): def cmd_all(args): print('Decoding APK by Apktool...') try: - util.decode(args.apk_path) + result = util.decode(args.apk_path) + if not result: + raise Exception() except Exception as e: print(e) print(Fore.RED + 'Failed') @@ -153,7 +163,9 @@ def cmd_all(args): print('Aligning APK by zipalign...') try: - util.align(apk_path) + result = util.align(apk_path) + if not result: + raise Exception() except Exception as e: print(e) print(Fore.RED + 'Failed') @@ -161,7 +173,9 @@ def cmd_all(args): print('Signing APK by apksigner...') try: - util.sign(apk_path) + result = util.sign(apk_path) + if not result: + raise Exception() except Exception as e: print(e) print(Fore.RED + 'Failed') @@ -173,7 +187,9 @@ def cmd_all(args): def cmd_decode(args): print('Decoding APK by Apktool...') try: - util.decode(args.apk_path, no_res=args.no_res, no_src=args.no_src) + result = util.decode(args.apk_path, no_res=args.no_res, no_src=args.no_src) + if not result: + raise Exception() except Exception as e: print(e) print(Fore.RED + 'Failed') @@ -194,7 +210,9 @@ def cmd_build(args): if args.output is None: apk_path = args.dir_name + ".patched.apk" try: - util.build(args.dir_name, apk_path, aapt2=args.aapt2) + result = util.build(args.dir_name, apk_path, aapt2=args.aapt2) + if not result: + raise Exception() except Exception as e: print(e) print(Fore.RED + 'Failed') @@ -202,7 +220,9 @@ def cmd_build(args): print('Aligning APK by zipalign...') try: - util.align(apk_path) + result = util.align(apk_path) + if not result: + raise Exception() except Exception as e: print(e) print(Fore.RED + 'Failed') @@ -210,7 +230,9 @@ def cmd_build(args): print('Signing APK by apksigner...') try: - util.sign(apk_path) + result = util.sign(apk_path) + if not result: + raise Exception() except Exception as e: print(e) print(Fore.RED + 'Failed') @@ -221,7 +243,9 @@ def cmd_build(args): def cmd_align(args): print('Aligning APK by zipalign...') try: - util.align(args.apk_path) + result = util.align(args.apk_path) + if not result: + raise Exception() except Exception as e: print(e) print(Fore.RED + 'Failed') @@ -231,7 +255,9 @@ def cmd_align(args): def cmd_sign(args): print('Signing APK by apksigner...') try: - util.sign(args.apk_path) + result = util.sign(args.apk_path) + if not result: + raise Exception() except Exception as e: print(e) print(Fore.RED + 'Failed') @@ -243,7 +269,9 @@ def cmd_sign(args): def cmd_info(args): print('Getting package name by aapt...') try: - util.get_packagename(args.apk_path) + result = util.get_packagename(args.apk_path) + if not result: + raise Exception() except Exception as e: print(e) print(Fore.RED + 'Failed') @@ -252,7 +280,9 @@ def cmd_info(args): def cmd_screenshot(args): print('Getting a screenshot from connected device...') try: - file_name = util.get_screenshot() + result = file_name = util.get_screenshot() + if not result: + raise Exception() print(Fore.CYAN + 'Output: ' + file_name) except Exception as e: print(e) @@ -262,7 +292,9 @@ def cmd_screenshot(args): def cmd_pull_apks(args): print('Pulling APKs from device...') try: - util.pull_apks(args.keyword) + result = util.pull_apks(args.keyword) + if not result: + raise Exception() except Exception as e: print(e) print(Fore.RED + 'Failed') diff --git a/apkutil/util.py b/apkutil/util.py index 5e08656..2fefb37 100644 --- a/apkutil/util.py +++ b/apkutil/util.py @@ -24,14 +24,14 @@ def pull_apks(keyword): package_name = get_package_name(keyword) if not package_name: print(f"No package found matching keyword: {keyword}") - return + return False print(f"Package found: {package_name}") apk_paths = get_apk_paths(package_name) if not apk_paths: print(f"No APK paths found for package: {package_name}") - return + return False pull_apk_files(apk_paths) @@ -40,8 +40,10 @@ def pull_apks(keyword): except (IndexError, FileNotFoundError): print('adb not found.') print('Please install Android SDK Build Tools.') + return False except Exception as e: print(f"An error occurred: {e}") + return False def get_package_name(keyword): adb_path = glob.glob(ANDROID_HOME + '/platform-tools/adb')[0] @@ -94,10 +96,13 @@ def decode(apk_path, no_res=False, no_src=False): # unsupported `apktool d -f` errs = errs.replace('Use -f switch if you want to overwrite it.', '') raise Exception(errs) + + return True except FileNotFoundError as e: print('apktool not found.') print('Please install apktool') + return False def build(dir_name, apk_path, aapt2=False): @@ -125,6 +130,7 @@ def build(dir_name, apk_path, aapt2=False): except FileNotFoundError as e: print('apktool not found.') print('Please install apktool.') + return False def align(apk_path): try: @@ -139,10 +145,12 @@ def align(apk_path): raise Exception(errs) move('/tmp/apkutil_tmp.aligned.apk', apk_path) + return True + except (IndexError, FileNotFoundError) as e: print('zipalign not found.') print('Please install Android SDK Build Tools.') - + return False def sign(apk_path): home_dir = os.environ['HOME'] @@ -159,7 +167,7 @@ def sign(apk_path): except: print('Please place `~/apkutil.json` containing the keystore information') - return + return False try: if not os.path.isfile(apk_path): @@ -180,11 +188,15 @@ def sign(apk_path): print(Fore.CYAN + outs) if (errs is not None) and (len(errs) != 0): - raise Exception(errs) + print(Fore.RED + errs) + return False + + return True except (IndexError, FileNotFoundError) as e: print('apksigner not found.') print('Please install Android SDK Build Tools.') + return False def get_packagename(apk_path): @@ -204,11 +216,13 @@ def get_packagename(apk_path): if (errs is not None) and (len(errs) != 0): raise Exception(errs) + + return True except (IndexError, FileNotFoundError) as e: print('apksigner not found.') print('Please install Android SDK Build Tools.') - + return False def get_screenshot(): try: @@ -230,8 +244,6 @@ def get_screenshot(): pull_cmd.append('pull') pull_cmd.append(screenshot_path) outs, errs = run_subprocess(pull_cmd) - if (errs is not None) and (len(errs) != 0): - raise Exception(errs) if (outs is not None) and (len(outs) != 0): print(outs) @@ -249,6 +261,7 @@ def get_screenshot(): except (IndexError, FileNotFoundError) as e: print('adb not found.') print('Please install Android SDK Build Tools.') + return False def check_sensitive_files(target_path): diff --git a/setup.py b/setup.py index 2c952e8..3379cd8 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setup( name="apkutil", - version="0.1.7", + version="0.1.8", description="decode, patch, build, etc", author="Taichi Kotake", packages=['apkutil'], From 4b89fcd1e045b9001d41a1f0ea763c310dd16bb6 Mon Sep 17 00:00:00 2001 From: tkmru Date: Wed, 11 Dec 2024 14:44:39 +0900 Subject: [PATCH 2/3] Logs are output to stderr when adb pull is executed --- apkutil/util.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/apkutil/util.py b/apkutil/util.py index 2fefb37..c1f9605 100644 --- a/apkutil/util.py +++ b/apkutil/util.py @@ -243,10 +243,9 @@ def get_screenshot(): pull_cmd = [adb_path] pull_cmd.append('pull') pull_cmd.append(screenshot_path) - outs, errs = run_subprocess(pull_cmd) - - if (outs is not None) and (len(outs) != 0): - print(outs) + _, errs = run_subprocess(pull_cmd) + # Logs are output to stderr even if command execution is successful. + print(errs) rm_cmd = [adb_path] rm_cmd.append('shell') From 09e8749fff8fd1e56fd37f8486a7f95447d42ab3 Mon Sep 17 00:00:00 2001 From: tkmru Date: Wed, 11 Dec 2024 14:46:50 +0900 Subject: [PATCH 3/3] update old actions --- .github/workflows/codeql-analysis.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 0f07db2..b9644b5 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -39,11 +39,11 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v2 + uses: actions/checkout@v4 # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v1 + uses: github/codeql-action/init@v3 with: languages: ${{ matrix.language }} # If you wish to specify custom queries, you can do so here or in a config file. @@ -54,7 +54,7 @@ jobs: # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). # If this step fails, then you should remove it and run the build manually (see below) - name: Autobuild - uses: github/codeql-action/autobuild@v1 + uses: github/codeql-action/autobuild@v3 # ℹī¸ Command-line programs to run using the OS shell. # 📚 https://git.io/JvXDl @@ -68,4 +68,4 @@ jobs: # make release - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v1 + uses: github/codeql-action/analyze@v3