Created
June 21, 2022 10:37
-
-
Save thgoebel/1be04b5d8a36fc9efe8b75170f3391a7 to your computer and use it in GitHub Desktop.
Script to find Android apps that have a given permission
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# Script to find all installed Android apps that request a given permission | |
# | |
# Author: Thore Goebel <hello@thore.io> | |
# Version: 2022-06-21 | |
import shlex, subprocess | |
PERMISSION = "android.permission.READ_LOGS" | |
# List all installed packages | |
# Options: system packages -s, 3rd party packages -3 | |
args = shlex.split("adb shell pm list packages -s") | |
out = subprocess.run(args, capture_output=True) | |
if out.returncode != 0: | |
print(out.stderr.decode("utf8")) | |
exit() | |
packages = [] | |
for line in out.stdout.decode("utf8").splitlines(): | |
if line.startswith("package:"): | |
packages.append(line[8:]) | |
print(f"Found {len(packages)} packages.") | |
# Find the permissions for each package | |
count = 0 | |
print("Apps with the permission:") | |
for pkgname in packages: | |
args = shlex.split(f"adb shell dumpsys package {pkgname}") | |
out = subprocess.run(args, capture_output=True) | |
if out.returncode != 0: | |
print(out.stderr.decode("utf8")) | |
exit() | |
dump = out.stdout.decode("utf8") | |
if f"{PERMISSION}" in dump: | |
print(f"\t{pkgname}") | |
count += 1 | |
print(f"{count} packages have the {PERMISSION} permission") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment