$ python3 revoke_permission.py
$ python3 revoke_permission.py <device_code>
Last active
March 17, 2016 07:09
-
-
Save tuanchauict/c6e120e2c70abce90ec6 to your computer and use it in GitHub Desktop.
Revoke permissions for testing request permissions on android SDK >= 23
This file contains hidden or 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
import subprocess | |
import re | |
import sys | |
DANGEROUS_PERMISSIONS = [ | |
'READ_CALENDAR', | |
'WRITE_CALENDAR', | |
'CAMERA', | |
'READ_CONTACTS', | |
'WRITE_CONTACTS', | |
'GET_ACCOUNTS', | |
'ACCESS_FINE_LOCATION', | |
'ACCESS_COARSE_LOCATION', | |
'RECORD_AUDIO', | |
'READ_PHONE_STATE', | |
'CALL_PHONE', | |
'READ_CALL_LOG', | |
'WRITE_CALL_LOG', | |
'ADD_VOICEMAIL', | |
'USE_SIP', | |
'PROCESS_OUTGOING_CALLS', | |
'BODY_SENSORS', | |
'SEND_SMS', | |
'RECEIVE_SMS', | |
'READ_SMS', | |
'RECEIVE_WAP_PUSH', | |
'RECEIVE_MMS', | |
'READ_EXTERNAL_STORAGE', | |
'WRITE_EXTERNAL_STORAGE', | |
] | |
DANGEROUS_PERMISSIONS = ['android.permission.' + p for p in DANGEROUS_PERMISSIONS] | |
def get_device_version(device): | |
version = subprocess.check_output(['adb', '-s', device, 'shell', 'getprop', 'ro.build.version.sdk']) | |
version = int(version.decode('ascii').strip()) | |
return version | |
def get_v23_devices(): | |
devices = subprocess.check_output(["adb", "devices"]).decode("ascii").strip() | |
devices = devices.split("\n")[1:] | |
devices_23 = [] | |
for i in range(0, len(devices)): | |
d = devices[i] | |
devices[i] = d.split('\t')[0] | |
d = devices[i] | |
version = get_device_version(d) | |
if(version >= 23): | |
devices_23.append((d, version)) | |
return devices_23 | |
def get_v23_device(): | |
devices_23 = get_v23_devices() | |
if not devices_23: | |
print("No device with SDK >= 23 found") | |
return None | |
if len(devices_23) > 1: | |
print("There are %s devices have SDK >= 23:") | |
for d in devices_23: | |
print('\t',d) | |
return None | |
return devices_23[0] | |
def get_current_package(device): | |
o = subprocess.check_output(['adb', '-s', device, 'shell', 'dumpsys', 'window', 'windows', '|', 'grep', '-E', '"mCurrentFocus"']) | |
o = o.decode('utf-8').strip() | |
match = re.findall(r'.+ (.+)/', o) | |
if match: | |
return match[0] | |
def get_app_permissions(device, package): | |
info = subprocess.check_output(['adb', '-s', device, 'shell', 'dumpsys', 'package', package]).decode('utf-8') | |
lines = info.split('\n') | |
permissions = [] | |
flag = False | |
for line in lines: | |
if 'install permissions:' in line: | |
flag = False | |
break | |
if flag: | |
permissions.append(line.strip()) | |
if 'requested permissions:' in line: | |
flag = True | |
return permissions | |
def filter_dangerous_permissions(permissions): | |
dp = [] | |
for p in permissions: | |
if p in DANGEROUS_PERMISSIONS: | |
dp.append(p) | |
return dp | |
def revoke_permission(device, package,permission): | |
print('Revoking %s' % permission) | |
subprocess.call(['adb', '-s', device, 'shell', 'pm', 'revoke', package, permission]) | |
def check_and_revoke(device, sdk_version): | |
print("Start checking on device: \n\t%s\tSDK: %s\n" % (device, sdk_version)) | |
package = get_current_package(device) | |
permissions = get_app_permissions(device, package) | |
dangerous_permissions = filter_dangerous_permissions(permissions) | |
if not dangerous_permissions: | |
print("%s does not require any dangerous permissions" % package) | |
else: | |
print('Found out %s dangerous permissions:' % len(dangerous_permissions)) | |
for dp in dangerous_permissions: | |
revoke_permission(device, package, dp) | |
def main(): | |
args = sys.argv[1:] | |
if not args: | |
device, sdk_version = get_v23_device() | |
if not device: | |
return | |
check_and_revoke(device, sdk_version) | |
else: | |
device = args[0] | |
sdk_version = get_device_version(device) | |
if sdk_version < 23: | |
print('Device is at SDK %s, no need to revoke permissions' % sdk_version) | |
return | |
check_and_revoke(device, sdk_version) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment