Skip to content

Instantly share code, notes, and snippets.

@MrCarb0n
Last active August 8, 2023 13:02
Show Gist options
  • Save MrCarb0n/a2f5f0f3195b52a084a80e016348ad31 to your computer and use it in GitHub Desktop.
Save MrCarb0n/a2f5f0f3195b52a084a80e016348ad31 to your computer and use it in GitHub Desktop.
Android app runtime permission fixer.
#!/usr/bin/sh
# ╭─────────────────────────────────────────╮
# │ Android App Permissions Batch Fixer │
# │ gist.github.com/MrCarb0n │
# ├─────────────────────────────────────────┤
# │ l a z i n e s s d r i v e s │
# │ i n n o v a t i o n │
# ├─────────────────────────────────────────┤
# │ Author: @MrCarb0n │
# ╰─────────────────────────────────────────╯
# set -x
# Usage:
# ./fixperm.sh -[a|p|A|P] [com.pkg.name]
# A - AppOps
# P - Permission Manager (pm)
# set -euo pipefail
# Colors
{
GREEN="\033[1;32m"
BLUE="\033[1;34m"
GREY="\033[0;37m"
RESET="\033[0m"
} 2> /dev/null
PKG_LIST() { pm list packages | cut -d: -f2 | grep -v "com.termux"; }
PERM_LIST() { pm list permissions | cut -d: -f2; }
DUMP_PERM() {
dumpsys package "$1" |
awk '
/android.permission/ && \
match($0, /android.permission\.[A-Z_]+/) && \
!a[substr($0, RSTART, RLENGTH)]++ {
print substr($0, RSTART, RLENGTH)
}
'
}
APPOPS_PERM() {
dumpsys appops --package "$1" |
tr -s ' ' '\n' |
grep -oE '[A-Z_]{2,}' |
sort |
uniq
}
EXEC_PERM() {
TP=$(echo "$1" | wc -w)
CPN=0
for package in $1; do
CPN=$((CPN + 1))
echo "${BLUE} - PKG: $package ($CPN/$TP)${RESET}"
for permission in $(DUMP_PERM "$package"); do
if pm grant "$package" "$permission" 2> /dev/null; then
echo "${GREEN} GRANT: $permission ${RESET}"
else
echo "${GREY} FAIL: $permission ${RESET}"
fi
done
echo
done
}
EXEC_APPOPS() {
TP=$(echo "$1" | wc -w)
CPN=0
for package in $1; do
CPN=$((CPN + 1))
echo "${BLUE} - PKG: $package ($CPN/$TP)${RESET}"
for permission in $(APPOPS_PERM "$package"); do
if cmd appops set "$package" "$permission" allow 2> /dev/null; then
echo "${GREEN} GRANT: $permission ${RESET}"
else
echo "${GREY} FAIL: $permission ${RESET}"
fi
done
echo
done
}
if [ $# -lt 1 ]; then
echo "${BLUE} Usage: $0 -[a|p|A|P] [com.pkg.name]"
echo ""
echo " e.g.,"
echo " $0 -p com.google.android.youtube $RESET"
exit 1
fi
if [ $# -eq 2 ]; then
if [ -n "$2" ]; then
case "$1" in
-a|-A) EXEC_APPOPS "$2" ;;
-p|-P) EXEC_PERM "$2" ;;
esac
else
echo "${BLUE} Error: Package name is empty. ${RESET}"
exit 1
fi
else
case "$1" in
-a|-A) EXEC_APPOPS "$(PKG_LIST)" ;;
-p|-P) EXEC_PERM "$(PKG_LIST)" ;;
esac
fi
# END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment